diff --git a/context.go b/context.go index 2d0a8a9..b0a84bd 100644 --- a/context.go +++ b/context.go @@ -189,7 +189,7 @@ func (c *Context) injectSignals(sigs map[string]any) { } } -func (c *Context) getSSE() chan patch { +func (c *Context) getPatchChan() chan patch { // components use parent page sse stream var patchChan chan patch if c.isComponent() { @@ -220,12 +220,13 @@ func (c *Context) prepareSignalsForPatch() 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() { + patchChan := c.getPatchChan() elemsPatch := bytes.NewBuffer(make([]byte, 0)) if err := c.view().Render(elemsPatch); err != nil { c.app.logErr(c, "sync view failed: %v", err) return } - c.patchChan <- patch{patchTypeElements, elemsPatch.String()} + patchChan <- patch{patchTypeElements, elemsPatch.String()} c.mutex.RLock() defer c.mutex.RUnlock() @@ -233,7 +234,7 @@ func (c *Context) Sync() { if len(updatedSigs) != 0 { outgoingSigs, _ := json.Marshal(updatedSigs) - c.patchChan <- patch{patchTypeSignals, string(outgoingSigs)} + patchChan <- patch{patchTypeSignals, string(outgoingSigs)} } } @@ -255,11 +256,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) { - sse := c.getSSE() - if sse == nil { - c.app.logWarn(c, "elements out of sync: no sse stream") - return - } + patchChan := c.getPatchChan() if c.view == nil { c.app.logErr(c, "sync element failed: viewfn is nil") return @@ -270,29 +267,29 @@ func (c *Context) SyncElements(elem h.H) { } b := bytes.NewBuffer(make([]byte, 0)) _ = elem.Render(b) - c.patchChan <- patch{patchTypeElements, b.String()} + patchChan <- patch{patchTypeElements, b.String()} } // SyncSignals pushes the current signal changes to the browser immediately // over the live SSE event stream. func (c *Context) SyncSignals() { + patchChan := c.getPatchChan() c.mutex.RLock() updatedSigs := c.prepareSignalsForPatch() defer c.mutex.RUnlock() if len(updatedSigs) != 0 { outgoingSignals, _ := json.Marshal(updatedSigs) - c.patchChan <- patch{patchTypeSignals, string(outgoingSignals)} + patchChan <- patch{patchTypeSignals, string(outgoingSignals)} } } func (c *Context) ExecScript(s string) { - sse := c.getSSE() - if sse == nil { - c.app.logWarn(c, "script out of sync: no sse stream") + if s == "" { return } - c.patchChan <- patch{patchTypeScript, s} + patchChan := c.getPatchChan() + patchChan <- patch{patchTypeScript, s} } func newContext(id string, route string, app *V) *Context { diff --git a/internal/examples/chatroom/main.go b/internal/examples/chatroom/main.go index 641845c..f06af41 100644 --- a/internal/examples/chatroom/main.go +++ b/internal/examples/chatroom/main.go @@ -111,11 +111,11 @@ func main() { newRoom.Join(&UserAndSync[Chat, UserInfo]{user: ¤tUser, sync: c}) currentRoom = newRoom roomNameString = newRoom.Name - c.Sync() } switchRoomAction := c.Action(func() { switchRoom() + c.Sync() }) switchRoom() diff --git a/internal/examples/realtimechart/main.go b/internal/examples/realtimechart/main.go index f73f3ee..77bd544 100644 --- a/internal/examples/realtimechart/main.go +++ b/internal/examples/realtimechart/main.go @@ -13,6 +13,11 @@ import ( func main() { v := via.New() + v.Config(via.Options{ + LogLvl: via.LogLevelDebug, + DevMode: true, + }) + v.AppendToHead( h.Link(h.Rel("stylesheet"), h.Href("https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css")), h.Script(h.Src("https://unpkg.com/echarts@6.0.0/dist/echarts.min.js")), @@ -35,7 +40,7 @@ func chartCompFn(c *via.Context) { data := make([]float64, 1000) labels := make([]string, 1000) - isLive := false + isLive := true refreshRate := c.Signal(1) tkr := time.NewTicker(1000 * time.Millisecond) @@ -71,7 +76,7 @@ func chartCompFn(c *via.Context) { return h.Div( h.Div(h.ID("chart"), h.Style("width:100%;height:400px;"), h.Script(h.Raw(` - const prefersDark = window.matchMedia('(prefers-color-scheme: dark)'); + prefersDark = window.matchMedia('(prefers-color-scheme: dark)'); var myChart = echarts.init(document.getElementById('chart'), prefersDark.matches ? 'dark' : 'light'); var option = { backgroundColor: prefersDark.matches ? 'transparent' : '#ffffff', diff --git a/via.go b/via.go index 30663fb..119d88c 100644 --- a/via.go +++ b/via.go @@ -153,6 +153,7 @@ func (v *V) Page(route string, initContextFn func(c *Context)) { bottomBodyElements = append(bottomBodyElements, v.documentFootIncludes...) if v.cfg.DevMode { bottomBodyElements = append(bottomBodyElements, h.Script(h.Type("module"), h.Src("https://cdn.jsdelivr.net/gh/dataSPA/dataSPA-inspector@latest/dataspa-inspector.bundled.js"))) + bottomBodyElements = append(bottomBodyElements, h.Raw("")) } view := h.HTML5(h.HTML5Props{ Title: v.cfg.DocumentTitle,