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

@@ -137,15 +137,12 @@ func HandleSnakeEvents(snakeStore *snake.SnakeStore, nc *nats.Conn, sm *scs.Sess
defer gameSub.Unsubscribe() //nolint:errcheck
// Chat subscription (multiplayer only)
var chatCh chan *nats.Msg
var chatSub *nats.Subscription
var chatCh <-chan chat.Message
var cleanupChat func()
if room != nil {
chatCh, chatSub, err = room.Subscribe()
if err != nil {
return
}
defer chatSub.Unsubscribe() //nolint:errcheck
chatCh, cleanupChat = room.Subscribe()
defer cleanupChat()
}
ctx := r.Context()
@@ -168,11 +165,10 @@ func HandleSnakeEvents(snakeStore *snake.SnakeStore, nc *nats.Conn, sm *scs.Sess
return
}
case msg := <-chatCh:
if msg == nil {
case chatMsg, ok := <-chatCh:
if !ok {
continue
}
chatMsg, _ := room.Receive(msg.Data)
err := sse.PatchElementTempl(
chatcomponents.ChatMessage(chatMsg, chatCfg),
datastar.WithSelectorID("snake-chat-history"),