Skip to content

Commit

Permalink
fix: Clear dirty UI state across clients on broadcast/multicast user …
Browse files Browse the repository at this point in the history
…interaction for consistency. #1705 (#2123)
  • Loading branch information
mturoci authored Sep 4, 2023
1 parent 89e6768 commit 95b73f3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
18 changes: 14 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ const (
)

var (
newline = []byte{'\n'}
notFoundMsg = []byte(`{"e":"not_found"}`)
upgrader = websocket.Upgrader{
newline = []byte{'\n'}
notFoundMsg = []byte(`{"e":"not_found"}`)
disconnectMsg = []byte(`{"data": {"":{"@system":{"client_disconnect":true}}}}`)
clearStateMsg = []byte(`{"c":1}`)
upgrader = websocket.Upgrader{
ReadBufferSize: 1024, // TODO review
WriteBufferSize: 1024, // TODO review
}
Expand Down Expand Up @@ -94,7 +96,7 @@ func (c *Client) listen() {
defer func() {
app := c.broker.getApp(c.appPath)
if app != nil {
app.forward(c.id, c.session, []byte(`{"data": {"":{"@system":{"client_disconnect":true}}}}`))
app.forward(c.id, c.session, disconnectMsg)
if err := app.disconnect(c.id); err != nil {
echo(Log{"t": "disconnect", "client": c.addr, "route": c.appPath, "err": err.Error()})
}
Expand Down Expand Up @@ -148,6 +150,14 @@ func (c *Client) listen() {
}

app.forward(c.id, c.session, []byte("{\"data\":"+string(m.data)+"}"))

// Remove any dirty UI state if broadcast or multicast.
if app.mode == multicastMode {
c.broker.sendAll(c.broker.clients["/"+c.session.subject], clearStateMsg)
}
if app.mode == broadcastMode {
c.broker.sendAll(c.broker.clients[app.route], clearStateMsg)
}
case watchMsgT:
c.subscribe(m.addr) // subscribe even if page is currently NA

Expand Down
1 change: 1 addition & 0 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type OpsD struct {
U string `json:"u,omitempty"` // redirect
E string `json:"e,omitempty"` // error
M *Meta `json:"m,omitempty"` // metadata
C int `json:"c,omitempty"` // clear UI state
}

// Meta represents metadata unrelated to commands
Expand Down
8 changes: 8 additions & 0 deletions ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ interface OpsD {
u: S // active user's username
e: B // can the user edit pages?
}
c?: U // clear UI state
}
interface OpD {
k?: S
Expand Down Expand Up @@ -327,6 +328,8 @@ export enum WaveEventType {
Page,
/** Daemon sent some data. */
Data,
/** Daemon asked to clear any dirty state. */
Clear,
}

/** */
Expand All @@ -348,10 +351,13 @@ export type WaveEvent = {
t: WaveEventType.Disconnect, retry: U
} | {
t: WaveEventType.Data
} | {
t: WaveEventType.Clear
}
const
connectEvent: WaveEvent = { t: WaveEventType.Connect },
resetEvent: WaveEvent = { t: WaveEventType.Reset },
clearEvent: WaveEvent = { t: WaveEventType.Clear },
dataEvent: WaveEvent = { t: WaveEventType.Data }

type WaveEventHandler = (e: WaveEvent) => void
Expand Down Expand Up @@ -976,6 +982,8 @@ export const
handle({ t: WaveEventType.Page, page })
} else if (msg.e) {
handle({ t: WaveEventType.Error, code: errorCodes[msg.e] || WaveErrorCode.Unknown })
} else if (msg.c) {
handle(clearEvent)
} else if (msg.r) {
handle(resetEvent)
} else if (msg.u) {
Expand Down
5 changes: 4 additions & 1 deletion ui/src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ export const
case WaveEventType.Data:
busyB(false)
break
case WaveEventType.Clear:
clearRec(args)
break
}
})
},
Expand All @@ -149,7 +152,7 @@ export const

// Unconditionally set location hash so that the app doesn't have to track changes.
const h = window.location.hash
if (h?.length > 1) args['#'] = h.substr(1)
if (h?.length > 1) args['#'] = h.substring(1)

const d: Dict<any> = { ...args } // shallow clone
clearRec(args) // clear
Expand Down

0 comments on commit 95b73f3

Please sign in to comment.