1 Commits

Author SHA1 Message Date
Ryan Hamamura
d6e64763cc fix: use templ.NewOnceHandle to prevent script duplication on SSE patches
All checks were successful
CI / Deploy / test (pull_request) Successful in 15s
CI / Deploy / lint (pull_request) Successful in 25s
CI / Deploy / deploy (pull_request) Has been skipped
Replace ConnectionIndicatorWithScript wrapper with a single ConnectionIndicator
component that uses templ.NewOnceHandle() to ensure the watcher script is only
rendered once per page, even when the indicator is patched via SSE.
2026-03-03 10:43:23 -10:00
3 changed files with 7 additions and 10 deletions

View File

@@ -18,7 +18,7 @@ templ GamePage(g *connect4.Game, myColor int, messages []chat.Message, chatCfg c
data-signals="{chatMsg: ''}" data-signals="{chatMsg: ''}"
data-init={ fmt.Sprintf("@get('/games/%s/events',{requestCancellation:'disabled'})", g.ID) } data-init={ fmt.Sprintf("@get('/games/%s/events',{requestCancellation:'disabled'})", g.ID) }
> >
@sharedcomponents.ConnectionIndicatorWithScript(0) @sharedcomponents.ConnectionIndicator(0)
@GameContent(g, myColor, messages, chatCfg) @GameContent(g, myColor, messages, chatCfg)
</main> </main>
} }

View File

@@ -52,9 +52,10 @@ func isStale(lastPing int64) bool {
return lastPing == 0 return lastPing == 0
} }
var connectionWatcherHandle = templ.NewOnceHandle()
// ConnectionIndicator shows a small dot indicating SSE connection status. // ConnectionIndicator shows a small dot indicating SSE connection status.
// Server patches this with a timestamp; client JS detects staleness. // Server patches this with a timestamp; client JS detects staleness.
// Use ConnectionIndicatorWithScript for initial render, ConnectionIndicator for patches.
templ ConnectionIndicator(lastPing int64) { templ ConnectionIndicator(lastPing int64) {
<div <div
id="connection-indicator" id="connection-indicator"
@@ -80,13 +81,9 @@ templ ConnectionIndicator(lastPing int64) {
></div> ></div>
</div> </div>
</div> </div>
} @connectionWatcherHandle.Once() {
@connectionWatcher()
// ConnectionIndicatorWithScript renders the indicator with the staleness watcher script. }
// Use this for initial page render only.
templ ConnectionIndicatorWithScript(lastPing int64) {
@ConnectionIndicator(lastPing)
@connectionWatcher()
} }
script connectionWatcher() { script connectionWatcher() {

View File

@@ -37,7 +37,7 @@ templ GamePage(sg *snake.SnakeGame, mySlot int, messages []chat.Message, chatCfg
data-on:keydown__throttle.100ms={ keydownScript(gameID) } data-on:keydown__throttle.100ms={ keydownScript(gameID) }
tabindex="0" tabindex="0"
> >
@components.ConnectionIndicatorWithScript(0) @components.ConnectionIndicator(0)
@GameContent(sg, mySlot, messages, chatCfg, gameID) @GameContent(sg, mySlot, messages, chatCfg, gameID)
</main> </main>
} }