refactor: simplify chat subscription API
Room.Subscribe() now returns a channel of parsed Message structs instead of raw NATS messages. The room handles NATS subscription and message parsing internally, so callers no longer need to call Receive() separately.
This commit is contained in:
@@ -132,11 +132,8 @@ func HandleGameEvents(store *connect4.Store, nc *nats.Conn, sm *scs.SessionManag
|
||||
defer gameSub.Unsubscribe() //nolint:errcheck
|
||||
|
||||
// Subscribe to chat messages
|
||||
chatCh, chatSub, err := room.Subscribe()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer chatSub.Unsubscribe() //nolint:errcheck
|
||||
chatCh, cleanupChat := room.Subscribe()
|
||||
defer cleanupChat()
|
||||
|
||||
ctx := r.Context()
|
||||
for {
|
||||
@@ -147,8 +144,7 @@ func HandleGameEvents(store *connect4.Store, nc *nats.Conn, sm *scs.SessionManag
|
||||
if err := patchAll(); err != nil {
|
||||
return
|
||||
}
|
||||
case msg := <-chatCh:
|
||||
chatMsg, _ := room.Receive(msg.Data)
|
||||
case chatMsg := <-chatCh:
|
||||
err := sse.PatchElementTempl(
|
||||
chatcomponents.ChatMessage(chatMsg, chatCfg),
|
||||
datastar.WithSelectorID("c4-chat-history"),
|
||||
|
||||
Reference in New Issue
Block a user