diff options
| author | ojacobson <ojacobson@noreply.codeberg.org> | 2025-07-01 02:45:39 +0200 |
|---|---|---|
| committer | ojacobson <ojacobson@noreply.codeberg.org> | 2025-07-01 02:45:39 +0200 |
| commit | c0c825477e476d6d7331bfc409bceff9c376b484 (patch) | |
| tree | 2658c58137c64beec670b1a87b46e6ba4d6eb7e7 /ui/lib/state/remote | |
| parent | 022ae4ecf5f05b72280b1d702cb92d6795485639 (diff) | |
| parent | 7778cdf0c495a04f4f5f3f85b78348c8037a5771 (diff) | |
Send back the current state as events, not snapshots, during client boot.
There are a couple of contributing reasons for this.
* Each client's author - even ourselves - is best positioned to know how best to convert history into state to meet the needs of that specific client. There is (probably) no universal solution.
You can already see this with the built-in client, where unread tracking gets stapled onto snapshots locally and maintained as events roll in, and I would expect this to happen more and more regularly over time. If we ever sprout other clients, I'd also expect their local state to be different.
The API, on the other hand, must expose a surface that's universal to all clients. For boot, that was a very rote list-of-nouns data model. The other option is to expose a surface specific to one client and make other clients accommodate, which is contrary to the goals of this project.
* The need to compute snapshots adds friction when adding or changing the behaviour of the API, even when those changes only tangentially touch `/api/boot`. For example, my work on adding messages to multiple conversations got hung up in trying to figure out how to represent that at boot time, plus how to represent that in the event stream.
* The rationale for sending back a computed snapshot of the state was to avoid having the client replay events from the beginning of time, and to limit the amount of data sent back. This didn't pan out - most snapshots in practice consisted of the same data you'd get from the event stream anyways, almost with a 1:1 correspondence (with a `sent` or `created` event standing in for a `messages`, `channels`, or `users` entry). Exceptions - deleted messages and channels - were rare, and are ephemeral.
* Generating the snapshots requires loading the entire history into memory anyways. We're not saving any server-side IO by computing snapshots, but we are spending server-side compute time to generate them for clients that are then going to throw them away, as above.
This change resolves these tensions by delegating state management _entirely_ to the client, removing the server-side state snapshots. The server communicates in events only.
## Alternatives
I joked to @wlonk that the "2.0-bis" version of this change always returns `resume_point` 0 and an empty events list. That would be correct, and compatible with the client logic in this change, and would actually work. In fact, we could get rid of the event part of `/api/boot` entirely, and require clients to consume the event stream from the beginning every time they reconnect.
The main reason I _don't_ want to do this has to do with reconnects. Right now - both with snapshots, before this change, and with events, after - the client can cleanly delineate "historical" events (to be applied while the state is not presented to the user) and "current" events (to be presented to the user immediately). The `application/event-stream` protocol has no way to make that distinction out of the box, and while we can hack something in, all the approaches I can think of are nasty.
Merges boot-events into main.
Diffstat (limited to 'ui/lib/state/remote')
| -rw-r--r-- | ui/lib/state/remote/channels.svelte.js | 9 | ||||
| -rw-r--r-- | ui/lib/state/remote/messages.svelte.js | 9 | ||||
| -rw-r--r-- | ui/lib/state/remote/state.svelte.js | 22 | ||||
| -rw-r--r-- | ui/lib/state/remote/users.svelte.js | 11 |
4 files changed, 11 insertions, 40 deletions
diff --git a/ui/lib/state/remote/channels.svelte.js b/ui/lib/state/remote/channels.svelte.js index b2888cb..1e40075 100644 --- a/ui/lib/state/remote/channels.svelte.js +++ b/ui/lib/state/remote/channels.svelte.js @@ -19,15 +19,6 @@ class Channel { export class Channels { all = $state([]); - static boot(channels) { - const all = channels.map((channel) => Channel.boot(channel)); - return new Channels({ all }); - } - - constructor({ all }) { - this.all = all; - } - add({ at, id, name }) { this.all.push(Channel.boot({ at, id, name })); } diff --git a/ui/lib/state/remote/messages.svelte.js b/ui/lib/state/remote/messages.svelte.js index 7ce28b4..1be001b 100644 --- a/ui/lib/state/remote/messages.svelte.js +++ b/ui/lib/state/remote/messages.svelte.js @@ -26,15 +26,6 @@ class Message { export class Messages { all = $state([]); - static boot(messages) { - const all = messages.map(Message.boot); - return new Messages({ all }); - } - - constructor({ all }) { - this.all = all; - } - add({ id, at, channel, sender, body }) { const message = Message.boot({ id, at, channel, sender, body }); this.all.push(message); diff --git a/ui/lib/state/remote/state.svelte.js b/ui/lib/state/remote/state.svelte.js index a30e0fe..fb46489 100644 --- a/ui/lib/state/remote/state.svelte.js +++ b/ui/lib/state/remote/state.svelte.js @@ -4,27 +4,25 @@ import { Messages } from './messages.svelte.js'; export class State { currentUser = $state(); - users = $state(); - channels = $state(); - messages = $state(); + users = $state(new Users()); + channels = $state(new Channels()); + messages = $state(new Messages()); - static boot({ currentUser, heartbeat, users, channels, messages, resumePoint }) { - return new State({ + static boot({ currentUser, heartbeat, resumePoint, events }) { + const state = new State({ currentUser: User.boot(currentUser), heartbeat, - users: Users.boot(users), - channels: Channels.boot(channels), - messages: Messages.boot(messages), resumePoint, }); + for (const event of events) { + state.onEvent(event); + } + return state; } - constructor({ currentUser, heartbeat, users, channels, messages, resumePoint }) { + constructor({ currentUser, heartbeat, resumePoint }) { this.currentUser = currentUser; this.heartbeat = heartbeat; - this.users = users; - this.channels = channels; - this.messages = messages; this.resumePoint = resumePoint; } diff --git a/ui/lib/state/remote/users.svelte.js b/ui/lib/state/remote/users.svelte.js index a15d1da..391ab3a 100644 --- a/ui/lib/state/remote/users.svelte.js +++ b/ui/lib/state/remote/users.svelte.js @@ -16,16 +16,7 @@ export class User { } export class Users { - all = $state(); - - static boot(users) { - const all = new SvelteMap(users.map((user) => [user.id, User.boot(user)])); - return new Users({ all }); - } - - constructor({ all }) { - this.all = all; - } + all = $state(new SvelteMap()); add({ id, name }) { this.all.set(id, new User(id, name)); |
