@devanachat/sdk 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,32 @@ All notable changes to this project are documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project follows
5
5
  [semver](https://semver.org) (`0.x` minor releases may break the API).
6
6
 
7
+ ## [0.8.0] - Unreleased
8
+
9
+ ### Added
10
+
11
+ - **State snapshot** - restore the tracked state at the start of an application, before the connection is
12
+ established: `client.state.exportSnapshot()` gives a plain, JSON-serializable `StateSnapshot` (or `null` before the
13
+ first session), `client.state.importSnapshot(snapshot)` restores it into a new client. The snapshot holds the current
14
+ user, the joined spaces (with roles) and rooms (with the default topics, PM recipients), and the lists already
15
+ loaded: rooms of spaces, followed topics (unread counters), emoticons and relationships. Presence is left out (every
16
+ user is saved as `Offline`), as are members of spaces and rooms, message histories, permission overwrites and other
17
+ topics.
18
+ The import emits the usual change events of the collections and resolves the getters waiting for the session. The
19
+ first `Session` then replaces the restored state like on a reconnect (removed spaces and rooms dropped, together with
20
+ their followed topics and emoticons; the rest updated in place; the lazily loaded lists refetched on next access).
21
+ A snapshot of another `version` (`STATE_SNAPSHOT_VERSION`, now `1`) or malformed data is rejected with `false`,
22
+ without changing the state and without throwing; so is an import after the first `Session` or a second import.
23
+ - Message histories in the snapshot: `exportSnapshot(intent)` takes a `StateSnapshotIntent` -
24
+ `histories: [{roomId, topicId?}]` (no `topicId` for the default topic of the room), `historyLimit` (messages per
25
+ topic, never fewer than the window's `fetchLimit`, 50 by default) and `roomMembersLimit` (members of the rooms of the
26
+ histories, saved when loaded and not more than that; 0 by default). Only a window holding the latest messages
27
+ (`LATEST`) of a room that is not ephemeral is saved, with the reactions of the user and the topics the messages refer
28
+ to. The import restores the window in the `LATEST` state, so it is shown without a request; the first `Session`
29
+ fetches it anew (as on a reconnect) and fetches the restored topics other than the default ones.
30
+ - Type exports `StateSnapshot`, `StateSnapshotIntent`, `TopicHistorySnapshot`, `RoomMembersSnapshot`,
31
+ `SpaceRoomsSnapshot`, `RoomFollowedTopicsSnapshot`, `EmoticonsSnapshot` and the `STATE_SNAPSHOT_VERSION` constant.
32
+
7
33
  ## [0.7.0] - Unreleased
8
34
 
9
35
  ### Added
package/README.md CHANGED
@@ -330,6 +330,37 @@ if (await client.state!.relationships.exists('user-id', 'Ignore')) {
330
330
  }
331
331
  ```
332
332
 
333
+ ### Restore the state at startup
334
+
335
+ An application can show the last known state right away, before the connection is established (or with no
336
+ connection at all): save a snapshot of the state and import it into a new client before `connect()`.
337
+
338
+ ```ts
339
+ // While running (e.g. throttled, and when the app goes to the background):
340
+ const snapshot = client.state!.exportSnapshot({
341
+ // Optional: the latest messages of the topics last open, members of their rooms (up to 200)
342
+ histories: [{roomId: lastRoomId}, {roomId: lastRoomId, topicId: lastThreadId}],
343
+ roomMembersLimit: 200,
344
+ }); // plain JSON object, or null before the first session
345
+ if (snapshot) {
346
+ storage.setItem(`snapshot:${accountId}`, JSON.stringify(snapshot));
347
+ }
348
+
349
+ // At the next start:
350
+ const client = new WebSocketChatClient({url, token});
351
+ const restored = client.state!.importSnapshot(JSON.parse(storage.getItem(`snapshot:${accountId}`) ?? 'null'));
352
+ // The spaces, rooms, unread counters, ... are available now; connect in the background.
353
+ client.connect();
354
+ ```
355
+
356
+ `importSnapshot()` returns `false` and changes nothing for a snapshot of another format version
357
+ (`STATE_SNAPSHOT_VERSION`) or malformed data, and after the first `Session`. The first `Session` replaces the
358
+ restored state as a reconnect does - a restored history window is fetched anew, like a window holding the latest
359
+ messages on a reconnect. The snapshot leaves out presence (every user is saved as `Offline`), members of spaces and of
360
+ rooms not asked for, other message histories, histories of ephemeral rooms, permission overwrites and other topics -
361
+ these are fetched once connected. Requests sent before the connection is established are queued while connecting and fail
362
+ when the client is waiting to retry.
363
+
333
364
  ## Package entitlements
334
365
 
335
366
  `GetEntitlements` answers what a space or the current account has. A space package and a user