-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathchannel_manager.ts
More file actions
737 lines (647 loc) · 22.5 KB
/
Copy pathchannel_manager.ts
File metadata and controls
737 lines (647 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
import type { StreamChat } from './client';
import type {
ChannelFilters,
ChannelOptions,
ChannelSort,
ChannelStateOptions,
Event,
} from './types';
import type { ValueOrPatch } from './store';
import { isPatch, StateStore } from './store';
import type { Channel } from './channel';
import {
extractSortValue,
findLastPinnedChannelIndex,
getAndWatchChannel,
isChannelArchived,
isChannelPinned,
promoteChannel,
shouldConsiderArchivedChannels,
shouldConsiderPinnedChannels,
sleep,
uniqBy,
} from './utils';
import { generateUUIDv4 } from './utils';
import {
DEFAULT_QUERY_CHANNELS_MS_BETWEEN_RETRIES,
DEFAULT_QUERY_CHANNELS_RETRY_COUNT,
} from './constants';
import { WithSubscriptions } from './utils/WithSubscriptions';
export type ChannelManagerPagination = {
filters: ChannelFilters;
hasNext: boolean;
isLoading: boolean;
isLoadingNext: boolean;
options: ChannelOptions;
sort: ChannelSort;
};
export type ChannelManagerState = {
channels: Channel[];
/**
* This value will become true the first time queryChannels is successfully executed and
* will remain false otherwise. It's used as a control property regarding whether the list
* has been initialized yet (i.e a query has already been done at least once) or not. We do
* this to prevent state.channels from being forced to be nullable.
*/
initialized: boolean;
pagination: ChannelManagerPagination;
error: Error | undefined;
};
export type ChannelSetterParameterType = ValueOrPatch<ChannelManagerState['channels']>;
export type ChannelSetterType = (arg: ChannelSetterParameterType) => void;
export type GenericEventHandlerType<T extends unknown[]> = (
...args: T
) => void | (() => void) | ((...args: T) => Promise<void>) | Promise<void>;
export type EventHandlerType = GenericEventHandlerType<[Event]>;
export type EventHandlerOverrideType = GenericEventHandlerType<
[ChannelSetterType, Event]
>;
export type ChannelManagerEventTypes =
| 'notification.added_to_channel'
| 'notification.message_new'
| 'notification.removed_from_channel'
| 'message.new'
| 'member.updated'
| 'channel.deleted'
| 'channel.hidden'
| 'channel.truncated'
| 'channel.visible'
| 'channel.updated';
export type ChannelManagerEventHandlerNames =
| 'channelDeletedHandler'
| 'channelHiddenHandler'
| 'channelTruncatedHandler'
| 'channelUpdatedHandler'
| 'channelVisibleHandler'
| 'newMessageHandler'
| 'memberUpdatedHandler'
| 'notificationAddedToChannelHandler'
| 'notificationNewMessageHandler'
| 'notificationRemovedFromChannelHandler';
export type ChannelManagerEventHandlerOverrides = Partial<
Record<ChannelManagerEventHandlerNames, EventHandlerOverrideType>
>;
export type ExecuteChannelsQueryPayload = Pick<
ChannelManagerPagination,
'filters' | 'sort' | 'options'
> & { stateOptions: ChannelStateOptions };
export const channelManagerEventToHandlerMapping: {
[key in ChannelManagerEventTypes]: ChannelManagerEventHandlerNames;
} = {
'channel.deleted': 'channelDeletedHandler',
'channel.hidden': 'channelHiddenHandler',
'channel.truncated': 'channelTruncatedHandler',
'channel.updated': 'channelUpdatedHandler',
'channel.visible': 'channelVisibleHandler',
'message.new': 'newMessageHandler',
'member.updated': 'memberUpdatedHandler',
'notification.added_to_channel': 'notificationAddedToChannelHandler',
'notification.message_new': 'notificationNewMessageHandler',
'notification.removed_from_channel': 'notificationRemovedFromChannelHandler',
};
export type ChannelManagerOptions = {
/**
* Aborts a channels query that is already in progress and runs the new one.
*/
abortInFlightQuery?: boolean;
/**
* Allows channel promotion to be applied where applicable for channels that are
* currently not part of the channel list within the state. A good example of
* this would be a channel that is being watched and it receives a new message,
* but is not part of the list initially.
*/
allowNotLoadedChannelPromotionForEvent?: {
'channel.visible': boolean;
'message.new': boolean;
'notification.added_to_channel': boolean;
'notification.message_new': boolean;
};
/**
* Allows us to lock the order of channels within the list. Any event that would
* change the order of channels within the list will do nothing.
*/
lockChannelOrder?: boolean;
};
export type QueryChannelsRequestType = (
...params: Parameters<StreamChat['queryChannels']>
) => Promise<Channel[]>;
export const DEFAULT_CHANNEL_MANAGER_OPTIONS = {
abortInFlightQuery: false,
allowNotLoadedChannelPromotionForEvent: {
'channel.visible': true,
'message.new': true,
'notification.added_to_channel': true,
'notification.message_new': true,
},
lockChannelOrder: false,
};
export const DEFAULT_CHANNEL_MANAGER_PAGINATION_OPTIONS = {
limit: 10,
offset: 0,
};
/**
* A class that manages a list of channels and changes it based on configuration and WS events. The
* list of channels is reactive as well as the pagination and it can be subscribed to for state updates.
*
* @internal
*/
export class ChannelManager extends WithSubscriptions {
public readonly state: StateStore<ChannelManagerState>;
private client: StreamChat;
private eventHandlers: Map<string, EventHandlerType> = new Map();
private eventHandlerOverrides: Map<string, EventHandlerOverrideType> = new Map();
private queryChannelsRequest: QueryChannelsRequestType;
private options: ChannelManagerOptions = {};
private stateOptions: ChannelStateOptions = {};
private id: string;
constructor({
client,
eventHandlerOverrides = {},
options = {},
queryChannelsOverride,
}: {
client: StreamChat;
eventHandlerOverrides?: ChannelManagerEventHandlerOverrides;
options?: ChannelManagerOptions;
queryChannelsOverride?: QueryChannelsRequestType;
}) {
super();
this.id = `channel-manager-${generateUUIDv4()}`;
this.client = client;
this.state = new StateStore<ChannelManagerState>({
channels: [],
pagination: {
isLoading: false,
isLoadingNext: false,
hasNext: false,
filters: {},
sort: {},
options: DEFAULT_CHANNEL_MANAGER_PAGINATION_OPTIONS,
},
initialized: false,
error: undefined,
});
this.setEventHandlerOverrides(eventHandlerOverrides);
this.setOptions(options);
this.queryChannelsRequest =
queryChannelsOverride ?? ((...params) => this.client.queryChannels(...params));
this.eventHandlers = new Map(
Object.entries<EventHandlerType>({
channelDeletedHandler: this.channelDeletedHandler,
channelHiddenHandler: this.channelHiddenHandler,
channelVisibleHandler: this.channelVisibleHandler,
memberUpdatedHandler: this.memberUpdatedHandler,
newMessageHandler: this.newMessageHandler,
notificationAddedToChannelHandler: this.notificationAddedToChannelHandler,
notificationNewMessageHandler: this.notificationNewMessageHandler,
notificationRemovedFromChannelHandler: this.notificationRemovedFromChannelHandler,
}),
);
}
public setChannels = (valueOrFactory: ChannelSetterParameterType) => {
this.state.next((current) => {
const { channels: currentChannels } = current;
const newChannels = isPatch(valueOrFactory)
? valueOrFactory(currentChannels)
: valueOrFactory;
// If the references between the two values are the same, just return the
// current state; otherwise trigger a state change.
if (currentChannels === newChannels) {
return current;
}
return { ...current, channels: newChannels };
});
const {
channels,
pagination: { filters, sort },
} = this.state.getLatestValue();
this.client.offlineDb?.executeQuerySafely(
(db) =>
db.upsertCidsForQuery({
cids: channels.map((channel) => channel.cid),
filters,
sort,
}),
{ method: 'upsertCidsForQuery' },
);
};
public setEventHandlerOverrides = (
eventHandlerOverrides: ChannelManagerEventHandlerOverrides = {},
) => {
const truthyEventHandlerOverrides = Object.entries(eventHandlerOverrides).reduce<
Partial<ChannelManagerEventHandlerOverrides>
>((acc, [key, value]) => {
if (value) {
acc[key as keyof ChannelManagerEventHandlerOverrides] = value;
}
return acc;
}, {});
this.eventHandlerOverrides = new Map(
Object.entries<EventHandlerOverrideType>(truthyEventHandlerOverrides),
);
};
public setQueryChannelsRequest = (queryChannelsRequest: QueryChannelsRequestType) => {
this.queryChannelsRequest = queryChannelsRequest;
};
public setOptions = (options: ChannelManagerOptions = {}) => {
this.options = { ...DEFAULT_CHANNEL_MANAGER_OPTIONS, ...options };
};
private executeChannelsQuery = async (
payload: ExecuteChannelsQueryPayload,
retryCount = 0,
): Promise<void> => {
const { filters, sort, options, stateOptions } = payload;
const { offset, limit } = {
...DEFAULT_CHANNEL_MANAGER_PAGINATION_OPTIONS,
...options,
};
try {
const channels = await this.queryChannelsRequest(
filters,
sort,
options,
stateOptions,
);
const newOffset = offset + (channels?.length ?? 0);
const newOptions = { ...options, offset: newOffset };
const { pagination } = this.state.getLatestValue();
this.state.partialNext({
channels,
pagination: {
...pagination,
hasNext: (channels?.length ?? 0) >= limit,
isLoading: false,
options: newOptions,
},
initialized: true,
error: undefined,
});
this.client.offlineDb?.executeQuerySafely(
(db) =>
db.upsertCidsForQuery({
cids: channels.map((channel) => channel.cid),
filters: pagination.filters,
sort: pagination.sort,
}),
{ method: 'upsertCidsForQuery' },
);
} catch (err) {
if (retryCount >= DEFAULT_QUERY_CHANNELS_RETRY_COUNT) {
console.warn(err);
const wrappedError = new Error(
`Maximum number of retries reached in queryChannels. Last error message is: ${err}`,
);
this.state.partialNext({ error: wrappedError });
return;
}
await sleep(DEFAULT_QUERY_CHANNELS_MS_BETWEEN_RETRIES);
return this.executeChannelsQuery(payload, retryCount + 1);
}
};
public queryChannels = async (
filters: ChannelFilters,
sort: ChannelSort = [],
options: ChannelOptions = {},
stateOptions: ChannelStateOptions = {},
) => {
const {
pagination: { isLoading, filters: filtersFromState },
initialized,
} = this.state.getLatestValue();
if (
isLoading &&
!this.options.abortInFlightQuery &&
// TODO: Figure a proper way to either deeply compare these or
// create hashes from each.
JSON.stringify(filtersFromState) === JSON.stringify(filters)
) {
return;
}
const executeChannelsQueryPayload = { filters, sort, options, stateOptions };
try {
this.stateOptions = stateOptions;
this.state.next((currentState) => ({
...currentState,
pagination: {
...currentState.pagination,
isLoading: true,
isLoadingNext: false,
filters,
sort,
options,
},
error: undefined,
}));
if (this.client.offlineDb?.getChannelsForQuery && this.client.user?.id) {
if (!initialized) {
const channelsFromDB = await this.client.offlineDb.getChannelsForQuery({
userId: this.client.user.id,
filters,
sort,
});
if (channelsFromDB) {
const offlineChannels = this.client.hydrateActiveChannels(channelsFromDB, {
offlineMode: true,
skipInitialization: [], // passing empty array will clear out the existing messages from channel state, this removes the possibility of duplicate messages
});
this.state.partialNext({ channels: offlineChannels });
}
}
if (!this.client.offlineDb.syncManager.syncStatus) {
this.client.offlineDb.syncManager.scheduleSyncStatusChangeCallback(
this.id,
async () => {
await this.executeChannelsQuery(executeChannelsQueryPayload);
},
);
return;
}
}
await this.executeChannelsQuery(executeChannelsQueryPayload);
} catch (error) {
this.client.logger('error', (error as Error).message);
this.state.next((currentState) => ({
...currentState,
pagination: { ...currentState.pagination, isLoading: false },
}));
throw error;
}
};
public loadNext = async () => {
const { pagination, initialized } = this.state.getLatestValue();
const { filters, sort, options, isLoadingNext, hasNext } = pagination;
if (!initialized || isLoadingNext || !hasNext) {
return;
}
try {
const { offset, limit } = {
...DEFAULT_CHANNEL_MANAGER_PAGINATION_OPTIONS,
...options,
};
this.state.partialNext({
pagination: { ...pagination, isLoading: false, isLoadingNext: true },
});
const nextChannels = await this.queryChannelsRequest(
filters,
sort,
options,
this.stateOptions,
);
const { channels } = this.state.getLatestValue();
const newOffset = offset + (nextChannels?.length ?? 0);
const newOptions = { ...options, offset: newOffset };
this.state.partialNext({
channels: uniqBy<Channel>([...(channels || []), ...nextChannels], 'cid'),
pagination: {
...pagination,
hasNext: (nextChannels?.length ?? 0) >= limit,
isLoading: false,
isLoadingNext: false,
options: newOptions,
},
});
} catch (error) {
this.client.logger('error', (error as Error).message);
this.state.next((currentState) => ({
...currentState,
pagination: { ...currentState.pagination, isLoadingNext: false },
}));
throw error;
}
};
private notificationAddedToChannelHandler = async (event: Event) => {
const { id, type, members } = event?.channel ?? {};
if (
!type ||
!this.options.allowNotLoadedChannelPromotionForEvent?.[
'notification.added_to_channel'
]
) {
return;
}
const channel = await getAndWatchChannel({
client: this.client,
id,
members: members?.reduce<string[]>((acc, { user, user_id }) => {
const userId = user_id || user?.id;
if (userId) {
acc.push(userId);
}
return acc;
}, []),
type,
});
const { pagination, channels } = this.state.getLatestValue();
if (!channels) {
return;
}
const { sort } = pagination ?? {};
this.setChannels(
promoteChannel({
channels,
channelToMove: channel,
sort,
}),
);
};
private channelDeletedHandler = (event: Event) => {
const { channels } = this.state.getLatestValue();
if (!channels) {
return;
}
const newChannels = [...channels];
const channelIndex = newChannels.findIndex(
(channel) => channel.cid === (event.cid || event.channel?.cid),
);
if (channelIndex < 0) {
return;
}
newChannels.splice(channelIndex, 1);
this.setChannels(newChannels);
};
private channelHiddenHandler = this.channelDeletedHandler;
private newMessageHandler = (event: Event) => {
const { pagination, channels } = this.state.getLatestValue();
if (!channels) {
return;
}
const { filters, sort } = pagination ?? {};
const channelType = event.channel_type;
const channelId = event.channel_id;
if (!channelType || !channelId) {
return;
}
const targetChannel = this.client.channel(channelType, channelId);
const targetChannelIndex = channels.indexOf(targetChannel);
const targetChannelExistsWithinList = targetChannelIndex >= 0;
const isTargetChannelPinned = isChannelPinned(targetChannel);
const isTargetChannelArchived = isChannelArchived(targetChannel);
const considerArchivedChannels = shouldConsiderArchivedChannels(filters);
const considerPinnedChannels = shouldConsiderPinnedChannels(sort);
if (
// filter is defined, target channel is archived and filter option is set to false
(considerArchivedChannels && isTargetChannelArchived && !filters.archived) ||
// filter is defined, target channel isn't archived and filter option is set to true
(considerArchivedChannels && !isTargetChannelArchived && filters.archived) ||
// sort option is defined, target channel is pinned
(considerPinnedChannels && isTargetChannelPinned) ||
// list order is locked
this.options.lockChannelOrder ||
// target channel is not within the loaded list and loading from cache is disallowed
(!targetChannelExistsWithinList &&
!this.options.allowNotLoadedChannelPromotionForEvent?.['message.new'])
) {
return;
}
this.setChannels(
promoteChannel({
channels,
channelToMove: targetChannel,
channelToMoveIndexWithinChannels: targetChannelIndex,
sort,
}),
);
};
private notificationNewMessageHandler = async (event: Event) => {
const { id, type } = event?.channel ?? {};
if (!id || !type) {
return;
}
const channel = await getAndWatchChannel({
client: this.client,
id,
type,
});
const { channels, pagination } = this.state.getLatestValue();
const { filters, sort } = pagination ?? {};
const considerArchivedChannels = shouldConsiderArchivedChannels(filters);
const isTargetChannelArchived = isChannelArchived(channel);
if (
!channels ||
(considerArchivedChannels && isTargetChannelArchived && !filters.archived) ||
(considerArchivedChannels && !isTargetChannelArchived && filters.archived) ||
!this.options.allowNotLoadedChannelPromotionForEvent?.['notification.message_new']
) {
return;
}
this.setChannels(
promoteChannel({
channels,
channelToMove: channel,
sort,
}),
);
};
private channelVisibleHandler = async (event: Event) => {
const { channel_type: channelType, channel_id: channelId } = event;
if (!channelType || !channelId) {
return;
}
const channel = await getAndWatchChannel({
client: this.client,
id: event.channel_id,
type: event.channel_type,
});
const { channels, pagination } = this.state.getLatestValue();
const { sort, filters } = pagination ?? {};
const considerArchivedChannels = shouldConsiderArchivedChannels(filters);
const isTargetChannelArchived = isChannelArchived(channel);
if (
!channels ||
(considerArchivedChannels && isTargetChannelArchived && !filters.archived) ||
(considerArchivedChannels && !isTargetChannelArchived && filters.archived) ||
!this.options.allowNotLoadedChannelPromotionForEvent?.['channel.visible']
) {
return;
}
this.setChannels(
promoteChannel({
channels,
channelToMove: channel,
sort,
}),
);
};
private notificationRemovedFromChannelHandler = this.channelDeletedHandler;
private memberUpdatedHandler = (event: Event) => {
const { pagination, channels } = this.state.getLatestValue();
const { filters, sort } = pagination;
if (
!event.member?.user ||
event.member.user.id !== this.client.userID ||
!event.channel_type ||
!event.channel_id
) {
return;
}
const channelType = event.channel_type;
const channelId = event.channel_id;
const considerPinnedChannels = shouldConsiderPinnedChannels(sort);
const considerArchivedChannels = shouldConsiderArchivedChannels(filters);
const pinnedAtSort = extractSortValue({ atIndex: 0, sort, targetKey: 'pinned_at' });
if (
!channels ||
(!considerPinnedChannels && !considerArchivedChannels) ||
this.options.lockChannelOrder
) {
return;
}
const targetChannel = this.client.channel(channelType, channelId);
// assumes that channel instances are not changing
const targetChannelIndex = channels.indexOf(targetChannel);
const targetChannelExistsWithinList = targetChannelIndex >= 0;
const isTargetChannelPinned = isChannelPinned(targetChannel);
const isTargetChannelArchived = isChannelArchived(targetChannel);
const newChannels = [...channels];
if (targetChannelExistsWithinList) {
newChannels.splice(targetChannelIndex, 1);
}
// handle archiving (remove channel)
if (
// When archived filter true, and channel is unarchived
(considerArchivedChannels && !isTargetChannelArchived && filters?.archived) ||
// When archived filter false, and channel is archived
(considerArchivedChannels && isTargetChannelArchived && !filters?.archived)
) {
this.setChannels(newChannels);
return;
}
// handle pinning
let lastPinnedChannelIndex: number | null = null;
if (pinnedAtSort === 1 || (pinnedAtSort === -1 && !isTargetChannelPinned)) {
lastPinnedChannelIndex = findLastPinnedChannelIndex({ channels: newChannels });
}
const newTargetChannelIndex =
typeof lastPinnedChannelIndex === 'number' ? lastPinnedChannelIndex + 1 : 0;
// skip state update if the position of the channel does not change
if (channels[newTargetChannelIndex] === targetChannel) {
return;
}
newChannels.splice(newTargetChannelIndex, 0, targetChannel);
this.setChannels(newChannels);
};
private subscriptionOrOverride = (event: Event) => {
const handlerName =
channelManagerEventToHandlerMapping[event.type as ChannelManagerEventTypes];
const defaultEventHandler = this.eventHandlers.get(handlerName);
const eventHandlerOverride = this.eventHandlerOverrides.get(handlerName);
if (eventHandlerOverride && typeof eventHandlerOverride === 'function') {
eventHandlerOverride(this.setChannels, event);
return;
}
if (defaultEventHandler && typeof defaultEventHandler === 'function') {
defaultEventHandler(event);
}
};
public registerSubscriptions = () => {
if (this.hasSubscriptions) {
// Already listening for events and changes
return;
}
for (const eventType of Object.keys(channelManagerEventToHandlerMapping)) {
this.addUnsubscribeFunction(
this.client.on(eventType, this.subscriptionOrOverride).unsubscribe,
);
}
};
}