diff --git a/CHANGELOG.md b/CHANGELOG.md index 03727f47..e15ec1b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ tagged `vX.Y.Z`. ### Fixed - **Application icon access.** Widgets that subscribe to `dock` receive icons through the dashboard host; cross-site requests to the icon route are refused, so other widget frames cannot probe installed applications. +- **Fresh widget data after a provider retires.** Socket replay caches discard a retired channel snapshot for newly opened tiles and after a server disconnect. Already open tiles keep their last rendered value until the replacement provider publishes fresh data. - **Widgets load on the native dashboard again.** WebKit can now load the bridge and a widget's own script, style, image, font and media files inside the sandboxed frame. - Shortcut buttons draw the real icon of an application that is not in the Dock again. The widget diff --git a/server/src/ws/hub.ts b/server/src/ws/hub.ts index 1a687321..01df62e3 100644 --- a/server/src/ws/hub.ts +++ b/server/src/ws/hub.ts @@ -45,9 +45,9 @@ export class Hub { this.contexts.set(socket, ctx) socket.on('message', (raw: Buffer | string) => this.onMessage(socket, channels, raw)) socket.on('close', () => { - for (const ch of channels) this.registry.removeSubscriber(ch) this.clients.delete(socket) this.contexts.delete(socket) + for (const ch of channels) this.registry.removeSubscriber(ch) }) } @@ -58,9 +58,14 @@ export class Hub { * channel last said belonged to a connection or a provider that is gone, and replaying it to * the next subscriber would show data from credentials that no longer exist. A widget that * subscribes after this waits for the first fresh poll instead, which the registry runs at once. + * Current socket clients also discard their replay cache; already open widgets keep their rendered value until the next publish. */ forget(channel: string): void { this.lastValues.delete(channel) + const msg = JSON.stringify({ type: 'invalidate', channel }) + for (const [socket, channels] of this.clients) { + if (channels.has(channel)) this.safeSend(socket, msg) + } } broadcast(channel: string, data: unknown): void { diff --git a/server/test/hub.test.ts b/server/test/hub.test.ts index 9d3d859d..fe558f53 100644 --- a/server/test/hub.test.ts +++ b/server/test/hub.test.ts @@ -103,6 +103,25 @@ describe('the cached last value of a channel', () => { expect(subscribe(hub, 'homey:old').sent).toEqual([]) }) + it('invalidates only live subscribers when a provider is replaced', () => { + let hub!: Hub + const registry = new ProviderRegistry((c, d) => hub.broadcast(c, d), (c) => hub.forget(c)) + hub = new Hub(registry) + registry.register({ channel: 'homey:x', intervalMs: 1000 }) + const active = subscribe(hub, 'homey:x') + const unrelated = subscribe(hub, 'config') + hub.broadcast('homey:x', { devices: ['old host'] }) + + registry.register({ channel: 'homey:x', intervalMs: 1000 }) + + expect(active.sent).toEqual([ + { type: 'data', channel: 'homey:x', data: { devices: ['old host'] } }, + { type: 'invalidate', channel: 'homey:x' }, + ]) + expect(unrelated.sent).toEqual([]) + expect(subscribe(hub, 'homey:x').sent).toEqual([]) + }) + it('is dropped when the provider is replaced, so the old host is not shown', () => { let hub!: Hub const registry = new ProviderRegistry((c, d) => hub.broadcast(c, d), (c) => hub.forget(c)) diff --git a/ui/src/shared/socket.ts b/ui/src/shared/socket.ts index 77ffa5bb..ae00b8e5 100644 --- a/ui/src/shared/socket.ts +++ b/ui/src/shared/socket.ts @@ -35,6 +35,8 @@ export function useSocket(): Socket { try { m = JSON.parse(ev.data) } catch { console.warn('socket: invalid JSON message', ev.data); return } if (m.type === 'data' && m.channel) { last.set(m.channel, m.data); subs.get(m.channel)?.forEach((cb) => cb(m.data)) } + // A provider can retire a channel while widgets remain subscribed. Do not replay its old snapshot. + else if (m.type === 'invalidate' && m.channel) last.delete(m.channel) else if (m.type === 'result' || m.type === 'error') { if (!m.id) return const p = pending.get(m.id) @@ -45,6 +47,8 @@ export function useSocket(): Socket { } ws.onclose = () => { status.value = 'closed' + // Invalidation messages may have been missed; wait for fresh data after reconnect. + last.clear() for (const p of pending.values()) p.reject(new Error(t('socket.lost'))) pending.clear() setTimeout(connect, retryMs) diff --git a/ui/test/socket-cache-invalidation.test.ts b/ui/test/socket-cache-invalidation.test.ts new file mode 100644 index 00000000..65de6e4b --- /dev/null +++ b/ui/test/socket-cache-invalidation.test.ts @@ -0,0 +1,69 @@ +// @vitest-environment jsdom +import { afterEach, expect, it, vi } from 'vitest' + +class FakeWebSocket { + static OPEN = 1 + static latest: FakeWebSocket + readyState = 1 + onopen: (() => void) | null = null + onmessage: ((event: { data: string }) => void) | null = null + onclose: (() => void) | null = null + onerror: (() => void) | null = null + sent: unknown[] = [] + + constructor() { FakeWebSocket.latest = this } + send(message: string) { this.sent.push(JSON.parse(message)) } + receive(message: unknown) { this.onmessage?.({ data: JSON.stringify(message) }) } +} + +afterEach(() => { + vi.unstubAllGlobals() + vi.resetModules() +}) + +it('does not replay a retired snapshot to a later subscriber', async () => { + vi.stubGlobal('WebSocket', FakeWebSocket) + const { useSocket } = await import('../src/shared/socket') + const socket = useSocket() + const wire = FakeWebSocket.latest + wire.onopen?.() + const first: unknown[] = [] + const second: unknown[] = [] + const offFirst = socket.subscribe('service:account-a', (value) => first.push(value)) + + wire.receive({ type: 'data', channel: 'service:account-a', data: 'old snapshot' }) + wire.receive({ type: 'invalidate', channel: 'service:account-a' }) + const offSecond = socket.subscribe('service:account-a', (value) => second.push(value)) + expect(second).toEqual([]) + expect(wire.sent).toEqual([{ type: 'subscribe', channel: 'service:account-a' }]) + + wire.receive({ type: 'data', channel: 'service:account-a', data: 'fresh snapshot' }) + expect(first).toEqual(['old snapshot', 'fresh snapshot']) + expect(second).toEqual(['fresh snapshot']) + offFirst() + offSecond() +}) + +it('does not replay a snapshot after a disconnect that may have missed invalidation', async () => { + vi.useFakeTimers() + try { + vi.stubGlobal('WebSocket', FakeWebSocket) + const { useSocket } = await import('../src/shared/socket') + const socket = useSocket() + const wire = FakeWebSocket.latest + wire.onopen?.() + const offFirst = socket.subscribe('service:account-a', () => {}) + wire.receive({ type: 'data', channel: 'service:account-a', data: 'before disconnect' }) + wire.readyState = 3 + wire.onclose?.() + + const second: unknown[] = [] + const offSecond = socket.subscribe('service:account-a', (value) => second.push(value)) + expect(second).toEqual([]) + offFirst() + offSecond() + } finally { + vi.clearAllTimers() + vi.useRealTimers() + } +})