Signals fix (#17)

* Fix signals bug, add test.

* Test
This commit is contained in:
Jeff Winkler
2025-11-16 04:51:04 -05:00
committed by GitHub
parent f7b5b24dd5
commit e0fa5560ab
2 changed files with 24 additions and 2 deletions

View File

@@ -280,10 +280,10 @@ func (c *Context) SyncSignals() {
}
updatedSigs := make(map[string]any)
c.signals.Range(func(idAny, val any) bool {
c.signals.Range(func(key, val any) bool {
// We know the types.
sig, _ := val.(*signal) // adjust *Signal to your actual signal type
id, _ := val.(string)
id, _ := key.(string)
if sig.err != nil {
c.app.logWarn(c, "signal out of sync'%s': %v", sig.id, sig.err)
}

View File

@@ -1,6 +1,7 @@
package via
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
@@ -84,3 +85,24 @@ func TestConfig(t *testing.T) {
v.Config(Options{DocumentTitle: "Test"})
assert.Equal(t, "Test", v.cfg.DocumentTitle)
}
func TestSyncSignals(t *testing.T) {
var ctx *Context
var sig *signal
v := New()
v.Page("/", func(c *Context) {
ctx = c
sig = c.Signal("initial")
c.View(func() h.H { return h.Div() })
})
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
v.mux.ServeHTTP(w, req)
sig.SetValue("updated")
ctx.SyncSignals()
patch := <-ctx.patchChan
assert.Equal(t, patch.content, fmt.Sprintf(`{"%s":"updated"}`, sig.ID()))
}