@devanachat/sdk 0.7.0 → 0.8.1

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,55 @@ 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.1] - Unreleased
8
+
9
+ ### Added
10
+
11
+ - History gaps: `TopicHistoryWindow.gaps` lists the IDs of the messages with a gap in front of them - the message above
12
+ in the window is not the real predecessor, an unknown number of messages in between was never fetched. A marker
13
+ disappears with its message (deleted, trimmed, the window replaced by `resetToLatest()` or `jumpTo()`) and is closed
14
+ once the messages before it are fetched (`fetchPrevious()` from it, or a later page reaching over it).
15
+ - `TopicHistoryWindow.resyncToLatest()` - the latest page merged on top of the loaded messages instead of replacing
16
+ them (a message in both is taken from the page); a full page that does not reach the loaded messages marks a gap.
17
+
18
+ ### Changed
19
+
20
+ - On a reconnect the `LATEST` windows of rooms with a time-limited history (`MaxAge`) keep the loaded messages and get
21
+ the missed ones with `resyncToLatest()` - the server may no longer serve the older ones. Full history rooms are still
22
+ reset to the latest page, ephemeral ones are left untouched.
23
+ - A snapshot of a window with a gap saves only the messages below the newest gap.
24
+
25
+ ### Fixed
26
+
27
+ - `TopicHistoryWindow.jumpTo()` asked for while another fetch of the window runs (e.g. the reset of a reconnect, or an
28
+ earlier jump) waits for it to end and then jumps, instead of returning at once without loading anything - a caller
29
+ checking `has(id)` afterwards took the message for missing.
30
+
31
+ ## [0.8.0] - Unreleased
32
+
33
+ ### Added
34
+
35
+ - **State snapshot** - restore the tracked state at the start of an application, before the connection is
36
+ established: `client.state.exportSnapshot()` gives a plain, JSON-serializable `StateSnapshot` (or `null` before the
37
+ first session), `client.state.importSnapshot(snapshot)` restores it into a new client. The snapshot holds the current
38
+ user, the joined spaces (with roles) and rooms (with the default topics, PM recipients), and the lists already
39
+ loaded: rooms of spaces, followed topics (unread counters), emoticons and relationships. Presence is left out (every
40
+ user is saved as `Offline`), as are members of spaces and rooms, message histories, permission overwrites and other
41
+ topics.
42
+ The import emits the usual change events of the collections and resolves the getters waiting for the session. The
43
+ first `Session` then replaces the restored state like on a reconnect (removed spaces and rooms dropped, together with
44
+ their followed topics and emoticons; the rest updated in place; the lazily loaded lists refetched on next access).
45
+ A snapshot of another `version` (`STATE_SNAPSHOT_VERSION`, now `1`) or malformed data is rejected with `false`,
46
+ without changing the state and without throwing; so is an import after the first `Session` or a second import.
47
+ - Message histories in the snapshot: `exportSnapshot(intent)` takes a `StateSnapshotIntent` -
48
+ `histories: [{roomId, topicId?}]` (no `topicId` for the default topic of the room), `historyLimit` (messages per
49
+ topic, never fewer than the window's `fetchLimit`, 50 by default) and `roomMembersLimit` (members of the rooms of the
50
+ histories, saved when loaded and not more than that; 0 by default). Only a window holding the latest messages
51
+ (`LATEST`) of a room that is not ephemeral is saved, with the reactions of the user and the topics the messages refer
52
+ to. The import restores the window in the `LATEST` state, so it is shown without a request; the first `Session`
53
+ fetches it anew (as on a reconnect) and fetches the restored topics other than the default ones.
54
+ - Type exports `StateSnapshot`, `StateSnapshotIntent`, `TopicHistorySnapshot`, `RoomMembersSnapshot`,
55
+ `SpaceRoomsSnapshot`, `RoomFollowedTopicsSnapshot`, `EmoticonsSnapshot` and the `STATE_SNAPSHOT_VERSION` constant.
7
56
  ## [0.7.0] - Unreleased
8
57
 
9
58
  ### 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