refactor: simplify chat subscription API
All checks were successful
CI / Deploy / test (pull_request) Successful in 14s
CI / Deploy / lint (pull_request) Successful in 25s
CI / Deploy / deploy (pull_request) Has been skipped

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:
Ryan Hamamura
2026-03-03 09:45:56 -10:00
parent bf9a8755f0
commit bcb1fa3872
3 changed files with 37 additions and 31 deletions

View File

@@ -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"),