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
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import type { ReadStateResponse, UserResponse } from '../types';
|
|
2
|
-
import { StateStore } from '
|
|
1
|
+
import type { ReadStateResponse, TimestampNS, UserResponse } from '../types';
|
|
2
|
+
import { StateStore } from '@stream-io/state-store';
|
|
3
3
|
import type { Channel } from '../channel';
|
|
4
|
+
import { asTimestampNS } from '../utils/time';
|
|
4
5
|
import { WithSubscriptions } from '../utils/WithSubscriptions';
|
|
5
6
|
|
|
6
7
|
type UserId = string;
|
|
7
8
|
type MessageId = string;
|
|
8
|
-
export type MsgRef = { timestamp:
|
|
9
|
+
export type MsgRef = { timestamp: TimestampNS; msgId: MessageId };
|
|
9
10
|
export type OwnMessageReceiptsTrackerMessageLocator = (
|
|
10
|
-
timestamp:
|
|
11
|
+
timestamp: TimestampNS,
|
|
11
12
|
) => MsgRef | null;
|
|
12
13
|
export type UserProgress = {
|
|
13
14
|
user: UserResponse;
|
|
@@ -16,31 +17,41 @@ export type UserProgress = {
|
|
|
16
17
|
};
|
|
17
18
|
export type MessageReceiptsSnapshot = {
|
|
18
19
|
revision: number;
|
|
20
|
+
/**
|
|
21
|
+
* Users whose read cursor sits on each message: one entry per user, not everyone who has read it.
|
|
22
|
+
* For "who has read X" use {@link MessageReceiptsTracker.readersForMessage}.
|
|
23
|
+
*/
|
|
19
24
|
readersByMessageId: Record<MessageId, UserResponse[]>;
|
|
25
|
+
/** As {@link readersByMessageId}, for delivery. */
|
|
20
26
|
deliveredByMessageId: Record<MessageId, UserResponse[]>;
|
|
27
|
+
/** Furthest delivered ref of any other member, or `null`. A message at or before it is delivered. */
|
|
28
|
+
lastDeliveredRefByOthers: MsgRef | null;
|
|
29
|
+
/** As {@link lastDeliveredRefByOthers}, for reads. */
|
|
30
|
+
lastReadRefByOthers: MsgRef | null;
|
|
21
31
|
};
|
|
22
32
|
export type ReadStoreReconcileMeta = {
|
|
23
33
|
changedUserIds?: string[];
|
|
24
34
|
removedUserIds?: string[];
|
|
25
35
|
};
|
|
26
36
|
type ReadStoreUserState = {
|
|
27
|
-
last_read?:
|
|
37
|
+
last_read?: TimestampNS;
|
|
28
38
|
unread_messages?: number;
|
|
29
39
|
user?: UserResponse;
|
|
30
40
|
first_unread_message_id?: string;
|
|
31
41
|
last_read_message_id?: string;
|
|
32
|
-
last_delivered_at?:
|
|
42
|
+
last_delivered_at?: TimestampNS;
|
|
33
43
|
last_delivered_message_id?: string;
|
|
34
44
|
};
|
|
35
45
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
const MIN_REF: MsgRef = {
|
|
47
|
+
timestamp: asTimestampNS(Number.NEGATIVE_INFINITY),
|
|
48
|
+
msgId: '',
|
|
49
|
+
} as const;
|
|
39
50
|
|
|
40
51
|
const isValidReadState = (
|
|
41
52
|
readState: ReadStoreUserState | undefined,
|
|
42
53
|
): readState is ReadStoreUserState & {
|
|
43
|
-
last_read:
|
|
54
|
+
last_read: TimestampNS;
|
|
44
55
|
user: UserResponse;
|
|
45
56
|
} => !!readState?.user && Number.isFinite(readState.last_read);
|
|
46
57
|
|
|
@@ -58,15 +69,7 @@ const findIndex = <T>(arr: T[], target: MsgRef, keyOf: (x: T) => MsgRef): number
|
|
|
58
69
|
return lo;
|
|
59
70
|
};
|
|
60
71
|
|
|
61
|
-
/**
|
|
62
|
-
* Finds the insertion index after the last equal item. E.g. when array `[a]` exists and `b` is
|
|
63
|
-
* being inserted we want `[a, b]`, not `[b, a]`.
|
|
64
|
-
*
|
|
65
|
-
* @param arr - The sorted array to search.
|
|
66
|
-
* @param target - The reference value to compare against.
|
|
67
|
-
* @param keyOf - Accessor that maps an item to its comparable reference.
|
|
68
|
-
* @returns The insertion index in `arr`.
|
|
69
|
-
*/
|
|
72
|
+
/** Insertion index after the last equal item, so ties keep arrival order. */
|
|
70
73
|
const findUpperIndex = <T>(arr: T[], target: MsgRef, keyOf: (x: T) => MsgRef): number => {
|
|
71
74
|
let lo = 0,
|
|
72
75
|
hi = arr.length;
|
|
@@ -90,7 +93,7 @@ const removeByOldKey = (
|
|
|
90
93
|
oldKey: MsgRef,
|
|
91
94
|
keyOf: (x: UserProgress) => MsgRef,
|
|
92
95
|
) => {
|
|
93
|
-
// Find the plateau for oldKey, scan to match by user
|
|
96
|
+
// Find the plateau for oldKey, scan to match by user ID
|
|
94
97
|
let i = findIndex(arr, oldKey, keyOf);
|
|
95
98
|
while (i < arr.length && compareRefsAsc(keyOf(arr[i]), oldKey) === 0) {
|
|
96
99
|
if (arr[i].user.id === item.user.id) {
|
|
@@ -107,62 +110,13 @@ export type OwnMessageReceiptsTrackerOptions = {
|
|
|
107
110
|
};
|
|
108
111
|
|
|
109
112
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
* How it works
|
|
116
|
-
* ------------
|
|
117
|
-
* - Each user has a compact progress record:
|
|
118
|
-
* - `lastReadRef`: latest message they have **read**
|
|
119
|
-
* - `lastDeliveredRef`: latest message they have **received** (always `>= lastReadRef`)
|
|
120
|
-
* - Internally keeps two arrays sorted **ascending by timestamp**:
|
|
121
|
-
* - `readSorted` (by `lastReadRef`)
|
|
122
|
-
* - `deliveredSorted` (by `lastDeliveredRef`)
|
|
123
|
-
* - Queries like “who read message M?” become a **binary search + suffix slice**.
|
|
124
|
-
*
|
|
125
|
-
* Construction
|
|
126
|
-
* ------------
|
|
127
|
-
* `new MessageReceiptsTracker({ channel, locateMessage? })`
|
|
128
|
-
* - By default, message references are read through `channel.state.findMessageByTimestamp`.
|
|
129
|
-
* - `locateMessage` can override this lookup strategy.
|
|
130
|
-
* If a message cannot be resolved locally, the event is ignored.
|
|
113
|
+
* Tracks other participants' delivery and read progress toward own messages in one timeline
|
|
114
|
+
* (channel or thread). Per user it keeps `lastReadRef` and `lastDeliveredRef` (always
|
|
115
|
+
* `>= lastReadRef`) in two arrays sorted by timestamp, so "who read message M?" is a binary search
|
|
116
|
+
* plus a suffix slice.
|
|
131
117
|
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* - `ingestInitial(rows: ReadStateResponse[])`: Builds initial state from server snapshot.
|
|
135
|
-
* If a user’s `last_read` is ahead of `last_delivered_at`, the tracker enforces
|
|
136
|
-
* the invariant `lastDeliveredRef >= lastReadRef`.
|
|
137
|
-
* - `onMessageRead(user, readAt)`:
|
|
138
|
-
* Advances the user’s read; also bumps delivered to match if needed.
|
|
139
|
-
* - `onMessageDelivered(user, deliveredAt)`:
|
|
140
|
-
* Advances the user’s delivered to `max(currentRead, deliveredAt)`.
|
|
141
|
-
*
|
|
142
|
-
* Queries
|
|
143
|
-
* -------
|
|
144
|
-
* - `readersForMessage(msgRef) : UserResponse[]` → users with `lastReadRef >= msgRef`
|
|
145
|
-
* - `deliveredForMessage(msgRef) : UserResponse[]` → users with `lastDeliveredRef >= msgRef`
|
|
146
|
-
* - `deliveredNotReadForMessage(msgRef): UserResponse[]` → delivered but `lastReadRef < msgRef`
|
|
147
|
-
* - `usersWhoseLastReadIs : UserResponse[]` → users for whom `msgRef` is their *last read* (exact match)
|
|
148
|
-
* - `usersWhoseLastDeliveredIs : UserResponse[]` → users for whom `msgRef` is their *last delivered* (exact match)
|
|
149
|
-
* - `groupUsersByLastReadMessage : Record<MsgId, UserResponse[]> → mapping of messages to their readers
|
|
150
|
-
* - `groupUsersByLastDeliveredMessage : Record<MsgId, UserResponse[]> → mapping of messages to their receivers
|
|
151
|
-
* - `hasUserRead(msgRef, userId) : boolean`
|
|
152
|
-
* - `hasUserDelivered(msgRef, userId) : boolean`
|
|
153
|
-
*
|
|
154
|
-
* Complexity
|
|
155
|
-
* ----------
|
|
156
|
-
* - Update on read/delivered: **O(log U)** (binary search + one splice) per event, where U is count of users stored by tracker.
|
|
157
|
-
* - Query lists: **O(log U + K)** where `K` is the number of returned users (suffix length).
|
|
158
|
-
* - Memory: **O(U)** - tracker’s memory grows linearly with the number of users in the channel/thread and does not depend on the number of messages.
|
|
159
|
-
*
|
|
160
|
-
* Scope & notes
|
|
161
|
-
* -------------
|
|
162
|
-
* - One tracker instance is **scoped to a single timeline**. Instantiate per channel/thread.
|
|
163
|
-
* - Ordering is by **ascending timestamp**; ties are kept stable by inserting at the end of the
|
|
164
|
-
* equal-timestamp plateau (upper-bound insertion), preserving intuitive arrival order.
|
|
165
|
-
* - This tracker models **others’ progress toward own messages**;
|
|
118
|
+
* Messages resolve through `channel.messagePaginator.findItemByTimestamp` unless `locateMessage` is
|
|
119
|
+
* passed. Events for messages that cannot be resolved are ignored.
|
|
166
120
|
*/
|
|
167
121
|
export class MessageReceiptsTracker extends WithSubscriptions {
|
|
168
122
|
private byUser = new Map<UserId, UserProgress>();
|
|
@@ -175,6 +129,8 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
175
129
|
revision: 0,
|
|
176
130
|
readersByMessageId: {},
|
|
177
131
|
deliveredByMessageId: {},
|
|
132
|
+
lastDeliveredRefByOthers: null,
|
|
133
|
+
lastReadRefByOthers: null,
|
|
178
134
|
});
|
|
179
135
|
|
|
180
136
|
constructor({ channel, locateMessage }: OwnMessageReceiptsTrackerOptions) {
|
|
@@ -182,7 +138,7 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
182
138
|
this.channel = channel;
|
|
183
139
|
this.locateMessage =
|
|
184
140
|
locateMessage ??
|
|
185
|
-
((timestamp:
|
|
141
|
+
((timestamp: TimestampNS) => {
|
|
186
142
|
const message = this.channel.messagePaginator.findItemByTimestamp(timestamp);
|
|
187
143
|
return message ? { timestamp, msgId: message.id } : null;
|
|
188
144
|
});
|
|
@@ -193,8 +149,7 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
193
149
|
if (this.hasSubscriptions) return;
|
|
194
150
|
|
|
195
151
|
this.addUnsubscribeFunction(
|
|
196
|
-
//
|
|
197
|
-
// read changes, not on every unrelated channel-state write (typing/members/…).
|
|
152
|
+
// Only the `read` slice, to skip unrelated channel-state writes.
|
|
198
153
|
this.channel.state.subscribeWithSelector(
|
|
199
154
|
(currentState) => ({ read: currentState.read }),
|
|
200
155
|
(next, prev) => {
|
|
@@ -232,7 +187,7 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
232
187
|
return;
|
|
233
188
|
}
|
|
234
189
|
|
|
235
|
-
//
|
|
190
|
+
// After the initial bootstrap, only updates that carry patch metadata are applied.
|
|
236
191
|
if (!meta) return;
|
|
237
192
|
|
|
238
193
|
const removedUserIds = new Set(meta?.removedUserIds ?? []);
|
|
@@ -265,7 +220,7 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
265
220
|
}
|
|
266
221
|
}
|
|
267
222
|
|
|
268
|
-
/**
|
|
223
|
+
/** Replaces the tracker's state with the given server read states. */
|
|
269
224
|
ingestInitial(responses: ReadStateResponse[]) {
|
|
270
225
|
this.byUser.clear();
|
|
271
226
|
this.readSorted = [];
|
|
@@ -301,7 +256,7 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
301
256
|
this.emitSnapshot();
|
|
302
257
|
}
|
|
303
258
|
|
|
304
|
-
/** message.delivered
|
|
259
|
+
/** `message.delivered`: delivery confirmed up to and including the message. */
|
|
305
260
|
onMessageDelivered({
|
|
306
261
|
user,
|
|
307
262
|
deliveredAt,
|
|
@@ -309,7 +264,7 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
309
264
|
}: {
|
|
310
265
|
user: UserResponse;
|
|
311
266
|
/** Unix nanoseconds, as the API sends it. */
|
|
312
|
-
deliveredAt:
|
|
267
|
+
deliveredAt: TimestampNS;
|
|
313
268
|
lastDeliveredMessageId?: string;
|
|
314
269
|
}) {
|
|
315
270
|
const timestamp = deliveredAt;
|
|
@@ -323,7 +278,6 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
323
278
|
compareRefsAsc(msgRef, userProgress.lastReadRef) < 0
|
|
324
279
|
? userProgress.lastReadRef
|
|
325
280
|
: msgRef; // max(read, loc)
|
|
326
|
-
// newly announced delivered is older than or equal what is already registered
|
|
327
281
|
if (compareRefsAsc(newDelivered, userProgress.lastDeliveredRef) <= 0) return;
|
|
328
282
|
|
|
329
283
|
removeByOldKey(
|
|
@@ -337,7 +291,7 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
337
291
|
this.emitSnapshot();
|
|
338
292
|
}
|
|
339
293
|
|
|
340
|
-
/** message.read
|
|
294
|
+
/** `message.read`: read up to and including the message. */
|
|
341
295
|
onMessageRead({
|
|
342
296
|
user,
|
|
343
297
|
readAt,
|
|
@@ -345,7 +299,7 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
345
299
|
}: {
|
|
346
300
|
user: UserResponse;
|
|
347
301
|
/** Unix nanoseconds, as the API sends it. */
|
|
348
|
-
readAt:
|
|
302
|
+
readAt: TimestampNS;
|
|
349
303
|
lastReadMessageId?: string;
|
|
350
304
|
}) {
|
|
351
305
|
const timestamp = readAt;
|
|
@@ -354,10 +308,8 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
354
308
|
: this.locateMessage(timestamp);
|
|
355
309
|
if (!msgRef) return;
|
|
356
310
|
const userProgress = this.ensureUser(user);
|
|
357
|
-
// newly announced read message is older than or equal the already recorded last read message
|
|
358
311
|
if (compareRefsAsc(msgRef, userProgress.lastReadRef) <= 0) return;
|
|
359
312
|
|
|
360
|
-
// move in readSorted
|
|
361
313
|
removeByOldKey(
|
|
362
314
|
this.readSorted,
|
|
363
315
|
userProgress,
|
|
@@ -382,12 +334,7 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
382
334
|
this.emitSnapshot();
|
|
383
335
|
}
|
|
384
336
|
|
|
385
|
-
/** notification.mark_unread
|
|
386
|
-
* Sets lastReadRef to the event’s last_read_* values. Delivery never moves backward.
|
|
387
|
-
* The event is sent only to the user that triggered the action (own user), so we will never adjust read ref
|
|
388
|
-
* for other users - we will not see changes in the UI for other users. However, this implementation does not
|
|
389
|
-
* take into consideration this fact and is ready to handle the mark-unread event for any user.
|
|
390
|
-
*/
|
|
337
|
+
/** `notification.mark_unread`: sets `lastReadRef` from `last_read_*`; delivery never moves back. */
|
|
391
338
|
onNotificationMarkUnread({
|
|
392
339
|
user,
|
|
393
340
|
lastReadAt,
|
|
@@ -395,18 +342,17 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
395
342
|
}: {
|
|
396
343
|
user: UserResponse;
|
|
397
344
|
/** Unix nanoseconds, as the API sends it. */
|
|
398
|
-
lastReadAt?:
|
|
345
|
+
lastReadAt?: TimestampNS;
|
|
399
346
|
lastReadMessageId?: string;
|
|
400
347
|
}) {
|
|
401
348
|
const userProgress = this.ensureUser(user);
|
|
402
349
|
|
|
403
|
-
// `
|
|
350
|
+
// A missing or non-finite `lastReadAt` means "never read".
|
|
404
351
|
const newReadRef: MsgRef =
|
|
405
352
|
lastReadAt != null && Number.isFinite(lastReadAt)
|
|
406
353
|
? { timestamp: lastReadAt, msgId: lastReadMessageId ?? '' }
|
|
407
354
|
: { ...MIN_REF };
|
|
408
355
|
|
|
409
|
-
// If no change, exit early.
|
|
410
356
|
if (
|
|
411
357
|
compareRefsAsc(newReadRef, userProgress.lastReadRef) === 0 &&
|
|
412
358
|
newReadRef.msgId === userProgress.lastReadRef.msgId
|
|
@@ -454,6 +400,21 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
454
400
|
return this.deliveredSorted.slice(pos).map((x) => x.user);
|
|
455
401
|
}
|
|
456
402
|
|
|
403
|
+
/**
|
|
404
|
+
* Whether any other member has this message delivered. Uses the furthest cursor, so it is monotonic
|
|
405
|
+
* across messages.
|
|
406
|
+
*/
|
|
407
|
+
isDeliveredToOthers(msgRef: MsgRef): boolean {
|
|
408
|
+
const ref = this.snapshotStore.getLatestValue().lastDeliveredRefByOthers;
|
|
409
|
+
return !!ref && compareRefsAsc(msgRef, ref) <= 0;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** As {@link isDeliveredToOthers}, for reads. */
|
|
413
|
+
isReadByOthers(msgRef: MsgRef): boolean {
|
|
414
|
+
const ref = this.snapshotStore.getLatestValue().lastReadRefByOthers;
|
|
415
|
+
return !!ref && compareRefsAsc(msgRef, ref) <= 0;
|
|
416
|
+
}
|
|
417
|
+
|
|
457
418
|
/** Users who delivered but have NOT read. */
|
|
458
419
|
deliveredNotReadForMessage(msgRef: MsgRef): UserResponse[] {
|
|
459
420
|
const pos = findIndex(
|
|
@@ -496,8 +457,6 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
496
457
|
return users;
|
|
497
458
|
}
|
|
498
459
|
|
|
499
|
-
// ---- queries: per-user status ----
|
|
500
|
-
|
|
501
460
|
hasUserRead(msgRef: MsgRef, userId: string): boolean {
|
|
502
461
|
const up = this.byUser.get(userId);
|
|
503
462
|
return !!up && compareRefsAsc(up.lastReadRef, msgRef) >= 0;
|
|
@@ -592,7 +551,8 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
592
551
|
) === 0 &&
|
|
593
552
|
existingUserProgress.lastDeliveredRef.msgId ===
|
|
594
553
|
nextUserProgress.lastDeliveredRef.msgId;
|
|
595
|
-
|
|
554
|
+
// by reference: an updated user (a new name or image) arrives as a new object with the same ID
|
|
555
|
+
const hasSameUser = existingUserProgress.user === nextUserProgress.user;
|
|
596
556
|
|
|
597
557
|
if (hasSameReadRef && hasSameDeliveredRef && hasSameUser) {
|
|
598
558
|
return false;
|
|
@@ -622,12 +582,12 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
622
582
|
}
|
|
623
583
|
|
|
624
584
|
private readStateToUserProgress(readState: {
|
|
625
|
-
last_read:
|
|
585
|
+
last_read: TimestampNS;
|
|
626
586
|
unread_messages?: number;
|
|
627
587
|
user: UserResponse;
|
|
628
588
|
first_unread_message_id?: string;
|
|
629
589
|
last_read_message_id?: string;
|
|
630
|
-
last_delivered_at?:
|
|
590
|
+
last_delivered_at?: TimestampNS;
|
|
631
591
|
last_delivered_message_id?: string;
|
|
632
592
|
}): UserProgress {
|
|
633
593
|
const lastReadTimestamp = readState.last_read;
|
|
@@ -677,6 +637,25 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
677
637
|
);
|
|
678
638
|
}
|
|
679
639
|
|
|
640
|
+
/**
|
|
641
|
+
* The furthest ref in an ascending-by-cursor array, ignoring this user's own progress - your own
|
|
642
|
+
* cursor says nothing about whether anyone received the message.
|
|
643
|
+
*/
|
|
644
|
+
private furthestRefByOthers(
|
|
645
|
+
sorted: UserProgress[],
|
|
646
|
+
refOf: (progress: UserProgress) => MsgRef,
|
|
647
|
+
): MsgRef | null {
|
|
648
|
+
const ownUserId = this.channel.getClient().userId;
|
|
649
|
+
|
|
650
|
+
for (let i = sorted.length - 1; i >= 0; i -= 1) {
|
|
651
|
+
if (sorted[i].user.id === ownUserId) continue;
|
|
652
|
+
const ref = refOf(sorted[i]);
|
|
653
|
+
return Number.isFinite(ref.timestamp) ? ref : null;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
return null;
|
|
657
|
+
}
|
|
658
|
+
|
|
680
659
|
private emitSnapshot() {
|
|
681
660
|
const readersByMessageId = this.groupUsersByLastReadMessage();
|
|
682
661
|
const deliveredByMessageId = this.groupUsersByLastDeliveredMessage();
|
|
@@ -686,6 +665,14 @@ export class MessageReceiptsTracker extends WithSubscriptions {
|
|
|
686
665
|
revision: currentSnapshot.revision + 1,
|
|
687
666
|
readersByMessageId,
|
|
688
667
|
deliveredByMessageId,
|
|
668
|
+
lastDeliveredRefByOthers: this.furthestRefByOthers(
|
|
669
|
+
this.deliveredSorted,
|
|
670
|
+
({ lastDeliveredRef }) => lastDeliveredRef,
|
|
671
|
+
),
|
|
672
|
+
lastReadRefByOthers: this.furthestRefByOthers(
|
|
673
|
+
this.readSorted,
|
|
674
|
+
({ lastReadRef }) => lastReadRef,
|
|
675
|
+
),
|
|
689
676
|
});
|
|
690
677
|
}
|
|
691
678
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { localMessageToNewMessagePayload } from '../utils';
|
|
2
|
+
import type { LocalMessage, MessageRequest } from '../types';
|
|
3
|
+
import type { MessageOperationsConfig } from './MessageOperations';
|
|
4
|
+
import type { OperationParams } from './types';
|
|
5
|
+
|
|
6
|
+
type FailedSendCacheEntry = {
|
|
7
|
+
message: MessageRequest;
|
|
8
|
+
options?: OperationParams<'send'>['options'];
|
|
9
|
+
cachedAt: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Payloads of failed sends, kept so a retry sends exactly what failed. Entries expire after
|
|
14
|
+
* `failedSendCacheTtlMs`, and past `failedSendCacheMaxSize` the oldest is dropped. Both limits are read
|
|
15
|
+
* from the owner's config on every call.
|
|
16
|
+
*/
|
|
17
|
+
export class FailedSendCache {
|
|
18
|
+
private readonly entries = new Map<string, FailedSendCacheEntry>();
|
|
19
|
+
|
|
20
|
+
constructor(private readonly getConfig: () => Readonly<MessageOperationsConfig>) {}
|
|
21
|
+
|
|
22
|
+
add({
|
|
23
|
+
message,
|
|
24
|
+
messageId,
|
|
25
|
+
options,
|
|
26
|
+
}: {
|
|
27
|
+
messageId: string;
|
|
28
|
+
message: MessageRequest;
|
|
29
|
+
options?: OperationParams<'send'>['options'];
|
|
30
|
+
}) {
|
|
31
|
+
this.pruneExpired();
|
|
32
|
+
|
|
33
|
+
if (
|
|
34
|
+
!this.entries.has(messageId) &&
|
|
35
|
+
this.entries.size >= this.getConfig().failedSendCacheMaxSize
|
|
36
|
+
) {
|
|
37
|
+
const oldestMessageId = this.entries.keys().next().value;
|
|
38
|
+
if (oldestMessageId) {
|
|
39
|
+
this.clear(oldestMessageId);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.entries.set(messageId, { cachedAt: Date.now(), message, options });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get(messageId: string) {
|
|
47
|
+
const cached = this.entries.get(messageId);
|
|
48
|
+
if (!cached) return;
|
|
49
|
+
|
|
50
|
+
if (Date.now() - cached.cachedAt > this.getConfig().failedSendCacheTtlMs) {
|
|
51
|
+
this.clear(messageId);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return cached;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
clear(messageId: string) {
|
|
59
|
+
this.entries.delete(messageId);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Folds an edit into the cached payload, so a retry resends the message as it is now. */
|
|
63
|
+
rewriteWithEdit(localMessage: LocalMessage) {
|
|
64
|
+
const cached = this.get(localMessage.id);
|
|
65
|
+
if (!cached) return;
|
|
66
|
+
|
|
67
|
+
this.entries.set(localMessage.id, {
|
|
68
|
+
...cached,
|
|
69
|
+
message: {
|
|
70
|
+
...cached.message,
|
|
71
|
+
...localMessageToNewMessagePayload(localMessage),
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private pruneExpired() {
|
|
77
|
+
const now = Date.now();
|
|
78
|
+
|
|
79
|
+
for (const [messageId, entry] of this.entries) {
|
|
80
|
+
if (now - entry.cachedAt > this.getConfig().failedSendCacheTtlMs) {
|
|
81
|
+
this.clear(messageId);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|