From e68e4b48f58c01cb396f796f6cacd072b3077566 Mon Sep 17 00:00:00 2001 From: Ryan Hamamura <58859899+ryanhamamura@users.noreply.github.com> Date: Fri, 20 Feb 2026 12:37:28 -1000 Subject: [PATCH] fix: resolve nil pubsub preventing live game updates v.PubSub() was captured at startup before v.Start() initialized NATS, so both stores held nil and notify() silently no-oped. Replace the PubSub interface with a callback that evaluates v.PubSub() lazily at call time. --- game/store.go | 16 ++++++---------- main.go | 8 ++++++-- snake/store.go | 16 ++++++---------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/game/store.go b/game/store.go index cd5ddc6..9e7aae8 100644 --- a/game/store.go +++ b/game/store.go @@ -6,10 +6,6 @@ import ( "sync" ) -type PubSub interface { - Publish(subject string, data []byte) error -} - type PlayerSession struct { Player *Player } @@ -25,8 +21,8 @@ type Persister interface { type GameStore struct { games map[string]*GameInstance gamesMu sync.RWMutex - persister Persister - pubsub PubSub + persister Persister + notifyFunc func(gameID string) } func NewGameStore() *GameStore { @@ -39,14 +35,14 @@ func (gs *GameStore) SetPersister(p Persister) { gs.persister = p } -func (gs *GameStore) SetPubSub(ps PubSub) { - gs.pubsub = ps +func (gs *GameStore) SetNotifyFunc(f func(gameID string)) { + gs.notifyFunc = f } func (gs *GameStore) makeNotify(gameID string) func() { return func() { - if gs.pubsub != nil { - gs.pubsub.Publish("game."+gameID, nil) + if gs.notifyFunc != nil { + gs.notifyFunc(gameID) } } } diff --git a/main.go b/main.go index 848326d..fa6bce1 100644 --- a/main.go +++ b/main.go @@ -81,8 +81,12 @@ func main() { subFS, _ := fs.Sub(assets, "assets") v.StaticFS("/assets/", subFS) - store.SetPubSub(v.PubSub()) - snakeStore.SetPubSub(v.PubSub()) + store.SetNotifyFunc(func(gameID string) { + v.PubSub().Publish("game."+gameID, nil) + }) + snakeStore.SetNotifyFunc(func(gameID string) { + v.PubSub().Publish("snake."+gameID, nil) + }) // Home page - tabbed lobby v.Page("/", func(c *via.Context) { diff --git a/snake/store.go b/snake/store.go index ea90557..fac2574 100644 --- a/snake/store.go +++ b/snake/store.go @@ -6,10 +6,6 @@ import ( "sync" ) -type PubSub interface { - Publish(subject string, data []byte) error -} - type Persister interface { SaveSnakeGame(sg *SnakeGame) error LoadSnakeGame(id string) (*SnakeGame, error) @@ -21,8 +17,8 @@ type Persister interface { type SnakeStore struct { games map[string]*SnakeGameInstance gamesMu sync.RWMutex - persister Persister - pubsub PubSub + persister Persister + notifyFunc func(gameID string) } func NewSnakeStore() *SnakeStore { @@ -35,14 +31,14 @@ func (ss *SnakeStore) SetPersister(p Persister) { ss.persister = p } -func (ss *SnakeStore) SetPubSub(ps PubSub) { - ss.pubsub = ps +func (ss *SnakeStore) SetNotifyFunc(f func(gameID string)) { + ss.notifyFunc = f } func (ss *SnakeStore) makeNotify(gameID string) func() { return func() { - if ss.pubsub != nil { - ss.pubsub.Publish("snake."+gameID, nil) + if ss.notifyFunc != nil { + ss.notifyFunc(gameID) } } }