Signals were always being sent down - changed flag never cleared. This wiped out the user input area in chat. (#13)

Publish every 100ms

Co-authored-by: João Gonçalves <joao.goncalves01@gmail.com>
This commit is contained in:
Jeff Winkler
2025-11-15 06:40:07 -05:00
committed by GitHub
parent 03019364ca
commit 762635d7d9
3 changed files with 21 additions and 11 deletions

View File

@@ -273,11 +273,21 @@ func (c *Context) SyncElements(elem h.H) {
// SyncSignals pushes the current signal changes to the browser immediately // SyncSignals pushes the current signal changes to the browser immediately
// over the live SSE event stream. // over the live SSE event stream.
func (c *Context) SyncSignals() { func (c *Context) SyncSignals() {
patchChan := c.getPatchChan() sse := c.getSSE()
c.mutex.RLock() if sse == nil {
updatedSigs := c.prepareSignalsForPatch() c.app.logWarn(c, "signals out of sync: no sse stream")
defer c.mutex.RUnlock() return
}
updatedSigs := make(map[string]any)
for id, sig := range c.signals {
if sig.err != nil {
c.app.logWarn(c, "signal out of sync'%s': %v", sig.id, sig.err)
}
if sig.changed && sig.err == nil {
updatedSigs[id] = fmt.Sprintf("%v", sig.v)
sig.changed = false
}
}
if len(updatedSigs) != 0 { if len(updatedSigs) != 0 {
outgoingSignals, _ := json.Marshal(updatedSigs) outgoingSignals, _ := json.Marshal(updatedSigs)
patchChan <- patch{patchTypeSignals, string(outgoingSignals)} patchChan <- patch{patchTypeSignals, string(outgoingSignals)}

View File

@@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"math/rand" "math/rand"
"github.com/go-via/via" "github.com/go-via/via"
@@ -84,6 +83,11 @@ func main() {
} }
`)), `)),
) )
// Uncomment for inspector. Plugin candidate.
// v.AppendToFoot(
// h.Script(h.Src("https://cdn.jsdelivr.net/gh/dataSPA/dataSPA-inspector@latest/dataspa-inspector.bundled.js"), h.Type("module")),
// h.Raw("<dataspa-inspector/>"),
// )
rooms := NewRooms[Chat, UserInfo]("Clojure", "Dotnet", "Go", "Java", "JS", "Kotlin", "Python", "Rust") rooms := NewRooms[Chat, UserInfo]("Clojure", "Dotnet", "Go", "Java", "JS", "Kotlin", "Python", "Rust")
rooms.Start() rooms.Start()
@@ -103,9 +107,7 @@ func main() {
if !ok { if !ok {
return return
} }
fmt.Println(">> switchRoom to ", newRoom.Name)
if currentRoom != nil && currentRoom != newRoom { if currentRoom != nil && currentRoom != newRoom {
fmt.Println("LEAVING old room")
currentRoom.Leave(&currentUser) currentRoom.Leave(&currentUser)
} }
newRoom.Join(&UserAndSync[Chat, UserInfo]{user: &currentUser, sync: c}) newRoom.Join(&UserAndSync[Chat, UserInfo]{user: &currentUser, sync: c})
@@ -136,8 +138,6 @@ func main() {
}) })
}) })
statement.SetValue("") statement.SetValue("")
// Update the UI right away so feels snappy.
c.Sync()
} }
}) })

View File

@@ -129,7 +129,7 @@ func NewRoom[TR any, TU comparable](n string) *Room[TR, TU] {
func (r *Room[TR, TU]) run() { func (r *Room[TR, TU]) run() {
defer close(r.done) defer close(r.done)
publishTicker := time.NewTicker(250 * time.Millisecond) publishTicker := time.NewTicker(100 * time.Millisecond)
defer publishTicker.Stop() defer publishTicker.Stop()
for { for {
select { select {