stream-chat 10.0.0-rc.2 → 10.0.0-rc.21
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/README.md +11 -8
- package/dist/cjs/index.browser.js +15397 -12090
- package/dist/cjs/index.browser.js.map +4 -4
- package/dist/cjs/index.node.js +15349 -11991
- package/dist/cjs/index.node.js.map +4 -4
- package/dist/esm/index.mjs +15164 -11880
- package/dist/esm/index.mjs.map +4 -4
- package/dist/types/ChannelManager.d.ts +246 -6
- package/dist/types/CooldownTimer.d.ts +22 -8
- package/dist/types/EventHandlerPipeline.d.ts +12 -16
- package/dist/types/LiveLocationManager.d.ts +69 -6
- package/dist/types/api-client.d.ts +22 -9
- package/dist/types/channel.d.ts +550 -300
- package/dist/types/channel_state.d.ts +167 -22
- package/dist/types/client.d.ts +325 -305
- package/dist/types/configuration/ConfigController.d.ts +172 -0
- package/dist/types/configuration/InstanceConfigurationRegistry.d.ts +192 -0
- package/dist/types/configuration/index.d.ts +3 -0
- package/dist/types/configuration/keys.d.ts +31 -0
- package/dist/types/configuration/shape.d.ts +58 -0
- package/dist/types/configuration/types.d.ts +206 -50
- package/dist/types/configuration/utils/applyInstanceConfiguration.d.ts +59 -0
- package/dist/types/configuration/utils/copyConfigPatch.d.ts +1 -0
- package/dist/types/configuration/utils/declarativeSlices.d.ts +24 -0
- package/dist/types/configuration/utils/deepFreezeConfig.d.ts +22 -0
- package/dist/types/configuration/utils/index.d.ts +1 -0
- package/dist/types/configuration/utils/serverAuthority.d.ts +49 -0
- package/dist/types/connection/ConnectionIdManager.d.ts +67 -0
- package/dist/types/connection/ConnectionRecoveryManager.d.ts +164 -0
- package/dist/types/connection/index.d.ts +4 -0
- package/dist/types/connection/networkConnection/NetworkConnectionObserver.d.ts +153 -0
- package/dist/types/connection/networkConnection/index.d.ts +3 -0
- package/dist/types/connection/networkConnection/reporters.d.ts +43 -0
- package/dist/types/connection/networkConnection/types.d.ts +55 -0
- package/dist/types/connection/wsConnection/StableWSConnection.d.ts +204 -0
- package/dist/types/connection/wsConnection/WSConnection.d.ts +168 -0
- package/dist/types/connection/wsConnection/WSConnectionFallback.d.ts +55 -0
- package/dist/types/connection/wsConnection/config.d.ts +51 -0
- package/dist/types/connection/wsConnection/index.d.ts +4 -0
- package/dist/types/connection/wsConnection/types.d.ts +99 -0
- package/dist/types/constants.d.ts +3 -1
- package/dist/types/entityStore/EntityStore.d.ts +160 -40
- package/dist/types/entityStore/StoreBackedItemIndex.d.ts +48 -29
- package/dist/types/entityStore/index.d.ts +0 -1
- package/dist/types/errors.d.ts +24 -4
- package/dist/types/formatMessageMemo.d.ts +5 -0
- package/dist/types/gen/chat/ChannelApi.d.ts +51 -23
- package/dist/types/gen/chat/ChatApi.d.ts +215 -168
- package/dist/types/gen/models/index.d.ts +2164 -557
- package/dist/types/gen/models/timestamp-guard.d.ts +22 -0
- package/dist/types/gen/moderation/ModerationApi.d.ts +39 -32
- package/dist/types/gen-imports.d.ts +1 -0
- package/dist/types/index.d.ts +20 -7
- package/dist/types/languageNames.d.ts +97 -0
- package/dist/types/logger.d.ts +1 -1
- package/dist/types/messageComposer/CustomDataManager.d.ts +7 -1
- package/dist/types/messageComposer/LocationComposer.d.ts +10 -4
- package/dist/types/messageComposer/attachmentIdentity.d.ts +11 -0
- package/dist/types/messageComposer/attachmentManager.d.ts +48 -10
- package/dist/types/messageComposer/attachmentUtils.d.ts +34 -0
- package/dist/types/messageComposer/configuration/commands.configuration.d.ts +1 -3
- package/dist/types/messageComposer/configuration/configuration.d.ts +9 -1
- package/dist/types/messageComposer/configuration/index.d.ts +0 -1
- package/dist/types/messageComposer/configuration/types.d.ts +57 -1
- package/dist/types/messageComposer/index.d.ts +1 -0
- package/dist/types/messageComposer/linkPreviewsManager.d.ts +10 -4
- package/dist/types/messageComposer/messageComposer.d.ts +210 -14
- package/dist/types/messageComposer/middleware/attachmentManager/postUpload/attachmentEnrichment.d.ts +20 -1
- package/dist/types/messageComposer/middleware/messageComposer/attachments.d.ts +11 -0
- package/dist/types/messageComposer/middleware/messageComposer/types.d.ts +2 -2
- package/dist/types/messageComposer/middleware/pollComposer/index.d.ts +1 -0
- package/dist/types/messageComposer/middleware/pollComposer/state.d.ts +3 -2
- package/dist/types/messageComposer/middleware/pollComposer/types.d.ts +9 -2
- package/dist/types/messageComposer/middleware/pollComposer/validation.d.ts +44 -0
- package/dist/types/messageComposer/middleware/textComposer/commands.d.ts +2 -2
- package/dist/types/messageComposer/middleware/textComposer/mentions.d.ts +94 -229
- package/dist/types/messageComposer/pollComposer.d.ts +8 -3
- package/dist/types/messageComposer/textComposer.d.ts +9 -1
- package/dist/types/messageComposer/types.d.ts +7 -3
- package/dist/types/messageDelivery/MessageDeliveryReporter.d.ts +64 -8
- package/dist/types/messageDelivery/MessageReceiptsTracker.d.ts +48 -11
- package/dist/types/messageOperations/FailedSendCache.d.ts +34 -0
- package/dist/types/messageOperations/MessageOperationStatePolicy.d.ts +52 -6
- package/dist/types/messageOperations/MessageOperations.d.ts +94 -11
- package/dist/types/{entityStore → messageOperations}/applyReactionLocally.d.ts +8 -2
- package/dist/types/messageOperations/createMessageOperations.d.ts +11 -0
- package/dist/types/messageOperations/index.d.ts +7 -1
- package/dist/types/messageOperations/reflectReactionEvent.d.ts +24 -0
- package/dist/types/messageOperations/sendOrdering.d.ts +17 -0
- package/dist/types/messageOperations/settlePendingAttachmentUploads.d.ts +28 -0
- package/dist/types/messageOperations/types.d.ts +66 -1
- package/dist/types/middleware.d.ts +8 -0
- package/dist/types/moderation.d.ts +18 -4
- package/dist/types/notifications/NotificationManager.d.ts +43 -2
- package/dist/types/notifications/types.d.ts +55 -42
- package/dist/types/offline-support/offline_support_api.d.ts +128 -12
- package/dist/types/offline-support/offline_sync_manager.d.ts +26 -5
- package/dist/types/offline-support/queueableOperations.d.ts +106 -0
- package/dist/types/offline-support/types.d.ts +77 -28
- package/dist/types/pagination/FilterBuilder.d.ts +1 -18
- package/dist/types/pagination/cursorDerivation/createdAtAroundPaginationFlags.d.ts +2 -1
- package/dist/types/pagination/cursorDerivation/linearPaginationFlags.d.ts +21 -3
- package/dist/types/pagination/filterCompiler.d.ts +5 -0
- package/dist/types/pagination/paginators/BasePaginator.d.ts +331 -7
- package/dist/types/pagination/paginators/ChannelPaginator.d.ts +54 -11
- package/dist/types/pagination/paginators/MessageIntervalPaginator.d.ts +283 -66
- package/dist/types/pagination/paginators/MessagePaginator.d.ts +10 -9
- package/dist/types/pagination/paginators/PinnedMessagePaginator.d.ts +17 -1
- package/dist/types/pagination/paginators/ReminderPaginator.d.ts +8 -8
- package/dist/types/pagination/paginators/ThreadPaginator.d.ts +57 -0
- package/dist/types/pagination/paginators/index.d.ts +1 -0
- package/dist/types/pagination/utility.normalization.d.ts +29 -2
- package/dist/types/poll.d.ts +41 -14
- package/dist/types/poll_manager.d.ts +3 -2
- package/dist/types/poll_optimistic_votes.d.ts +61 -0
- package/dist/types/reminders/Reminder.d.ts +17 -7
- package/dist/types/reminders/ReminderManager.d.ts +34 -6
- package/dist/types/search/BaseSearchSource.d.ts +50 -25
- package/dist/types/search/ChannelMemberSearchSource.d.ts +11 -6
- package/dist/types/search/ChannelSearchSource.d.ts +27 -5
- package/dist/types/search/MessageSearchSource.d.ts +36 -6
- package/dist/types/search/SearchController.d.ts +44 -11
- package/dist/types/search/UserSearchSource.d.ts +11 -5
- package/dist/types/search/types.d.ts +14 -1
- package/dist/types/signing.d.ts +0 -157
- package/dist/types/thread.d.ts +118 -62
- package/dist/types/thread_manager.d.ts +124 -35
- package/dist/types/token_manager.d.ts +5 -8
- package/dist/types/types.d.ts +138 -562
- package/dist/types/types.utility.d.ts +8 -0
- package/dist/types/upload-utils.d.ts +20 -0
- package/dist/types/uploadManager.d.ts +50 -2
- package/dist/types/utils/FixedSizeQueueCache.d.ts +2 -0
- package/dist/types/utils/WithSubscriptions.d.ts +1 -1
- package/dist/types/utils/objectPath.d.ts +49 -0
- package/dist/types/utils/time.d.ts +85 -0
- package/dist/types/utils.d.ts +63 -40
- package/package.json +22 -33
- package/src/ChannelManager.ts +561 -109
- package/src/CooldownTimer.ts +55 -45
- package/src/EventHandlerPipeline.ts +13 -23
- package/src/LiveLocationManager.ts +191 -37
- package/src/api-client.ts +144 -53
- package/src/channel.ts +1573 -1050
- package/src/channel_state.ts +239 -129
- package/src/client.ts +843 -892
- package/src/client_state.ts +2 -2
- package/src/configuration/ConfigController.ts +298 -0
- package/src/configuration/InstanceConfigurationRegistry.ts +430 -0
- package/src/configuration/index.ts +7 -0
- package/src/configuration/keys.ts +86 -0
- package/src/configuration/shape.ts +776 -0
- package/src/configuration/types.ts +218 -60
- package/src/configuration/utils/applyInstanceConfiguration.ts +184 -0
- package/src/configuration/utils/copyConfigPatch.ts +61 -0
- package/src/configuration/utils/declarativeSlices.ts +81 -0
- package/src/configuration/utils/deepFreezeConfig.ts +33 -0
- package/src/configuration/utils/index.ts +5 -0
- package/src/configuration/utils/serverAuthority.ts +146 -0
- package/src/connection/ConnectionIdManager.ts +153 -0
- package/src/connection/ConnectionRecoveryManager.ts +371 -0
- package/src/connection/index.ts +4 -0
- package/src/connection/networkConnection/NetworkConnectionObserver.ts +281 -0
- package/src/connection/networkConnection/index.ts +3 -0
- package/src/connection/networkConnection/reporters.ts +85 -0
- package/src/connection/networkConnection/types.ts +60 -0
- package/src/{connection.ts → connection/wsConnection/StableWSConnection.ts} +242 -151
- package/src/connection/wsConnection/WSConnection.ts +345 -0
- package/src/connection/wsConnection/WSConnectionFallback.ts +259 -0
- package/src/connection/wsConnection/config.ts +108 -0
- package/src/connection/wsConnection/index.ts +4 -0
- package/src/connection/wsConnection/types.ts +101 -0
- package/src/constants.ts +5 -3
- package/src/entityStore/EntityStore.ts +350 -10
- package/src/entityStore/StoreBackedItemIndex.ts +66 -37
- package/src/entityStore/index.ts +0 -1
- package/src/errors.ts +40 -7
- package/src/formatMessageMemo.ts +23 -0
- package/src/gen/chat/ChannelApi.ts +230 -44
- package/src/gen/chat/ChatApi.ts +1047 -1745
- package/src/gen/models/index.ts +2378 -3721
- package/src/gen/models/timestamp-guard.ts +27 -0
- package/src/gen/moderation/ModerationApi.ts +218 -388
- package/src/gen-imports.ts +1 -0
- package/src/index.ts +82 -6
- package/src/languageNames.ts +103 -0
- package/src/logger.ts +1 -1
- package/src/messageComposer/CustomDataManager.ts +10 -1
- package/src/messageComposer/LocationComposer.ts +68 -20
- package/src/messageComposer/attachmentIdentity.ts +39 -0
- package/src/messageComposer/attachmentManager.ts +191 -68
- package/src/messageComposer/attachmentUtils.ts +58 -0
- package/src/messageComposer/configuration/commands.configuration.ts +1 -26
- package/src/messageComposer/configuration/configuration.ts +22 -3
- package/src/messageComposer/configuration/index.ts +0 -1
- package/src/messageComposer/configuration/types.ts +58 -1
- package/src/messageComposer/index.ts +1 -0
- package/src/messageComposer/linkPreviewsManager.ts +16 -10
- package/src/messageComposer/messageComposer.ts +487 -98
- package/src/messageComposer/middleware/attachmentManager/postUpload/AttachmentPostUploadMiddlewareExecutor.ts +1 -1
- package/src/messageComposer/middleware/attachmentManager/postUpload/attachmentEnrichment.ts +58 -33
- package/src/messageComposer/middleware/attachmentManager/postUpload/uploadErrorHandler.ts +7 -2
- package/src/messageComposer/middleware/attachmentManager/preUpload/blockedUploadNotification.ts +3 -2
- package/src/messageComposer/middleware/messageComposer/attachments.ts +98 -1
- package/src/messageComposer/middleware/messageComposer/linkPreviews.ts +20 -10
- package/src/messageComposer/middleware/messageComposer/sharedLocation.ts +7 -2
- package/src/messageComposer/middleware/messageComposer/types.ts +2 -7
- package/src/messageComposer/middleware/messageComposer/userDataInjection.ts +11 -6
- package/src/messageComposer/middleware/pollComposer/index.ts +1 -0
- package/src/messageComposer/middleware/pollComposer/state.ts +43 -11
- package/src/messageComposer/middleware/pollComposer/types.ts +9 -2
- package/src/messageComposer/middleware/pollComposer/validation.ts +76 -0
- package/src/messageComposer/middleware/textComposer/commandUtils.ts +3 -2
- package/src/messageComposer/middleware/textComposer/commands.ts +1 -2
- package/src/messageComposer/middleware/textComposer/mentions.ts +91 -39
- package/src/messageComposer/pollComposer.ts +11 -3
- package/src/messageComposer/textComposer.ts +36 -4
- package/src/messageComposer/types.ts +19 -14
- package/src/messageDelivery/MessageDeliveryReporter.ts +154 -53
- package/src/messageDelivery/MessageReceiptsTracker.ts +136 -64
- package/src/messageOperations/FailedSendCache.ts +90 -0
- package/src/messageOperations/MessageOperationStatePolicy.ts +269 -27
- package/src/messageOperations/MessageOperations.ts +340 -136
- package/src/{entityStore → messageOperations}/applyReactionLocally.ts +10 -3
- package/src/messageOperations/createMessageOperations.ts +192 -0
- package/src/messageOperations/index.ts +7 -0
- package/src/messageOperations/reflectReactionEvent.ts +51 -0
- package/src/messageOperations/sendOrdering.ts +28 -0
- package/src/messageOperations/settlePendingAttachmentUploads.ts +110 -0
- package/src/messageOperations/types.ts +74 -0
- package/src/middleware.ts +11 -0
- package/src/moderation.ts +47 -24
- package/src/notifications/NotificationManager.ts +73 -3
- package/src/notifications/configuration.ts +10 -8
- package/src/notifications/types.ts +58 -42
- package/src/offline-support/offline_support_api.ts +380 -197
- package/src/offline-support/offline_sync_manager.ts +105 -32
- package/src/offline-support/queueableOperations.ts +270 -0
- package/src/offline-support/types.ts +85 -39
- package/src/pagination/FilterBuilder.ts +1 -22
- package/src/pagination/cursorDerivation/createdAtAroundPaginationFlags.ts +11 -7
- package/src/pagination/cursorDerivation/linearPaginationFlags.ts +25 -7
- package/src/pagination/filterCompiler.ts +17 -0
- package/src/pagination/paginators/BasePaginator.ts +731 -81
- package/src/pagination/paginators/ChannelPaginator.ts +210 -102
- package/src/pagination/paginators/MessageIntervalPaginator.ts +703 -193
- package/src/pagination/paginators/MessagePaginator.ts +45 -36
- package/src/pagination/paginators/PinnedMessagePaginator.ts +74 -28
- package/src/pagination/paginators/ReminderPaginator.ts +15 -13
- package/src/pagination/paginators/ThreadPaginator.ts +181 -0
- package/src/pagination/paginators/UserGroupPaginator.ts +8 -1
- package/src/pagination/paginators/index.ts +1 -0
- package/src/pagination/utility.normalization.ts +37 -9
- package/src/poll.ts +189 -112
- package/src/poll_manager.ts +14 -8
- package/src/poll_optimistic_votes.ts +335 -0
- package/src/reminders/Reminder.ts +34 -16
- package/src/reminders/ReminderManager.ts +68 -41
- package/src/reminders/ReminderTimer.ts +7 -6
- package/src/search/BaseSearchSource.ts +133 -40
- package/src/search/ChannelMemberSearchSource.ts +27 -18
- package/src/search/ChannelSearchSource.ts +61 -7
- package/src/search/MessageSearchSource.ts +133 -28
- package/src/search/SearchController.ts +93 -16
- package/src/search/UserSearchSource.ts +27 -13
- package/src/search/types.ts +14 -1
- package/src/signing.ts +1 -315
- package/src/thread.ts +503 -511
- package/src/thread_manager.ts +339 -204
- package/src/token_manager.ts +12 -32
- package/src/types.ts +159 -780
- package/src/types.utility.ts +14 -0
- package/src/upload-utils.ts +53 -0
- package/src/uploadManager.ts +85 -7
- package/src/utils/FixedSizeQueueCache.ts +5 -0
- package/src/utils/WithSubscriptions.ts +1 -1
- package/src/utils/objectPath.ts +67 -0
- package/src/utils/time.ts +104 -0
- package/src/utils.ts +209 -157
- package/dist/types/base64.d.ts +0 -2
- package/dist/types/configuration/InstanceConfigurationService.d.ts +0 -26
- package/dist/types/connection.d.ts +0 -146
- package/dist/types/connection_fallback.d.ts +0 -42
- package/dist/types/gen/model-decoders/decoders.d.ts +0 -3
- package/dist/types/gen/model-decoders/event-decoder-mapping.d.ts +0 -6
- package/dist/types/insights.d.ts +0 -58
- package/dist/types/pagination/utility.queryChannel.d.ts +0 -26
- package/dist/types/permissions.d.ts +0 -55
- package/dist/types/store.d.ts +0 -125
- package/src/base64.ts +0 -78
- package/src/configuration/InstanceConfigurationService.ts +0 -73
- package/src/connection_fallback.ts +0 -215
- package/src/gen/model-decoders/decoders.ts +0 -2680
- package/src/gen/model-decoders/event-decoder-mapping.ts +0 -198
- package/src/insights.ts +0 -83
- package/src/pagination/utility.queryChannel.ts +0 -84
- package/src/permissions.ts +0 -102
- package/src/store.ts +0 -304
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { WithSubscriptions } from '../utils/WithSubscriptions';
|
|
2
|
+
import type { StreamChat } from '../client';
|
|
3
|
+
import type { StateStore } from '@stream-io/state-store';
|
|
4
|
+
export type ConnectionRecoveryManagerConfig = {
|
|
5
|
+
/**
|
|
6
|
+
* Whether the client recovers its own state when the connection comes back.
|
|
7
|
+
*
|
|
8
|
+
* Set it to `false` only if the application recovers state itself — nothing is then re-queried or
|
|
9
|
+
* re-watched on reconnect, and refreshing whatever is on screen becomes the consumer's job.
|
|
10
|
+
* Read when a recovery actually runs, so flipping it takes effect from the next reconnect.
|
|
11
|
+
*/
|
|
12
|
+
enabled: boolean;
|
|
13
|
+
};
|
|
14
|
+
export declare const DEFAULT_CONNECTION_RECOVERY_MANAGER_CONFIG: ConnectionRecoveryManagerConfig;
|
|
15
|
+
/**
|
|
16
|
+
* Owns connection recovery: after the socket comes back, brings the surfaces the app is actually
|
|
17
|
+
* consuming back in line with the server.
|
|
18
|
+
*
|
|
19
|
+
* Recovery is deliberately narrow — three things, and nothing else:
|
|
20
|
+
*
|
|
21
|
+
* 1. **Every loaded channel list re-runs its own first-page query** (`ChannelManager.recover`). Since
|
|
22
|
+
* `queryChannels` watches by default, that also re-establishes the watches for the channels on
|
|
23
|
+
* that page.
|
|
24
|
+
* 2. **Every active channel reloads itself** (`Channel.reload`), because a list page carries far fewer
|
|
25
|
+
* messages per channel than an open channel's loaded window, so the open channel cannot be served
|
|
26
|
+
* by the list query.
|
|
27
|
+
* 3. **Every active thread reloads its replies** (`Thread.reload`), because none of the above touches
|
|
28
|
+
* them: a channel reload refreshes the main message list, and `ThreadManager`'s own recovery
|
|
29
|
+
* refreshes the thread *list*, reusing thread instances without rehydrating them unless something
|
|
30
|
+
* separately marked them stale.
|
|
31
|
+
*
|
|
32
|
+
* It is explicitly **not** a sweep over the channel store (`client.channelManager.values()`). Watches
|
|
33
|
+
* are a bounded server resource, and after any scrolling that store holds far more channels than a query returns — so
|
|
34
|
+
* re-watching all of them would be both wasteful and a watch-limit hazard. Channels outside the
|
|
35
|
+
* refreshed pages come back demand-driven, when an event proves them relevant (see
|
|
36
|
+
* `restoreInterruptedWatch` in `ChannelManager`).
|
|
37
|
+
*
|
|
38
|
+
* This replaces the removed `client.recoverState()` bulk query
|
|
39
|
+
* (`cid: { $in: <every loaded channel> }, limit: 30`), which invented its own query shape unrelated to any
|
|
40
|
+
* list's filters and silently dropped everything past the thirtieth channel.
|
|
41
|
+
*
|
|
42
|
+
* ### Triggers, and why there are two
|
|
43
|
+
*
|
|
44
|
+
* One per surface, mirroring what the React Native SDK arrived at:
|
|
45
|
+
*
|
|
46
|
+
* - **Lists** — the socket's status store going online. `ChannelPaginator.executeQuery` carries its
|
|
47
|
+
* own first-page deferral for the unsynced case, so the list query orders itself against the
|
|
48
|
+
* offline sync.
|
|
49
|
+
* - **Active channels** — the offline DB's *sync-status edge* when offline support is enabled, and
|
|
50
|
+
* that same store otherwise. `Channel.reload()` has no deferral of its own, so it has to be
|
|
51
|
+
* triggered by something that is already post-sync.
|
|
52
|
+
*
|
|
53
|
+
* The edge is what guarantees `executePendingTasks()` → `sync()` → query ordering, on every reconnect
|
|
54
|
+
* path: `OfflineDBSyncManager` calls `invokeSyncStatusListeners(true)` **unconditionally** after
|
|
55
|
+
* `syncAndExecutePendingTasks()` on each online transition — it does not require `isSynced` to have
|
|
56
|
+
* been `false` first. That matters because the sync edge, unlike anything derived from a single
|
|
57
|
+
* transition, is guaranteed to land after the replay and the sync rather than alongside them.
|
|
58
|
+
*
|
|
59
|
+
* Recovery deliberately keeps no "did we drop?" flag of its own: `ChannelWatchStatus.WasWatching`
|
|
60
|
+
* already records exactly that, written from the truthful hooks
|
|
61
|
+
* (`StableWSConnection._setHealth(false)` and `closeConnection()`, plus
|
|
62
|
+
* `WSConnectionFallback._setState()` after an `enableWSFallback` switch).
|
|
63
|
+
*
|
|
64
|
+
* The status store it follows is the long-poll's once `enableWSFallback` has switched to it, so a
|
|
65
|
+
* long-poll reconnect recovers the same way a WebSocket reconnect does.
|
|
66
|
+
*/
|
|
67
|
+
export declare class ConnectionRecoveryManager extends WithSubscriptions {
|
|
68
|
+
client: StreamChat;
|
|
69
|
+
/** The shared configuration machinery — see {@link ConfigController}. */
|
|
70
|
+
private readonly configController;
|
|
71
|
+
/** Set while a pass over the active channels and threads is in flight. */
|
|
72
|
+
private isRecovering;
|
|
73
|
+
/**
|
|
74
|
+
* A reconnect that arrived while a pass was already running, so the pass re-runs when it finishes.
|
|
75
|
+
*
|
|
76
|
+
* A second pass cannot start alongside the first: they would reload every active channel twice and
|
|
77
|
+
* race on {@link isRecovering}, the earlier one clearing it while the later is still going. Nor can
|
|
78
|
+
* the reconnect simply be dropped — the running pass will withhold its `connection.recovered`
|
|
79
|
+
* precisely because the socket moved under it, so nothing else would bring the newer connection's
|
|
80
|
+
* state back in line.
|
|
81
|
+
*
|
|
82
|
+
* Re-entry is not hypothetical. The trigger is a subscription to the socket's status store, and a
|
|
83
|
+
* reload that moves that status calls straight back into here before the first `await`.
|
|
84
|
+
*/
|
|
85
|
+
private recoveryRequestedAgain;
|
|
86
|
+
/** Undone by `unregisterSubscriptions`, so re-registering does not stack listeners. */
|
|
87
|
+
private unsubscribeSyncStatus?;
|
|
88
|
+
constructor({ client }: {
|
|
89
|
+
client: StreamChat;
|
|
90
|
+
});
|
|
91
|
+
/**
|
|
92
|
+
* Resolved configuration, as a store — the shape every configurable class exposes
|
|
93
|
+
* (`configState` / `config` / `updateConfig`).
|
|
94
|
+
*/
|
|
95
|
+
get configState(): StateStore<ConnectionRecoveryManagerConfig>;
|
|
96
|
+
/** The current resolved configuration. `Readonly` — change it through {@link updateConfig}. */
|
|
97
|
+
get config(): Readonly<ConnectionRecoveryManagerConfig>;
|
|
98
|
+
/** Merges a partial configuration into the resolved config and notifies subscribers. */
|
|
99
|
+
updateConfig(config: Partial<ConnectionRecoveryManagerConfig>): void;
|
|
100
|
+
/**
|
|
101
|
+
* Rebuilds the resolved configuration from package defaults plus the declarative slice — the
|
|
102
|
+
* derivation entry point every configurable entity exposes, so the client routes a slice here and
|
|
103
|
+
* knows nothing about this manager's defaults or merge semantics.
|
|
104
|
+
*/
|
|
105
|
+
initializeConfig(config?: Partial<ConnectionRecoveryManagerConfig>): void;
|
|
106
|
+
registerSubscriptions: () => () => symbol;
|
|
107
|
+
/**
|
|
108
|
+
* Bind to the offline DB's sync-status edge, if there is one to bind to yet.
|
|
109
|
+
*
|
|
110
|
+
* Resolved off `this.client` rather than injected: the DB is already reachable there, it is simply
|
|
111
|
+
* attached after this manager is constructed (and initialized later still), so the binding has to be
|
|
112
|
+
* deferred rather than handed in. Doing it from the status handler is safe even when this manager's
|
|
113
|
+
* subscription runs before the sync manager's: `OfflineDBSyncManager` awaits
|
|
114
|
+
* `syncAndExecutePendingTasks()` before publishing, so the edge cannot land in the same synchronous
|
|
115
|
+
* notification that registers us.
|
|
116
|
+
*
|
|
117
|
+
* An offline DB whose `init()` never succeeded publishes no edge, so it must not be treated as the
|
|
118
|
+
* trigger — hence the `initialized` check, which also means a later successful init is picked up on
|
|
119
|
+
* the next reconnect.
|
|
120
|
+
*
|
|
121
|
+
* @returns whether the sync-status edge is now driving active-channel recovery.
|
|
122
|
+
*/
|
|
123
|
+
private ensureSyncStatusSubscription;
|
|
124
|
+
/**
|
|
125
|
+
* The channels this recovery reloads: the ones a consumer has declared it is currently reading.
|
|
126
|
+
*
|
|
127
|
+
* Deliberately NOT filtered on `watchStatus`. A channel opened while offline never reached
|
|
128
|
+
* `Watching` at all (its `watch()` threw, leaving `offlineMode`), so a `WasWatching` filter here
|
|
129
|
+
* would permanently strand the single most important offline case. `Channel.reload()`'s own
|
|
130
|
+
* `initialized || offlineMode` guard is the correct gate, and this is not a "restore a lost watch"
|
|
131
|
+
* decision anyway — it is "the channel being read must show the truth".
|
|
132
|
+
*/
|
|
133
|
+
private get recoverableActiveChannels();
|
|
134
|
+
/**
|
|
135
|
+
* The threads this recovery reloads: the ones a consumer has declared it is displaying.
|
|
136
|
+
*
|
|
137
|
+
* Threads need their own pass because nothing else brings an open thread's replies back — a
|
|
138
|
+
* channel reload refreshes the main message list, `ChannelManager.recover()` refreshes the channel
|
|
139
|
+
* lists*, and `ThreadManager`'s own recovery refreshes the thread *list*, reusing existing
|
|
140
|
+
* instances without rehydrating them unless they were separately marked stale.
|
|
141
|
+
*
|
|
142
|
+
* `active` is the filter that matters: the registry also holds every thread the list has paged
|
|
143
|
+
* in, which is not what should be re-fetched on a reconnect — only what someone is actually
|
|
144
|
+
* reading. An active thread is always registered, listed or not (`thread.activate()` registers it).
|
|
145
|
+
* Guarded on the owning channel the same way active channels are: a thread whose channel was
|
|
146
|
+
* disconnected (`disconnect()`) has nothing to recover into.
|
|
147
|
+
*/
|
|
148
|
+
private get recoverableActiveThreads();
|
|
149
|
+
/**
|
|
150
|
+
* An opened thread stays registered and subscribed after it closes, but a drop loses events it
|
|
151
|
+
* cannot replay. Flag only, no request: a listed one is rehydrated by the thread list's reload,
|
|
152
|
+
* and any other reloads on its next `activate()`.
|
|
153
|
+
*/
|
|
154
|
+
private markInactiveThreadsStale;
|
|
155
|
+
private recoverChannelLists;
|
|
156
|
+
private recoverActiveChannels;
|
|
157
|
+
/**
|
|
158
|
+
* Run a full recovery now, without waiting for a connection event.
|
|
159
|
+
*
|
|
160
|
+
* A no-op while one is already running: {@link recoverActiveChannels} defers the request to the
|
|
161
|
+
* end of the pass in flight rather than stacking a second one alongside it.
|
|
162
|
+
*/
|
|
163
|
+
recover: () => Promise<void>;
|
|
164
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { WithSubscriptions } from '../../utils/WithSubscriptions';
|
|
2
|
+
import { StateStore } from '@stream-io/state-store';
|
|
3
|
+
import type { NetworkConnectionObserverConfig, NetworkConnectionState, NetworkStatusReporter } from './types';
|
|
4
|
+
import type { StreamChat } from '../../client';
|
|
5
|
+
export declare const DEFAULT_NETWORK_CONNECTION_OBSERVER_CONFIG: NetworkConnectionObserverConfig;
|
|
6
|
+
/**
|
|
7
|
+
* The status of every connection this client depends on, as reactive state — one store per
|
|
8
|
+
* `ConnectionType` — this one owns the network half.
|
|
9
|
+
*
|
|
10
|
+
* They are two facts, not one, and they disagree in both directions. A socket dies on a working
|
|
11
|
+
* network (server close, expired token, health-check timeout); a device goes offline while the socket
|
|
12
|
+
* has not noticed yet and keeps looking healthy for up to its 35s connection-check window. Conflating them is
|
|
13
|
+
* what makes offline UI blame the network for a dead socket, so nothing here derives one from the
|
|
14
|
+
* other:
|
|
15
|
+
*
|
|
16
|
+
* - the `network*` fields are written **only** via {@link setStatus}, from the platform
|
|
17
|
+
* listener the integrator registers ({@link NetworkStatusReporter}). The SDK cannot detect
|
|
18
|
+
* device network status itself — every platform reports it differently — so it has to be told. A
|
|
19
|
+
* browser reports it properly and that reporter is installed automatically; everywhere else the
|
|
20
|
+
* stand-in mirrors the WebSocket until a real reporter is installed, and `isOnline` stays
|
|
21
|
+
* `undefined` — *unknown* rather than offline — until the socket has been up once.
|
|
22
|
+
* Combining the two is a presentation decision and deliberately absent: whether "network down and
|
|
23
|
+
* socket down" reads as *no network* or as *reconnecting* is copy taxonomy, and a boolean named for
|
|
24
|
+
* reachability invites gating requests on it, which network status must never do. Read both and branch.
|
|
25
|
+
*
|
|
26
|
+
* The store is the whole interface. There is no companion event: a store already says both what the
|
|
27
|
+
* status is and that it just changed, and publishing the same fact twice only created the chance for
|
|
28
|
+
* the two to disagree.
|
|
29
|
+
*/
|
|
30
|
+
export declare class NetworkConnectionObserver extends WithSubscriptions {
|
|
31
|
+
client: StreamChat;
|
|
32
|
+
state: StateStore<NetworkConnectionState>;
|
|
33
|
+
/** The shared configuration machinery — see {@link ConfigController}. */
|
|
34
|
+
private readonly configController;
|
|
35
|
+
/**
|
|
36
|
+
* Held separately from `addUnsubscribeFunction` because it has to be **replaced**, not just torn
|
|
37
|
+
* down: that set only clears on unregister, so it cannot express a swap.
|
|
38
|
+
*/
|
|
39
|
+
private unsubscribeStatusReporter?;
|
|
40
|
+
/**
|
|
41
|
+
* The reporter currently installed, so re-installing the same one is a no-op. Configuration is
|
|
42
|
+
* re-derived more than once — the client calls `initializeConfig` directly and again through the
|
|
43
|
+
* config store's immediate subscribe — and tearing a platform listener down only to recreate it
|
|
44
|
+
* identical is wasteful at best and, for something like `NetInfo`, a needless native round trip.
|
|
45
|
+
*/
|
|
46
|
+
private installedReporter?;
|
|
47
|
+
/**
|
|
48
|
+
* What {@link setStatusReporter} was last given — `null` to clear one, `undefined` when it has
|
|
49
|
+
* never been called.
|
|
50
|
+
*
|
|
51
|
+
* Held here rather than written into the resolved configuration, which a derivation rebuilds from
|
|
52
|
+
* the declarative tree: a reporter set imperatively has to survive `client.config.set` on any
|
|
53
|
+
* `client` key.
|
|
54
|
+
*/
|
|
55
|
+
private imperativeReporter?;
|
|
56
|
+
/**
|
|
57
|
+
* The declared reporter the last installation saw, so a *change* to it can be told apart from a
|
|
58
|
+
* re-derivation that named the same thing. Whichever source spoke last wins, and this is what makes
|
|
59
|
+
* that comparison possible.
|
|
60
|
+
*/
|
|
61
|
+
private declaredReporter?;
|
|
62
|
+
/**
|
|
63
|
+
* Whether configuration has been derived at least once. {@link registerSubscriptions} runs while
|
|
64
|
+
* the client is still being constructed, before its event machinery exists, so installing a
|
|
65
|
+
* reporter there on the first call could report a status into a half-built client.
|
|
66
|
+
*/
|
|
67
|
+
private configInitialized;
|
|
68
|
+
/**
|
|
69
|
+
* The host default, built once. {@link getDefaultNetworkStatusReporter} returns a fresh closure for
|
|
70
|
+
* the socket-derived variant, and a fresh identity every derivation would defeat the
|
|
71
|
+
* re-installation guard below — tearing a platform listener down only to recreate it identical.
|
|
72
|
+
*/
|
|
73
|
+
private defaultReporter?;
|
|
74
|
+
constructor({ client }: {
|
|
75
|
+
client: StreamChat;
|
|
76
|
+
});
|
|
77
|
+
/**
|
|
78
|
+
* Resolved configuration, as a store — the shape every configurable class exposes
|
|
79
|
+
* (`configState` / `config` / `updateConfig`).
|
|
80
|
+
*/
|
|
81
|
+
get configState(): StateStore<NetworkConnectionObserverConfig>;
|
|
82
|
+
/** The current resolved configuration. `Readonly` — change it through {@link updateConfig}. */
|
|
83
|
+
get config(): Readonly<NetworkConnectionObserverConfig>;
|
|
84
|
+
/**
|
|
85
|
+
* Merges a partial configuration into the resolved config and notifies subscribers.
|
|
86
|
+
*
|
|
87
|
+
* Installs whatever reporter the result names, so this is a real alternative to
|
|
88
|
+
* {@link setStatusReporter} rather than a write nobody acts on. Without it the field was
|
|
89
|
+
* stored and never invoked, leaving `isOnline` `undefined` forever with no error anywhere.
|
|
90
|
+
*/
|
|
91
|
+
updateConfig(config: Partial<NetworkConnectionObserverConfig>): void;
|
|
92
|
+
/**
|
|
93
|
+
* Rebuilds the resolved configuration from package defaults plus the declarative slice — the
|
|
94
|
+
* derivation entry point every configurable entity exposes, so the client routes a slice here and
|
|
95
|
+
* knows nothing about this class's defaults or merge semantics.
|
|
96
|
+
*
|
|
97
|
+
* Installs whatever reporter the resolved config names, falling back to the platform default. This
|
|
98
|
+
* is what makes `client.config.set({ client: { connections: { statusReporter } } })` work.
|
|
99
|
+
*/
|
|
100
|
+
initializeConfig(config?: Partial<NetworkConnectionObserverConfig>): void;
|
|
101
|
+
/** The reporter for this host, resolved once. See {@link defaultReporter}. */
|
|
102
|
+
private get hostDefaultReporter();
|
|
103
|
+
/**
|
|
104
|
+
* Installs the reporter the resolved configuration names, falling back to the host default.
|
|
105
|
+
*
|
|
106
|
+
* The single place installation happens, so every path that can change what should be installed —
|
|
107
|
+
* a derivation, a patch — goes through the same resolution instead of each reimplementing it.
|
|
108
|
+
*/
|
|
109
|
+
private installConfiguredReporter;
|
|
110
|
+
/**
|
|
111
|
+
* Device network status. `undefined` means unknown — see {@link NetworkConnectionState.isOnline}.
|
|
112
|
+
* Guards must test `=== false`, never `!isOnline`.
|
|
113
|
+
*/
|
|
114
|
+
get isOnline(): boolean | undefined;
|
|
115
|
+
/**
|
|
116
|
+
* Whether the installed reporter is the host default that mirrors this client's WebSocket
|
|
117
|
+
* (`createWSConnectionNetworkStatusReporter`). Under it `isOnline === false` means "the socket is
|
|
118
|
+
* down", not "the device is offline".
|
|
119
|
+
*
|
|
120
|
+
* @internal
|
|
121
|
+
*/
|
|
122
|
+
get usesDefaultWSConnectionNetworkStatusReporter(): boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Installs the platform listener that reports device network status, unsubscribing whatever was
|
|
125
|
+
* there. Pass `null` to clear it, which leaves the last known status alone rather than reverting to
|
|
126
|
+
* unknown — an edge is not a state, and forgetting what we were last told would be a regression.
|
|
127
|
+
*
|
|
128
|
+
* Available as a setter, not just a constructor option, because React Native registers its native
|
|
129
|
+
* handlers *after* the client exists.
|
|
130
|
+
*
|
|
131
|
+
* What is set here survives every later configuration derivation, and is only superseded by the
|
|
132
|
+
* declarative tree naming a *different* reporter — `client.config.set({ client: { networkConnection:
|
|
133
|
+
* { statusReporter } } })`.
|
|
134
|
+
*/
|
|
135
|
+
setStatusReporter(reporter: NetworkStatusReporter | null | undefined): void;
|
|
136
|
+
/** Installs one reporter, unsubscribing whatever was there. */
|
|
137
|
+
private installReporter;
|
|
138
|
+
/**
|
|
139
|
+
* Reports device network status.
|
|
140
|
+
*
|
|
141
|
+
* The reporter calls this; so may a host with no listener API to register at all, and it is the
|
|
142
|
+
* supported replacement for reaching into `client.wsConnection.onlineStatusChanged` with a
|
|
143
|
+
* synthesized DOM event.
|
|
144
|
+
*/
|
|
145
|
+
setStatus(isOnline: boolean): void;
|
|
146
|
+
/**
|
|
147
|
+
* Nothing to subscribe to beyond the reporter, which {@link initializeConfig} installs — this
|
|
148
|
+
* class has no client events to listen to, by design: deriving either status from an event would
|
|
149
|
+
* be the conflation it exists to prevent. Present because `WithSubscriptions` requires it, and so
|
|
150
|
+
* that teardown releases the platform listener.
|
|
151
|
+
*/
|
|
152
|
+
registerSubscriptions: () => () => symbol;
|
|
153
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { NetworkStatusReporter } from './types';
|
|
2
|
+
import type { WSConnection } from '../wsConnection/WSConnection';
|
|
3
|
+
/**
|
|
4
|
+
* The built-in reporter for browsers: `navigator.onLine` plus one `online`/`offline` listener pair.
|
|
5
|
+
* Installed automatically where {@link getDefaultNetworkStatusReporter} can pick it.
|
|
6
|
+
*
|
|
7
|
+
* Feature-detects rather than trusting its own name. It is exported, so it can be handed to
|
|
8
|
+
* `client.config.set` on any host — a Node process, a server-side render of code written for the
|
|
9
|
+
* browser — and dereferencing `window` there threw, where a reporter is meant to fail quietly.
|
|
10
|
+
*/
|
|
11
|
+
export declare const browserNetworkStatusReporter: NetworkStatusReporter;
|
|
12
|
+
/**
|
|
13
|
+
* The stand-in reporter for hosts that cannot report their own network status.
|
|
14
|
+
*
|
|
15
|
+
* It mirrors this client's WebSocket, which is **not** a measurement of the device's network: the two
|
|
16
|
+
* are different facts and routinely disagree. It exists because the alternative is worse. React
|
|
17
|
+
* Native has no built-in way to answer the question, so an integrator who forgets to install
|
|
18
|
+
* `@react-native-community/netinfo` would otherwise leave `isOnline` unknown forever, and every
|
|
19
|
+
* consumer branching on it dead. Mirroring the socket is wrong in the cases where the two disagree,
|
|
20
|
+
* and right in the common case where the device really did lose its network and took the socket with
|
|
21
|
+
* it.
|
|
22
|
+
*
|
|
23
|
+
* Nothing is reported until the socket has been up once. `isHealthy` on the WebSocket store is
|
|
24
|
+
* `false` from construction, and forwarding that would claim the device is offline before
|
|
25
|
+
* anything had been attempted — a fabricated reading, which is the one thing this module refuses
|
|
26
|
+
* to produce. Until then the device's status stays `undefined`, meaning unknown.
|
|
27
|
+
*
|
|
28
|
+
* Install a real reporter wherever one exists. This one can only ever repeat what the socket already
|
|
29
|
+
* said, so it cannot tell you that the network came back before the socket noticed, which is the
|
|
30
|
+
* whole reason the network signal is worth having.
|
|
31
|
+
*/
|
|
32
|
+
export declare const createWSConnectionNetworkStatusReporter: (wsConnection: WSConnection) => NetworkStatusReporter;
|
|
33
|
+
/**
|
|
34
|
+
* The reporter to install when the integrator supplied none.
|
|
35
|
+
*
|
|
36
|
+
* {@link browserNetworkStatusReporter} in a browser, where the platform answers the question
|
|
37
|
+
* properly, and {@link createWSConnectionNetworkStatusReporter} everywhere else.
|
|
38
|
+
*
|
|
39
|
+
* Always returns something, so an integration missing a line of setup gets a coarse signal rather
|
|
40
|
+
* than none at all, which would fail silently. Nothing is fabricated: the stand-in reports nothing
|
|
41
|
+
* until the socket has been up once, so a client that has never connected still answers `undefined`.
|
|
42
|
+
*/
|
|
43
|
+
export declare const getDefaultNetworkStatusReporter: (wsConnection: WSConnection) => NetworkStatusReporter;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Unsubscribe } from '@stream-io/state-store';
|
|
2
|
+
/**
|
|
3
|
+
* Registers a platform-specific network status listener.
|
|
4
|
+
*
|
|
5
|
+
* The SDK cannot detect the device's network status itself — the browser has
|
|
6
|
+
* `window.addEventListener('online' | 'offline')` plus `navigator.onLine`, React Native has
|
|
7
|
+
* `@react-native-community/netinfo`, and other hosts have something else or nothing at all. So it has
|
|
8
|
+
* to be told, and this one function is the whole contract.
|
|
9
|
+
*
|
|
10
|
+
* The reporter **MUST** call `onStatusChange` with the current status as soon as it is known —
|
|
11
|
+
* synchronously where the platform allows it (`navigator.onLine`), after an await where it does not
|
|
12
|
+
* (`NetInfo.fetch()`) — and again on every subsequent change. Making initial emission the reporter's
|
|
13
|
+
* job is what lets one function cover both shapes, and it matches this package's convention:
|
|
14
|
+
* `StateStore.subscribe` also fires once with the current value before returning.
|
|
15
|
+
*
|
|
16
|
+
* A reporter that never emits leaves {@link NetworkConnectionState.isOnline} as `undefined`, which is
|
|
17
|
+
* a safe state rather than a broken one — see that field.
|
|
18
|
+
*
|
|
19
|
+
* @returns A function that removes the listener.
|
|
20
|
+
*
|
|
21
|
+
* @example React Native
|
|
22
|
+
* ```ts
|
|
23
|
+
* const netInfoReporter: NetworkStatusReporter = (onStatusChange) => {
|
|
24
|
+
* NetInfo.fetch().then((s) => onStatusChange(!!s.isConnected));
|
|
25
|
+
* return NetInfo.addEventListener((s) => onStatusChange(!!s.isConnected));
|
|
26
|
+
* };
|
|
27
|
+
*
|
|
28
|
+
* client.networkConnection.setStatusReporter(netInfoReporter);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export type NetworkStatusReporter = (onStatusChange: (isOnline: boolean) => void) => Unsubscribe;
|
|
32
|
+
export type NetworkConnectionObserverConfig = {
|
|
33
|
+
/**
|
|
34
|
+
* The platform listener that feeds `client.networkConnection`. `undefined` on any host where
|
|
35
|
+
* the SDK cannot pick a default, which is everywhere except the browser.
|
|
36
|
+
*/
|
|
37
|
+
statusReporter: NetworkStatusReporter | undefined;
|
|
38
|
+
};
|
|
39
|
+
export type NetworkConnectionState = {
|
|
40
|
+
/**
|
|
41
|
+
* Device network status, as reported by the registered listener.
|
|
42
|
+
*
|
|
43
|
+
* `undefined` means **unknown**: no reporter is installed, or it has not reported yet. It does not
|
|
44
|
+
* mean offline, and guards must therefore test `isOnline === false` rather than `!isOnline` —
|
|
45
|
+
* treating unknown as offline silently disables behaviour on every host without a built-in
|
|
46
|
+
* reporter, React Native among them.
|
|
47
|
+
*
|
|
48
|
+
* Never derived from the WebSocket. That is a separate fact with separate causes: a socket dies on
|
|
49
|
+
* a working network (server close, expired token, health-check timeout), and a device goes offline
|
|
50
|
+
* while the socket has not noticed yet.
|
|
51
|
+
*/
|
|
52
|
+
isOnline: boolean | undefined;
|
|
53
|
+
lastOnlineAt: Date | null;
|
|
54
|
+
lastOfflineAt: Date | null;
|
|
55
|
+
};
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import type { ConnectAPIResponse, ConnectionOpen } from '../../types';
|
|
2
|
+
import type { WSConnection } from './WSConnection';
|
|
3
|
+
import type { StreamChat } from '../../client';
|
|
4
|
+
/**
|
|
5
|
+
* A WS connection that reconnects upon failure.
|
|
6
|
+
*
|
|
7
|
+
* - the WS connection can break and fail; a ping goes out every 25s (`pingInterval`)
|
|
8
|
+
* - sometimes the WS connection seems to work while the device is in fact offline
|
|
9
|
+
* - the device's own network status is a separate fact, owned by `client.networkConnection` and fed by a
|
|
10
|
+
* platform listener the integrator registers; this class consumes it, it does not detect it
|
|
11
|
+
*
|
|
12
|
+
* There are 4 ways in which this connection can go down:
|
|
13
|
+
* - WebSocket.onerror is called
|
|
14
|
+
* - WebSocket.onclose is called
|
|
15
|
+
* - no frame arrives for 35s, so the health-check loop gives up (`connectionCheckTimeout`)
|
|
16
|
+
* - the device reports its network went offline
|
|
17
|
+
*
|
|
18
|
+
* There are 2 assumptions we make about the server:
|
|
19
|
+
* - state can be recovered by querying the channel again
|
|
20
|
+
* - if the servers fails to publish a message to the client, the WS connection is destroyed
|
|
21
|
+
*/
|
|
22
|
+
export declare class StableWSConnection {
|
|
23
|
+
/**
|
|
24
|
+
* The `WSConnection` this socket belongs to. Held instead of the client, because the status store
|
|
25
|
+
* lives here and everything the socket needs from the client is reachable through it.
|
|
26
|
+
*/
|
|
27
|
+
wsConnection: WSConnection;
|
|
28
|
+
connectionOpen?: ConnectAPIResponse;
|
|
29
|
+
consecutiveFailures: number;
|
|
30
|
+
healthCheckTimeoutRef?: NodeJS.Timeout;
|
|
31
|
+
isConnecting: boolean;
|
|
32
|
+
isDisconnected: boolean;
|
|
33
|
+
isHealthy: boolean;
|
|
34
|
+
isResolved?: boolean;
|
|
35
|
+
lastEvent: Date | null;
|
|
36
|
+
connectionCheckTimeoutRef?: NodeJS.Timeout;
|
|
37
|
+
rejectPromise?: (reason?: Error & {
|
|
38
|
+
code?: string | number;
|
|
39
|
+
isWSFailure?: boolean;
|
|
40
|
+
StatusCode?: string | number;
|
|
41
|
+
}) => void;
|
|
42
|
+
requestID: string | undefined;
|
|
43
|
+
resolvePromise?: (value: ConnectionOpen) => void;
|
|
44
|
+
totalFailures: number;
|
|
45
|
+
ws?: WebSocket;
|
|
46
|
+
wsID: number;
|
|
47
|
+
constructor({ wsConnection }: {
|
|
48
|
+
wsConnection: WSConnection;
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* The timing knobs, read from the parent **live** rather than snapshotted, so an `updateConfig`
|
|
52
|
+
* reaches the socket that is already open instead of only the next one.
|
|
53
|
+
*/
|
|
54
|
+
private get config();
|
|
55
|
+
/** How often a health-check ping goes out. Configurable as `pingIntervalMs`. */
|
|
56
|
+
get pingInterval(): number;
|
|
57
|
+
/**
|
|
58
|
+
* How long the connection check tolerates silence before tearing the socket down.
|
|
59
|
+
*
|
|
60
|
+
* Derived at read time rather than stored, so changing the ping interval moves the connection check
|
|
61
|
+
* with it instead of leaving the socket to kill itself between pings.
|
|
62
|
+
*/
|
|
63
|
+
get connectionCheckTimeout(): number;
|
|
64
|
+
/** The client, reached through the parent. See {@link wsConnection}. */
|
|
65
|
+
get client(): StreamChat;
|
|
66
|
+
/**
|
|
67
|
+
* Re-parents this socket, for one supplied through `config.connection` and built against a
|
|
68
|
+
* different `WSConnection` than the client adopting it.
|
|
69
|
+
*/
|
|
70
|
+
setWSConnection(wsConnection: WSConnection): void;
|
|
71
|
+
/**
|
|
72
|
+
* Connects to the WS URL.
|
|
73
|
+
*
|
|
74
|
+
* @param timeout - Connect timeout in milliseconds. Defaults to `config.connectTimeoutMs` (15s),
|
|
75
|
+
* which allows between 2 and 3 tries.
|
|
76
|
+
* @returns A promise that resolves once the server's `connection.ok` hello event arrives.
|
|
77
|
+
*/
|
|
78
|
+
connect(timeout?: number): Promise<void | ConnectionOpen>;
|
|
79
|
+
/**
|
|
80
|
+
* _waitForHealthy polls the promise connection to see if its resolved until it times out.
|
|
81
|
+
*
|
|
82
|
+
* @param timeout - duration (ms). Defaults to `config.connectTimeoutMs` (15s), which allows
|
|
83
|
+
* between 2~3 tries.
|
|
84
|
+
*/
|
|
85
|
+
_waitForHealthy(timeout?: number): Promise<void | ConnectionOpen>;
|
|
86
|
+
/**
|
|
87
|
+
* Builds and returns the URL for the WebSocket connection.
|
|
88
|
+
*
|
|
89
|
+
* @private
|
|
90
|
+
* @returns url string
|
|
91
|
+
*/
|
|
92
|
+
_buildUrl: () => string;
|
|
93
|
+
/**
|
|
94
|
+
* Disconnects the connection without attempting to recover.
|
|
95
|
+
*
|
|
96
|
+
* @param timeout - Optional timeout in milliseconds to wait for the close frame from the server.
|
|
97
|
+
*/
|
|
98
|
+
disconnect(timeout?: number): Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* Connects to the WS endpoint.
|
|
101
|
+
*
|
|
102
|
+
* @returns A promise that resolves once the server's `connection.ok` hello event arrives.
|
|
103
|
+
*/
|
|
104
|
+
_connect(): Promise<ConnectionOpen | undefined>;
|
|
105
|
+
/**
|
|
106
|
+
* Retries the connection to the WS endpoint.
|
|
107
|
+
*
|
|
108
|
+
* @param options - Reconnect options.
|
|
109
|
+
* @param options.interval - Number of milliseconds to wait before reconnecting.
|
|
110
|
+
* @param options.refreshToken - Reload/refresh the user token before attempting to reconnect.
|
|
111
|
+
*/
|
|
112
|
+
_reconnect(options?: {
|
|
113
|
+
interval?: number;
|
|
114
|
+
refreshToken?: boolean;
|
|
115
|
+
}): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Handles a change reported for the `'network'` connection — the device's own — and applies it to
|
|
118
|
+
* the `'ws'` connection this class owns.
|
|
119
|
+
*
|
|
120
|
+
* The logs name both by their {@link ConnectionType} value rather than by prose, so a reader is
|
|
121
|
+
* never left guessing which connection a line is about. The effect clause is conditional because there may not be an effect: `_setHealth` returns
|
|
122
|
+
* early when the status is unchanged, and a `'network'` report commonly arrives after the socket has
|
|
123
|
+
* already died on its own.
|
|
124
|
+
*
|
|
125
|
+
* @deprecated Report network status through `client.networkConnection.setStatus(isOnline)` instead, or
|
|
126
|
+
* register a listener with `client.networkConnection.setStatusReporter(…)`. This entry point takes
|
|
127
|
+
* a DOM-shaped object and reaches into the socket, neither of which a caller should need. Kept
|
|
128
|
+
* for one major because `stream-chat-react-native` calls it directly.
|
|
129
|
+
*
|
|
130
|
+
* @param event - A DOM-shaped object whose `type` is `'online'` or `'offline'`. It carries no
|
|
131
|
+
* connection identifier and never has: it was fed by `window`'s own `online`/`offline` events,
|
|
132
|
+
* which are always about the device's network, and React Native synthesizes the same shape from
|
|
133
|
+
* `NetInfo`.
|
|
134
|
+
*/
|
|
135
|
+
onlineStatusChanged: (event: Event) => void;
|
|
136
|
+
/**
|
|
137
|
+
* Applies a reported change in the `'network'` connection to the `'ws'` connection this class owns.
|
|
138
|
+
*
|
|
139
|
+
* The single body behind both entry points — the network-store subscription the parent owns, and
|
|
140
|
+
* the deprecated {@link onlineStatusChanged} shim React Native still calls — so the two cannot
|
|
141
|
+
* drift.
|
|
142
|
+
*
|
|
143
|
+
* @internal
|
|
144
|
+
*/
|
|
145
|
+
_applyNetworkStatus(online: boolean): void;
|
|
146
|
+
onopen: (wsId: number, authMessage: string) => void;
|
|
147
|
+
onmessage: (wsId: number, event: MessageEvent) => void;
|
|
148
|
+
onclose: (wsId: number, event: CloseEvent) => void;
|
|
149
|
+
onerror: (wsId: number, event: Event) => void;
|
|
150
|
+
/**
|
|
151
|
+
* Writes this connection's status to the field and to `client.wsConnection.state`, and drops the
|
|
152
|
+
* connection id when it goes down. Returns whether the status changed.
|
|
153
|
+
*
|
|
154
|
+
* Every path that transitions the status goes through here — including `disconnect()`, which
|
|
155
|
+
* `closeConnection()` uses, and the two error paths. Covering all of them is why the store exists,
|
|
156
|
+
* and why invalidating the id belongs here rather than beside any one of those paths.
|
|
157
|
+
*
|
|
158
|
+
* Construction is not routed through here: initializing the field is not a transition, and stamping
|
|
159
|
+
* `lastUnhealthyAt` because a socket object was built would be a lie.
|
|
160
|
+
*/
|
|
161
|
+
private _applyHealth;
|
|
162
|
+
/**
|
|
163
|
+
* Applies a status change and does the bookkeeping that goes with it.
|
|
164
|
+
*
|
|
165
|
+
* Announcing it is not this class's job. The status lives in `client.wsConnection.state`, which
|
|
166
|
+
* {@link _applyHealth} writes on every transition — including the ones that were always silent —
|
|
167
|
+
* so a consumer subscribes there rather than waiting to be told.
|
|
168
|
+
*
|
|
169
|
+
* The five-second wait before announcing a drop moved to whoever renders the banner, its length
|
|
170
|
+
* configurable as `offlineNotificationDisplayDelayMs`. The timer here outlived the socket that
|
|
171
|
+
* armed it, so a replaced connection announced a drop its replacement had already recovered from.
|
|
172
|
+
*/
|
|
173
|
+
_setHealth: (healthy: boolean) => void;
|
|
174
|
+
/**
|
|
175
|
+
* Creates an error object for the WS event.
|
|
176
|
+
*
|
|
177
|
+
* @param event - The raw WebSocket close / data / error event.
|
|
178
|
+
* @param isWSFailure - Whether the underlying cause is a WebSocket failure (optional, defaults to `true`).
|
|
179
|
+
* @returns A normalized error describing the WS failure.
|
|
180
|
+
*/
|
|
181
|
+
_errorFromWSEvent: (event: CloseEvent | MessageEvent | ErrorEvent | Event, isWSFailure?: boolean) => Error & {
|
|
182
|
+
code?: string | number;
|
|
183
|
+
isWSFailure?: boolean;
|
|
184
|
+
StatusCode?: string | number;
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Removes the current WS connection.
|
|
188
|
+
*/
|
|
189
|
+
_destroyCurrentWSConnection(): void;
|
|
190
|
+
/**
|
|
191
|
+
* Sets up the `this.connectionOpen` promise.
|
|
192
|
+
*/
|
|
193
|
+
_setupConnectionPromise: () => void;
|
|
194
|
+
/**
|
|
195
|
+
* Schedules the next health check ping for the WebSocket connection.
|
|
196
|
+
*/
|
|
197
|
+
scheduleNextPing: () => void;
|
|
198
|
+
/**
|
|
199
|
+
* Schedules a check for the time difference between the last received event and now. If the
|
|
200
|
+
* difference is more than 35 seconds, it means our health check logic has failed and the
|
|
201
|
+
* WebSocket needs to be reconnected.
|
|
202
|
+
*/
|
|
203
|
+
scheduleConnectionCheck: () => void;
|
|
204
|
+
}
|