polfan-server-js-client 0.2.90 → 0.2.91
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/.idea/workspace.xml +11 -13
- package/build/index.cjs.js +155 -129
- package/build/index.cjs.js.map +1 -1
- package/build/index.umd.js +1 -1
- package/build/index.umd.js.map +1 -1
- package/build/types/state-tracker/TopicHistoryWindow.d.ts +3 -2
- package/package.json +1 -1
- package/src/state-tracker/TopicHistoryWindow.ts +51 -38
|
@@ -73,6 +73,7 @@ export declare abstract class TraversableRemoteCollection<ItemT, EventMapT exten
|
|
|
73
73
|
protected abstract isLatestItemLoaded(): Promise<boolean>;
|
|
74
74
|
protected refreshFetchedState(): Promise<void>;
|
|
75
75
|
protected addItems(newItems: ItemT[], to: 'head' | 'tail'): void;
|
|
76
|
+
protected emitChangeWithDiff(itemChanged: boolean, originalState: WindowState): void;
|
|
76
77
|
/**
|
|
77
78
|
* Return array with messages trimmed using High/Low Watermark strategy.
|
|
78
79
|
*/
|
|
@@ -104,12 +105,12 @@ export declare class TopicHistoryWindow extends TraversableRemoteCollection<Mess
|
|
|
104
105
|
* @internal
|
|
105
106
|
*/
|
|
106
107
|
_updateMessageReference(refTopic: Topic): void;
|
|
107
|
-
private handleNewMessage;
|
|
108
|
-
private handleMessagesRedacted;
|
|
109
108
|
protected fetchItemsAfter(): Promise<Message[] | null>;
|
|
110
109
|
protected fetchItemsBefore(): Promise<Message[] | null>;
|
|
111
110
|
protected fetchLatestItems(): Promise<Message[]>;
|
|
112
111
|
private getTopic;
|
|
113
112
|
private getLatestMessageId;
|
|
114
113
|
protected isLatestItemLoaded(): Promise<boolean>;
|
|
114
|
+
private handleNewMessage;
|
|
115
|
+
private handleMessagesRedacted;
|
|
115
116
|
}
|
package/package.json
CHANGED
|
@@ -116,9 +116,9 @@ export abstract class TraversableRemoteCollection<
|
|
|
116
116
|
return;
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
this.internalState.ongoing = WindowState.LATEST;
|
|
120
|
-
|
|
121
119
|
let result;
|
|
120
|
+
const originalState = this.state;
|
|
121
|
+
this.internalState.ongoing = WindowState.LATEST;
|
|
122
122
|
|
|
123
123
|
try {
|
|
124
124
|
result = await this.fetchLatestItems();
|
|
@@ -130,6 +130,7 @@ export abstract class TraversableRemoteCollection<
|
|
|
130
130
|
this._items.deleteAll(); // Directly call deleteAll to prevent event emit.
|
|
131
131
|
this.addItems(result, 'tail');
|
|
132
132
|
this.internalState.current = WindowState.LATEST;
|
|
133
|
+
this.emitChangeWithDiff(true, originalState);
|
|
133
134
|
}
|
|
134
135
|
|
|
135
136
|
public async fetchPrevious(): Promise<void> {
|
|
@@ -137,9 +138,9 @@ export abstract class TraversableRemoteCollection<
|
|
|
137
138
|
return;
|
|
138
139
|
}
|
|
139
140
|
|
|
140
|
-
this.internalState.ongoing = WindowState.PAST;
|
|
141
|
-
|
|
142
141
|
let result;
|
|
142
|
+
const originalState = this.state;
|
|
143
|
+
this.internalState.ongoing = WindowState.PAST;
|
|
143
144
|
|
|
144
145
|
try {
|
|
145
146
|
result = await this.fetchItemsBefore();
|
|
@@ -163,11 +164,13 @@ export abstract class TraversableRemoteCollection<
|
|
|
163
164
|
this.internalState.current = WindowState.OLDEST;
|
|
164
165
|
}
|
|
165
166
|
|
|
167
|
+
this.emitChangeWithDiff(false, originalState);
|
|
166
168
|
return;
|
|
167
169
|
}
|
|
168
170
|
|
|
169
171
|
this.addItems(result, 'head');
|
|
170
172
|
await this.refreshFetchedState();
|
|
173
|
+
this.emitChangeWithDiff(true, originalState);
|
|
171
174
|
}
|
|
172
175
|
|
|
173
176
|
public async fetchNext(): Promise<void> {
|
|
@@ -175,9 +178,9 @@ export abstract class TraversableRemoteCollection<
|
|
|
175
178
|
return;
|
|
176
179
|
}
|
|
177
180
|
|
|
178
|
-
this.internalState.ongoing = WindowState.PAST;
|
|
179
|
-
|
|
180
181
|
let result;
|
|
182
|
+
const originalState = this.state;
|
|
183
|
+
this.internalState.ongoing = WindowState.PAST;
|
|
181
184
|
|
|
182
185
|
try {
|
|
183
186
|
result = await this.fetchItemsAfter();
|
|
@@ -194,6 +197,7 @@ export abstract class TraversableRemoteCollection<
|
|
|
194
197
|
if (result.length) {
|
|
195
198
|
this.addItems(result, 'tail');
|
|
196
199
|
await this.refreshFetchedState();
|
|
200
|
+
this.emitChangeWithDiff(true, originalState);
|
|
197
201
|
return;
|
|
198
202
|
}
|
|
199
203
|
}
|
|
@@ -221,8 +225,15 @@ export abstract class TraversableRemoteCollection<
|
|
|
221
225
|
result = this.trimItemsArrayToLimit([...this.items, ...newItems], 'head');
|
|
222
226
|
}
|
|
223
227
|
|
|
224
|
-
|
|
225
|
-
this.
|
|
228
|
+
// Directly calls to prevent event emit.
|
|
229
|
+
this._items.deleteAll();
|
|
230
|
+
this._items.set(...(result.map(item => [this.getId(item), item] as [string, ItemT])));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
protected emitChangeWithDiff(itemChanged: boolean, originalState: WindowState): void {
|
|
234
|
+
if (itemChanged || originalState !== this.state) {
|
|
235
|
+
this.eventTarget.emit('change', { setItems: Array.from(this._items.items.keys()) })
|
|
236
|
+
}
|
|
226
237
|
}
|
|
227
238
|
|
|
228
239
|
/**
|
|
@@ -336,36 +347,6 @@ export class TopicHistoryWindow extends TraversableRemoteCollection<
|
|
|
336
347
|
}
|
|
337
348
|
}
|
|
338
349
|
|
|
339
|
-
private async handleNewMessage(ev: NewMessage): Promise<void> {
|
|
340
|
-
if (
|
|
341
|
-
[WindowState.LATEST, WindowState.LIVE].includes(this.state)
|
|
342
|
-
&& ev.message.location.roomId === this.roomId
|
|
343
|
-
&& ev.message.location.topicId === this.topicId
|
|
344
|
-
) {
|
|
345
|
-
this.addItems([ev.message], 'tail');
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
private async handleMessagesRedacted(ev: MessagesRedacted): Promise<void> {
|
|
350
|
-
if (ev.location.topicId !== this.topicId || ev.location.roomId !== this.roomId) {
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
const refTopicIds = this.items
|
|
355
|
-
.filter(msg => msg.topicRef && ev.ids.includes(msg.id))
|
|
356
|
-
.map(msg => msg.topicRef as string);
|
|
357
|
-
|
|
358
|
-
this.delete(...ev.ids);
|
|
359
|
-
|
|
360
|
-
if (this.length === 0) {
|
|
361
|
-
await this.resetToLatest();
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
if (refTopicIds.length > 0) {
|
|
365
|
-
this.eventTarget.emit('reftopicsdeleted', refTopicIds);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
350
|
protected async fetchItemsAfter(): Promise<Message[] | null> {
|
|
370
351
|
const afterId = this.getAt(this.length - 1)?.id;
|
|
371
352
|
|
|
@@ -433,4 +414,36 @@ export class TopicHistoryWindow extends TraversableRemoteCollection<
|
|
|
433
414
|
const lastMessageId = await this.getLatestMessageId();
|
|
434
415
|
return lastMessageId ? this.has(lastMessageId) : true;
|
|
435
416
|
}
|
|
417
|
+
|
|
418
|
+
private async handleNewMessage(ev: NewMessage): Promise<void> {
|
|
419
|
+
if (
|
|
420
|
+
[WindowState.LATEST, WindowState.LIVE].includes(this.state)
|
|
421
|
+
&& ev.message.location.roomId === this.roomId
|
|
422
|
+
&& ev.message.location.topicId === this.topicId
|
|
423
|
+
) {
|
|
424
|
+
const originalState = this.state;
|
|
425
|
+
this.addItems([ev.message], 'tail');
|
|
426
|
+
this.emitChangeWithDiff(true, originalState);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private async handleMessagesRedacted(ev: MessagesRedacted): Promise<void> {
|
|
431
|
+
if (ev.location.topicId !== this.topicId || ev.location.roomId !== this.roomId) {
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const refTopicIds = this.items
|
|
436
|
+
.filter(msg => msg.topicRef && ev.ids.includes(msg.id))
|
|
437
|
+
.map(msg => msg.topicRef as string);
|
|
438
|
+
|
|
439
|
+
this.delete(...ev.ids);
|
|
440
|
+
|
|
441
|
+
if (this.length === 0) {
|
|
442
|
+
await this.resetToLatest();
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (refTopicIds.length > 0) {
|
|
446
|
+
this.eventTarget.emit('reftopicsdeleted', refTopicIds);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
436
449
|
}
|