@knocklabs/client 0.9.1 → 0.9.3

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.
@@ -0,0 +1,774 @@
1
+ import EventEmitter from "eventemitter2";
2
+ import { Channel } from "phoenix";
3
+ import { StoreApi } from "zustand";
4
+
5
+ import Knock from "../../knock";
6
+ import { NetworkStatus, isRequestInFlight } from "../../networkStatus";
7
+
8
+ import {
9
+ FeedClientOptions,
10
+ FeedItem,
11
+ FeedMetadata,
12
+ FeedResponse,
13
+ FetchFeedOptions,
14
+ } from "./interfaces";
15
+ import createStore from "./store";
16
+ import {
17
+ BindableFeedEvent,
18
+ FeedEvent,
19
+ FeedEventCallback,
20
+ FeedEventPayload,
21
+ FeedItemOrItems,
22
+ FeedMessagesReceivedPayload,
23
+ FeedRealTimeCallback,
24
+ FeedStoreState,
25
+ } from "./types";
26
+
27
+ export type Status =
28
+ | "seen"
29
+ | "read"
30
+ | "interacted"
31
+ | "archived"
32
+ | "unseen"
33
+ | "unread"
34
+ | "unarchived";
35
+
36
+ // Default options to apply
37
+ const feedClientDefaults: Pick<FeedClientOptions, "archived"> = {
38
+ archived: "exclude",
39
+ };
40
+
41
+ const DEFAULT_DISCONNECT_DELAY = 2000;
42
+
43
+ class Feed {
44
+ private userFeedId: string;
45
+ private channel?: Channel;
46
+ private broadcaster: EventEmitter;
47
+ private defaultOptions: FeedClientOptions;
48
+ private broadcastChannel!: BroadcastChannel | null;
49
+ private disconnectTimer: ReturnType<typeof setTimeout> | null = null;
50
+ private hasSubscribedToRealTimeUpdates: Boolean = false;
51
+
52
+ // The raw store instance, used for binding in React and other environments
53
+ public store: StoreApi<FeedStoreState>;
54
+
55
+ constructor(
56
+ readonly knock: Knock,
57
+ readonly feedId: string,
58
+ options: FeedClientOptions,
59
+ ) {
60
+ this.feedId = feedId;
61
+ this.userFeedId = this.buildUserFeedId();
62
+ this.store = createStore();
63
+ this.broadcaster = new EventEmitter({ wildcard: true, delimiter: "." });
64
+ this.defaultOptions = { ...feedClientDefaults, ...options };
65
+
66
+ this.knock.log(`[Feed] Initialized a feed on channel ${feedId}`);
67
+
68
+ // Attempt to setup a realtime connection (does not join)
69
+ this.initializeRealtimeConnection();
70
+
71
+ this.setupBroadcastChannel();
72
+ }
73
+
74
+ /**
75
+ * Used to reinitialize a current feed instance, which is useful when reauthenticating users
76
+ */
77
+ reinitialize() {
78
+ // Reinitialize the user feed id incase the userId changed
79
+ this.userFeedId = this.buildUserFeedId();
80
+
81
+ // Reinitialize the real-time connection
82
+ this.initializeRealtimeConnection();
83
+
84
+ // Reinitialize our broadcast channel
85
+ this.setupBroadcastChannel();
86
+ }
87
+
88
+ /**
89
+ * Cleans up a feed instance by destroying the store and disconnecting
90
+ * an open socket connection.
91
+ */
92
+ teardown() {
93
+ this.knock.log("[Feed] Tearing down feed instance");
94
+
95
+ if (this.channel) {
96
+ this.channel.leave();
97
+ this.channel.off("new-message");
98
+ }
99
+
100
+ if (this.disconnectTimer) {
101
+ clearTimeout(this.disconnectTimer);
102
+ this.disconnectTimer = null;
103
+ }
104
+
105
+ if (this.broadcastChannel) {
106
+ this.broadcastChannel.close();
107
+ }
108
+ }
109
+
110
+ /** Tears down an instance and removes it entirely from the feed manager */
111
+ dispose() {
112
+ this.knock.log("[Feed] Disposing of feed instance");
113
+ this.teardown();
114
+ this.broadcaster.removeAllListeners();
115
+ this.knock.feeds.removeInstance(this);
116
+ }
117
+
118
+ /*
119
+ Initializes a real-time connection to Knock, connecting the websocket for the
120
+ current ApiClient instance if the socket is not already connected.
121
+ */
122
+ listenForUpdates() {
123
+ this.knock.log("[Feed] Connecting to real-time service");
124
+
125
+ this.hasSubscribedToRealTimeUpdates = true;
126
+
127
+ const maybeSocket = this.knock.client().socket;
128
+
129
+ // Connect the socket only if we don't already have a connection
130
+ if (maybeSocket && !maybeSocket.isConnected()) {
131
+ maybeSocket.connect();
132
+ }
133
+
134
+ // Only join the channel if we're not already in a joining state
135
+ if (this.channel && ["closed", "errored"].includes(this.channel.state)) {
136
+ this.channel.join();
137
+ }
138
+ }
139
+
140
+ /* Binds a handler to be invoked when event occurs */
141
+ on(
142
+ eventName: BindableFeedEvent,
143
+ callback: FeedEventCallback | FeedRealTimeCallback,
144
+ ) {
145
+ this.broadcaster.on(eventName, callback);
146
+ }
147
+
148
+ off(
149
+ eventName: BindableFeedEvent,
150
+ callback: FeedEventCallback | FeedRealTimeCallback,
151
+ ) {
152
+ this.broadcaster.off(eventName, callback);
153
+ }
154
+
155
+ getState() {
156
+ return this.store.getState();
157
+ }
158
+
159
+ async markAsSeen(itemOrItems: FeedItemOrItems) {
160
+ const now = new Date().toISOString();
161
+ this.optimisticallyPerformStatusUpdate(
162
+ itemOrItems,
163
+ "seen",
164
+ { seen_at: now },
165
+ "unseen_count",
166
+ );
167
+
168
+ return this.makeStatusUpdate(itemOrItems, "seen");
169
+ }
170
+
171
+ async markAllAsSeen() {
172
+ // To mark all of the messages as seen we:
173
+ // 1. Optimistically update *everything* we have in the store
174
+ // 2. We decrement the `unseen_count` to zero optimistically
175
+ // 3. We issue the API call to the endpoint
176
+ //
177
+ // Note: there is the potential for a race condition here because the bulk
178
+ // update is an async method, so if a new message comes in during this window before
179
+ // the update has been processed we'll effectively reset the `unseen_count` to be what it was.
180
+ //
181
+ // Note: here we optimistically handle the case whereby the feed is scoped to show only `unseen`
182
+ // items by removing everything from view.
183
+ const { getState, setState } = this.store;
184
+ const { metadata, items } = getState();
185
+
186
+ const isViewingOnlyUnseen = this.defaultOptions.status === "unseen";
187
+
188
+ // If we're looking at the unseen view, then we want to remove all of the items optimistically
189
+ // from the store given that nothing should be visible. We do this by resetting the store state
190
+ // and setting the current metadata counts to 0
191
+ if (isViewingOnlyUnseen) {
192
+ setState((store) =>
193
+ store.resetStore({
194
+ ...metadata,
195
+ total_count: 0,
196
+ unseen_count: 0,
197
+ }),
198
+ );
199
+ } else {
200
+ // Otherwise we want to update the metadata and mark all of the items in the store as seen
201
+ setState((store) => store.setMetadata({ ...metadata, unseen_count: 0 }));
202
+
203
+ const attrs = { seen_at: new Date().toISOString() };
204
+ const itemIds = items.map((item) => item.id);
205
+
206
+ setState((store) => store.setItemAttrs(itemIds, attrs));
207
+ }
208
+
209
+ // Issue the API request to the bulk status change API
210
+ const result = await this.makeBulkStatusUpdate("seen");
211
+
212
+ this.broadcaster.emit(`items:all_seen`, { items });
213
+ this.broadcastOverChannel(`items:all_seen`, { items });
214
+
215
+ return result;
216
+ }
217
+
218
+ async markAsUnseen(itemOrItems: FeedItemOrItems) {
219
+ this.optimisticallyPerformStatusUpdate(
220
+ itemOrItems,
221
+ "unseen",
222
+ { seen_at: null },
223
+ "unseen_count",
224
+ );
225
+
226
+ return this.makeStatusUpdate(itemOrItems, "unseen");
227
+ }
228
+
229
+ async markAsRead(itemOrItems: FeedItemOrItems) {
230
+ const now = new Date().toISOString();
231
+ this.optimisticallyPerformStatusUpdate(
232
+ itemOrItems,
233
+ "read",
234
+ { read_at: now },
235
+ "unread_count",
236
+ );
237
+
238
+ return this.makeStatusUpdate(itemOrItems, "read");
239
+ }
240
+
241
+ async markAllAsRead() {
242
+ // To mark all of the messages as read we:
243
+ // 1. Optimistically update *everything* we have in the store
244
+ // 2. We decrement the `unread_count` to zero optimistically
245
+ // 3. We issue the API call to the endpoint
246
+ //
247
+ // Note: there is the potential for a race condition here because the bulk
248
+ // update is an async method, so if a new message comes in during this window before
249
+ // the update has been processed we'll effectively reset the `unread_count` to be what it was.
250
+ //
251
+ // Note: here we optimistically handle the case whereby the feed is scoped to show only `unread`
252
+ // items by removing everything from view.
253
+ const { getState, setState } = this.store;
254
+ const { metadata, items } = getState();
255
+
256
+ const isViewingOnlyUnread = this.defaultOptions.status === "unread";
257
+
258
+ // If we're looking at the unread view, then we want to remove all of the items optimistically
259
+ // from the store given that nothing should be visible. We do this by resetting the store state
260
+ // and setting the current metadata counts to 0
261
+ if (isViewingOnlyUnread) {
262
+ setState((store) =>
263
+ store.resetStore({
264
+ ...metadata,
265
+ total_count: 0,
266
+ unread_count: 0,
267
+ }),
268
+ );
269
+ } else {
270
+ // Otherwise we want to update the metadata and mark all of the items in the store as seen
271
+ setState((store) => store.setMetadata({ ...metadata, unread_count: 0 }));
272
+
273
+ const attrs = { read_at: new Date().toISOString() };
274
+ const itemIds = items.map((item) => item.id);
275
+
276
+ setState((store) => store.setItemAttrs(itemIds, attrs));
277
+ }
278
+
279
+ // Issue the API request to the bulk status change API
280
+ const result = await this.makeBulkStatusUpdate("read");
281
+
282
+ this.broadcaster.emit(`items:all_read`, { items });
283
+ this.broadcastOverChannel(`items:all_read`, { items });
284
+
285
+ return result;
286
+ }
287
+
288
+ async markAsUnread(itemOrItems: FeedItemOrItems) {
289
+ this.optimisticallyPerformStatusUpdate(
290
+ itemOrItems,
291
+ "unread",
292
+ { read_at: null },
293
+ "unread_count",
294
+ );
295
+
296
+ return this.makeStatusUpdate(itemOrItems, "unread");
297
+ }
298
+
299
+ async markAsInteracted(itemOrItems: FeedItemOrItems) {
300
+ const now = new Date().toISOString();
301
+ this.optimisticallyPerformStatusUpdate(
302
+ itemOrItems,
303
+ "interacted",
304
+ {
305
+ read_at: now,
306
+ interacted_at: now,
307
+ },
308
+ "unread_count",
309
+ );
310
+
311
+ return this.makeStatusUpdate(itemOrItems, "interacted");
312
+ }
313
+
314
+ /*
315
+ Marking one or more items as archived should:
316
+
317
+ - Decrement the badge count for any unread / unseen items
318
+ - Remove the item from the feed list when the `archived` flag is "exclude" (default)
319
+
320
+ TODO: how do we handle rollbacks?
321
+ */
322
+ async markAsArchived(itemOrItems: FeedItemOrItems) {
323
+ const { getState, setState } = this.store;
324
+ const state = getState();
325
+
326
+ const shouldOptimisticallyRemoveItems =
327
+ this.defaultOptions.archived === "exclude";
328
+
329
+ const normalizedItems = Array.isArray(itemOrItems)
330
+ ? itemOrItems
331
+ : [itemOrItems];
332
+
333
+ const itemIds: string[] = normalizedItems.map((item) => item.id);
334
+
335
+ /*
336
+ In the code here we want to optimistically update counts and items
337
+ that are persisted such that we can display updates immediately on the feed
338
+ without needing to make a network request.
339
+
340
+ Note: right now this does *not* take into account offline handling or any extensive retry
341
+ logic, so rollbacks aren't considered. That probably needs to be a future consideration for
342
+ this library.
343
+
344
+ Scenarios to consider:
345
+
346
+ ## Feed scope to archived *only*
347
+
348
+ - Counts should not be decremented
349
+ - Items should not be removed
350
+
351
+ ## Feed scoped to exclude archived items (the default)
352
+
353
+ - Counts should be decremented
354
+ - Items should be removed
355
+
356
+ ## Feed scoped to include archived items as well
357
+
358
+ - Counts should not be decremented
359
+ - Items should not be removed
360
+ */
361
+
362
+ if (shouldOptimisticallyRemoveItems) {
363
+ // If any of the items are unseen or unread, then capture as we'll want to decrement
364
+ // the counts for these in the metadata we have
365
+ const unseenCount = normalizedItems.filter((i) => !i.seen_at).length;
366
+ const unreadCount = normalizedItems.filter((i) => !i.read_at).length;
367
+
368
+ // Build the new metadata
369
+ const updatedMetadata = {
370
+ ...state.metadata,
371
+ total_count: state.metadata.total_count - normalizedItems.length,
372
+ unseen_count: state.metadata.unseen_count - unseenCount,
373
+ unread_count: state.metadata.unread_count - unreadCount,
374
+ };
375
+
376
+ // Remove the archiving entries
377
+ const entriesToSet = state.items.filter(
378
+ (item) => !itemIds.includes(item.id),
379
+ );
380
+
381
+ setState((state) =>
382
+ state.setResult({
383
+ entries: entriesToSet,
384
+ meta: updatedMetadata,
385
+ page_info: state.pageInfo,
386
+ }),
387
+ );
388
+ } else {
389
+ // Mark all the entries being updated as archived either way so the state is correct
390
+ state.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });
391
+ }
392
+
393
+ return this.makeStatusUpdate(itemOrItems, "archived");
394
+ }
395
+
396
+ async markAllAsArchived() {
397
+ // Note: there is the potential for a race condition here because the bulk
398
+ // update is an async method, so if a new message comes in during this window before
399
+ // the update has been processed we'll effectively reset the `unseen_count` to be what it was.
400
+ const { setState, getState } = this.store;
401
+ const { items } = getState();
402
+
403
+ // Here if we're looking at a feed that excludes all of the archived items by default then we
404
+ // will want to optimistically remove all of the items from the feed as they are now all excluded
405
+ const shouldOptimisticallyRemoveItems =
406
+ this.defaultOptions.archived === "exclude";
407
+
408
+ if (shouldOptimisticallyRemoveItems) {
409
+ // Reset the store to clear out all of items and reset the badge count
410
+ setState((store) => store.resetStore());
411
+ } else {
412
+ // Mark all the entries being updated as archived either way so the state is correct
413
+ setState((store) => {
414
+ const itemIds = items.map((i) => i.id);
415
+ store.setItemAttrs(itemIds, { archived_at: new Date().toISOString() });
416
+ });
417
+ }
418
+
419
+ // Issue the API request to the bulk status change API
420
+ const result = await this.makeBulkStatusUpdate("archive");
421
+
422
+ this.broadcaster.emit(`items:all_archived`, { items });
423
+ this.broadcastOverChannel(`items:all_archived`, { items });
424
+
425
+ return result;
426
+ }
427
+
428
+ async markAsUnarchived(itemOrItems: FeedItemOrItems) {
429
+ this.optimisticallyPerformStatusUpdate(itemOrItems, "unarchived", {
430
+ archived_at: null,
431
+ });
432
+
433
+ return this.makeStatusUpdate(itemOrItems, "unarchived");
434
+ }
435
+
436
+ /* Fetches the feed content, appending it to the store */
437
+ async fetch(options: FetchFeedOptions = {}) {
438
+ const { setState, getState } = this.store;
439
+ const { networkStatus } = getState();
440
+
441
+ // If there's an existing request in flight, then do nothing
442
+ if (isRequestInFlight(networkStatus)) {
443
+ return;
444
+ }
445
+
446
+ // Set the loading type based on the request type it is
447
+ setState((store) =>
448
+ store.setNetworkStatus(options.__loadingType ?? NetworkStatus.loading),
449
+ );
450
+
451
+ // Always include the default params, if they have been set
452
+ const queryParams = {
453
+ ...this.defaultOptions,
454
+ ...options,
455
+ // Unset options that should not be sent to the API
456
+ __loadingType: undefined,
457
+ __fetchSource: undefined,
458
+ __experimentalCrossBrowserUpdates: undefined,
459
+ auto_manage_socket_connection: undefined,
460
+ auto_manage_socket_connection_delay: undefined,
461
+ };
462
+
463
+ const result = await this.knock.client().makeRequest({
464
+ method: "GET",
465
+ url: `/v1/users/${this.knock.userId}/feeds/${this.feedId}`,
466
+ params: queryParams,
467
+ });
468
+
469
+ if (result.statusCode === "error" || !result.body) {
470
+ setState((store) => store.setNetworkStatus(NetworkStatus.error));
471
+
472
+ return {
473
+ status: result.statusCode,
474
+ data: result.error || result.body,
475
+ };
476
+ }
477
+
478
+ const response = {
479
+ entries: result.body.entries,
480
+ meta: result.body.meta,
481
+ page_info: result.body.page_info,
482
+ };
483
+
484
+ if (options.before) {
485
+ const opts = { shouldSetPage: false, shouldAppend: true };
486
+ setState((state) => state.setResult(response, opts));
487
+ } else if (options.after) {
488
+ const opts = { shouldSetPage: true, shouldAppend: true };
489
+ setState((state) => state.setResult(response, opts));
490
+ } else {
491
+ setState((state) => state.setResult(response));
492
+ }
493
+
494
+ // Legacy `messages.new` event, should be removed in a future version
495
+ this.broadcast("messages.new", response);
496
+
497
+ // Broadcast the appropriate event type depending on the fetch source
498
+ const feedEventType: FeedEvent =
499
+ options.__fetchSource === "socket"
500
+ ? "items.received.realtime"
501
+ : "items.received.page";
502
+
503
+ const eventPayload = {
504
+ items: response.entries as FeedItem[],
505
+ metadata: response.meta as FeedMetadata,
506
+ event: feedEventType,
507
+ };
508
+
509
+ this.broadcast(eventPayload.event, eventPayload);
510
+
511
+ return { data: response, status: result.statusCode };
512
+ }
513
+
514
+ async fetchNextPage() {
515
+ // Attempts to fetch the next page of results (if we have any)
516
+ const { getState } = this.store;
517
+ const { pageInfo } = getState();
518
+
519
+ if (!pageInfo.after) {
520
+ // Nothing more to fetch
521
+ return;
522
+ }
523
+
524
+ this.fetch({
525
+ after: pageInfo.after,
526
+ __loadingType: NetworkStatus.fetchMore,
527
+ });
528
+ }
529
+
530
+ private broadcast(
531
+ eventName: FeedEvent,
532
+ data: FeedResponse | FeedEventPayload,
533
+ ) {
534
+ this.broadcaster.emit(eventName, data);
535
+ }
536
+
537
+ // Invoked when a new real-time message comes in from the socket
538
+ private async onNewMessageReceived({
539
+ metadata,
540
+ }: FeedMessagesReceivedPayload) {
541
+ this.knock.log("[Feed] Received new real-time message");
542
+
543
+ // Handle the new message coming in
544
+ const { getState, setState } = this.store;
545
+ const { items } = getState();
546
+ const currentHead: FeedItem | undefined = items[0];
547
+ // Optimistically set the badge counts
548
+ setState((state) => state.setMetadata(metadata));
549
+ // Fetch the items before the current head (if it exists)
550
+ this.fetch({ before: currentHead?.__cursor, __fetchSource: "socket" });
551
+ }
552
+
553
+ private buildUserFeedId() {
554
+ return `${this.feedId}:${this.knock.userId}`;
555
+ }
556
+
557
+ private optimisticallyPerformStatusUpdate(
558
+ itemOrItems: FeedItemOrItems,
559
+ type: Status,
560
+ attrs: object,
561
+ badgeCountAttr?: "unread_count" | "unseen_count",
562
+ ) {
563
+ const { getState, setState } = this.store;
564
+ const normalizedItems = Array.isArray(itemOrItems)
565
+ ? itemOrItems
566
+ : [itemOrItems];
567
+ const itemIds = normalizedItems.map((item) => item.id);
568
+
569
+ if (badgeCountAttr) {
570
+ const { metadata } = getState();
571
+
572
+ // We only want to update the counts of items that have not already been counted towards the
573
+ // badge count total to avoid updating the badge count unnecessarily.
574
+ const itemsToUpdate = normalizedItems.filter((item) => {
575
+ switch (type) {
576
+ case "seen":
577
+ return item.seen_at === null;
578
+ case "unseen":
579
+ return item.seen_at !== null;
580
+ case "read":
581
+ case "interacted":
582
+ return item.read_at === null;
583
+ case "unread":
584
+ return item.read_at !== null;
585
+ default:
586
+ return true;
587
+ }
588
+ });
589
+
590
+ // Tnis is a hack to determine the direction of whether we're
591
+ // adding or removing from the badge count
592
+ const direction = type.startsWith("un")
593
+ ? itemsToUpdate.length
594
+ : -itemsToUpdate.length;
595
+
596
+ setState((store) =>
597
+ store.setMetadata({
598
+ ...metadata,
599
+ [badgeCountAttr]: Math.max(0, metadata[badgeCountAttr] + direction),
600
+ }),
601
+ );
602
+ }
603
+
604
+ // Update the items with the given attributes
605
+ setState((store) => store.setItemAttrs(itemIds, attrs));
606
+ }
607
+
608
+ private async makeStatusUpdate(itemOrItems: FeedItemOrItems, type: Status) {
609
+ // Always treat items as a batch to use the corresponding batch endpoint
610
+ const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];
611
+ const itemIds = items.map((item) => item.id);
612
+
613
+ const result = await this.knock.client().makeRequest({
614
+ method: "POST",
615
+ url: `/v1/messages/batch/${type}`,
616
+ data: { message_ids: itemIds },
617
+ });
618
+
619
+ // Emit the event that these items had their statuses changed
620
+ // Note: we do this after the update to ensure that the server event actually completed
621
+ this.broadcaster.emit(`items.${type}`, { items });
622
+
623
+ // Note: `items.type` format is being deprecated in favor over the `items:type` format,
624
+ // but emit both formats to make it backward compatible for now.
625
+ this.broadcaster.emit(`items:${type}`, { items });
626
+ this.broadcastOverChannel(`items:${type}`, { items });
627
+
628
+ return result;
629
+ }
630
+
631
+ private async makeBulkStatusUpdate(type: "seen" | "read" | "archive") {
632
+ // The base scope for the call should take into account all of the options currently
633
+ // set on the feed, as well as being scoped for the current user. We do this so that
634
+ // we ONLY make changes to the messages that are currently in view on this feed, and not
635
+ // all messages that exist.
636
+ const options = {
637
+ user_ids: [this.knock.userId],
638
+ engagement_status:
639
+ this.defaultOptions.status !== "all"
640
+ ? this.defaultOptions.status
641
+ : undefined,
642
+ archived: this.defaultOptions.archived,
643
+ has_tenant: this.defaultOptions.has_tenant,
644
+ tenants: this.defaultOptions.tenant
645
+ ? [this.defaultOptions.tenant]
646
+ : undefined,
647
+ };
648
+
649
+ return await this.knock.client().makeRequest({
650
+ method: "POST",
651
+ url: `/v1/channels/${this.feedId}/messages/bulk/${type}`,
652
+ data: options,
653
+ });
654
+ }
655
+
656
+ private setupBroadcastChannel() {
657
+ // Attempt to bind to listen to other events from this feed in different tabs
658
+ // Note: here we ensure `self` is available (it's not in server rendered envs)
659
+ this.broadcastChannel =
660
+ typeof self !== "undefined" && "BroadcastChannel" in self
661
+ ? new BroadcastChannel(`knock:feed:${this.userFeedId}`)
662
+ : null;
663
+
664
+ // Opt into receiving updates from _other tabs for the same user / feed_ via the broadcast
665
+ // channel (iff it's enabled and exists)
666
+ if (
667
+ this.broadcastChannel &&
668
+ this.defaultOptions.__experimentalCrossBrowserUpdates === true
669
+ ) {
670
+ this.broadcastChannel.onmessage = (e) => {
671
+ switch (e.data.type) {
672
+ case "items:archived":
673
+ case "items:unarchived":
674
+ case "items:seen":
675
+ case "items:unseen":
676
+ case "items:read":
677
+ case "items:unread":
678
+ case "items:all_read":
679
+ case "items:all_seen":
680
+ case "items:all_archived":
681
+ // When items are updated in any other tab, simply refetch to get the latest state
682
+ // to make sure that the state gets updated accordingly. In the future here we could
683
+ // maybe do this optimistically without the fetch.
684
+ return this.fetch();
685
+ default:
686
+ return null;
687
+ }
688
+ };
689
+ }
690
+ }
691
+
692
+ private broadcastOverChannel(type: string, payload: any) {
693
+ // The broadcastChannel may not be available in non-browser environments
694
+ if (!this.broadcastChannel) {
695
+ return;
696
+ }
697
+
698
+ // Here we stringify our payload and try and send as JSON such that we
699
+ // don't get any `An object could not be cloned` errors when trying to broadcast
700
+ try {
701
+ const stringifiedPayload = JSON.parse(JSON.stringify(payload));
702
+
703
+ this.broadcastChannel.postMessage({
704
+ type,
705
+ payload: stringifiedPayload,
706
+ });
707
+ } catch (e) {
708
+ console.warn(`Could not broadcast ${type}, got error: ${e}`);
709
+ }
710
+ }
711
+
712
+ private initializeRealtimeConnection() {
713
+ const { socket: maybeSocket } = this.knock.client();
714
+
715
+ // In server environments we might not have a socket connection
716
+ if (!maybeSocket) return;
717
+
718
+ // Reinitialize channel connections incase the socket changed
719
+ this.channel = maybeSocket.channel(
720
+ `feeds:${this.userFeedId}`,
721
+ this.defaultOptions,
722
+ );
723
+
724
+ this.channel.on("new-message", (resp) => this.onNewMessageReceived(resp));
725
+
726
+ if (this.defaultOptions.auto_manage_socket_connection) {
727
+ this.setupAutoSocketManager(
728
+ this.defaultOptions.auto_manage_socket_connection_delay,
729
+ );
730
+ }
731
+
732
+ // If we're initializing but they have previously opted to listen to real-time updates
733
+ // then we will automatically reconnect on their behalf
734
+ if (this.hasSubscribedToRealTimeUpdates) {
735
+ if (!maybeSocket.isConnected()) maybeSocket.connect();
736
+ this.channel.join();
737
+ }
738
+ }
739
+
740
+ /**
741
+ * Listen for changes to document visibility and automatically disconnect
742
+ * or reconnect the socket after a delay
743
+ */
744
+ private setupAutoSocketManager(autoManageSocketConnectionDelay?: number) {
745
+ const disconnectDelay =
746
+ autoManageSocketConnectionDelay ?? DEFAULT_DISCONNECT_DELAY;
747
+
748
+ document.addEventListener("visibilitychange", () => {
749
+ const client = this.knock.client();
750
+
751
+ if (document.visibilityState === "hidden") {
752
+ // When the tab is hidden, clean up the socket connection after a delay
753
+ this.disconnectTimer = setTimeout(() => {
754
+ client.socket?.disconnect();
755
+ this.disconnectTimer = null;
756
+ }, disconnectDelay);
757
+ } else if (document.visibilityState === "visible") {
758
+ // When the tab is visible, clear the disconnect timer if active to cancel disconnecting
759
+ // This handles cases where the tab is only briefly hidden to avoid unnecessary disconnects
760
+ if (this.disconnectTimer) {
761
+ clearTimeout(this.disconnectTimer);
762
+ this.disconnectTimer = null;
763
+ }
764
+
765
+ // If the socket is not connected, try to reconnect
766
+ if (!client.socket?.isConnected()) {
767
+ this.initializeRealtimeConnection();
768
+ }
769
+ }
770
+ });
771
+ }
772
+ }
773
+
774
+ export default Feed;