fix: resolve nil pubsub preventing live game updates
All checks were successful
Deploy c4 / deploy (push) Successful in 45s

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.
This commit is contained in:
Ryan Hamamura
2026-02-20 12:37:28 -10:00
parent 91b5f2b80c
commit e68e4b48f5
3 changed files with 18 additions and 22 deletions

View File

@@ -6,10 +6,6 @@ import (
"sync" "sync"
) )
type PubSub interface {
Publish(subject string, data []byte) error
}
type PlayerSession struct { type PlayerSession struct {
Player *Player Player *Player
} }
@@ -26,7 +22,7 @@ type GameStore struct {
games map[string]*GameInstance games map[string]*GameInstance
gamesMu sync.RWMutex gamesMu sync.RWMutex
persister Persister persister Persister
pubsub PubSub notifyFunc func(gameID string)
} }
func NewGameStore() *GameStore { func NewGameStore() *GameStore {
@@ -39,14 +35,14 @@ func (gs *GameStore) SetPersister(p Persister) {
gs.persister = p gs.persister = p
} }
func (gs *GameStore) SetPubSub(ps PubSub) { func (gs *GameStore) SetNotifyFunc(f func(gameID string)) {
gs.pubsub = ps gs.notifyFunc = f
} }
func (gs *GameStore) makeNotify(gameID string) func() { func (gs *GameStore) makeNotify(gameID string) func() {
return func() { return func() {
if gs.pubsub != nil { if gs.notifyFunc != nil {
gs.pubsub.Publish("game."+gameID, nil) gs.notifyFunc(gameID)
} }
} }
} }

View File

@@ -81,8 +81,12 @@ func main() {
subFS, _ := fs.Sub(assets, "assets") subFS, _ := fs.Sub(assets, "assets")
v.StaticFS("/assets/", subFS) v.StaticFS("/assets/", subFS)
store.SetPubSub(v.PubSub()) store.SetNotifyFunc(func(gameID string) {
snakeStore.SetPubSub(v.PubSub()) v.PubSub().Publish("game."+gameID, nil)
})
snakeStore.SetNotifyFunc(func(gameID string) {
v.PubSub().Publish("snake."+gameID, nil)
})
// Home page - tabbed lobby // Home page - tabbed lobby
v.Page("/", func(c *via.Context) { v.Page("/", func(c *via.Context) {

View File

@@ -6,10 +6,6 @@ import (
"sync" "sync"
) )
type PubSub interface {
Publish(subject string, data []byte) error
}
type Persister interface { type Persister interface {
SaveSnakeGame(sg *SnakeGame) error SaveSnakeGame(sg *SnakeGame) error
LoadSnakeGame(id string) (*SnakeGame, error) LoadSnakeGame(id string) (*SnakeGame, error)
@@ -22,7 +18,7 @@ type SnakeStore struct {
games map[string]*SnakeGameInstance games map[string]*SnakeGameInstance
gamesMu sync.RWMutex gamesMu sync.RWMutex
persister Persister persister Persister
pubsub PubSub notifyFunc func(gameID string)
} }
func NewSnakeStore() *SnakeStore { func NewSnakeStore() *SnakeStore {
@@ -35,14 +31,14 @@ func (ss *SnakeStore) SetPersister(p Persister) {
ss.persister = p ss.persister = p
} }
func (ss *SnakeStore) SetPubSub(ps PubSub) { func (ss *SnakeStore) SetNotifyFunc(f func(gameID string)) {
ss.pubsub = ps ss.notifyFunc = f
} }
func (ss *SnakeStore) makeNotify(gameID string) func() { func (ss *SnakeStore) makeNotify(gameID string) func() {
return func() { return func() {
if ss.pubsub != nil { if ss.notifyFunc != nil {
ss.pubsub.Publish("snake."+gameID, nil) ss.notifyFunc(gameID)
} }
} }
} }