Chatroom 2 (#10)

* Remove unused method. Don't panic if unknown room.

* Need a Connected() check for rooms publishing - don't do the work of rendering for a dead connection

* Make vars private.

* Linter issues

* Remove Connected()

* Mutation observer. Publish 4x / second.

---------

Co-authored-by: João Gonçalves <joao.goncalves01@gmail.com>
This commit is contained in:
Jeff Winkler
2025-11-13 10:39:37 -05:00
committed by GitHub
parent 7670926733
commit 351bed3ea1
7 changed files with 34 additions and 46 deletions

View File

@@ -182,9 +182,7 @@ func (c *Context) injectSignals(sigs map[string]any) {
}
}
// Sync pushes the current view state and signal changes to the browser immediately
// over the live SSE event stream.
func (c *Context) Sync() {
func (c *Context) getSSE() *datastar.ServerSentEventGenerator {
// components use parent page sse stream
var sse *datastar.ServerSentEventGenerator
if c.isComponent() {
@@ -192,6 +190,13 @@ func (c *Context) Sync() {
} else {
sse = c.sse
}
return sse
}
// Sync pushes the current view state and signal changes to the browser immediately
// over the live SSE event stream.
func (c *Context) Sync() {
sse := c.getSSE()
if sse == nil {
c.app.logWarn(c, "view out of sync: no sse stream")
return
@@ -234,12 +239,7 @@ func (c *Context) Sync() {
// Then, the merge will only occur if the ID of the top level element in the patch
// matches 'my-element'.
func (c *Context) SyncElements(elem h.H) {
var sse *datastar.ServerSentEventGenerator
if c.isComponent() {
sse = c.parentPageCtx.sse
} else {
sse = c.sse
}
sse := c.getSSE()
if sse == nil {
c.app.logWarn(c, "elements out of sync: no sse stream")
return
@@ -260,12 +260,7 @@ func (c *Context) SyncElements(elem h.H) {
// SyncSignals pushes the current signal changes to the browser immediately
// over the live SSE event stream.
func (c *Context) SyncSignals() {
var sse *datastar.ServerSentEventGenerator
if c.isComponent() {
sse = c.parentPageCtx.sse
} else {
sse = c.sse
}
sse := c.getSSE()
if sse == nil {
c.app.logWarn(c, "signals out of sync: no sse stream")
return
@@ -285,12 +280,7 @@ func (c *Context) SyncSignals() {
}
func (c *Context) ExecScript(s string) {
var sse *datastar.ServerSentEventGenerator
if c.isComponent() {
sse = c.parentPageCtx.sse
} else {
sse = c.sse
}
sse := c.getSSE()
if sse == nil {
c.app.logWarn(c, "script out of sync: no sse stream")
return