@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/build/index.mjs
CHANGED
|
@@ -439,7 +439,8 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
439
439
|
retainRatio: 1,
|
|
440
440
|
fetchLimit: 50,
|
|
441
441
|
lastFetchCount: 0,
|
|
442
|
-
oldestId: null
|
|
442
|
+
oldestId: null,
|
|
443
|
+
gaps: []
|
|
443
444
|
};
|
|
444
445
|
|
|
445
446
|
/**
|
|
@@ -494,6 +495,16 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
494
495
|
get hasLatest() {
|
|
495
496
|
return [WindowState.LATEST, WindowState.LIVE].includes(this.state);
|
|
496
497
|
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* IDs of the items with a gap in front of them: the item right above in the window is not the real predecessor,
|
|
501
|
+
* an unknown number of items in between was never fetched. Such a seam appears when {@link resyncToLatest} gets
|
|
502
|
+
* a full page that does not reach the loaded items. A marker lives as long as its item stays in the window and
|
|
503
|
+
* is closed once the items right before it are fetched.
|
|
504
|
+
*/
|
|
505
|
+
get gaps() {
|
|
506
|
+
return this.internalState.gaps;
|
|
507
|
+
}
|
|
497
508
|
get hasOldest() {
|
|
498
509
|
return this.state === WindowState.OLDEST || this.state === WindowState.LATEST && this.length < this.fetchLimit || this.internalState.oldestId !== null && this.has(this.internalState.oldestId);
|
|
499
510
|
}
|
|
@@ -503,15 +514,51 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
503
514
|
}
|
|
504
515
|
let result;
|
|
505
516
|
const originalState = this.state;
|
|
506
|
-
this.
|
|
517
|
+
this.beginFetch(WindowState.LATEST);
|
|
507
518
|
try {
|
|
508
519
|
result = await this.fetchLatestItems();
|
|
509
520
|
this.internalState.lastFetchCount = result.length;
|
|
510
521
|
} finally {
|
|
511
|
-
this.
|
|
522
|
+
this.endFetch();
|
|
523
|
+
}
|
|
524
|
+
this.replaceItems(result);
|
|
525
|
+
this.internalState.current = WindowState.LATEST;
|
|
526
|
+
this.emitChangeWithDiff(true, originalState);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Refresh the window with the latest page, keeping the already loaded items instead of replacing them - the
|
|
531
|
+
* reconnect-friendly variant of {@link resetToLatest}. The page is merged on top of the loaded items (an item in
|
|
532
|
+
* both is taken from the page) and only the window size limit pushes the oldest ones out. When the page is full
|
|
533
|
+
* and does not reach the newest loaded item, both parts are still kept and the seam is recorded in {@link gaps}.
|
|
534
|
+
*/
|
|
535
|
+
async resyncToLatest() {
|
|
536
|
+
if (this.internalState.ongoing) {
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
let result;
|
|
540
|
+
const originalState = this.state;
|
|
541
|
+
this.beginFetch(WindowState.LATEST);
|
|
542
|
+
try {
|
|
543
|
+
result = await this.fetchLatestItems();
|
|
544
|
+
this.internalState.lastFetchCount = result.length;
|
|
545
|
+
} finally {
|
|
546
|
+
this.endFetch();
|
|
547
|
+
}
|
|
548
|
+
const loaded = this.items;
|
|
549
|
+
const fetchedIds = new Set(result.map(item => this.getId(item)));
|
|
550
|
+
const retained = loaded.filter(item => !fetchedIds.has(this.getId(item)));
|
|
551
|
+
|
|
552
|
+
// A page shorter than the limit is everything the remote side has, so nothing can be missing in between.
|
|
553
|
+
const isContinuous = !retained.length || result.length < this.internalState.fetchLimit || fetchedIds.has(this.getId(loaded[loaded.length - 1]));
|
|
554
|
+
|
|
555
|
+
// The page is continuous, so only its first item can have a gap in front of it.
|
|
556
|
+
this.forgetGapsOf(result.slice(1).map(item => this.getId(item)));
|
|
557
|
+
if (!isContinuous && !this.internalState.gaps.includes(this.getId(result[0]))) {
|
|
558
|
+
this.internalState.gaps = [...this.internalState.gaps, this.getId(result[0])];
|
|
512
559
|
}
|
|
513
560
|
this._items.deleteAll(); // Directly call deleteAll to prevent event emit.
|
|
514
|
-
this.addItems(result, 'tail');
|
|
561
|
+
this.addItems([...retained, ...result], 'tail');
|
|
515
562
|
this.internalState.current = WindowState.LATEST;
|
|
516
563
|
this.emitChangeWithDiff(true, originalState);
|
|
517
564
|
}
|
|
@@ -521,18 +568,18 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
521
568
|
}
|
|
522
569
|
let result;
|
|
523
570
|
const originalState = this.state;
|
|
524
|
-
|
|
571
|
+
const firstItem = this.getAt(0);
|
|
572
|
+
this.beginFetch(WindowState.PAST);
|
|
525
573
|
try {
|
|
526
574
|
result = await this.fetchItemsBefore();
|
|
527
575
|
this.internalState.lastFetchCount = result ? result.length : 0;
|
|
528
576
|
} finally {
|
|
529
|
-
this.
|
|
577
|
+
this.endFetch();
|
|
530
578
|
}
|
|
531
579
|
if (!result) {
|
|
532
580
|
return this.resetToLatest();
|
|
533
581
|
}
|
|
534
582
|
if (!result.length) {
|
|
535
|
-
const firstItem = this.getAt(0);
|
|
536
583
|
this.internalState.oldestId = firstItem ? this.getId(firstItem) : null;
|
|
537
584
|
await this.refreshFetchedState();
|
|
538
585
|
|
|
@@ -543,6 +590,10 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
543
590
|
this.emitChangeWithDiff(false, originalState);
|
|
544
591
|
return;
|
|
545
592
|
}
|
|
593
|
+
if (firstItem) {
|
|
594
|
+
// The page was fetched as the direct predecessors of the first item, so a gap in front of it is closed.
|
|
595
|
+
this.forgetGapsOf([this.getId(firstItem)]);
|
|
596
|
+
}
|
|
546
597
|
this.addItems(result, 'head');
|
|
547
598
|
await this.refreshFetchedState();
|
|
548
599
|
this.emitChangeWithDiff(true, originalState);
|
|
@@ -563,12 +614,12 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
563
614
|
}
|
|
564
615
|
let result;
|
|
565
616
|
const originalState = this.state;
|
|
566
|
-
this.
|
|
617
|
+
this.beginFetch(WindowState.PAST);
|
|
567
618
|
try {
|
|
568
619
|
result = await this.fetchItemsAfter();
|
|
569
620
|
this.internalState.lastFetchCount = result ? result.length : 0;
|
|
570
621
|
} finally {
|
|
571
|
-
this.
|
|
622
|
+
this.endFetch();
|
|
572
623
|
}
|
|
573
624
|
if (!result) {
|
|
574
625
|
await this.resetToLatest(true);
|
|
@@ -581,26 +632,41 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
581
632
|
return;
|
|
582
633
|
}
|
|
583
634
|
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* Load the items around the given one. Asked for while another fetch is running, it waits for that fetch to end
|
|
638
|
+
* instead of being dropped - when the call returns, the window either holds the item or it could not be loaded.
|
|
639
|
+
*/
|
|
584
640
|
async jumpTo(id) {
|
|
585
|
-
|
|
641
|
+
while (this.internalState.ongoing) {
|
|
642
|
+
await this.internalState.fetchSettled;
|
|
643
|
+
}
|
|
644
|
+
if (this.state !== WindowState.LIVE && this._items.has(id)) {
|
|
586
645
|
return;
|
|
587
646
|
}
|
|
588
647
|
let result;
|
|
589
648
|
const originalState = this.state;
|
|
590
|
-
this.
|
|
649
|
+
this.beginFetch(WindowState.PAST);
|
|
591
650
|
try {
|
|
592
651
|
result = await this.fetchItemsAround(id);
|
|
593
652
|
this.internalState.lastFetchCount = result ? result.length : 0;
|
|
594
653
|
if (result) {
|
|
595
|
-
this.
|
|
596
|
-
this.addItems(result, 'tail');
|
|
654
|
+
this.replaceItems(result);
|
|
597
655
|
await this.refreshFetchedState();
|
|
598
656
|
}
|
|
599
657
|
} finally {
|
|
600
|
-
this.
|
|
658
|
+
this.endFetch();
|
|
601
659
|
}
|
|
602
660
|
this.emitChangeWithDiff(!!result, originalState);
|
|
603
661
|
}
|
|
662
|
+
delete(...ids) {
|
|
663
|
+
super.delete(...ids);
|
|
664
|
+
this.forgetGapsOf(ids);
|
|
665
|
+
}
|
|
666
|
+
deleteAll() {
|
|
667
|
+
super.deleteAll();
|
|
668
|
+
this.internalState.gaps = [];
|
|
669
|
+
}
|
|
604
670
|
async refreshFetchedState() {
|
|
605
671
|
this.internalState.current = (await this.isLatestItemLoaded()) ? WindowState.LATEST : WindowState.PAST;
|
|
606
672
|
}
|
|
@@ -623,6 +689,29 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
623
689
|
this._items.set(...entries, ...loaded);
|
|
624
690
|
return this.trimTail(this.getLowWatermark(highWatermark));
|
|
625
691
|
}
|
|
692
|
+
beginFetch(kind) {
|
|
693
|
+
this.internalState.ongoing = kind;
|
|
694
|
+
this.internalState.fetchSettled = new Promise(resolve => this.internalState.settleFetch = resolve);
|
|
695
|
+
}
|
|
696
|
+
endFetch() {
|
|
697
|
+
this.internalState.ongoing = undefined;
|
|
698
|
+
this.internalState.settleFetch?.();
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* Replace the whole content without emitting an event; the old gap markers mean nothing for the new content.
|
|
703
|
+
*/
|
|
704
|
+
replaceItems(items) {
|
|
705
|
+
this._items.deleteAll();
|
|
706
|
+
this.internalState.gaps = [];
|
|
707
|
+
this.addItems(items, 'tail');
|
|
708
|
+
}
|
|
709
|
+
forgetGapsOf(ids) {
|
|
710
|
+
if (this.internalState.gaps.length && ids.length) {
|
|
711
|
+
const removed = new Set(ids);
|
|
712
|
+
this.internalState.gaps = this.internalState.gaps.filter(id => !removed.has(id));
|
|
713
|
+
}
|
|
714
|
+
}
|
|
626
715
|
emitChangeWithDiff(itemChanged, originalState) {
|
|
627
716
|
if (itemChanged || originalState !== this.state) {
|
|
628
717
|
this.eventTarget.emit('change', {
|
|
@@ -649,6 +738,7 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
649
738
|
deletedItems.push(id);
|
|
650
739
|
}
|
|
651
740
|
this._items.delete(...deletedItems);
|
|
741
|
+
this.forgetGapsOf(deletedItems);
|
|
652
742
|
if (this.internalState.current === WindowState.OLDEST) {
|
|
653
743
|
this.internalState.current = WindowState.PAST;
|
|
654
744
|
}
|
|
@@ -660,6 +750,7 @@ class TraversableRemoteCollection extends ObservableIndexedObjectCollection {
|
|
|
660
750
|
}
|
|
661
751
|
const deletedItems = Array.from(this._items.items.keys()).slice(keep);
|
|
662
752
|
this._items.delete(...deletedItems);
|
|
753
|
+
this.forgetGapsOf(deletedItems);
|
|
663
754
|
return deletedItems;
|
|
664
755
|
}
|
|
665
756
|
|
|
@@ -752,6 +843,12 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
752
843
|
}
|
|
753
844
|
return super.resetToLatest(force);
|
|
754
845
|
}
|
|
846
|
+
async resyncToLatest() {
|
|
847
|
+
if (this.internalState.traverseLock) {
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
return super.resyncToLatest();
|
|
851
|
+
}
|
|
755
852
|
async fetchNext() {
|
|
756
853
|
if (this.internalState.traverseLock) {
|
|
757
854
|
return;
|
|
@@ -782,7 +879,10 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
782
879
|
if (this.state !== WindowState.LATEST) {
|
|
783
880
|
return null;
|
|
784
881
|
}
|
|
785
|
-
const
|
|
882
|
+
const items = this.items;
|
|
883
|
+
// A restored window is taken for continuous, so nothing above the newest gap goes into it.
|
|
884
|
+
const continuousFrom = Math.max(0, ...this.internalState.gaps.map(id => items.findIndex(item => item.id === id)));
|
|
885
|
+
const messages = items.slice(Math.max(continuousFrom, items.length - Math.max(limit, this.fetchLimit)));
|
|
786
886
|
const myReactions = {};
|
|
787
887
|
for (const message of messages) {
|
|
788
888
|
if (this.internalState.myReactions[message.id]) {
|
|
@@ -802,8 +902,7 @@ class TopicHistoryWindow extends TraversableRemoteCollection {
|
|
|
802
902
|
*/
|
|
803
903
|
_restoreLatest(messages, myReactions) {
|
|
804
904
|
const originalState = this.state;
|
|
805
|
-
this.
|
|
806
|
-
this.addItems(messages, 'tail');
|
|
905
|
+
this.replaceItems(messages);
|
|
807
906
|
this.internalState.myReactions = {
|
|
808
907
|
...myReactions
|
|
809
908
|
};
|
|
@@ -1017,6 +1116,7 @@ class RoomMessagesHistory {
|
|
|
1017
1116
|
roomId;
|
|
1018
1117
|
historyWindows = new IndexedCollection();
|
|
1019
1118
|
traverseLock = false;
|
|
1119
|
+
timeLimitedHistory = false;
|
|
1020
1120
|
room;
|
|
1021
1121
|
constructor(room, tracker) {
|
|
1022
1122
|
this.tracker = tracker;
|
|
@@ -1024,7 +1124,7 @@ class RoomMessagesHistory {
|
|
|
1024
1124
|
this.room = typeof room === 'string' ? null : room;
|
|
1025
1125
|
this.isPreview = this.room === null;
|
|
1026
1126
|
if (this.room) {
|
|
1027
|
-
this.
|
|
1127
|
+
this.updateHistoryMode(this.room);
|
|
1028
1128
|
}
|
|
1029
1129
|
if (this.room?.defaultTopic) {
|
|
1030
1130
|
this.createHistoryWindowForTopic(this.room.defaultTopic);
|
|
@@ -1057,13 +1157,19 @@ class RoomMessagesHistory {
|
|
|
1057
1157
|
*
|
|
1058
1158
|
* The window bindings are preserved; only windows that the application had
|
|
1059
1159
|
* actually pulled to the latest page (state === LATEST) are refreshed, with
|
|
1060
|
-
* a single
|
|
1061
|
-
*
|
|
1160
|
+
* a single request instead of a chain of catch-up requests. Windows that
|
|
1161
|
+
* were never pulled (LIVE) or belong to an ephemeral room are left
|
|
1062
1162
|
* untouched so their in-memory context survives the reconnect.
|
|
1163
|
+
*
|
|
1164
|
+
* Rooms keeping the full history are reset to the latest page (the rest can
|
|
1165
|
+
* always be traversed back), while the windows of rooms with a time-limited
|
|
1166
|
+
* history (MaxAge) keep what they had loaded and get the missed messages on
|
|
1167
|
+
* top - the server may no longer serve the older ones. If more messages were
|
|
1168
|
+
* missed than a single page holds, the seam is recorded in the window gaps.
|
|
1063
1169
|
*/
|
|
1064
1170
|
async resync(room) {
|
|
1065
1171
|
this.room = room;
|
|
1066
|
-
this.
|
|
1172
|
+
this.updateHistoryMode(room);
|
|
1067
1173
|
if (this.room.defaultTopic) {
|
|
1068
1174
|
this.createHistoryWindowForTopic(this.room.defaultTopic);
|
|
1069
1175
|
}
|
|
@@ -1076,7 +1182,12 @@ class RoomMessagesHistory {
|
|
|
1076
1182
|
if (this.traverseLock) {
|
|
1077
1183
|
continue;
|
|
1078
1184
|
}
|
|
1079
|
-
if (window.state
|
|
1185
|
+
if (window.state !== WindowState.LATEST) {
|
|
1186
|
+
continue;
|
|
1187
|
+
}
|
|
1188
|
+
if (this.timeLimitedHistory) {
|
|
1189
|
+
await window.resyncToLatest();
|
|
1190
|
+
} else {
|
|
1080
1191
|
await window.resetToLatest(true);
|
|
1081
1192
|
}
|
|
1082
1193
|
} catch (_e) {
|
|
@@ -1118,7 +1229,7 @@ class RoomMessagesHistory {
|
|
|
1118
1229
|
*/
|
|
1119
1230
|
async _handleRoomUpdated(ev) {
|
|
1120
1231
|
this.room = ev.room;
|
|
1121
|
-
this.
|
|
1232
|
+
this.updateHistoryMode(ev.room);
|
|
1122
1233
|
if (ev.room.defaultTopic) {
|
|
1123
1234
|
this.createHistoryWindowForTopic(ev.room.defaultTopic);
|
|
1124
1235
|
}
|
|
@@ -1204,8 +1315,9 @@ class RoomMessagesHistory {
|
|
|
1204
1315
|
this.tracker.rooms._deleteTopicsFromRoom(this.roomId, ...deletedTopicIds);
|
|
1205
1316
|
});
|
|
1206
1317
|
}
|
|
1207
|
-
|
|
1318
|
+
updateHistoryMode(room) {
|
|
1208
1319
|
this.traverseLock = room.history.mode === 'Ephemeral';
|
|
1320
|
+
this.timeLimitedHistory = room.history.mode === 'MaxAge';
|
|
1209
1321
|
}
|
|
1210
1322
|
}
|
|
1211
1323
|
;// ./src/state-tracker/MessagesManager.ts
|