@devanachat/sdk 0.8.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 +24 -1
- package/build/index.cjs.js +136 -24
- package/build/index.cjs.js.map +1 -1
- package/build/index.mjs +136 -24
- package/build/index.mjs.map +1 -1
- package/build/index.umd.js +1 -1
- package/build/index.umd.js.map +1 -1
- package/build/types/state-tracker/RoomMessagesHistory.d.ts +10 -3
- package/build/types/state-tracker/TopicHistoryWindow.d.ts +31 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,30 @@ 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
|
+
|
|
7
31
|
## [0.8.0] - Unreleased
|
|
8
32
|
|
|
9
33
|
### Added
|
|
@@ -29,7 +53,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
|
29
53
|
fetches it anew (as on a reconnect) and fetches the restored topics other than the default ones.
|
|
30
54
|
- Type exports `StateSnapshot`, `StateSnapshotIntent`, `TopicHistorySnapshot`, `RoomMembersSnapshot`,
|
|
31
55
|
`SpaceRoomsSnapshot`, `RoomFollowedTopicsSnapshot`, `EmoticonsSnapshot` and the `STATE_SNAPSHOT_VERSION` constant.
|
|
32
|
-
|
|
33
56
|
## [0.7.0] - Unreleased
|
|
34
57
|
|
|
35
58
|
### Added
|
package/build/index.cjs.js
CHANGED
|
@@ -482,7 +482,8 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
482
482
|
retainRatio: 1,
|
|
483
483
|
fetchLimit: 50,
|
|
484
484
|
lastFetchCount: 0,
|
|
485
|
-
oldestId: null
|
|
485
|
+
oldestId: null,
|
|
486
|
+
gaps: []
|
|
486
487
|
};
|
|
487
488
|
|
|
488
489
|
/**
|
|
@@ -537,6 +538,16 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
537
538
|
get hasLatest() {
|
|
538
539
|
return [WindowState.LATEST, WindowState.LIVE].includes(this.state);
|
|
539
540
|
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* IDs of the items with a gap in front of them: the item right above in the window is not the real predecessor,
|
|
544
|
+
* an unknown number of items in between was never fetched. Such a seam appears when {@link resyncToLatest} gets
|
|
545
|
+
* a full page that does not reach the loaded items. A marker lives as long as its item stays in the window and
|
|
546
|
+
* is closed once the items right before it are fetched.
|
|
547
|
+
*/
|
|
548
|
+
get gaps() {
|
|
549
|
+
return this.internalState.gaps;
|
|
550
|
+
}
|
|
540
551
|
get hasOldest() {
|
|
541
552
|
return this.state === WindowState.OLDEST || this.state === WindowState.LATEST && this.length < this.fetchLimit || this.internalState.oldestId !== null && this.has(this.internalState.oldestId);
|
|
542
553
|
}
|
|
@@ -546,15 +557,51 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
546
557
|
}
|
|
547
558
|
let result;
|
|
548
559
|
const originalState = this.state;
|
|
549
|
-
this.
|
|
560
|
+
this.beginFetch(WindowState.LATEST);
|
|
550
561
|
try {
|
|
551
562
|
result = await this.fetchLatestItems();
|
|
552
563
|
this.internalState.lastFetchCount = result.length;
|
|
553
564
|
} finally {
|
|
554
|
-
this.
|
|
565
|
+
this.endFetch();
|
|
566
|
+
}
|
|
567
|
+
this.replaceItems(result);
|
|
568
|
+
this.internalState.current = WindowState.LATEST;
|
|
569
|
+
this.emitChangeWithDiff(true, originalState);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Refresh the window with the latest page, keeping the already loaded items instead of replacing them - the
|
|
574
|
+
* reconnect-friendly variant of {@link resetToLatest}. The page is merged on top of the loaded items (an item in
|
|
575
|
+
* both is taken from the page) and only the window size limit pushes the oldest ones out. When the page is full
|
|
576
|
+
* and does not reach the newest loaded item, both parts are still kept and the seam is recorded in {@link gaps}.
|
|
577
|
+
*/
|
|
578
|
+
async resyncToLatest() {
|
|
579
|
+
if (this.internalState.ongoing) {
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
let result;
|
|
583
|
+
const originalState = this.state;
|
|
584
|
+
this.beginFetch(WindowState.LATEST);
|
|
585
|
+
try {
|
|
586
|
+
result = await this.fetchLatestItems();
|
|
587
|
+
this.internalState.lastFetchCount = result.length;
|
|
588
|
+
} finally {
|
|
589
|
+
this.endFetch();
|
|
590
|
+
}
|
|
591
|
+
const loaded = this.items;
|
|
592
|
+
const fetchedIds = new Set(result.map(item => this.getId(item)));
|
|
593
|
+
const retained = loaded.filter(item => !fetchedIds.has(this.getId(item)));
|
|
594
|
+
|
|
595
|
+
// A page shorter than the limit is everything the remote side has, so nothing can be missing in between.
|
|
596
|
+
const isContinuous = !retained.length || result.length < this.internalState.fetchLimit || fetchedIds.has(this.getId(loaded[loaded.length - 1]));
|
|
597
|
+
|
|
598
|
+
// The page is continuous, so only its first item can have a gap in front of it.
|
|
599
|
+
this.forgetGapsOf(result.slice(1).map(item => this.getId(item)));
|
|
600
|
+
if (!isContinuous && !this.internalState.gaps.includes(this.getId(result[0]))) {
|
|
601
|
+
this.internalState.gaps = [...this.internalState.gaps, this.getId(result[0])];
|
|
555
602
|
}
|
|
556
603
|
this._items.deleteAll(); // Directly call deleteAll to prevent event emit.
|
|
557
|
-
this.addItems(result, 'tail');
|
|
604
|
+
this.addItems([...retained, ...result], 'tail');
|
|
558
605
|
this.internalState.current = WindowState.LATEST;
|
|
559
606
|
this.emitChangeWithDiff(true, originalState);
|
|
560
607
|
}
|
|
@@ -564,18 +611,18 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
564
611
|
}
|
|
565
612
|
let result;
|
|
566
613
|
const originalState = this.state;
|
|
567
|
-
|
|
614
|
+
const firstItem = this.getAt(0);
|
|
615
|
+
this.beginFetch(WindowState.PAST);
|
|
568
616
|
try {
|
|
569
617
|
result = await this.fetchItemsBefore();
|
|
570
618
|
this.internalState.lastFetchCount = result ? result.length : 0;
|
|
571
619
|
} finally {
|
|
572
|
-
this.
|
|
620
|
+
this.endFetch();
|
|
573
621
|
}
|
|
574
622
|
if (!result) {
|
|
575
623
|
return this.resetToLatest();
|
|
576
624
|
}
|
|
577
625
|
if (!result.length) {
|
|
578
|
-
const firstItem = this.getAt(0);
|
|
579
626
|
this.internalState.oldestId = firstItem ? this.getId(firstItem) : null;
|
|
580
627
|
await this.refreshFetchedState();
|
|
581
628
|
|
|
@@ -586,6 +633,10 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
586
633
|
this.emitChangeWithDiff(false, originalState);
|
|
587
634
|
return;
|
|
588
635
|
}
|
|
636
|
+
if (firstItem) {
|
|
637
|
+
// The page was fetched as the direct predecessors of the first item, so a gap in front of it is closed.
|
|
638
|
+
this.forgetGapsOf([this.getId(firstItem)]);
|
|
639
|
+
}
|
|
589
640
|
this.addItems(result, 'head');
|
|
590
641
|
await this.refreshFetchedState();
|
|
591
642
|
this.emitChangeWithDiff(true, originalState);
|
|
@@ -606,12 +657,12 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
606
657
|
}
|
|
607
658
|
let result;
|
|
608
659
|
const originalState = this.state;
|
|
609
|
-
this.
|
|
660
|
+
this.beginFetch(WindowState.PAST);
|
|
610
661
|
try {
|
|
611
662
|
result = await this.fetchItemsAfter();
|
|
612
663
|
this.internalState.lastFetchCount = result ? result.length : 0;
|
|
613
664
|
} finally {
|
|
614
|
-
this.
|
|
665
|
+
this.endFetch();
|
|
615
666
|
}
|
|
616
667
|
if (!result) {
|
|
617
668
|
await this.resetToLatest(true);
|
|
@@ -624,26 +675,41 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
624
675
|
return;
|
|
625
676
|
}
|
|
626
677
|
}
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* Load the items around the given one. Asked for while another fetch is running, it waits for that fetch to end
|
|
681
|
+
* instead of being dropped - when the call returns, the window either holds the item or it could not be loaded.
|
|
682
|
+
*/
|
|
627
683
|
async jumpTo(id) {
|
|
628
|
-
|
|
684
|
+
while (this.internalState.ongoing) {
|
|
685
|
+
await this.internalState.fetchSettled;
|
|
686
|
+
}
|
|
687
|
+
if (this.state !== WindowState.LIVE && this._items.has(id)) {
|
|
629
688
|
return;
|
|
630
689
|
}
|
|
631
690
|
let result;
|
|
632
691
|
const originalState = this.state;
|
|
633
|
-
this.
|
|
692
|
+
this.beginFetch(WindowState.PAST);
|
|
634
693
|
try {
|
|
635
694
|
result = await this.fetchItemsAround(id);
|
|
636
695
|
this.internalState.lastFetchCount = result ? result.length : 0;
|
|
637
696
|
if (result) {
|
|
638
|
-
this.
|
|
639
|
-
this.addItems(result, 'tail');
|
|
697
|
+
this.replaceItems(result);
|
|
640
698
|
await this.refreshFetchedState();
|
|
641
699
|
}
|
|
642
700
|
} finally {
|
|
643
|
-
this.
|
|
701
|
+
this.endFetch();
|
|
644
702
|
}
|
|
645
703
|
this.emitChangeWithDiff(!!result, originalState);
|
|
646
704
|
}
|
|
705
|
+
delete(...ids) {
|
|
706
|
+
super.delete(...ids);
|
|
707
|
+
this.forgetGapsOf(ids);
|
|
708
|
+
}
|
|
709
|
+
deleteAll() {
|
|
710
|
+
super.deleteAll();
|
|
711
|
+
this.internalState.gaps = [];
|
|
712
|
+
}
|
|
647
713
|
async refreshFetchedState() {
|
|
648
714
|
this.internalState.current = (await this.isLatestItemLoaded()) ? WindowState.LATEST : WindowState.PAST;
|
|
649
715
|
}
|
|
@@ -666,6 +732,29 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
666
732
|
this._items.set(...entries, ...loaded);
|
|
667
733
|
return this.trimTail(this.getLowWatermark(highWatermark));
|
|
668
734
|
}
|
|
735
|
+
beginFetch(kind) {
|
|
736
|
+
this.internalState.ongoing = kind;
|
|
737
|
+
this.internalState.fetchSettled = new Promise(resolve => this.internalState.settleFetch = resolve);
|
|
738
|
+
}
|
|
739
|
+
endFetch() {
|
|
740
|
+
this.internalState.ongoing = undefined;
|
|
741
|
+
this.internalState.settleFetch?.();
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* Replace the whole content without emitting an event; the old gap markers mean nothing for the new content.
|
|
746
|
+
*/
|
|
747
|
+
replaceItems(items) {
|
|
748
|
+
this._items.deleteAll();
|
|
749
|
+
this.internalState.gaps = [];
|
|
750
|
+
this.addItems(items, 'tail');
|
|
751
|
+
}
|
|
752
|
+
forgetGapsOf(ids) {
|
|
753
|
+
if (this.internalState.gaps.length && ids.length) {
|
|
754
|
+
const removed = new Set(ids);
|
|
755
|
+
this.internalState.gaps = this.internalState.gaps.filter(id => !removed.has(id));
|
|
756
|
+
}
|
|
757
|
+
}
|
|
669
758
|
emitChangeWithDiff(itemChanged, originalState) {
|
|
670
759
|
if (itemChanged || originalState !== this.state) {
|
|
671
760
|
this.eventTarget.emit('change', {
|
|
@@ -692,6 +781,7 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
692
781
|
deletedItems.push(id);
|
|
693
782
|
}
|
|
694
783
|
this._items.delete(...deletedItems);
|
|
784
|
+
this.forgetGapsOf(deletedItems);
|
|
695
785
|
if (this.internalState.current === WindowState.OLDEST) {
|
|
696
786
|
this.internalState.current = WindowState.PAST;
|
|
697
787
|
}
|
|
@@ -703,6 +793,7 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
703
793
|
}
|
|
704
794
|
const deletedItems = Array.from(this._items.items.keys()).slice(keep);
|
|
705
795
|
this._items.delete(...deletedItems);
|
|
796
|
+
this.forgetGapsOf(deletedItems);
|
|
706
797
|
return deletedItems;
|
|
707
798
|
}
|
|
708
799
|
|
|
@@ -795,6 +886,12 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
795
886
|
}
|
|
796
887
|
return super.resetToLatest(force);
|
|
797
888
|
}
|
|
889
|
+
async resyncToLatest() {
|
|
890
|
+
if (this.internalState.traverseLock) {
|
|
891
|
+
return;
|
|
892
|
+
}
|
|
893
|
+
return super.resyncToLatest();
|
|
894
|
+
}
|
|
798
895
|
async fetchNext() {
|
|
799
896
|
if (this.internalState.traverseLock) {
|
|
800
897
|
return;
|
|
@@ -825,7 +922,10 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
825
922
|
if (this.state !== WindowState.LATEST) {
|
|
826
923
|
return null;
|
|
827
924
|
}
|
|
828
|
-
const
|
|
925
|
+
const items = this.items;
|
|
926
|
+
// A restored window is taken for continuous, so nothing above the newest gap goes into it.
|
|
927
|
+
const continuousFrom = Math.max(0, ...this.internalState.gaps.map(id => items.findIndex(item => item.id === id)));
|
|
928
|
+
const messages = items.slice(Math.max(continuousFrom, items.length - Math.max(limit, this.fetchLimit)));
|
|
829
929
|
const myReactions = {};
|
|
830
930
|
for (const message of messages) {
|
|
831
931
|
if (this.internalState.myReactions[message.id]) {
|
|
@@ -845,8 +945,7 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
845
945
|
*/
|
|
846
946
|
_restoreLatest(messages, myReactions) {
|
|
847
947
|
const originalState = this.state;
|
|
848
|
-
this.
|
|
849
|
-
this.addItems(messages, 'tail');
|
|
948
|
+
this.replaceItems(messages);
|
|
850
949
|
this.internalState.myReactions = {
|
|
851
950
|
...myReactions
|
|
852
951
|
};
|
|
@@ -1060,6 +1159,7 @@ class RoomMessagesHistory {
|
|
|
1060
1159
|
roomId;
|
|
1061
1160
|
historyWindows = new IndexedCollection();
|
|
1062
1161
|
traverseLock = false;
|
|
1162
|
+
timeLimitedHistory = false;
|
|
1063
1163
|
room;
|
|
1064
1164
|
constructor(room, tracker) {
|
|
1065
1165
|
this.tracker = tracker;
|
|
@@ -1067,7 +1167,7 @@ class RoomMessagesHistory {
|
|
|
1067
1167
|
this.room = typeof room === 'string' ? null : room;
|
|
1068
1168
|
this.isPreview = this.room === null;
|
|
1069
1169
|
if (this.room) {
|
|
1070
|
-
this.
|
|
1170
|
+
this.updateHistoryMode(this.room);
|
|
1071
1171
|
}
|
|
1072
1172
|
if (this.room?.defaultTopic) {
|
|
1073
1173
|
this.createHistoryWindowForTopic(this.room.defaultTopic);
|
|
@@ -1100,13 +1200,19 @@ class RoomMessagesHistory {
|
|
|
1100
1200
|
*
|
|
1101
1201
|
* The window bindings are preserved; only windows that the application had
|
|
1102
1202
|
* actually pulled to the latest page (state === LATEST) are refreshed, with
|
|
1103
|
-
* a single
|
|
1104
|
-
*
|
|
1203
|
+
* a single request instead of a chain of catch-up requests. Windows that
|
|
1204
|
+
* were never pulled (LIVE) or belong to an ephemeral room are left
|
|
1105
1205
|
* untouched so their in-memory context survives the reconnect.
|
|
1206
|
+
*
|
|
1207
|
+
* Rooms keeping the full history are reset to the latest page (the rest can
|
|
1208
|
+
* always be traversed back), while the windows of rooms with a time-limited
|
|
1209
|
+
* history (MaxAge) keep what they had loaded and get the missed messages on
|
|
1210
|
+
* top - the server may no longer serve the older ones. If more messages were
|
|
1211
|
+
* missed than a single page holds, the seam is recorded in the window gaps.
|
|
1106
1212
|
*/
|
|
1107
1213
|
async resync(room) {
|
|
1108
1214
|
this.room = room;
|
|
1109
|
-
this.
|
|
1215
|
+
this.updateHistoryMode(room);
|
|
1110
1216
|
if (this.room.defaultTopic) {
|
|
1111
1217
|
this.createHistoryWindowForTopic(this.room.defaultTopic);
|
|
1112
1218
|
}
|
|
@@ -1119,7 +1225,12 @@ class RoomMessagesHistory {
|
|
|
1119
1225
|
if (this.traverseLock) {
|
|
1120
1226
|
continue;
|
|
1121
1227
|
}
|
|
1122
|
-
if (window.state
|
|
1228
|
+
if (window.state !== WindowState.LATEST) {
|
|
1229
|
+
continue;
|
|
1230
|
+
}
|
|
1231
|
+
if (this.timeLimitedHistory) {
|
|
1232
|
+
await window.resyncToLatest();
|
|
1233
|
+
} else {
|
|
1123
1234
|
await window.resetToLatest(true);
|
|
1124
1235
|
}
|
|
1125
1236
|
} catch (_e) {
|
|
@@ -1161,7 +1272,7 @@ class RoomMessagesHistory {
|
|
|
1161
1272
|
*/
|
|
1162
1273
|
async _handleRoomUpdated(ev) {
|
|
1163
1274
|
this.room = ev.room;
|
|
1164
|
-
this.
|
|
1275
|
+
this.updateHistoryMode(ev.room);
|
|
1165
1276
|
if (ev.room.defaultTopic) {
|
|
1166
1277
|
this.createHistoryWindowForTopic(ev.room.defaultTopic);
|
|
1167
1278
|
}
|
|
@@ -1247,8 +1358,9 @@ class RoomMessagesHistory {
|
|
|
1247
1358
|
this.tracker.rooms._deleteTopicsFromRoom(this.roomId, ...deletedTopicIds);
|
|
1248
1359
|
});
|
|
1249
1360
|
}
|
|
1250
|
-
|
|
1361
|
+
updateHistoryMode(room) {
|
|
1251
1362
|
this.traverseLock = room.history.mode === 'Ephemeral';
|
|
1363
|
+
this.timeLimitedHistory = room.history.mode === 'MaxAge';
|
|
1252
1364
|
}
|
|
1253
1365
|
}
|
|
1254
1366
|
;// ./src/state-tracker/MessagesManager.ts
|