3 Commits

Author SHA1 Message Date
Ryan Hamamura
e68e4b48f5 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.
2026-02-20 12:37:28 -10:00
Ryan Hamamura
91b5f2b80c deps: update ryanhamamura/via to v0.23.0
All checks were successful
Deploy c4 / deploy (push) Successful in 57s
Remove ContextSuspendAfter and ContextTTL options, which were
deleted upstream. Contexts now persist until SSE close beacon
or server shutdown.
2026-02-20 12:06:51 -10:00
Ryan Hamamura
73b6e3bcc5 deps: update ryanhamamura/via to v0.21.2
All checks were successful
Deploy c4 / deploy (push) Successful in 1m0s
2026-02-20 09:26:24 -10:00
5 changed files with 23 additions and 27 deletions

View File

@@ -6,10 +6,6 @@ import (
"sync"
)
type PubSub interface {
Publish(subject string, data []byte) error
}
type PlayerSession struct {
Player *Player
}
@@ -26,7 +22,7 @@ type GameStore struct {
games map[string]*GameInstance
gamesMu sync.RWMutex
persister Persister
pubsub PubSub
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)
}
}
}

2
go.mod
View File

@@ -6,7 +6,7 @@ require (
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/pressly/goose/v3 v3.26.0
github.com/ryanhamamura/via v0.19.0
github.com/ryanhamamura/via v0.23.0
golang.org/x/crypto v0.47.0
modernc.org/sqlite v1.44.0
)

6
go.sum
View File

@@ -78,8 +78,10 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
github.com/ryanhamamura/via v0.19.0 h1:mNQLOZAkforeiSVaoIUX5Mc1kDYvNKDhL33jV/0sArM=
github.com/ryanhamamura/via v0.19.0/go.mod h1:rpJewNVG6tgginZN7Be3qqRuol70+v1sFCKD4UjHsQo=
github.com/ryanhamamura/via v0.21.2 h1:osR6peY/mZSl9SNPeEv6IvzGU0akkQfQzQJgA74+7mk=
github.com/ryanhamamura/via v0.21.2/go.mod h1:rpJewNVG6tgginZN7Be3qqRuol70+v1sFCKD4UjHsQo=
github.com/ryanhamamura/via v0.23.0 h1:0e7nytisazcWq7uxs6T27GM3FwzosCMenkxJd+78Lko=
github.com/ryanhamamura/via v0.23.0/go.mod h1:rpJewNVG6tgginZN7Be3qqRuol70+v1sFCKD4UjHsQo=
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
github.com/starfederation/datastar-go v1.0.3 h1:DnzgsJ6tDHDM6y5Nxsk0AGW/m8SyKch2vQg3P1xGTcU=

10
main.go
View File

@@ -76,15 +76,17 @@ func main() {
ServerAddress: ":" + port(),
SessionManager: sessionManager,
Plugins: []via.Plugin{DaisyUIPlugin},
ContextSuspendAfter: 5 * time.Minute,
ContextTTL: 30 * time.Minute,
})
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) {

View File

@@ -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)
@@ -22,7 +18,7 @@ type SnakeStore struct {
games map[string]*SnakeGameInstance
gamesMu sync.RWMutex
persister Persister
pubsub PubSub
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)
}
}
}