Skip to content

fix(WebSocketClientManager): use localStorage for clients persistence#2127

Merged
kettanaito merged 4 commits intofeat/wsfrom
fix/ws-manager-clients
Apr 12, 2024
Merged

fix(WebSocketClientManager): use localStorage for clients persistence#2127
kettanaito merged 4 commits intofeat/wsfrom
fix/ws-manager-clients

Conversation

@kettanaito
Copy link
Copy Markdown
Member

@kettanaito kettanaito commented Apr 7, 2024

Changes

We have a bug that a new runtime doesn't really know about any other runtimes. It only keeps itself in the list of clients and notifies other runtimes about itself. That's not right.

BroadcastChannel is not a good API to introduce a shared state because one has to constantly keep that state in-sync. Instead, I propose we use localStorage to keep the serialized list of all the WebSocket clients for the app.

Roadmap

  • Rewrite src/core/ws/WebSocketClientManager.test.ts tests, the constructor signature and the behavior of WebSocketClientManager have changed.
  • Clear the localStorage key at some point.

@kettanaito kettanaito changed the title fix(ws): keep clients in localStorage fix(WebSocketClientManager): use localStorage for clients persistence Apr 7, 2024
@kettanaito kettanaito marked this pull request as ready for review April 12, 2024 18:50
if (typeof localStorage !== 'undefined') {
// When the worker clears the local storage key in "worker.stop()",
// also clear the in-memory clients map.
localStorage.removeItem = new Proxy(localStorage.removeItem, {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we would know when worker.stop() is called but the manager lives in ws.link() while the stop event is only ever available in handleWebSocketEvent().

This would be an insane context drilling. Instead, let's proxy the localStorage.removeItem() calls.

@kettanaito kettanaito force-pushed the fix/ws-manager-clients branch from aca9f05 to ee4ca2e Compare April 12, 2024 18:53
@kettanaito kettanaito merged commit ef0ebe3 into feat/ws Apr 12, 2024
@kettanaito kettanaito deleted the fix/ws-manager-clients branch April 12, 2024 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant