commit abc20703e3f2f5e645dd14ac166b646fa89547bd
parent 061b58d665f28b354abd847d699c15080c41fbe3
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 15 Jun 2023 21:54:48 -0700
use hasset to make the code slightly easier to read
Diffstat:
2 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/pkg/hashset/hashset.go b/pkg/hashset/hashset.go
@@ -12,6 +12,10 @@ func New[V comparable]() *HashSet[V] {
return h
}
+func (h *HashSet[V]) Set(v V) {
+ h.items[v] = struct{}{}
+}
+
// Insert adds a value to the set.
// If the set did not have this value present, true is returned.
// If the set did have this value present, false is returned.
@@ -136,6 +140,10 @@ func (h *HashSet[V]) Remove(v V) bool {
return true
}
+func (h *HashSet[V]) Delete(v V) {
+ delete(h.items, v)
+}
+
// Take removes and returns the value in the set, if any, that is equal to the given one.
func (h *HashSet[V]) Take(v V) (out V, err error) {
if !h.Contains(v) {
diff --git a/pkg/web/handlers/api/v1/chat.go b/pkg/web/handlers/api/v1/chat.go
@@ -4,6 +4,7 @@ import (
"dkforest/pkg/config"
"dkforest/pkg/database"
dutils "dkforest/pkg/database/utils"
+ "dkforest/pkg/hashset"
"dkforest/pkg/managers"
"dkforest/pkg/pubsub"
"dkforest/pkg/utils"
@@ -167,10 +168,10 @@ func ChatStreamMessagesHandler(c echo.Context) error {
// Keep track of messages that are after the read-marker (unread).
// When we receive a "delete msg", and this map is empty, we should hide the read-marker
// as it means the read marker is now at the very top.
- msgsMap := make(map[int64]struct{})
+ msgsMap := hashset.New[int64]()
for _, msg := range msgs {
if msg.CreatedAt.After(data.ReadMarker.ReadAt) {
- msgsMap[msg.ID] = struct{}{}
+ msgsMap.Set(msg.ID)
}
}
@@ -242,7 +243,7 @@ Loop:
// We receive this event when the "update read-marker" is clicked.
// This means the user is saying that all messages are read, and read-marker should be at the very top.
if topic == readMarkerTopic {
- msgsMap = make(map[int64]struct{}) // read-marker at the top, so no unread message
+ msgsMap.Clear() // read-marker at the top, so no unread message
updateReadMarker()
c.Response().Flush()
continue
@@ -261,8 +262,8 @@ Loop:
if msgTyp.Typ == database.DeleteMsg {
// Delete msg from the map that keep track of unread messages.
// If the map is now empty, we hide the read-marker.
- delete(msgsMap, msgTyp.Msg.ID)
- if len(msgsMap) == 0 {
+ msgsMap.Delete(msgTyp.Msg.ID)
+ if msgsMap.Len() == 0 {
updateReadMarker()
}
@@ -322,7 +323,7 @@ Loop:
renderedMsg := RenderMessage(1, *msg, authUser, data, baseTopBarURL, &readMarkerRendered, &isFirstMsg, csrf, nullUsername)
// Keep track of unread messages
- msgsMap[msg.ID] = struct{}{}
+ msgsMap.Set(msg.ID)
send(renderedMsg)
showReadMarker()