stream-chat 10.0.0-rc.9 → 10.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -6
- package/dist/cjs/index.browser.js +21653 -19788
- package/dist/cjs/index.browser.js.map +4 -4
- package/dist/cjs/index.node.js +21693 -19796
- package/dist/cjs/index.node.js.map +4 -4
- package/dist/esm/index.mjs +21651 -19786
- package/dist/esm/index.mjs.map +4 -4
- package/dist/types/ChannelManager.d.ts +216 -135
- package/dist/types/CooldownTimer.d.ts +13 -27
- package/dist/types/EventHandlerPipeline.d.ts +18 -38
- package/dist/types/LiveLocationManager.d.ts +24 -52
- package/dist/types/api-client.d.ts +17 -5
- package/dist/types/channel.d.ts +432 -470
- package/dist/types/channel_state.d.ts +68 -112
- package/dist/types/client.d.ts +321 -435
- package/dist/types/client_state.d.ts +1 -3
- package/dist/types/configuration/ConfigController.d.ts +31 -125
- package/dist/types/configuration/InstanceConfigurationRegistry.d.ts +32 -124
- package/dist/types/configuration/keys.d.ts +5 -19
- package/dist/types/configuration/shape.d.ts +12 -29
- package/dist/types/configuration/types.d.ts +64 -79
- package/dist/types/configuration/utils/applyInstanceConfiguration.d.ts +14 -36
- package/dist/types/configuration/utils/copyConfigPatch.d.ts +8 -0
- package/dist/types/configuration/utils/declarativeSlices.d.ts +5 -16
- package/dist/types/configuration/utils/deepFreezeConfig.d.ts +3 -17
- package/dist/types/configuration/utils/serverAuthority.d.ts +9 -29
- package/dist/types/connection/ConnectionIdManager.d.ts +45 -0
- package/dist/types/connection/ConnectionRecoveryManager.d.ts +84 -0
- package/dist/types/connection/index.d.ts +4 -0
- package/dist/types/connection/networkConnection/NetworkConnectionObserver.d.ts +82 -0
- package/dist/types/connection/networkConnection/index.d.ts +3 -0
- package/dist/types/connection/networkConnection/reporters.d.ts +20 -0
- package/dist/types/connection/networkConnection/types.d.ts +40 -0
- package/dist/types/connection/wsConnection/StableWSConnection.d.ts +137 -0
- package/dist/types/connection/wsConnection/WSConnection.d.ts +105 -0
- package/dist/types/connection/wsConnection/WSConnectionFallback.d.ts +47 -0
- package/dist/types/connection/wsConnection/config.d.ts +31 -0
- package/dist/types/connection/wsConnection/index.d.ts +4 -0
- package/dist/types/connection/wsConnection/types.d.ts +46 -0
- package/dist/types/constants.d.ts +2 -0
- package/dist/types/custom_types.d.ts +0 -2
- package/dist/types/entityStore/EntityStore.d.ts +226 -65
- package/dist/types/entityStore/StoreBackedItemIndex.d.ts +19 -33
- package/dist/types/entityStore/index.d.ts +0 -1
- package/dist/types/errors.d.ts +16 -19
- package/dist/types/formatMessageMemo.d.ts +5 -0
- package/dist/types/gen/chat/ChannelApi.d.ts +5 -1
- package/dist/types/gen/chat/ChatApi.d.ts +149 -105
- package/dist/types/gen/models/index.d.ts +2528 -690
- package/dist/types/gen/models/timestamp-guard.d.ts +22 -0
- package/dist/types/gen/moderation/ModerationApi.d.ts +33 -14
- package/dist/types/index.d.ts +10 -6
- package/dist/types/{i18n/languageNames.d.ts → languageNames.d.ts} +9 -24
- package/dist/types/logger.d.ts +1 -3
- package/dist/types/messageComposer/CustomDataManager.d.ts +7 -1
- package/dist/types/messageComposer/LocationComposer.d.ts +7 -1
- package/dist/types/messageComposer/attachmentIdentity.d.ts +24 -0
- package/dist/types/messageComposer/attachmentManager.d.ts +52 -24
- package/dist/types/messageComposer/attachmentUtils.d.ts +23 -0
- package/dist/types/messageComposer/configuration/configuration.d.ts +2 -5
- package/dist/types/messageComposer/configuration/types.d.ts +38 -50
- package/dist/types/messageComposer/index.d.ts +1 -0
- package/dist/types/messageComposer/linkPreviewsManager.d.ts +12 -9
- package/dist/types/messageComposer/messageComposer.d.ts +60 -128
- package/dist/types/messageComposer/middleware/attachmentManager/postUpload/attachmentEnrichment.d.ts +10 -1
- package/dist/types/messageComposer/middleware/messageComposer/attachments.d.ts +4 -0
- package/dist/types/messageComposer/middleware/messageComposer/commandInjection.d.ts +5 -0
- package/dist/types/messageComposer/middleware/messageComposer/index.d.ts +2 -0
- package/dist/types/messageComposer/middleware/messageComposer/pollOnly.d.ts +6 -0
- package/dist/types/messageComposer/middleware/messageComposer/userDataInjection.d.ts +5 -0
- package/dist/types/messageComposer/middleware/pollComposer/types.d.ts +2 -4
- package/dist/types/messageComposer/middleware/pollComposer/validation.d.ts +7 -19
- package/dist/types/messageComposer/middleware/textComposer/commandUtils.d.ts +4 -0
- package/dist/types/messageComposer/middleware/textComposer/commands.d.ts +11 -2
- package/dist/types/messageComposer/middleware/textComposer/mentions.d.ts +19 -18
- package/dist/types/messageComposer/middleware/textComposer/textMiddlewareUtils.d.ts +9 -3
- package/dist/types/messageComposer/middleware/textComposer/types.d.ts +1 -6
- package/dist/types/messageComposer/pollComposer.d.ts +15 -5
- package/dist/types/messageComposer/textComposer.d.ts +14 -7
- package/dist/types/messageComposer/types.d.ts +27 -7
- package/dist/types/messageDelivery/MessageDeliveryReporter.d.ts +27 -80
- package/dist/types/messageDelivery/MessageReceiptsTracker.d.ts +40 -73
- package/dist/types/messageOperations/FailedSendCache.d.ts +29 -0
- package/dist/types/messageOperations/MessageOperationStatePolicy.d.ts +16 -22
- package/dist/types/messageOperations/MessageOperations.d.ts +63 -34
- package/dist/types/messageOperations/applyReactionLocally.d.ts +15 -0
- package/dist/types/messageOperations/createMessageOperations.d.ts +9 -0
- package/dist/types/messageOperations/index.d.ts +6 -4
- package/dist/types/messageOperations/reflectReactionEvent.d.ts +16 -0
- package/dist/types/messageOperations/sendOrdering.d.ts +14 -0
- package/dist/types/messageOperations/settlePendingAttachmentUploads.d.ts +20 -0
- package/dist/types/messageOperations/types.d.ts +43 -14
- package/dist/types/middleware.d.ts +10 -0
- package/dist/types/moderation.d.ts +21 -14
- package/dist/types/notifications/NotificationManager.d.ts +10 -23
- package/dist/types/notifications/types.d.ts +21 -57
- package/dist/types/offline-support/offline_support_api.d.ts +150 -528
- package/dist/types/offline-support/offline_sync_manager.d.ts +21 -42
- package/dist/types/offline-support/queueableOperations.d.ts +35 -37
- package/dist/types/offline-support/types.d.ts +40 -186
- package/dist/types/pagination/FilterBuilder.d.ts +1 -18
- package/dist/types/pagination/ItemIndex.d.ts +4 -12
- package/dist/types/pagination/cursorDerivation/aroundPaginationFlags.d.ts +13 -0
- package/dist/types/pagination/cursorDerivation/createdAtAroundPaginationFlags.d.ts +4 -5
- package/dist/types/pagination/cursorDerivation/linearPaginationFlags.d.ts +19 -5
- package/dist/types/pagination/index.d.ts +1 -0
- package/dist/types/pagination/paginators/BasePaginator.d.ts +604 -550
- package/dist/types/pagination/paginators/ChannelPaginator.d.ts +123 -106
- package/dist/types/pagination/paginators/MessageIntervalPaginator.d.ts +576 -334
- package/dist/types/pagination/paginators/MessagePaginator.d.ts +239 -131
- package/dist/types/pagination/paginators/PinnedMessagePaginator.d.ts +15 -26
- package/dist/types/pagination/paginators/ReminderPaginator.d.ts +4 -3
- package/dist/types/pagination/paginators/ThreadPaginator.d.ts +44 -0
- package/dist/types/pagination/paginators/UserGroupPaginator.d.ts +5 -8
- package/dist/types/pagination/paginators/index.d.ts +1 -0
- package/dist/types/pagination/sortCompiler.d.ts +11 -22
- package/dist/types/pagination/utility.normalization.d.ts +5 -23
- package/dist/types/pagination/utility.search.d.ts +3 -8
- package/dist/types/poll.d.ts +34 -8
- package/dist/types/poll_manager.d.ts +12 -2
- package/dist/types/poll_optimistic_votes.d.ts +57 -0
- package/dist/types/reminders/Reminder.d.ts +11 -11
- package/dist/types/reminders/ReminderManager.d.ts +13 -25
- package/dist/types/search/BaseSearchSource.d.ts +10 -8
- package/dist/types/search/ChannelSearchSource.d.ts +16 -0
- package/dist/types/search/MessageSearchSource.d.ts +17 -0
- package/dist/types/search/SearchController.d.ts +26 -28
- package/dist/types/search/types.d.ts +6 -10
- package/dist/types/signing.d.ts +12 -3
- package/dist/types/thread.d.ts +63 -129
- package/dist/types/thread_manager.d.ts +95 -66
- package/dist/types/token_manager.d.ts +8 -13
- package/dist/types/types.d.ts +81 -199
- package/dist/types/types.utility.d.ts +3 -3
- package/dist/types/upload-utils.d.ts +5 -10
- package/dist/types/uploadManager.d.ts +31 -11
- package/dist/types/utils/FixedSizeQueueCache.d.ts +8 -11
- package/dist/types/utils/WithSubscriptions.d.ts +6 -16
- package/dist/types/utils/abort.d.ts +17 -0
- package/dist/types/utils/concurrency.d.ts +12 -20
- package/dist/types/utils/mergeWith/mergeWith.d.ts +8 -11
- package/dist/types/utils/mergeWith/mergeWithCore.d.ts +5 -13
- package/dist/types/utils/mergeWith/mergeWithDiff.d.ts +22 -0
- package/dist/types/utils/objectPath.d.ts +6 -41
- package/dist/types/utils/retryable.d.ts +8 -12
- package/dist/types/utils/throttling/throttle.d.ts +8 -23
- package/dist/types/utils/time.d.ts +33 -57
- package/dist/types/utils.d.ts +72 -91
- package/package.json +14 -40
- package/src/ChannelManager.ts +494 -303
- package/src/CooldownTimer.ts +28 -38
- package/src/EventHandlerPipeline.ts +22 -45
- package/src/LiveLocationManager.ts +37 -76
- package/src/api-client.ts +113 -50
- package/src/campaign.ts +1 -1
- package/src/channel.ts +1067 -1276
- package/src/channel_batch_updater.ts +1 -1
- package/src/channel_state.ts +73 -168
- package/src/client.ts +744 -974
- package/src/client_state.ts +3 -8
- package/src/configuration/ConfigController.ts +42 -158
- package/src/configuration/InstanceConfigurationRegistry.ts +43 -172
- package/src/configuration/index.ts +1 -1
- package/src/configuration/keys.ts +13 -32
- package/src/configuration/shape.ts +169 -59
- package/src/configuration/types.ts +69 -92
- package/src/configuration/utils/applyInstanceConfiguration.ts +22 -54
- package/src/configuration/utils/copyConfigPatch.ts +11 -32
- package/src/configuration/utils/declarativeSlices.ts +9 -39
- package/src/configuration/utils/deepFreezeConfig.ts +3 -17
- package/src/configuration/utils/index.ts +2 -4
- package/src/configuration/utils/serverAuthority.ts +20 -68
- package/src/connection/ConnectionIdManager.ts +111 -0
- package/src/connection/ConnectionRecoveryManager.ts +257 -0
- package/src/connection/index.ts +4 -0
- package/src/connection/networkConnection/NetworkConnectionObserver.ts +204 -0
- package/src/connection/networkConnection/index.ts +3 -0
- package/src/connection/networkConnection/reporters.ts +58 -0
- package/src/connection/networkConnection/types.ts +45 -0
- package/src/{connection.ts → connection/wsConnection/StableWSConnection.ts} +230 -232
- package/src/connection/wsConnection/WSConnection.ts +270 -0
- package/src/connection/wsConnection/WSConnectionFallback.ts +250 -0
- package/src/connection/wsConnection/config.ts +73 -0
- package/src/connection/wsConnection/index.ts +4 -0
- package/src/connection/wsConnection/types.ts +48 -0
- package/src/constants.ts +6 -5
- package/src/custom_types.ts +0 -1
- package/src/entityStore/EntityStore.ts +415 -79
- package/src/entityStore/StoreBackedItemIndex.ts +37 -41
- package/src/entityStore/index.ts +0 -1
- package/src/errors.ts +39 -20
- package/src/formatMessageMemo.ts +17 -0
- package/src/gen/chat/ChannelApi.ts +122 -36
- package/src/gen/chat/ChatApi.ts +578 -1540
- package/src/gen/models/index.ts +2498 -3383
- package/src/gen/models/timestamp-guard.ts +27 -0
- package/src/gen/moderation/ModerationApi.ts +170 -367
- package/src/index.ts +22 -10
- package/src/languageNames.ts +88 -0
- package/src/logger.ts +1 -3
- package/src/messageComposer/CustomDataManager.ts +19 -6
- package/src/messageComposer/LocationComposer.ts +19 -23
- package/src/messageComposer/MessageComposerEffectHandlers.ts +1 -2
- package/src/messageComposer/attachmentIdentity.ts +54 -0
- package/src/messageComposer/attachmentManager.ts +170 -185
- package/src/messageComposer/attachmentUtils.ts +44 -0
- package/src/messageComposer/configuration/configuration.ts +15 -8
- package/src/messageComposer/configuration/types.ts +38 -50
- package/src/messageComposer/fileUtils.ts +1 -1
- package/src/messageComposer/index.ts +1 -0
- package/src/messageComposer/linkPreviewsManager.ts +18 -21
- package/src/messageComposer/messageComposer.ts +223 -174
- package/src/messageComposer/middleware/attachmentManager/postUpload/AttachmentPostUploadMiddlewareExecutor.ts +1 -1
- package/src/messageComposer/middleware/attachmentManager/postUpload/attachmentEnrichment.ts +46 -33
- package/src/messageComposer/middleware/attachmentManager/postUpload/uploadErrorHandler.ts +4 -1
- package/src/messageComposer/middleware/attachmentManager/preUpload/blockedUploadNotification.ts +1 -1
- package/src/messageComposer/middleware/messageComposer/MessageComposerMiddlewareExecutor.ts +6 -0
- package/src/messageComposer/middleware/messageComposer/attachments.ts +76 -5
- package/src/messageComposer/middleware/messageComposer/commandInjection.ts +10 -4
- package/src/messageComposer/middleware/messageComposer/customData.ts +17 -5
- package/src/messageComposer/middleware/messageComposer/index.ts +2 -0
- package/src/messageComposer/middleware/messageComposer/linkPreviews.ts +22 -12
- package/src/messageComposer/middleware/messageComposer/pollOnly.ts +9 -1
- package/src/messageComposer/middleware/messageComposer/textComposer.ts +1 -1
- package/src/messageComposer/middleware/messageComposer/userDataInjection.ts +12 -12
- package/src/messageComposer/middleware/pollComposer/state.ts +2 -2
- package/src/messageComposer/middleware/pollComposer/types.ts +2 -4
- package/src/messageComposer/middleware/pollComposer/validation.ts +8 -26
- package/src/messageComposer/middleware/textComposer/TextComposerMiddlewareExecutor.ts +8 -6
- package/src/messageComposer/middleware/textComposer/commandUtils.ts +10 -3
- package/src/messageComposer/middleware/textComposer/commands.ts +11 -16
- package/src/messageComposer/middleware/textComposer/mentions.ts +32 -70
- package/src/messageComposer/middleware/textComposer/textMiddlewareUtils.ts +10 -4
- package/src/messageComposer/middleware/textComposer/types.ts +1 -6
- package/src/messageComposer/pollComposer.ts +21 -9
- package/src/messageComposer/textComposer.ts +37 -57
- package/src/messageComposer/types.ts +39 -11
- package/src/messageDelivery/MessageDeliveryReporter.ts +86 -125
- package/src/messageDelivery/MessageReceiptsTracker.ts +88 -101
- package/src/messageOperations/FailedSendCache.ts +85 -0
- package/src/messageOperations/MessageOperationStatePolicy.ts +121 -112
- package/src/messageOperations/MessageOperations.ts +297 -175
- package/src/{entityStore → messageOperations}/applyReactionLocally.ts +9 -28
- package/src/messageOperations/createMessageOperations.ts +176 -0
- package/src/messageOperations/index.ts +6 -14
- package/src/messageOperations/reflectReactionEvent.ts +43 -0
- package/src/messageOperations/sendOrdering.ts +25 -0
- package/src/messageOperations/settlePendingAttachmentUploads.ts +98 -0
- package/src/messageOperations/types.ts +51 -13
- package/src/middleware.ts +13 -5
- package/src/moderation.ts +25 -21
- package/src/notifications/NotificationManager.ts +19 -28
- package/src/notifications/types.ts +21 -57
- package/src/offline-support/offline_support_api.ts +226 -669
- package/src/offline-support/offline_sync_manager.ts +51 -80
- package/src/offline-support/queueableOperations.ts +98 -73
- package/src/offline-support/types.ts +43 -186
- package/src/pagination/FilterBuilder.ts +1 -22
- package/src/pagination/ItemIndex.ts +4 -12
- package/src/pagination/cursorDerivation/aroundPaginationFlags.ts +29 -0
- package/src/pagination/cursorDerivation/createdAtAroundPaginationFlags.ts +15 -63
- package/src/pagination/cursorDerivation/idAroundPaginationFlags.ts +11 -39
- package/src/pagination/cursorDerivation/linearPaginationFlags.ts +53 -33
- package/src/pagination/filterCompiler.ts +11 -25
- package/src/pagination/index.ts +1 -0
- package/src/pagination/paginators/BasePaginator.ts +1415 -1041
- package/src/pagination/paginators/ChannelPaginator.ts +479 -217
- package/src/pagination/paginators/MessageIntervalPaginator.ts +1351 -772
- package/src/pagination/paginators/MessagePaginator.ts +518 -233
- package/src/pagination/paginators/PinnedMessagePaginator.ts +32 -40
- package/src/pagination/paginators/ReminderPaginator.ts +21 -16
- package/src/pagination/paginators/ThreadPaginator.ts +170 -0
- package/src/pagination/paginators/UserGroupPaginator.ts +16 -19
- package/src/pagination/paginators/index.ts +1 -0
- package/src/pagination/paginators/stateThrottling.ts +5 -10
- package/src/pagination/sortCompiler.ts +23 -50
- package/src/pagination/utility.normalization.ts +8 -30
- package/src/pagination/utility.search.ts +4 -11
- package/src/poll.ts +177 -98
- package/src/poll_manager.ts +24 -22
- package/src/poll_optimistic_votes.ts +335 -0
- package/src/reminders/Reminder.ts +16 -12
- package/src/reminders/ReminderManager.ts +22 -55
- package/src/reminders/ReminderTimer.ts +1 -2
- package/src/search/BaseSearchSource.ts +19 -25
- package/src/search/ChannelSearchSource.ts +43 -1
- package/src/search/MessageSearchSource.ts +77 -13
- package/src/search/SearchController.ts +39 -36
- package/src/search/types.ts +6 -10
- package/src/segment.ts +1 -1
- package/src/signing.ts +23 -3
- package/src/thread.ts +418 -703
- package/src/thread_manager.ts +300 -234
- package/src/token_manager.ts +12 -22
- package/src/types.ts +102 -216
- package/src/types.utility.ts +3 -3
- package/src/upload-utils.ts +5 -10
- package/src/uploadManager.ts +64 -18
- package/src/utils/FixedSizeQueueCache.ts +18 -21
- package/src/utils/WithSubscriptions.ts +6 -16
- package/src/utils/abort.ts +45 -0
- package/src/utils/concurrency.ts +16 -55
- package/src/utils/mergeWith/mergeWith.ts +10 -13
- package/src/utils/mergeWith/mergeWithCore.ts +24 -81
- package/src/utils/mergeWith/mergeWithDiff.ts +15 -33
- package/src/utils/objectPath.ts +6 -41
- package/src/utils/retryable.ts +10 -14
- package/src/utils/throttling/throttle.ts +9 -25
- package/src/utils/time.ts +40 -60
- package/src/utils.ts +181 -174
- package/codegen/i18n/callSites.ts +0 -126
- package/codegen/i18n/generate.ts +0 -226
- package/codegen/i18n/guards.ts +0 -178
- package/codegen/i18n/index.ts +0 -29
- package/codegen/i18n/stringMaps.ts +0 -91
- package/codegen/i18n/types.ts +0 -100
- package/dist/cjs/i18n.browser.js +0 -1131
- package/dist/cjs/i18n.browser.js.map +0 -7
- package/dist/cjs/i18n.node.js +0 -1160
- package/dist/cjs/i18n.node.js.map +0 -7
- package/dist/esm/i18n-codegen.mjs +0 -409
- package/dist/esm/i18n-codegen.mjs.map +0 -7
- package/dist/esm/i18n.mjs +0 -1098
- package/dist/esm/i18n.mjs.map +0 -7
- package/dist/types/ConnectionRecoveryManager.d.ts +0 -156
- package/dist/types/connection.d.ts +0 -139
- package/dist/types/entityStore/applyReactionLocally.d.ts +0 -32
- package/dist/types/i18n/Streami18n.d.ts +0 -168
- package/dist/types/i18n/TranslationBuilder.d.ts +0 -70
- package/dist/types/i18n/TranslationStore.d.ts +0 -57
- package/dist/types/i18n/dayjs.d.ts +0 -104
- package/dist/types/i18n/formatters.d.ts +0 -91
- package/dist/types/i18n/index.d.ts +0 -15
- package/dist/types/i18n/translator.d.ts +0 -29
- package/dist/types/i18n/types.d.ts +0 -262
- package/dist/types/i18n-codegen/callSites.d.ts +0 -10
- package/dist/types/i18n-codegen/generate.d.ts +0 -15
- package/dist/types/i18n-codegen/guards.d.ts +0 -61
- package/dist/types/i18n-codegen/index.d.ts +0 -29
- package/dist/types/i18n-codegen/stringMaps.d.ts +0 -14
- package/dist/types/i18n-codegen/types.d.ts +0 -94
- package/dist/types/insights.d.ts +0 -56
- package/dist/types/messageOperations/optimistic.d.ts +0 -118
- package/dist/types/messageOperations/persistence.d.ts +0 -21
- package/dist/types/pagination/utility.queryChannel.d.ts +0 -26
- package/dist/types/store.d.ts +0 -125
- package/src/ConnectionRecoveryManager.ts +0 -293
- package/src/i18n/Streami18n.ts +0 -520
- package/src/i18n/TranslationBuilder.ts +0 -176
- package/src/i18n/TranslationStore.ts +0 -96
- package/src/i18n/dayjs.ts +0 -228
- package/src/i18n/formatters.ts +0 -461
- package/src/i18n/index.ts +0 -15
- package/src/i18n/languageNames.ts +0 -103
- package/src/i18n/translator.ts +0 -76
- package/src/i18n/types.ts +0 -375
- package/src/insights.ts +0 -82
- package/src/messageOperations/optimistic.ts +0 -257
- package/src/messageOperations/persistence.ts +0 -40
- package/src/pagination/utility.queryChannel.ts +0 -84
- package/src/store.ts +0 -304
package/src/ChannelManager.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { EventHandlerPipeline } from './EventHandlerPipeline';
|
|
2
2
|
import { WithSubscriptions } from './utils/WithSubscriptions';
|
|
3
|
-
import type { EventType } from './types';
|
|
3
|
+
import type { ChannelInput, ChannelStateResponseFields, EventType } from './types';
|
|
4
4
|
import type { ChannelPaginator } from './pagination';
|
|
5
5
|
import type { StreamChat } from './client';
|
|
6
|
-
import type { Unsubscribe } from '
|
|
7
|
-
import { StateStore } from '
|
|
6
|
+
import type { Unsubscribe } from '@stream-io/state-store';
|
|
7
|
+
import { StateStore } from '@stream-io/state-store';
|
|
8
8
|
import type {
|
|
9
9
|
EventHandlerPipelineHandler,
|
|
10
10
|
FindEventHandlerParams,
|
|
@@ -13,10 +13,13 @@ import type {
|
|
|
13
13
|
PipelineEvent,
|
|
14
14
|
} from './EventHandlerPipeline';
|
|
15
15
|
import { filterConstrainsField } from './pagination/filterCompiler';
|
|
16
|
-
import {
|
|
17
|
-
import type { Channel } from './channel';
|
|
16
|
+
import { Channel } from './channel';
|
|
18
17
|
import { ChannelWatchStatus } from './channel_state';
|
|
19
|
-
import { runDetached } from './utils';
|
|
18
|
+
import { generateChannelTempId, getMemberUserId, runDetached } from './utils';
|
|
19
|
+
import { EntityStore } from './entityStore/EntityStore';
|
|
20
|
+
import { chatLoggerSystem } from './logger';
|
|
21
|
+
|
|
22
|
+
const logger = chatLoggerSystem.getLogger('channel-manager');
|
|
20
23
|
|
|
21
24
|
export type ChannelManagerEventHandlerContext = {
|
|
22
25
|
channelManager: ChannelManager;
|
|
@@ -27,11 +30,8 @@ type EventHandlerContext = ChannelManagerEventHandlerContext;
|
|
|
27
30
|
type SupportedEventType = EventType | (string & {});
|
|
28
31
|
|
|
29
32
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* Return a set of paginator ids that should keep/own the item.
|
|
34
|
-
* Returning an empty set means the channel will be removed everywhere.
|
|
33
|
+
* Picks which paginators own a channel that matches several, by returning their IDs. An empty list
|
|
34
|
+
* means every matching paginator keeps it.
|
|
35
35
|
*/
|
|
36
36
|
export type PaginatorOwnershipResolver = (args: {
|
|
37
37
|
channel: Channel;
|
|
@@ -39,10 +39,9 @@ export type PaginatorOwnershipResolver = (args: {
|
|
|
39
39
|
}) => string[];
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* - If none of the matching paginator ids are in the priority list, all matches are kept (back-compat).
|
|
42
|
+
* Priority-based ownership resolver. `priority` lists paginator IDs from highest to lowest: of the
|
|
43
|
+
* paginators matching a channel, the highest-priority one owns it. If none of them is listed, all
|
|
44
|
+
* keep it.
|
|
46
45
|
*/
|
|
47
46
|
export const createPriorityOwnershipResolver = (
|
|
48
47
|
priority?: string[],
|
|
@@ -55,7 +54,6 @@ export const createPriorityOwnershipResolver = (
|
|
|
55
54
|
if (matchingPaginators.length <= 1) {
|
|
56
55
|
return matchingPaginators.map((p) => p.id);
|
|
57
56
|
}
|
|
58
|
-
// The winner is the first item in the sorted array of matching paginators
|
|
59
57
|
const winner = [...matchingPaginators].sort((a, b) => {
|
|
60
58
|
const rankA = rank.get(a.id);
|
|
61
59
|
const rankB = rank.get(b.id);
|
|
@@ -64,7 +62,7 @@ export const createPriorityOwnershipResolver = (
|
|
|
64
62
|
return valueA - valueB;
|
|
65
63
|
})[0];
|
|
66
64
|
const winnerValue = rank.get(winner.id);
|
|
67
|
-
//
|
|
65
|
+
// None of the matches is listed: keep all.
|
|
68
66
|
if (winnerValue === undefined) {
|
|
69
67
|
return matchingPaginators.map((p) => p.id);
|
|
70
68
|
}
|
|
@@ -73,10 +71,8 @@ export const createPriorityOwnershipResolver = (
|
|
|
73
71
|
};
|
|
74
72
|
|
|
75
73
|
/**
|
|
76
|
-
* The cid the event refers to. Events
|
|
77
|
-
*
|
|
78
|
-
* `notification.added_to_channel`) have all three optional and identify the channel solely through the
|
|
79
|
-
* required `event.channel`.
|
|
74
|
+
* The cid the event refers to. Events carry it as `cid`, as `channel_type` + `channel_id`, or only
|
|
75
|
+
* in `event.channel` (e.g. `notification.added_to_channel`).
|
|
80
76
|
*/
|
|
81
77
|
const getCidFromEvent = (event: PipelineEvent): string | undefined => {
|
|
82
78
|
if (event.cid) return event.cid;
|
|
@@ -87,39 +83,24 @@ const getCidFromEvent = (event: PipelineEvent): string | undefined => {
|
|
|
87
83
|
return event.channel?.cid;
|
|
88
84
|
};
|
|
89
85
|
|
|
86
|
+
// A channel pending disposal counts as not stored, so an event that makes its cid relevant again
|
|
87
|
+
// gets a live instance.
|
|
90
88
|
const getCachedChannelFromEvent = (
|
|
91
89
|
event: PipelineEvent,
|
|
92
|
-
|
|
90
|
+
channelManager: ChannelManager,
|
|
93
91
|
): Channel | undefined => {
|
|
94
92
|
const cid = getCidFromEvent(event);
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const reEmit: EventHandlerPipelineHandler<EventHandlerContext> = ({
|
|
99
|
-
event,
|
|
100
|
-
ctx: { channelManager },
|
|
101
|
-
}) => {
|
|
102
|
-
if (!event.cid) return;
|
|
103
|
-
const channel = channelManager.client.activeChannels[event.cid];
|
|
104
|
-
if (!channel) return;
|
|
105
|
-
channelManager.paginators.forEach((paginator) => {
|
|
106
|
-
const items = paginator.items;
|
|
107
|
-
const { state } = paginator.locateByItem(channel);
|
|
108
|
-
if ((state?.currentIndex ?? -1) > -1 && items) {
|
|
109
|
-
paginator.state.partialNext({ items: [...items] });
|
|
110
|
-
}
|
|
111
|
-
});
|
|
93
|
+
const channel = cid ? channelManager.get(cid) : undefined;
|
|
94
|
+
return channel?.pendingDisposal ? undefined : channel;
|
|
112
95
|
};
|
|
113
96
|
|
|
114
97
|
const removeItem: EventHandlerPipelineHandler<EventHandlerContext> = ({
|
|
115
98
|
event,
|
|
116
99
|
ctx: { channelManager },
|
|
117
100
|
}) => {
|
|
118
|
-
// `getCidFromEvent`, not `event.cid`: on `channel.deleted` the cid can arrive nested in
|
|
119
|
-
// `event.channel` only, and the legacy ChannelManager removed by `event.cid || event.channel?.cid`
|
|
120
101
|
const cid = getCidFromEvent(event);
|
|
121
102
|
if (!cid) return;
|
|
122
|
-
const channel = channelManager.
|
|
103
|
+
const channel = channelManager.get(cid);
|
|
123
104
|
channelManager.paginators.forEach((paginator) => {
|
|
124
105
|
paginator.removeItem({ id: cid, item: channel });
|
|
125
106
|
});
|
|
@@ -130,35 +111,21 @@ const removeItem: EventHandlerPipelineHandler<EventHandlerContext> = ({
|
|
|
130
111
|
export const ignoreEventsForUnknownChannels: EventHandlerPipelineHandler<
|
|
131
112
|
EventHandlerContext
|
|
132
113
|
> = ({ event, ctx: { channelManager } }) => {
|
|
133
|
-
const channel
|
|
134
|
-
event,
|
|
135
|
-
channelManager.client.activeChannels,
|
|
136
|
-
);
|
|
114
|
+
const channel = getCachedChannelFromEvent(event, channelManager);
|
|
137
115
|
if (!channel) return { action: 'stop' };
|
|
138
116
|
};
|
|
139
117
|
|
|
140
118
|
/**
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
* held. Reconnect recovery re-watches the first page of each list plus the active channel; everything
|
|
145
|
-
* else stays `WasWatching` — pages 2+ of a scrolled list, channels visited and navigated away from,
|
|
146
|
-
* channels matching no mounted list. Such a channel still receives member-level events (e.g.
|
|
147
|
-
* `notification.message_new`), which relocate its row but carry no message body, so its preview would
|
|
148
|
-
* sit frozen until the channel was opened. Re-watching it the moment an event proves it relevant is
|
|
149
|
-
* what closes that gap, without ever eagerly re-watching the whole cache.
|
|
150
|
-
*
|
|
151
|
-
* The request is idempotent, so a successful watch flips the status to `Watching`, and `getChannel`
|
|
152
|
-
* dedupes concurrent watches for the same cid, so a burst of events cannot produce a burst of requests.
|
|
119
|
+
* Re-watches a channel whose watch a dropped socket ended (`WasWatching`). Recovery re-watches only
|
|
120
|
+
* the first page of each list and the active channel, so the others re-watch when an event proves
|
|
121
|
+
* them relevant. `watch()` joins an in-flight watch, so a burst sends one request.
|
|
153
122
|
*/
|
|
154
|
-
const restoreInterruptedWatch = (channel: Channel
|
|
123
|
+
const restoreInterruptedWatch = (channel: Channel) => {
|
|
124
|
+
// A disposed channel refuses requests.
|
|
155
125
|
if (channel.pendingDisposal) return;
|
|
156
126
|
if (channel.watchStatus !== ChannelWatchStatus.WasWatching) return;
|
|
157
127
|
|
|
158
|
-
|
|
159
|
-
// channel pending disposal — the guard above makes that unreachable today, but a throw here would
|
|
160
|
-
// reject the whole event handler, so it is not a hazard worth leaving one edit away.
|
|
161
|
-
runDetached(getChannel({ channel, client }), {
|
|
128
|
+
runDetached(channel.watch(), {
|
|
162
129
|
context: `restoreInterruptedWatch(${channel.cid})`,
|
|
163
130
|
});
|
|
164
131
|
};
|
|
@@ -167,82 +134,92 @@ const updateLists: EventHandlerPipelineHandler<EventHandlerContext> = async ({
|
|
|
167
134
|
event,
|
|
168
135
|
ctx: { channelManager },
|
|
169
136
|
}) => {
|
|
170
|
-
let channel: Channel | undefined = getCachedChannelFromEvent(
|
|
171
|
-
event,
|
|
172
|
-
channelManager.client.activeChannels,
|
|
173
|
-
);
|
|
137
|
+
let channel: Channel | undefined = getCachedChannelFromEvent(event, channelManager);
|
|
174
138
|
|
|
175
139
|
if (!channel) {
|
|
176
140
|
const [type, id] = getCidFromEvent(event)?.split(':') ?? [];
|
|
177
141
|
if (!type) return;
|
|
178
142
|
|
|
179
|
-
channel = await
|
|
180
|
-
client: channelManager.client,
|
|
181
|
-
id,
|
|
182
|
-
type,
|
|
183
|
-
});
|
|
143
|
+
channel = await channelManager.ensure({ id, type }).watch();
|
|
184
144
|
}
|
|
185
145
|
|
|
186
146
|
if (!channel) return;
|
|
187
147
|
|
|
188
|
-
|
|
189
|
-
p.matchesFilter(channel),
|
|
190
|
-
);
|
|
191
|
-
const matchingIds = new Set(matchingPaginators.map((p) => p.id));
|
|
192
|
-
|
|
193
|
-
const ownerIds = channelManager.resolveOwnership(channel, matchingPaginators);
|
|
148
|
+
routeToPaginators(channelManager, channel);
|
|
194
149
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
// remove if it does not match the filter anymore
|
|
198
|
-
paginator.removeItem({ item: channel });
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// Only if owners are specified, the items is removed from the non-owner matching paginators
|
|
203
|
-
if (ownerIds.size > 0 && !ownerIds.has(paginator.id)) {
|
|
204
|
-
// matched, but not selected to own - remove to enforce exclusivity
|
|
205
|
-
paginator.removeItem({ item: channel });
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// Selected owner: ingest. The manager never boosts by default on any event — the sort is the
|
|
210
|
-
// single source of truth for order, so a channel that just became relevant (new message, added,
|
|
211
|
-
// unhidden) relocates via its updated sort key. Boosting remains a public per-paginator primitive
|
|
212
|
-
// (`paginator.boost`) for integrators to opt into for specific channels (VIP/mention/deep-link).
|
|
213
|
-
paginator.ingestItem(channel);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
// AFTER routing, and not awaited: the row relocates immediately off the event, and the watch (plus
|
|
217
|
-
// the state it hydrates) lands whenever it lands. `channel.hidden` is excluded — a channel being
|
|
218
|
-
// hidden is the one routed event that must not resurrect a watch.
|
|
150
|
+
// After routing and not awaited, so the row relocates off the event without waiting for the
|
|
151
|
+
// watch. `channel.hidden` must not resurrect a watch.
|
|
219
152
|
if (event.type !== 'channel.hidden') {
|
|
220
|
-
restoreInterruptedWatch(channel
|
|
153
|
+
restoreInterruptedWatch(channel);
|
|
221
154
|
}
|
|
222
155
|
};
|
|
223
156
|
|
|
224
|
-
|
|
157
|
+
/**
|
|
158
|
+
* Re-inserts a loaded channel whose data changed in place (`channel.updated`, `channel.truncated`), so
|
|
159
|
+
* lists sorted or filtered by a changed field move or drop it. Unknown channels are ignored.
|
|
160
|
+
*/
|
|
161
|
+
const reinsertItem: EventHandlerPipelineHandler<EventHandlerContext> = ({
|
|
162
|
+
event,
|
|
163
|
+
ctx: { channelManager },
|
|
164
|
+
}) => {
|
|
165
|
+
const channel = getCachedChannelFromEvent(event, channelManager);
|
|
166
|
+
if (!channel) return;
|
|
167
|
+
routeToPaginators(channelManager, channel);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/** Ingests `channel` into the lists that match and own it, and removes it from the rest. */
|
|
171
|
+
function routeToPaginators(channelManager: ChannelManager, channel: Channel) {
|
|
172
|
+
// No boosting: the sort alone decides the order (`paginator.boost` is there for integrators).
|
|
173
|
+
channelManager.ingestChannel(channel);
|
|
174
|
+
}
|
|
175
|
+
|
|
225
176
|
const channelDeletedHandler: LabeledEventHandler<EventHandlerContext> = {
|
|
226
177
|
handle: removeItem,
|
|
227
178
|
id: 'ChannelManager:default-handler:channel.deleted',
|
|
228
179
|
};
|
|
229
180
|
|
|
230
|
-
// fixme: this handler should not be handled by the channel manager but as Channel does not have reactive state,
|
|
231
|
-
// we need to re-emit the whole list to reflect the changes
|
|
232
181
|
const channelUpdatedHandler: LabeledEventHandler<EventHandlerContext> = {
|
|
233
|
-
handle:
|
|
182
|
+
handle: reinsertItem,
|
|
234
183
|
id: 'ChannelManager:default-handler:channel.updated',
|
|
235
184
|
};
|
|
236
185
|
|
|
237
|
-
// fixme: this handler should not be handled by the channel manager but as Channel does not have reactive state,
|
|
238
|
-
// we need to re-emit the whole list to reflect the changes
|
|
239
186
|
const channelTruncatedHandler: LabeledEventHandler<EventHandlerContext> = {
|
|
240
|
-
handle:
|
|
187
|
+
handle: reinsertItem,
|
|
241
188
|
id: 'ChannelManager:default-handler:channel.truncated',
|
|
242
189
|
};
|
|
243
190
|
|
|
191
|
+
// a hard delete of the last message might move the channel's `last_message_at` back
|
|
192
|
+
const messageDeletedHandler: LabeledEventHandler<EventHandlerContext> = {
|
|
193
|
+
handle: (context) => (context.event.hard_delete ? reinsertItem(context) : undefined),
|
|
194
|
+
id: 'ChannelManager:default-handler:message.deleted',
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* A list every channel left (all hidden) has no loaded page any more, and channels shown again land
|
|
199
|
+
* only in its logical intervals, which a list UI does not render: one comes back, the rest do not.
|
|
200
|
+
* Such a list re-queries its first page, as v9 did on `channel.visible`, but only when the channel
|
|
201
|
+
* shown matches its filters: no other list can gain it. Debounced, so showing several channels at
|
|
202
|
+
* once sends one query.
|
|
203
|
+
*/
|
|
204
|
+
const reloadListsWithoutLoadedPage = (
|
|
205
|
+
channelManager: ChannelManager,
|
|
206
|
+
channel: Channel,
|
|
207
|
+
) => {
|
|
208
|
+
channelManager.paginators.forEach((paginator) => {
|
|
209
|
+
if (paginator.items === undefined || paginator.isLoading) return;
|
|
210
|
+
if (paginator.anchoredHeadItems.length > 0) return;
|
|
211
|
+
if (!paginator.matchesFilter(channel)) return;
|
|
212
|
+
paginator.toTailDebounced({ reset: 'yes' });
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
|
|
244
216
|
const channelVisibleHandler: LabeledEventHandler<EventHandlerContext> = {
|
|
245
|
-
handle:
|
|
217
|
+
handle: async (context) => {
|
|
218
|
+
await updateLists(context);
|
|
219
|
+
// `updateLists` caches a channel it did not know, so this misses only an event with no cid.
|
|
220
|
+
const channel = getCachedChannelFromEvent(context.event, context.ctx.channelManager);
|
|
221
|
+
if (channel) reloadListsWithoutLoadedPage(context.ctx.channelManager, channel);
|
|
222
|
+
},
|
|
246
223
|
id: 'ChannelManager:default-handler:channel.visible',
|
|
247
224
|
};
|
|
248
225
|
|
|
@@ -251,8 +228,7 @@ const channelHiddenHandler: LabeledEventHandler<EventHandlerContext> = {
|
|
|
251
228
|
id: 'ChannelManager:default-handler:channel.hidden',
|
|
252
229
|
};
|
|
253
230
|
|
|
254
|
-
//
|
|
255
|
-
// member.user.name - can be impacted
|
|
231
|
+
// Member IDs are stable, but `member.user.name` can affect a list's filter.
|
|
256
232
|
const memberUpdatedHandler: LabeledEventHandler<EventHandlerContext> = {
|
|
257
233
|
handle: updateLists,
|
|
258
234
|
id: 'ChannelManager:default-handler:member.updated',
|
|
@@ -263,16 +239,12 @@ const messageNewHandler: LabeledEventHandler<EventHandlerContext> = {
|
|
|
263
239
|
id: 'ChannelManager:default-handler:message.new',
|
|
264
240
|
};
|
|
265
241
|
|
|
266
|
-
/**
|
|
267
|
-
* Sort fields `channelSortPathResolver` resolves from read state. `SortParamRequest.field` is an open
|
|
268
|
-
* string, so there is no type enumerating them.
|
|
269
|
-
*/
|
|
242
|
+
/** Sort fields resolved from read state (`SortParamRequest.field` is an open string). */
|
|
270
243
|
const READ_STATE_SORT_FIELDS: string[] = ['has_unread', 'unread_count'];
|
|
271
244
|
|
|
272
245
|
/**
|
|
273
|
-
* Whether a read can change this list
|
|
274
|
-
* (`has_unread` / `unread_count` sort).
|
|
275
|
-
* about the list depends on it.
|
|
246
|
+
* Whether a read can change this list: its membership (`has_unread` filter) or its order
|
|
247
|
+
* (`has_unread` / `unread_count` sort).
|
|
276
248
|
*/
|
|
277
249
|
const dependsOnReadState = (paginator: ChannelPaginator) =>
|
|
278
250
|
paginator.effectiveSort.some(
|
|
@@ -281,10 +253,8 @@ const dependsOnReadState = (paginator: ChannelPaginator) =>
|
|
|
281
253
|
|
|
282
254
|
/**
|
|
283
255
|
* Re-routes a channel whose read state changed. `message.read` only reaches watchers, so
|
|
284
|
-
* `notification.mark_*` is the only signal for channels the user is not watching.
|
|
285
|
-
*
|
|
286
|
-
* Never fetches, unlike `updateLists`: a read cannot make an unknown channel belong to a list, and
|
|
287
|
-
* these events are far too frequent to query on.
|
|
256
|
+
* `notification.mark_*` is the only signal for channels the user is not watching. Never fetches
|
|
257
|
+
* (unlike `updateLists`): these events are too frequent to query on.
|
|
288
258
|
*/
|
|
289
259
|
const readStateChangedHandler: LabeledEventHandler<EventHandlerContext> = {
|
|
290
260
|
handle: ({ event, ctx: { channelManager } }) => {
|
|
@@ -296,7 +266,7 @@ const readStateChangedHandler: LabeledEventHandler<EventHandlerContext> = {
|
|
|
296
266
|
if (event.thread_id) return;
|
|
297
267
|
|
|
298
268
|
// a mark-all-read names no channel, so there is no target to route
|
|
299
|
-
const channel = getCachedChannelFromEvent(event,
|
|
269
|
+
const channel = getCachedChannelFromEvent(event, channelManager);
|
|
300
270
|
if (!channel) return;
|
|
301
271
|
|
|
302
272
|
// hot path: `message.read` fires on every channel the user opens
|
|
@@ -323,17 +293,15 @@ const notificationRemovedFromChannelHandler: LabeledEventHandler<EventHandlerCon
|
|
|
323
293
|
};
|
|
324
294
|
|
|
325
295
|
/**
|
|
326
|
-
* `muted` is filterable
|
|
327
|
-
*
|
|
328
|
-
* old list until some unrelated event touched it. The client applies `event.me.channel_mutes` before
|
|
329
|
-
* its listeners run, so the filters already see the new state.
|
|
296
|
+
* `muted` is filterable but a mute change emits only this user-level event, so channels are
|
|
297
|
+
* re-routed here. The client applies `event.me.channel_mutes` first, so filters see the new state.
|
|
330
298
|
*/
|
|
331
299
|
const notificationChannelMutesUpdatedHandler: LabeledEventHandler<EventHandlerContext> = {
|
|
332
300
|
handle: ({ ctx: { channelManager } }) => {
|
|
333
301
|
const seen = new Set<string>();
|
|
334
302
|
channelManager.paginators.forEach((paginator) => {
|
|
335
303
|
(paginator.items ?? []).forEach((channel) => {
|
|
336
|
-
if (seen.has(channel.cid)) return; //
|
|
304
|
+
if (seen.has(channel.cid)) return; // ingest once, not once per list
|
|
337
305
|
seen.add(channel.cid);
|
|
338
306
|
channelManager.ingestChannel(channel);
|
|
339
307
|
});
|
|
@@ -342,34 +310,6 @@ const notificationChannelMutesUpdatedHandler: LabeledEventHandler<EventHandlerCo
|
|
|
342
310
|
id: 'ChannelManager:default-handler:notification.channel_mutes_updated',
|
|
343
311
|
};
|
|
344
312
|
|
|
345
|
-
// fixme: updates users for member object in all the channels which are loaded with that member - normalization would be beneficial
|
|
346
|
-
const userPresenceChangedHandler: LabeledEventHandler<EventHandlerContext> = {
|
|
347
|
-
handle: ({ event, ctx: { channelManager } }) => {
|
|
348
|
-
const eventUser = event.user;
|
|
349
|
-
if (!eventUser?.id) return;
|
|
350
|
-
channelManager.paginators.forEach((paginator) => {
|
|
351
|
-
const paginatorItems = paginator.items;
|
|
352
|
-
if (!paginatorItems) return;
|
|
353
|
-
let updated = false;
|
|
354
|
-
paginatorItems.forEach((channel) => {
|
|
355
|
-
if (channel.state.members[eventUser.id]) {
|
|
356
|
-
channel.state.members[eventUser.id].user = event.user;
|
|
357
|
-
updated = true;
|
|
358
|
-
}
|
|
359
|
-
if (channel.state.membership.user?.id === eventUser.id) {
|
|
360
|
-
channel.state.membership.user = eventUser;
|
|
361
|
-
updated = true;
|
|
362
|
-
}
|
|
363
|
-
});
|
|
364
|
-
if (updated) {
|
|
365
|
-
// fixme: user is not reactive and so the whole list has to be re-rendered
|
|
366
|
-
paginator.state.partialNext({ items: [...paginatorItems] });
|
|
367
|
-
}
|
|
368
|
-
});
|
|
369
|
-
},
|
|
370
|
-
id: 'ChannelManager:default-handler:user.presence.changed',
|
|
371
|
-
};
|
|
372
|
-
|
|
373
313
|
export type ChannelManagerState = {
|
|
374
314
|
paginators: ChannelPaginator[];
|
|
375
315
|
};
|
|
@@ -382,41 +322,70 @@ export type ChannelManagerOptions = {
|
|
|
382
322
|
client: StreamChat;
|
|
383
323
|
paginators?: ChannelPaginator[];
|
|
384
324
|
/**
|
|
385
|
-
* The complete set of event handlers
|
|
386
|
-
*
|
|
387
|
-
*
|
|
388
|
-
*
|
|
389
|
-
* through `addEventHandler` / `setEventHandlers` / `removeEventHandlers`.
|
|
325
|
+
* The complete set of event handlers, replacing the defaults. Start from
|
|
326
|
+
* `ChannelManager.getDefaultHandlers()` (a copy) and enrich it. `client.channelManager` is built
|
|
327
|
+
* without this option: customize it through `addEventHandler` / `setEventHandlers` /
|
|
328
|
+
* `removeEventHandlers`.
|
|
390
329
|
*/
|
|
391
330
|
eventHandlers?: ChannelManagerEventHandlers;
|
|
392
331
|
/**
|
|
393
|
-
*
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
* Empty ownershipResolver array means that the channel is kept in all matching paginators.
|
|
332
|
+
* Picks which paginator(s) own a channel that matches several. Defaults to all of them. A function
|
|
333
|
+
* returns the owning paginator IDs; an array is a priority list (highest first), so the
|
|
334
|
+
* highest-priority match owns the channel, and if none of them is listed all keep it.
|
|
397
335
|
*/
|
|
398
336
|
ownershipResolver?: PaginatorOwnershipResolver | string[];
|
|
399
337
|
};
|
|
400
338
|
|
|
339
|
+
/**
|
|
340
|
+
* A stored channel, its store key, and what keeps it: its own state (`'watched'`, `'active'`,
|
|
341
|
+
* `'querying-channel'`) and the names of its holders. If none, `releaseUnusedChannels()` removes
|
|
342
|
+
* it.
|
|
343
|
+
*/
|
|
344
|
+
export type ChannelUsage = {
|
|
345
|
+
channel: Channel;
|
|
346
|
+
key: string;
|
|
347
|
+
keptBy: string[];
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
const keptByOwnState = (channel: Channel) => {
|
|
351
|
+
const reasons: string[] = [];
|
|
352
|
+
if (channel.watchStatus !== ChannelWatchStatus.NotWatching) reasons.push('watched');
|
|
353
|
+
if (channel.active) reasons.push('active');
|
|
354
|
+
if (channel.isQueryingChannel) reasons.push('querying-channel');
|
|
355
|
+
return reasons;
|
|
356
|
+
};
|
|
357
|
+
|
|
401
358
|
export class ChannelManager extends WithSubscriptions {
|
|
402
359
|
client: StreamChat;
|
|
403
360
|
state: StateStore<ChannelManagerState>;
|
|
361
|
+
/**
|
|
362
|
+
* Every `Channel`, one per cid (a channel created from members sits under a temporary cid until
|
|
363
|
+
* {@link ChannelManager.changeChannelId}). A channel stays until it is deleted, the user is
|
|
364
|
+
* removed from it or logs out, or {@link ChannelManager.releaseUnusedChannels} removes it;
|
|
365
|
+
* removal runs {@link Channel.disconnect}. Lists hold the channels they show by linking them,
|
|
366
|
+
* others by claims.
|
|
367
|
+
*
|
|
368
|
+
* @internal
|
|
369
|
+
*/
|
|
370
|
+
readonly channelStore = new EntityStore<Channel>({
|
|
371
|
+
getEntityId: (channel) => channel.cid,
|
|
372
|
+
onRelease: (channel) => channel.disconnect(),
|
|
373
|
+
releaseOnLastUnlink: false,
|
|
374
|
+
});
|
|
375
|
+
|
|
404
376
|
protected _pipelines = new Map<
|
|
405
377
|
SupportedEventType,
|
|
406
378
|
EventHandlerPipeline<EventHandlerContext>
|
|
407
379
|
>();
|
|
408
380
|
protected ownershipResolver?: PaginatorOwnershipResolver;
|
|
409
381
|
/**
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
* Keys = already wrapped (so re-inserting does not wrap twice); values = what to restore on
|
|
418
|
-
* removal, since a detached paginator must stop applying rules resolved against lists it left.
|
|
419
|
-
* Weakly keyed so a dropped paginator is not kept alive by this manager.
|
|
382
|
+
* Instances replaced by a stored one ({@link ChannelManager.supersedeChannel}). Not in the store,
|
|
383
|
+
* so they are disconnected at logout ({@link ChannelManager.clearChannels}).
|
|
384
|
+
*/
|
|
385
|
+
private readonly supersededChannels = new Set<Channel>();
|
|
386
|
+
/**
|
|
387
|
+
* The `filterQueryResults` each registered paginator had before `wrapPaginatorFiltering` wrapped
|
|
388
|
+
* it. Keys mark paginators already wrapped, values are restored on removal. Weakly keyed.
|
|
420
389
|
*/
|
|
421
390
|
protected filterQueryResultsBeforeWrapping = new WeakMap<
|
|
422
391
|
ChannelPaginator,
|
|
@@ -430,6 +399,7 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
430
399
|
'channel.hidden': [channelHiddenHandler],
|
|
431
400
|
'channel.visible': [channelVisibleHandler],
|
|
432
401
|
'member.updated': [memberUpdatedHandler],
|
|
402
|
+
'message.deleted': [messageDeletedHandler],
|
|
433
403
|
'message.new': [messageNewHandler],
|
|
434
404
|
'message.read': [readStateChangedHandler],
|
|
435
405
|
'message.read_locally': [readStateChangedHandler],
|
|
@@ -439,7 +409,6 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
439
409
|
'notification.mark_unread': [readStateChangedHandler],
|
|
440
410
|
'notification.message_new': [notificationMessageNewHandler],
|
|
441
411
|
'notification.removed_from_channel': [notificationRemovedFromChannelHandler],
|
|
442
|
-
'user.presence.changed': [userPresenceChangedHandler],
|
|
443
412
|
};
|
|
444
413
|
|
|
445
414
|
constructor({
|
|
@@ -453,13 +422,11 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
453
422
|
this.state = new StateStore({ paginators: paginators ?? [] });
|
|
454
423
|
this.setOwnershipResolver(ownershipResolver);
|
|
455
424
|
|
|
456
|
-
// A supplied map replaces the defaults
|
|
457
|
-
// handled. `getDefaultHandlers()` returns a copy to enrich when that is what you want.
|
|
425
|
+
// A supplied map replaces the defaults: an event type missing from it is not handled.
|
|
458
426
|
const finalEventHandlers = eventHandlers ?? ChannelManager.getDefaultHandlers();
|
|
459
427
|
for (const [type, handlers] of Object.entries(finalEventHandlers)) {
|
|
460
428
|
if (handlers) this.ensurePipeline(type).replaceAll(handlers);
|
|
461
429
|
}
|
|
462
|
-
// Ensure ownership rules are applied to initial paginators' query results
|
|
463
430
|
this.paginators.forEach((p) => this.wrapPaginatorFiltering(p));
|
|
464
431
|
}
|
|
465
432
|
|
|
@@ -467,6 +434,293 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
467
434
|
return this.state.getLatestValue().paginators;
|
|
468
435
|
}
|
|
469
436
|
|
|
437
|
+
/**
|
|
438
|
+
* Returns the stored channel for a cid.
|
|
439
|
+
*
|
|
440
|
+
* @param cid - The channel's cid.
|
|
441
|
+
* @returns The stored channel, or `undefined` when none is stored.
|
|
442
|
+
*/
|
|
443
|
+
get(cid: string): Channel | undefined {
|
|
444
|
+
return this.channelStore.get(cid);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* The instance a channel list should show for `channel`: the one that superseded it, else the one
|
|
449
|
+
* stored under its cid, else `channel` itself, so a list never displaces the stored instance.
|
|
450
|
+
* `undefined` when that instance is disconnected (`pendingDisposal`): it receives no events.
|
|
451
|
+
*
|
|
452
|
+
* @internal
|
|
453
|
+
*/
|
|
454
|
+
resolveListedChannel(channel: Channel): Channel | undefined {
|
|
455
|
+
const candidate = channel.supersededBy ?? channel;
|
|
456
|
+
const stored = this.get(candidate.cid);
|
|
457
|
+
const resolved = stored ?? candidate;
|
|
458
|
+
if (resolved.pendingDisposal) return undefined;
|
|
459
|
+
if (resolved !== channel) {
|
|
460
|
+
logger
|
|
461
|
+
.withExtraTags('resolveListedChannel', channel.cid)
|
|
462
|
+
.warn(
|
|
463
|
+
'A channel list was given an instance other than the stored one; listing the stored one.',
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
return resolved;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Returns every stored channel.
|
|
471
|
+
*
|
|
472
|
+
* @returns The stored channels.
|
|
473
|
+
*/
|
|
474
|
+
values(): Channel[] {
|
|
475
|
+
return this.channelStore.values();
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Returns the stored channel for `type` and `id`, or creates and stores it (one instance per cid;
|
|
480
|
+
* a stored `pendingDisposal` channel is replaced by a fresh one). Without `id`, the distinct
|
|
481
|
+
* channel between `data.members` (see {@link StreamChat.channel}, which documents both).
|
|
482
|
+
*
|
|
483
|
+
* Apps call {@link StreamChat.channel}, the public equivalent.
|
|
484
|
+
*
|
|
485
|
+
* @param params.type - The channel type.
|
|
486
|
+
* @param params.id - The channel ID; leave it out for a distinct channel created from members.
|
|
487
|
+
* @param params.data - Data for a new channel (members, custom fields). For a stored channel only
|
|
488
|
+
* `data.custom` is applied.
|
|
489
|
+
* @returns The channel; initialize it with `channel.watch()`.
|
|
490
|
+
* @internal
|
|
491
|
+
*/
|
|
492
|
+
ensure({
|
|
493
|
+
data = {},
|
|
494
|
+
id,
|
|
495
|
+
type,
|
|
496
|
+
}: {
|
|
497
|
+
type: string;
|
|
498
|
+
id?: string | null;
|
|
499
|
+
data?: ChannelInput;
|
|
500
|
+
}): Channel {
|
|
501
|
+
const { client } = this;
|
|
502
|
+
if (!client.userId) {
|
|
503
|
+
throw Error('Call connectUser or connectAnonymousUser before creating a channel');
|
|
504
|
+
}
|
|
505
|
+
if (type.includes(':')) {
|
|
506
|
+
throw new Error(`Invalid channel group ${type}, can't contain the : character`);
|
|
507
|
+
}
|
|
508
|
+
if (id) return this.ensureById(type, id, data);
|
|
509
|
+
if (data.members?.length) return this.ensureByMembers(type, data);
|
|
510
|
+
return new Channel(client, type, undefined, data);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
private ensureById(type: string, id: string, data: ChannelInput): Channel {
|
|
514
|
+
if (id.includes(':')) {
|
|
515
|
+
throw Error(`Invalid channel id ${id}, can't contain the : character`);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
const cid = `${type}:${id}`;
|
|
519
|
+
if (this.get(cid)?.pendingDisposal) this.removeChannel(cid);
|
|
520
|
+
|
|
521
|
+
return this.getOrCreateChannel(
|
|
522
|
+
cid,
|
|
523
|
+
() => new Channel(this.client, type, id, data),
|
|
524
|
+
(channel) => {
|
|
525
|
+
// Only `custom`, and only when passed: other fields would wipe the stored custom data (e.g.
|
|
526
|
+
// its name).
|
|
527
|
+
if (data.custom !== undefined) {
|
|
528
|
+
channel.data = { ...channel.data, custom: data.custom };
|
|
529
|
+
channel._data = { ...channel._data, custom: data.custom };
|
|
530
|
+
}
|
|
531
|
+
},
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
private ensureByMembers(type: string, data: ChannelInput): Channel {
|
|
536
|
+
// The ID `Channel` gives itself until the server answers.
|
|
537
|
+
const tempId = generateChannelTempId((data.members ?? []).map(getMemberUserId));
|
|
538
|
+
if (!tempId) {
|
|
539
|
+
throw Error('Please specify atleast one member when creating unique conversation');
|
|
540
|
+
}
|
|
541
|
+
const tempCid = `${type}:${tempId}`;
|
|
542
|
+
|
|
543
|
+
// The real ID of a distinct channel starts with `!members-`: a loaded one with the same members
|
|
544
|
+
// is this conversation.
|
|
545
|
+
if (this.get(tempCid)?.pendingDisposal) this.removeChannel(tempCid);
|
|
546
|
+
if (!this.get(tempCid)) {
|
|
547
|
+
const existing = this.values().find(
|
|
548
|
+
(channel) =>
|
|
549
|
+
!channel.pendingDisposal &&
|
|
550
|
+
channel.type === type &&
|
|
551
|
+
channel.id?.startsWith('!members-') &&
|
|
552
|
+
generateChannelTempId(Object.keys(channel.state.members)) === tempId,
|
|
553
|
+
);
|
|
554
|
+
if (existing) return existing;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
return this.getOrCreateChannel(
|
|
558
|
+
tempCid,
|
|
559
|
+
() => new Channel(this.client, type, undefined, data),
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Returns the channel stored under `cid`, passing it to `hydrate`, or stores and returns the result
|
|
565
|
+
* of `create`.
|
|
566
|
+
*
|
|
567
|
+
* @internal
|
|
568
|
+
*/
|
|
569
|
+
getOrCreateChannel(
|
|
570
|
+
cid: string,
|
|
571
|
+
create: () => Channel,
|
|
572
|
+
hydrate?: (stored: Channel) => void,
|
|
573
|
+
): Channel {
|
|
574
|
+
return this.channelStore.getOrCreate(cid, create, hydrate);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Moves a channel from its temporary cid to the one the server assigned (see `Channel.query`).
|
|
579
|
+
* Returns `false` when `oldCid` isn't stored or `newCid` already holds another channel.
|
|
580
|
+
*
|
|
581
|
+
* @internal
|
|
582
|
+
*/
|
|
583
|
+
changeChannelId(oldCid: string, newCid: string): boolean {
|
|
584
|
+
return this.channelStore.changeId(oldCid, newCid);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* `previous`, created without an ID, was answered with a cid `successor` already holds (stored by
|
|
589
|
+
* an event or a list meanwhile). `successor` stays the one instance and takes over: the response
|
|
590
|
+
* is applied to it like a `queryChannels` result (local messages are never removed),
|
|
591
|
+
* `previous`'s local messages (sending or failed) are added by ID (`successor`'s copy wins), and
|
|
592
|
+
* `previous`'s composer moves over when `previous` is open and `successor` is not.
|
|
593
|
+
*
|
|
594
|
+
* `successor` is marked watched only when the query asked to watch. `previous` is dropped from
|
|
595
|
+
* the store (`storedUnder`) without disconnecting, as it may be on screen, marked superseded
|
|
596
|
+
* (`state.supersededBy`) so its holders switch over, and disconnected at logout.
|
|
597
|
+
*
|
|
598
|
+
* @internal
|
|
599
|
+
*/
|
|
600
|
+
supersedeChannel(
|
|
601
|
+
previous: Channel,
|
|
602
|
+
successor: Channel,
|
|
603
|
+
response: ChannelStateResponseFields,
|
|
604
|
+
{ storedUnder, watch }: { storedUnder?: string; watch?: boolean } = {},
|
|
605
|
+
) {
|
|
606
|
+
if (storedUnder && this.get(storedUnder) === previous) {
|
|
607
|
+
this.channelStore.detach(storedUnder);
|
|
608
|
+
}
|
|
609
|
+
// `false`, not `undefined`: a missing `watch` means "watched if connected" to
|
|
610
|
+
// `hydrateChannels`.
|
|
611
|
+
this.client.hydrateChannels([response], {}, { watch: watch === true });
|
|
612
|
+
successor.messagePaginator.batch(
|
|
613
|
+
() => {
|
|
614
|
+
for (const message of previous.messagePaginator.items ?? []) {
|
|
615
|
+
if (message.status === 'received') continue;
|
|
616
|
+
if (successor.messagePaginator.getItem(message.id)) continue;
|
|
617
|
+
// Composed in `previous`, so it carries the temporary cid.
|
|
618
|
+
successor.messagePaginator.ingestItem({ ...message, cid: successor.cid });
|
|
619
|
+
}
|
|
620
|
+
},
|
|
621
|
+
{ coalesce: true },
|
|
622
|
+
);
|
|
623
|
+
if (previous.active && !successor.active) {
|
|
624
|
+
previous.messageComposer.transferTo(successor.messageComposer);
|
|
625
|
+
}
|
|
626
|
+
previous.state.partialNext({ supersededBy: successor });
|
|
627
|
+
this.supersededChannels.add(previous);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* Removes every stored channel the app disconnected (`pendingDisposal`), whatever holds it, then
|
|
632
|
+
* every one that is neither watched nor held; lists drop them. Kept: watched channels
|
|
633
|
+
* (`watching`, or `wasWatching` until restored), active ones ({@link Channel.activate}), ones
|
|
634
|
+
* being queried (`Channel.isQueryingChannel`), and ones held in
|
|
635
|
+
* {@link ChannelManager.channelStore} (lists link theirs; threads, the composer cache and channel
|
|
636
|
+
* search hold theirs through claims, {@link EntityStore.addClaim}).
|
|
637
|
+
*
|
|
638
|
+
* The SDK never calls this, as it cannot see channels an app keeps in its own state. Call it when
|
|
639
|
+
* the app is done with them (e.g. after `searchController.dispose()`), and keep a channel your
|
|
640
|
+
* code still uses with {@link Channel.activate} first.
|
|
641
|
+
*/
|
|
642
|
+
releaseUnusedChannels() {
|
|
643
|
+
for (const [key, channel] of this.channelStore.entries()) {
|
|
644
|
+
if (channel.pendingDisposal) this.removeChannel(key);
|
|
645
|
+
}
|
|
646
|
+
for (const [key, channel] of this.channelStore.unheldEntries()) {
|
|
647
|
+
if (!keptByOwnState(channel).length) this.removeChannel(key);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* Every stored channel with what keeps it, by the rule of
|
|
653
|
+
* {@link ChannelManager.releaseUnusedChannels}. For debugging tools.
|
|
654
|
+
*
|
|
655
|
+
* @internal
|
|
656
|
+
*/
|
|
657
|
+
getChannelUsage(): ChannelUsage[] {
|
|
658
|
+
return this.channelStore.entries().map(([key, channel]) => ({
|
|
659
|
+
channel,
|
|
660
|
+
key,
|
|
661
|
+
keptBy: [...keptByOwnState(channel), ...this.channelStore.holderNames(key)],
|
|
662
|
+
}));
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* Removes the channel stored under `cid` whatever uses it, and disconnects it
|
|
667
|
+
* ({@link Channel.disconnect}). Every list showing it drops it too.
|
|
668
|
+
*
|
|
669
|
+
* @internal
|
|
670
|
+
*/
|
|
671
|
+
removeChannel(cid: string) {
|
|
672
|
+
this.channelStore.remove(cid);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* Removes and disconnects ({@link Channel.disconnect}) every stored channel, and every superseded
|
|
677
|
+
* one.
|
|
678
|
+
*
|
|
679
|
+
* @internal
|
|
680
|
+
*/
|
|
681
|
+
clearChannels() {
|
|
682
|
+
this.channelStore.clear();
|
|
683
|
+
this.supersededChannels.forEach((channel) => channel.disconnect());
|
|
684
|
+
this.supersededChannels.clear();
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* Fans the client-owned `mutedChannels` out to every stored channel's `state.muteStatus` (a
|
|
689
|
+
* channel republishes only when its status changed, as this runs on every `health.check`).
|
|
690
|
+
*
|
|
691
|
+
* @internal
|
|
692
|
+
*/
|
|
693
|
+
reflectMutedChannels() {
|
|
694
|
+
for (const channel of this.values()) channel._syncMuteStatus();
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Resets the AI indicator state to `Idle` on every stored channel. Invoked from
|
|
699
|
+
* `closeConnection`, a deliberate shutdown that triggers no WS event.
|
|
700
|
+
*
|
|
701
|
+
* @internal
|
|
702
|
+
*/
|
|
703
|
+
resetAIStateOnChannels() {
|
|
704
|
+
for (const channel of this.values()) channel.state.resetAIState();
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Demotes every `Watching` channel to `WasWatching`: the server keys watches by connection ID, so
|
|
709
|
+
* a dropped socket ends them all. Channels stopped on purpose or disconnected stay `NotWatching`.
|
|
710
|
+
* Called when the socket goes unhealthy or offline (`StableWSConnection`, `WSConnectionFallback`)
|
|
711
|
+
* and by `closeConnection()`, whose `disconnect()` writes health through `_applyHealth` and never
|
|
712
|
+
* reaches `_setHealth`.
|
|
713
|
+
*
|
|
714
|
+
* @internal
|
|
715
|
+
*/
|
|
716
|
+
markChannelsWatchInterrupted() {
|
|
717
|
+
for (const channel of this.values()) {
|
|
718
|
+
if (channel.watchStatus === ChannelWatchStatus.Watching) {
|
|
719
|
+
channel.watchStatus = ChannelWatchStatus.WasWatching;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
|
|
470
724
|
get pipelines(): Map<SupportedEventType, EventHandlerPipeline<EventHandlerContext>> {
|
|
471
725
|
return this._pipelines;
|
|
472
726
|
}
|
|
@@ -475,10 +729,7 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
475
729
|
return { channelManager: this };
|
|
476
730
|
}
|
|
477
731
|
|
|
478
|
-
/**
|
|
479
|
-
* Returns deep copy of default handlers mapping.
|
|
480
|
-
* The defaults can be enriched with custom handlers or the custom handlers can be replaced.
|
|
481
|
-
*/
|
|
732
|
+
/** Returns a copy of the default handler mapping, to enrich or replace. */
|
|
482
733
|
static getDefaultHandlers(): ChannelManagerEventHandlers {
|
|
483
734
|
const src = ChannelManager.defaultEventHandlers;
|
|
484
735
|
const out: ChannelManagerEventHandlers = {};
|
|
@@ -490,13 +741,10 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
490
741
|
}
|
|
491
742
|
|
|
492
743
|
/**
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
* Available as a setter because the manager is constructed by the client, before the app has had
|
|
499
|
-
* a chance to create its paginators — their ids are therefore only known later.
|
|
744
|
+
* Sets the rule deciding which paginator(s) own a channel matched by several. Pass paginator IDs
|
|
745
|
+
* (highest priority first) for the built-in priority resolver, a custom resolver, or `undefined`
|
|
746
|
+
* for the default (every matching paginator keeps the channel). A setter because the client
|
|
747
|
+
* builds the manager before the app creates its paginators.
|
|
500
748
|
*/
|
|
501
749
|
setOwnershipResolver(ownershipResolver?: PaginatorOwnershipResolver | string[]) {
|
|
502
750
|
if (!ownershipResolver) {
|
|
@@ -508,10 +756,7 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
508
756
|
: ownershipResolver;
|
|
509
757
|
}
|
|
510
758
|
|
|
511
|
-
/**
|
|
512
|
-
* Which paginators should own the channel among the ones that matched.
|
|
513
|
-
* Default behavior keeps the channel in all matching paginators.
|
|
514
|
-
*/
|
|
759
|
+
/** Ids of the paginators that own the channel among the matches. Empty: all of them. */
|
|
515
760
|
resolveOwnership(
|
|
516
761
|
channel: Channel,
|
|
517
762
|
matchingPaginators: ChannelPaginator[],
|
|
@@ -520,42 +765,36 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
520
765
|
}
|
|
521
766
|
|
|
522
767
|
/**
|
|
523
|
-
*
|
|
524
|
-
*
|
|
525
|
-
*
|
|
526
|
-
*
|
|
527
|
-
*
|
|
528
|
-
*
|
|
529
|
-
* in the list(s) without a full re-query. `ingestItem` dedupes by cid and inserts in sort
|
|
530
|
-
* order, so calling this repeatedly is safe.
|
|
531
|
-
*
|
|
532
|
-
* A channel that matches no paginator is not added anywhere. To have such channels still
|
|
533
|
-
* appear, register a catch-all paginator (empty filter) with the lowest ownership priority as
|
|
534
|
-
* a local fallback list.
|
|
768
|
+
* Routes a channel into the paginator(s) that own it and removes it from any list it no longer
|
|
769
|
+
* belongs to, as for live WS updates. Use it to surface a channel the app just opened (a search
|
|
770
|
+
* result, a new DM) without a re-query; repeated calls are safe. A channel created from members
|
|
771
|
+
* is ignored until its first query gives it an ID. A channel matching no paginator is added
|
|
772
|
+
* nowhere: register a catch-all paginator (empty filter) with the lowest ownership priority as a
|
|
773
|
+
* fallback.
|
|
535
774
|
*/
|
|
536
|
-
ingestChannel(
|
|
775
|
+
ingestChannel(input: Channel) {
|
|
776
|
+
// Not listed until it has its ID (see `ChannelPaginator.ingestItem`).
|
|
777
|
+
if (input.isProvisional) return;
|
|
778
|
+
const channel = this.resolveListedChannel(input);
|
|
779
|
+
if (!channel) return;
|
|
537
780
|
const matchingPaginators = this.paginators.filter((p) => p.matchesFilter(channel));
|
|
538
|
-
const
|
|
781
|
+
const matchingPaginatorIds = new Set(matchingPaginators.map((p) => p.id));
|
|
539
782
|
const ownerIds = this.resolveOwnership(channel, matchingPaginators);
|
|
540
783
|
|
|
541
|
-
this.paginators
|
|
542
|
-
const
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
paginator.removeItem({ item: channel });
|
|
549
|
-
}
|
|
550
|
-
});
|
|
784
|
+
for (const paginator of this.paginators) {
|
|
785
|
+
const isOwner =
|
|
786
|
+
matchingPaginatorIds.has(paginator.id) &&
|
|
787
|
+
(ownerIds.size === 0 || ownerIds.has(paginator.id));
|
|
788
|
+
if (isOwner) paginator.ingestItem(channel);
|
|
789
|
+
else paginator.removeItem({ item: channel });
|
|
790
|
+
}
|
|
551
791
|
}
|
|
552
792
|
|
|
553
793
|
/**
|
|
554
|
-
* Drops a channel from the lists whose filters it no longer matches, adding it nowhere.
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
*
|
|
558
|
-
* a channel belong to a list, and `ingestChannel` would pull unloaded channels into matching ones.
|
|
794
|
+
* Drops a channel from the lists whose filters it no longer matches, adding it nowhere. For state
|
|
795
|
+
* the client changes without an event naming the channel (e.g. zeroing every unread count on a
|
|
796
|
+
* global read). Removal-only: becoming read cannot make a channel belong to a list (the backend
|
|
797
|
+
* rejects `has_unread: false`).
|
|
559
798
|
*/
|
|
560
799
|
dropFromUnmatchedLists(channel: Channel) {
|
|
561
800
|
this.paginators.forEach((paginator) => {
|
|
@@ -564,10 +803,7 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
564
803
|
});
|
|
565
804
|
}
|
|
566
805
|
|
|
567
|
-
/**
|
|
568
|
-
* Filter a page of query results for a specific paginator according to ownership rules.
|
|
569
|
-
* If no owners are specified by the resolver, all matching paginators keep the item.
|
|
570
|
-
*/
|
|
806
|
+
/** Filters a page of query results for `paginator` by ownership (no owners: all matches keep). */
|
|
571
807
|
protected filterItemsByOwnership({
|
|
572
808
|
paginator,
|
|
573
809
|
items,
|
|
@@ -591,26 +827,10 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
591
827
|
}
|
|
592
828
|
|
|
593
829
|
/**
|
|
594
|
-
* Makes a registered paginator apply
|
|
595
|
-
*
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
* - **WS events / `ingestChannel`** — the manager routes the channel and consults
|
|
599
|
-
* `resolveOwnership` before ingesting, so exclusivity holds by construction.
|
|
600
|
-
* - **A server query** — `paginator.next()` (a `ChannelList` scrolling, `reload()`) queries and
|
|
601
|
-
* ingests on its own; the manager is not involved. Without this wrapper a page would land in
|
|
602
|
-
* every list whose *server-side* filter matched it, so a channel matching both
|
|
603
|
-
* `channels:archived` and `channels:default` would show up in both lists until some later event
|
|
604
|
-
* re-routed it — the exact duplication the resolver exists to prevent.
|
|
605
|
-
*
|
|
606
|
-
* Wrapping (rather than subclassing or a constructor hook) is what lets the manager inject this
|
|
607
|
-
* into paginators it does not construct: `filterQueryResults` is an instance property, so the
|
|
608
|
-
* original is captured here and handed back by `unwrapPaginatorFiltering` when the paginator is
|
|
609
|
-
* detached. The paginator's own filtering runs first and stays authoritative — ownership only ever
|
|
610
|
-
* removes further items, never adds any back.
|
|
611
|
-
*
|
|
612
|
-
* Idempotent: the `WeakMap` guard keeps a re-inserted paginator from being wrapped twice, which
|
|
613
|
-
* would filter the same page repeatedly and, worse, lose the reference to the true original.
|
|
830
|
+
* Makes a registered paginator apply the ownership rules to the pages it fetches itself (server
|
|
831
|
+
* queries bypass the manager), by composing `filterItemsByOwnership` onto its
|
|
832
|
+
* `filterQueryResults`. Its own filtering runs first. Idempotent: wrapping twice would lose the
|
|
833
|
+
* true original.
|
|
614
834
|
*/
|
|
615
835
|
protected wrapPaginatorFiltering(paginator: ChannelPaginator) {
|
|
616
836
|
if (this.filterQueryResultsBeforeWrapping.has(paginator)) return;
|
|
@@ -623,10 +843,8 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
623
843
|
}
|
|
624
844
|
|
|
625
845
|
/**
|
|
626
|
-
*
|
|
627
|
-
*
|
|
628
|
-
* rules: those are resolved against the paginators the manager still holds, so a detached
|
|
629
|
-
* paginator would drop channels owned by lists it is no longer part of.
|
|
846
|
+
* Restores the `filterQueryResults` the paginator had before `wrapPaginatorFiltering`: a detached
|
|
847
|
+
* paginator must not keep applying ownership rules resolved against lists it left.
|
|
630
848
|
*/
|
|
631
849
|
protected unwrapPaginatorFiltering(paginator: ChannelPaginator) {
|
|
632
850
|
const original = this.filterQueryResultsBeforeWrapping.get(paginator);
|
|
@@ -640,10 +858,9 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
640
858
|
}
|
|
641
859
|
|
|
642
860
|
/**
|
|
643
|
-
*
|
|
644
|
-
* `removePaginator` build on
|
|
645
|
-
*
|
|
646
|
-
* `removePaginator` detaches them; a repeated id keeps only its first occurrence.
|
|
861
|
+
* Replaces the whole set of lists in one state update (the primitive `insertPaginator` and
|
|
862
|
+
* `removePaginator` build on). Dropped paginators are detached as `removePaginator` does; a
|
|
863
|
+
* repeated ID keeps only its first occurrence.
|
|
647
864
|
*
|
|
648
865
|
* @param paginators - The lists this manager should hold, in render order.
|
|
649
866
|
*/
|
|
@@ -657,8 +874,7 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
657
874
|
}
|
|
658
875
|
|
|
659
876
|
const currentPaginators = this.paginators;
|
|
660
|
-
//
|
|
661
|
-
// `state.paginators` selector shallow-compares by reference, so every list would re-render.
|
|
877
|
+
// An equivalent set would hand subscribers a new array and re-render every list.
|
|
662
878
|
const isUnchanged =
|
|
663
879
|
currentPaginators.length === nextPaginators.length &&
|
|
664
880
|
currentPaginators.every((paginator, i) => paginator === nextPaginators[i]);
|
|
@@ -674,14 +890,11 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
674
890
|
this.unwrapPaginatorFiltering(paginator);
|
|
675
891
|
paginator.cancelScheduledQuery();
|
|
676
892
|
});
|
|
677
|
-
// Wrap the current set to enforce ownership on their query results
|
|
678
893
|
nextPaginators.forEach((paginator) => this.wrapPaginatorFiltering(paginator));
|
|
679
894
|
}
|
|
680
895
|
|
|
681
896
|
/**
|
|
682
|
-
*
|
|
683
|
-
* released exactly as `removePaginator` releases it (ownership-filtering wrapper removed,
|
|
684
|
-
* scheduled query canceled, loaded items kept), in a single state update.
|
|
897
|
+
* Detaches every list as `removePaginator` does, in one update.
|
|
685
898
|
*
|
|
686
899
|
* @returns The paginators the manager held, in the order it held them.
|
|
687
900
|
*/
|
|
@@ -692,13 +905,11 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
692
905
|
}
|
|
693
906
|
|
|
694
907
|
/**
|
|
695
|
-
*
|
|
696
|
-
*
|
|
697
|
-
* If index provided → insert (or move) at that index.
|
|
908
|
+
* Inserts a paginator. One already held with the same ID is removed first, so this moves it, or
|
|
909
|
+
* replaces it when it is another instance.
|
|
698
910
|
*
|
|
699
|
-
* @param params - The insertion parameters.
|
|
700
911
|
* @param params.paginator - The paginator to insert or move.
|
|
701
|
-
* @param params.index - Target index
|
|
912
|
+
* @param params.index - Target index, clamped to the list. Defaults to appending.
|
|
702
913
|
*/
|
|
703
914
|
insertPaginator({ paginator, index }: { paginator: ChannelPaginator; index?: number }) {
|
|
704
915
|
const paginators = [...this.paginators];
|
|
@@ -715,15 +926,11 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
715
926
|
}
|
|
716
927
|
|
|
717
928
|
/**
|
|
718
|
-
*
|
|
719
|
-
*
|
|
720
|
-
*
|
|
929
|
+
* Removes a paginator: it leaves `state.paginators`, stops receiving WS-driven updates and gets
|
|
930
|
+
* its own `filterQueryResults` back. Its loaded items stay, and a query scheduled through
|
|
931
|
+
* `toTailDebounced` or `toHeadDebounced` is cancelled.
|
|
721
932
|
*
|
|
722
|
-
*
|
|
723
|
-
* queried on its own. Any query scheduled through `nextDebounced` is canceled, so a list that was
|
|
724
|
-
* just removed does not fire one more request.
|
|
725
|
-
*
|
|
726
|
-
* @param paginatorOrId - The paginator to remove, or its id.
|
|
933
|
+
* @param paginatorOrId - The paginator to remove, or its ID.
|
|
727
934
|
* @returns The removed paginator, or `undefined` when this manager did not hold it.
|
|
728
935
|
*/
|
|
729
936
|
removePaginator(
|
|
@@ -738,11 +945,8 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
738
945
|
}
|
|
739
946
|
|
|
740
947
|
/**
|
|
741
|
-
*
|
|
742
|
-
*
|
|
743
|
-
* user and become invalid on `disconnectUser`. Each paginator returns to "never queried"
|
|
744
|
-
* (`items: undefined`) with its debounced query canceled. Deliberately not named `resetState` —
|
|
745
|
-
* this manager's own `state` *is* the paginator list, which this must not touch.
|
|
948
|
+
* Discards the loaded data of every registered list but keeps the registrations: each paginator
|
|
949
|
+
* returns to "never queried" (`items: undefined`) with its debounced query cancelled.
|
|
746
950
|
*/
|
|
747
951
|
resetPaginatorStates() {
|
|
748
952
|
this.paginators.forEach((paginator) => {
|
|
@@ -782,7 +986,7 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
782
986
|
handlers.forEach((params) => pipeline.remove(params));
|
|
783
987
|
}
|
|
784
988
|
|
|
785
|
-
/**
|
|
989
|
+
/** Subscribes to client events. The returned function releases this registration. */
|
|
786
990
|
registerSubscriptions(): Unsubscribe {
|
|
787
991
|
if (!this.hasSubscriptions) {
|
|
788
992
|
this.addUnsubscribeFunction(
|
|
@@ -821,33 +1025,20 @@ export class ChannelManager extends WithSubscriptions {
|
|
|
821
1025
|
);
|
|
822
1026
|
|
|
823
1027
|
/**
|
|
824
|
-
* Re-
|
|
825
|
-
*
|
|
826
|
-
*
|
|
827
|
-
*
|
|
828
|
-
*
|
|
829
|
-
*
|
|
830
|
-
*
|
|
831
|
-
*
|
|
832
|
-
* `{ keepPreviousItems: true, reset: 'yes' }` is the non-destructive refresh: `reset` restarts the
|
|
833
|
-
* window at page 1, while `keepPreviousItems` merges the fetched page into the loaded list instead
|
|
834
|
-
* of replacing it and keeps the item index populated — the latter is what stops a concurrent
|
|
835
|
-
* offline-replay ingest from rebuilding the list out of a cleared index. Notably NOT `silent`: the
|
|
836
|
-
* `isLoading` transition it would suppress is the in-flight guard that keeps a second overlapping
|
|
837
|
-
* query from being issued.
|
|
838
|
-
*
|
|
839
|
-
* Distinct from {@link ChannelManager.reload}, which is a destructive `reset` that blanks each list
|
|
840
|
-
* to its loading state.
|
|
841
|
-
*
|
|
842
|
-
* Paginators that were never queried are skipped: they run their own first query when they mount,
|
|
843
|
-
* and querying them here would race it.
|
|
1028
|
+
* Re-runs every loaded list's own first-page query, the channel-list half of connection recovery
|
|
1029
|
+
* ({@link ConnectionRecoveryManager}). The page's channels are re-watched (`queryChannels`
|
|
1030
|
+
* watches by default), those below it come back through `restoreInterruptedWatch`.
|
|
1031
|
+
* Never-queried paginators are skipped: they run their own first query on mount, and querying them
|
|
1032
|
+
* here would race it. Unlike {@link ChannelManager.reload}, it merges the page into the loaded
|
|
1033
|
+
* list (`keepPreviousItems`) instead of blanking it. Its pages are `silent` so paging is held while it
|
|
1034
|
+
* runs and a recovery asked for meanwhile supersedes it (see `ChannelPaginator.recoverLoadedItems`).
|
|
844
1035
|
*/
|
|
845
1036
|
recover = async () =>
|
|
846
1037
|
await Promise.allSettled(
|
|
847
1038
|
this.paginators
|
|
848
1039
|
.filter((paginator) => paginator.isInitialized)
|
|
849
1040
|
.map(async (paginator) => {
|
|
850
|
-
await paginator.
|
|
1041
|
+
await paginator.recoverLoadedItems();
|
|
851
1042
|
}),
|
|
852
1043
|
);
|
|
853
1044
|
}
|