diff --git a/pkg/server/ui/websessions.go b/pkg/server/ui/websessions.go index 8fac497..0075ee8 100644 --- a/pkg/server/ui/websessions.go +++ b/pkg/server/ui/websessions.go @@ -3,6 +3,7 @@ package ui import ( "context" "git.wamblee.org/converge/pkg/models" + "git.wamblee.org/converge/pkg/support/throttling" "log" "net" "net/http" @@ -24,7 +25,8 @@ type WebSession struct { func NewWebSessions(notifications chan *models.State) *WebSessions { websessions := &WebSessions{ - sessions: make(map[*WebSession]bool), + sessions: make(map[*WebSession]bool), + lastNotification: models.NewState(), } go func() { @@ -76,7 +78,16 @@ func (sessions *WebSessions) NewSession(wsConnection net.Conn, ctx context.Conte conn: wsConnection, ctx: ctx, } + + // initial notification as soon as client connects. go func() { + session.notifications <- sessions.lastNotification + }() + + go func() { + throttler := throttling.NewAsyncThrottler(func(state *models.State) { + session.notifications <- state + }, time.Second, time.Second) for { // the web app opens one websocket connection and sends a hello // message asking for the latest state when a page is loaded that requires this. @@ -84,7 +95,7 @@ func (sessions *WebSessions) NewSession(wsConnection net.Conn, ctx context.Conte p := make([]byte, 1024) _, err := wsConnection.Read(p) if err == nil { - session.notifications <- sessions.lastNotification + throttler.Notify(sessions.lastNotification) } else { log.Printf("Got error reading %v", err) cancel()