stream-chat 10.0.0-rc.9 → 10.0.0
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 +21650 -19790
- package/dist/cjs/index.browser.js.map +4 -4
- package/dist/cjs/index.node.js +21690 -19798
- package/dist/cjs/index.node.js.map +4 -4
- package/dist/esm/index.mjs +21755 -19895
- 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 +237 -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 +513 -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
|
@@ -5,7 +5,6 @@ import type {
|
|
|
5
5
|
EventPayload,
|
|
6
6
|
EventType,
|
|
7
7
|
LocalMessage,
|
|
8
|
-
MessageRequest,
|
|
9
8
|
MessageResponse,
|
|
10
9
|
UserResponse,
|
|
11
10
|
} from '../types';
|
|
@@ -25,12 +24,11 @@ import type { StreamChat } from '../client';
|
|
|
25
24
|
import type { AxiosError } from 'axios';
|
|
26
25
|
import { OfflineDBSyncManager } from './offline_sync_manager';
|
|
27
26
|
import { chatLoggerSystem } from '../logger';
|
|
28
|
-
import { StateStore } from '
|
|
27
|
+
import { StateStore } from '@stream-io/state-store';
|
|
29
28
|
import {
|
|
30
|
-
channelHasReadEvents,
|
|
31
29
|
channelTracksReadLocally,
|
|
32
30
|
formatMessage,
|
|
33
|
-
|
|
31
|
+
isThreadOnlyReply,
|
|
34
32
|
runDetached,
|
|
35
33
|
} from '../utils';
|
|
36
34
|
import { nowNs } from '../utils/time';
|
|
@@ -42,11 +40,10 @@ const logger = chatLoggerSystem.getLogger('offline-db');
|
|
|
42
40
|
import type { WSEvent } from '../gen/models';
|
|
43
41
|
|
|
44
42
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* offline data.
|
|
43
|
+
* Base class for an offline DB. A subclass provides the storage by implementing the abstract
|
|
44
|
+
* {@link OfflineDBApi} members; the event handlers, channel guards and pending-task queue are built
|
|
45
|
+
* on them. A handler takes `execute`, which defaults to `true`: with `false` it returns its
|
|
46
|
+
* queries unexecuted, for one `executeSqlBatch`.
|
|
50
47
|
*
|
|
51
48
|
* @abstract
|
|
52
49
|
*/
|
|
@@ -60,11 +57,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
60
57
|
syncMaxEventCount,
|
|
61
58
|
}: {
|
|
62
59
|
client: StreamChat;
|
|
63
|
-
/**
|
|
64
|
-
* Optional positive cap forwarded to the {@link OfflineDBSyncManager}, which
|
|
65
|
-
* owns sync. `undefined` or any non-positive value means "no limit". See
|
|
66
|
-
* {@link OfflineDBSyncManager.syncMaxEventCount}.
|
|
67
|
-
*/
|
|
60
|
+
/** Forwarded to {@link OfflineDBSyncManager.syncMaxEventCount}. */
|
|
68
61
|
syncMaxEventCount?: number;
|
|
69
62
|
}) {
|
|
70
63
|
this.client = client;
|
|
@@ -79,417 +72,130 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
79
72
|
});
|
|
80
73
|
}
|
|
81
74
|
|
|
82
|
-
/**
|
|
83
|
-
* Inserts a reaction into the DB.
|
|
84
|
-
* Will write to:
|
|
85
|
-
* - The reactions table with the new reaction
|
|
86
|
-
* - The message table with the message containing the new reaction
|
|
87
|
-
* - The users table with any users associated
|
|
88
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
89
|
-
* already executed).
|
|
90
|
-
*
|
|
91
|
-
* @param {DBInsertReactionType} options
|
|
92
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
93
|
-
*/
|
|
75
|
+
/** Writes the reaction, the message carrying it and the associated users. */
|
|
94
76
|
abstract insertReaction: OfflineDBApi['insertReaction'];
|
|
95
77
|
|
|
96
|
-
/**
|
|
97
|
-
* Upserts the list of CIDs for a filter + sort query hash.
|
|
98
|
-
* Will write to only the table containing the cids.
|
|
99
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
100
|
-
* already executed).
|
|
101
|
-
*
|
|
102
|
-
* @param {DBUpsertCidsForQueryType} options
|
|
103
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
104
|
-
*/
|
|
78
|
+
/** Upserts the CIDs for a filter + sort query hash (cids table only). */
|
|
105
79
|
abstract upsertCidsForQuery: OfflineDBApi['upsertCidsForQuery'];
|
|
106
80
|
|
|
107
81
|
/**
|
|
108
|
-
* Upserts the channels
|
|
109
|
-
*
|
|
110
|
-
* tables.
|
|
111
|
-
* Will write to:
|
|
112
|
-
* - The channels table with the channel data
|
|
113
|
-
* - The messages table with each message associated
|
|
114
|
-
* - The reactions table if the messages contain reactions
|
|
115
|
-
* - The users table with all users associated
|
|
116
|
-
* - The members table for membership and members
|
|
117
|
-
* - The polls table for any messages that are polls
|
|
118
|
-
* - The reads table for each user
|
|
119
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
120
|
-
* already executed).
|
|
121
|
-
*
|
|
122
|
-
* @param {DBUpsertChannelsType} options
|
|
123
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
82
|
+
* Upserts the channels and their messages, reactions, users, members, polls (for poll messages)
|
|
83
|
+
* and reads, each kind into its own table.
|
|
124
84
|
*/
|
|
125
85
|
abstract upsertChannels: OfflineDBApi['upsertChannels'];
|
|
126
86
|
|
|
127
|
-
/**
|
|
128
|
-
* Upserts the current active user's sync status.
|
|
129
|
-
* Will only write to the sync status table.
|
|
130
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
131
|
-
* already executed).
|
|
132
|
-
*
|
|
133
|
-
* @param {DBUpsertUserSyncStatusType} options
|
|
134
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
135
|
-
*/
|
|
87
|
+
/** Upserts the current user's sync status (sync status table only). */
|
|
136
88
|
abstract upsertUserSyncStatus: OfflineDBApi['upsertUserSyncStatus'];
|
|
137
89
|
|
|
138
|
-
/**
|
|
139
|
-
* Upserts the app settings for the current Stream App into the DB. It
|
|
140
|
-
* is only intended to be run once per lifecycle of the app.
|
|
141
|
-
* Will only write to the respective app settings table.
|
|
142
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
143
|
-
* already executed).
|
|
144
|
-
*
|
|
145
|
-
* @param {DBUpsertAppSettingsType} options
|
|
146
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
147
|
-
*/
|
|
90
|
+
/** Upserts the app settings (app settings table only). Meant to run once per app lifecycle. */
|
|
148
91
|
abstract upsertAppSettings: OfflineDBApi['upsertAppSettings'];
|
|
149
92
|
|
|
150
|
-
/**
|
|
151
|
-
* Upserts a poll fully in the DB.
|
|
152
|
-
* Will write to the polls table. It should not update the message
|
|
153
|
-
* associated due to how the poll state works.
|
|
154
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
155
|
-
* already executed).
|
|
156
|
-
*
|
|
157
|
-
* @param {DBUpsertPollType} options
|
|
158
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
159
|
-
*/
|
|
93
|
+
/** Upserts a poll (polls table only). It does not update the poll's message. */
|
|
160
94
|
abstract upsertPoll: OfflineDBApi['upsertPoll'];
|
|
161
95
|
|
|
162
|
-
/**
|
|
163
|
-
* Upserts only the channel.data for the provided channels in the DB.
|
|
164
|
-
* Will only write to the channels table.
|
|
165
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
166
|
-
* already executed).
|
|
167
|
-
*
|
|
168
|
-
* @param {DBUpsertChannelDataType} options
|
|
169
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
170
|
-
*/
|
|
96
|
+
/** Upserts only `channel.data` (channels table only). */
|
|
171
97
|
abstract upsertChannelData: OfflineDBApi['upsertChannelData'];
|
|
172
98
|
|
|
173
|
-
/**
|
|
174
|
-
* Upserts the provided reads in the DB.
|
|
175
|
-
* Will write to:
|
|
176
|
-
* - The reads table
|
|
177
|
-
* - The users table for each user associated with a read
|
|
178
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
179
|
-
* already executed).
|
|
180
|
-
*
|
|
181
|
-
* @param {DBUpsertReadsType} options
|
|
182
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
183
|
-
*/
|
|
99
|
+
/** Upserts the reads and the users they reference. */
|
|
184
100
|
abstract upsertReads: OfflineDBApi['upsertReads'];
|
|
185
101
|
|
|
186
|
-
/**
|
|
187
|
-
* Upserts the messages in the DB.
|
|
188
|
-
* Will write to:
|
|
189
|
-
* - The messages table
|
|
190
|
-
* - The reads table
|
|
191
|
-
* - The polls table (if any messages contain polls)
|
|
192
|
-
* - The reactions table (if any messages contain reactions)
|
|
193
|
-
* - The users table
|
|
194
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
195
|
-
* already executed).
|
|
196
|
-
*
|
|
197
|
-
* @param {DBUpsertMessagesType} options
|
|
198
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
199
|
-
*/
|
|
102
|
+
/** Upserts the messages with their reads, polls, reactions and users. */
|
|
200
103
|
abstract upsertMessages: OfflineDBApi['upsertMessages'];
|
|
201
104
|
|
|
202
|
-
/**
|
|
203
|
-
* Upserts the members in the DB.
|
|
204
|
-
* Will write to:
|
|
205
|
-
* - The users table (for each user associated with a member)
|
|
206
|
-
* - The members table
|
|
207
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
208
|
-
* already executed).
|
|
209
|
-
*
|
|
210
|
-
* @param {DBUpsertMembersType} options
|
|
211
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
212
|
-
*/
|
|
105
|
+
/** Upserts the members and their users. */
|
|
213
106
|
abstract upsertMembers: OfflineDBApi['upsertMembers'];
|
|
214
107
|
|
|
215
108
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
* Will write to the reactions table.
|
|
220
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
221
|
-
* already executed).
|
|
222
|
-
*
|
|
223
|
-
* @param {DBUpdateReactionType} options
|
|
224
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
109
|
+
* Applies a reaction as a `reaction.updated` event would: it assumes `enforce_unique` and
|
|
110
|
+
* replaces the user's other reactions. Also writes the reacting user and the message's reaction
|
|
111
|
+
* groups.
|
|
225
112
|
*/
|
|
226
113
|
abstract updateReaction: OfflineDBApi['updateReaction'];
|
|
227
114
|
|
|
228
|
-
/**
|
|
229
|
-
* Updates a single message in the DB. This is used as a faster
|
|
230
|
-
* alternative to upsertMessages with more optimized queries.
|
|
231
|
-
* Will write to the messages table.
|
|
232
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
233
|
-
* already executed).
|
|
234
|
-
*
|
|
235
|
-
* @param {DBUpdateMessageType} options
|
|
236
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
237
|
-
*/
|
|
115
|
+
/** Updates a single message (messages table only) with leaner queries than `upsertMessages`. */
|
|
238
116
|
abstract updateMessage: OfflineDBApi['updateMessage'];
|
|
239
117
|
|
|
240
|
-
/**
|
|
241
|
-
* Fetches the provided draft from the DB. Should return as close to
|
|
242
|
-
* the server side DraftResponse as possible.
|
|
243
|
-
*
|
|
244
|
-
* @param {DBGetDraftType} options
|
|
245
|
-
* @returns {Promise<DraftResponse | null>}
|
|
246
|
-
*/
|
|
118
|
+
/** Returns the draft as close to the server's `DraftResponse` as possible. */
|
|
247
119
|
abstract getDraft: OfflineDBApi['getDraft'];
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
* Will write to the draft table upserting the draft.
|
|
251
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
252
|
-
* already executed).
|
|
253
|
-
*
|
|
254
|
-
* @param {DBUpsertDraftType} options
|
|
255
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
256
|
-
*/
|
|
120
|
+
|
|
121
|
+
/** Upserts a draft (drafts table only). */
|
|
257
122
|
abstract upsertDraft: OfflineDBApi['upsertDraft'];
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
* Will write to the draft table removing the draft.
|
|
261
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
262
|
-
* already executed).
|
|
263
|
-
*
|
|
264
|
-
* @param {DBDeleteDraftType} options
|
|
265
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
266
|
-
*/
|
|
123
|
+
|
|
124
|
+
/** Deletes a draft (drafts table only). */
|
|
267
125
|
abstract deleteDraft: OfflineDBApi['deleteDraft'];
|
|
268
126
|
|
|
269
127
|
/**
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
* for aggregating and serialization of all of the data. Should return as close to
|
|
273
|
-
* the server side ChannelStateResponseFields as possible.
|
|
274
|
-
*
|
|
275
|
-
* @param {DBGetChannelsType} options
|
|
276
|
-
* @returns {Promise<Omit<ChannelStateResponseFields, 'duration'>[] | null>}
|
|
128
|
+
* Returns each channel with all its stored data aggregated, as close to the server's
|
|
129
|
+
* `ChannelStateResponseFields` as possible. The implementation does the aggregation.
|
|
277
130
|
*/
|
|
278
131
|
abstract getChannels: OfflineDBApi['getChannels'];
|
|
279
132
|
|
|
280
133
|
/**
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
* The implementation itself is responsible for aggregating and serialization of all of the data.
|
|
284
|
-
* Should return as close to the server side ChannelStateResponseFields as possible.
|
|
285
|
-
*
|
|
286
|
-
* @param {DBGetChannelsForQueryType} options
|
|
287
|
-
* @returns {Promise<Omit<ChannelStateResponseFields, 'duration'>[] | null>}
|
|
134
|
+
* The channels last cached for a filters + sort hash, in query order, with the predefined filter
|
|
135
|
+
* that produced them. Channels are aggregated as in `getChannels`.
|
|
288
136
|
*/
|
|
289
137
|
abstract getChannelsForQuery: OfflineDBApi['getChannelsForQuery'];
|
|
290
138
|
|
|
291
|
-
/**
|
|
292
|
-
* Will return a list of all available CIDs in the DB. The same can be achieved
|
|
293
|
-
* by fetching all channels, however this is meant to be much faster as a query.
|
|
294
|
-
*
|
|
295
|
-
* @returns {Promise<string[]>}
|
|
296
|
-
*/
|
|
139
|
+
/** All CIDs in the DB. Much cheaper than fetching all channels. */
|
|
297
140
|
abstract getAllChannelCids: OfflineDBApi['getAllChannelCids'];
|
|
298
141
|
|
|
299
|
-
/**
|
|
300
|
-
* Fetches the timestamp of the last sync of the DB.
|
|
301
|
-
*
|
|
302
|
-
* @param {DBGetLastSyncedAtType} options
|
|
303
|
-
* @returns {Promise<string | undefined>}
|
|
304
|
-
*/
|
|
305
142
|
abstract getLastSyncedAt: OfflineDBApi['getLastSyncedAt'];
|
|
306
143
|
|
|
307
|
-
/**
|
|
308
|
-
* Fetches all pending tasks from the DB. It will return them in an
|
|
309
|
-
* ordered fashion by the time they were created.
|
|
310
|
-
*
|
|
311
|
-
* @param {DBGetPendingTasksType} [conditions]
|
|
312
|
-
* @returns {Promise<PendingTask[]>}
|
|
313
|
-
*/
|
|
144
|
+
/** Pending tasks ordered by creation time, optionally only one message's. */
|
|
314
145
|
abstract getPendingTasks: OfflineDBApi['getPendingTasks'];
|
|
315
146
|
|
|
316
|
-
/**
|
|
317
|
-
* Fetches the app settings stored in the DB. Is mainly meant to be used
|
|
318
|
-
* only while offline and opening the application, as we only update the
|
|
319
|
-
* app settings whenever they are fetched again so it has the potential to
|
|
320
|
-
* be stale.
|
|
321
|
-
*
|
|
322
|
-
* @param {DBGetAppSettingsType} options
|
|
323
|
-
* @returns {Promise<GetApplicationResponse | null>}
|
|
324
|
-
*/
|
|
147
|
+
/** The stored app settings. They only refresh when fetched again, so they can be stale. */
|
|
325
148
|
abstract getAppSettings: OfflineDBApi['getAppSettings'];
|
|
326
149
|
|
|
327
|
-
/**
|
|
328
|
-
* Fetches reactions from the DB for a given filter & sort hash and
|
|
329
|
-
* for a given message ID.
|
|
330
|
-
*
|
|
331
|
-
* @param {DBGetReactionsType} options
|
|
332
|
-
* @returns {Promise<ReactionResponse[] | null>}
|
|
333
|
-
*/
|
|
334
150
|
abstract getReactions: OfflineDBApi['getReactions'];
|
|
335
151
|
|
|
336
|
-
/**
|
|
337
|
-
* Executes multiple queries in a batched fashion. It will also be done
|
|
338
|
-
* within a transaction.
|
|
339
|
-
*
|
|
340
|
-
* @param {ExecuteBatchDBQueriesType} queries
|
|
341
|
-
* @returns {Promise<unknown>}
|
|
342
|
-
*/
|
|
152
|
+
/** Runs the queries in one transaction. */
|
|
343
153
|
abstract executeSqlBatch: OfflineDBApi['executeSqlBatch'];
|
|
344
154
|
|
|
345
|
-
/**
|
|
346
|
-
* Adds a pending task to the pending tasks table. Can only be one of the
|
|
347
|
-
* supported types of pending tasks, otherwise its execution will throw.
|
|
348
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
349
|
-
* already executed).
|
|
350
|
-
*
|
|
351
|
-
* @param {PendingTask} task
|
|
352
|
-
* @returns {Promise<() => Promise<void>>}
|
|
353
|
-
*/
|
|
155
|
+
/** Adds a pending task. Executing a task of an unsupported type throws. */
|
|
354
156
|
abstract addPendingTask: OfflineDBApi['addPendingTask'];
|
|
355
157
|
|
|
356
|
-
/**
|
|
357
|
-
* Updates a pending task in the DB, given its ID.
|
|
358
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
359
|
-
* already executed).
|
|
360
|
-
*
|
|
361
|
-
* @param {DBUpdatePendingTaskType} options
|
|
362
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
363
|
-
*/
|
|
364
158
|
abstract updatePendingTask: OfflineDBApi['updatePendingTask'];
|
|
365
159
|
|
|
366
|
-
/**
|
|
367
|
-
* Deletes a pending task from the DB, given its ID.
|
|
368
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
369
|
-
* already executed).
|
|
370
|
-
*
|
|
371
|
-
* @param {DBDeletePendingTaskType} options
|
|
372
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
373
|
-
*/
|
|
374
160
|
abstract deletePendingTask: OfflineDBApi['deletePendingTask'];
|
|
375
161
|
|
|
376
|
-
/**
|
|
377
|
-
* Deletes a reaction from the DB.
|
|
378
|
-
* Will write to the reactions table removing the reaction.
|
|
379
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
380
|
-
* already executed).
|
|
381
|
-
*
|
|
382
|
-
* @param {DBDeleteReactionType} options
|
|
383
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
384
|
-
*/
|
|
385
162
|
abstract deleteReaction: OfflineDBApi['deleteReaction'];
|
|
386
163
|
|
|
387
|
-
/**
|
|
388
|
-
* Deletes a member from the DB.
|
|
389
|
-
* Will only write to the members table.
|
|
390
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
391
|
-
* already executed).
|
|
392
|
-
*
|
|
393
|
-
* @param {DBDeleteMemberType} options
|
|
394
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
395
|
-
*/
|
|
396
164
|
abstract deleteMember: OfflineDBApi['deleteMember'];
|
|
397
165
|
|
|
398
|
-
/**
|
|
399
|
-
* Deletes a channel from the DB.
|
|
400
|
-
* It will also delete all other entities associated with the channel in
|
|
401
|
-
* a cascading fashion (messages, reactions, members etc.).
|
|
402
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
403
|
-
* already executed).
|
|
404
|
-
*
|
|
405
|
-
* @param {DBDeleteChannelType} options
|
|
406
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
407
|
-
*/
|
|
166
|
+
/** Deletes the channel and everything associated with it (messages, reactions, members, ...). */
|
|
408
167
|
abstract deleteChannel: OfflineDBApi['deleteChannel'];
|
|
409
168
|
|
|
410
|
-
/**
|
|
411
|
-
* Deletes multiple messages for a given channel. Works as `channel.truncated` would.
|
|
412
|
-
* Should remove entities primarily from the messages table and then from all associated
|
|
413
|
-
* tables in a cascading fashion (reactions, polls etc.).
|
|
414
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
415
|
-
* already executed).
|
|
416
|
-
*
|
|
417
|
-
* @param {DBDeleteMessagesForChannelType} options
|
|
418
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
419
|
-
*/
|
|
169
|
+
/** Deletes a channel's messages as `channel.truncated` would, cascading to their reactions. */
|
|
420
170
|
abstract deleteMessagesForChannel: OfflineDBApi['deleteMessagesForChannel'];
|
|
421
171
|
|
|
422
|
-
/**
|
|
423
|
-
* Deletes all pending tasks from the DB.
|
|
424
|
-
* Will only update the pending tasks table.
|
|
425
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
426
|
-
* already executed).
|
|
427
|
-
*
|
|
428
|
-
* @param {DBDropPendingTasksType} options
|
|
429
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
430
|
-
*/
|
|
172
|
+
/** Deletes all pending tasks of a message (pending tasks table only). */
|
|
431
173
|
abstract dropPendingTasks: OfflineDBApi['dropPendingTasks'];
|
|
432
174
|
|
|
433
|
-
/**
|
|
434
|
-
* Deletes a message from the DB.
|
|
435
|
-
* All other entities associated with the message will also be deleted
|
|
436
|
-
* in a cascading fashion (reactions, polls etc.).
|
|
437
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
438
|
-
* already executed).
|
|
439
|
-
*
|
|
440
|
-
* @param {DBDeleteMessageType} options
|
|
441
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
442
|
-
*/
|
|
175
|
+
/** Deletes the message and cascades to its reactions, polls, etc. */
|
|
443
176
|
abstract hardDeleteMessage: OfflineDBApi['hardDeleteMessage'];
|
|
444
177
|
|
|
445
|
-
/**
|
|
446
|
-
* Updates a message with a deleted_at value in the DB.
|
|
447
|
-
* Will only update the messages table, as the message is simply marked
|
|
448
|
-
* as deleted and not removed from the DB.
|
|
449
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
450
|
-
* already executed).
|
|
451
|
-
*
|
|
452
|
-
* @param {DBDeleteMessageType} options
|
|
453
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
454
|
-
*/
|
|
178
|
+
/** Sets `deleted_at` on the message (messages table only) instead of removing it. */
|
|
455
179
|
abstract softDeleteMessage: OfflineDBApi['softDeleteMessage'];
|
|
456
180
|
|
|
457
|
-
/**
|
|
458
|
-
* Drops all tables and reinitializes the connection to the DB.
|
|
459
|
-
*
|
|
460
|
-
* @returns {Promise<unknown>}
|
|
461
|
-
*/
|
|
181
|
+
/** Drops all tables and reinitializes the DB connection. */
|
|
462
182
|
abstract resetDB: OfflineDBApi['resetDB'];
|
|
463
183
|
|
|
464
|
-
/**
|
|
465
|
-
* A utility query that checks whether a specific channel exists in the DB.
|
|
466
|
-
* Technically the same as actually fetching that channel through other queries,
|
|
467
|
-
* but much faster.
|
|
468
|
-
*
|
|
469
|
-
* @param {DBChannelExistsType} options
|
|
470
|
-
* @returns {Promise<boolean>}
|
|
471
|
-
*/
|
|
184
|
+
/** Whether the channel is in the DB. Much cheaper than fetching it. */
|
|
472
185
|
abstract channelExists: OfflineDBApi['channelExists'];
|
|
473
186
|
|
|
474
|
-
/**
|
|
475
|
-
* Initializes the DB (typically creating a simple file handle as a connection pointer for
|
|
476
|
-
* SQLite and likely similar for other DBs).
|
|
477
|
-
*
|
|
478
|
-
* @returns {Promise<boolean>}
|
|
479
|
-
*/
|
|
187
|
+
/** Opens the DB connection (for SQLite, typically a file handle). */
|
|
480
188
|
abstract initializeDB: OfflineDBApi['initializeDB'];
|
|
481
189
|
|
|
482
190
|
/**
|
|
483
|
-
* Initializes the DB
|
|
484
|
-
* It will update the DBs reactive state with initialization values.
|
|
191
|
+
* Initializes the DB and its sync manager for a user, and updates `state`.
|
|
485
192
|
*
|
|
486
|
-
* @param userId -
|
|
193
|
+
* @param userId - The user ID for which we want to initialize.
|
|
487
194
|
*/
|
|
488
195
|
public init = async (userId: string) => {
|
|
489
196
|
try {
|
|
490
197
|
if (!this.shouldInitialize(userId)) {
|
|
491
|
-
//
|
|
492
|
-
// This will change in the next major release.
|
|
198
|
+
// `initializeDB` has to resolve to a boolean: changing it is a breaking change.
|
|
493
199
|
const initialized = await this.initializeDB();
|
|
494
200
|
if (initialized) {
|
|
495
201
|
await this.syncManager.init();
|
|
@@ -507,9 +213,10 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
507
213
|
};
|
|
508
214
|
|
|
509
215
|
/**
|
|
510
|
-
* Checks whether the DB
|
|
216
|
+
* Checks whether the DB is already initialized for a user.
|
|
511
217
|
*
|
|
512
|
-
* @param userId -
|
|
218
|
+
* @param userId - The user ID for which we want to check initialization.
|
|
219
|
+
* @returns Whether the DB is initialized for `userId`.
|
|
513
220
|
*/
|
|
514
221
|
public shouldInitialize(userId: string): boolean {
|
|
515
222
|
const { userId: userIdFromState, initialized } = this.state.getLatestValue();
|
|
@@ -517,14 +224,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
517
224
|
}
|
|
518
225
|
|
|
519
226
|
/**
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
* and silently. Only really meant to be used for write queries that need to
|
|
523
|
-
* be run in synchronous functions.
|
|
227
|
+
* Runs a write query detached, for synchronous callers. A failure is logged, not thrown. No-op
|
|
228
|
+
* until the DB is initialized.
|
|
524
229
|
*
|
|
525
|
-
* @param queryCallback -
|
|
526
|
-
* @param method -
|
|
527
|
-
* is logged on failure)
|
|
230
|
+
* @param queryCallback - A callback wrapping all query logic that is to be executed.
|
|
231
|
+
* @param options.method - The method name logged on failure.
|
|
528
232
|
*/
|
|
529
233
|
public executeQuerySafely = <T>(
|
|
530
234
|
queryCallback: (db: AbstractOfflineDB) => Promise<T>,
|
|
@@ -538,23 +242,17 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
538
242
|
};
|
|
539
243
|
|
|
540
244
|
/**
|
|
541
|
-
*
|
|
542
|
-
*
|
|
543
|
-
*
|
|
544
|
-
*
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
*
|
|
550
|
-
*
|
|
551
|
-
*
|
|
552
|
-
*
|
|
553
|
-
* @param event - The WS event we are trying to process.
|
|
554
|
-
* @param event.execute - Whether to immediately execute the operation (optional, defaults to `true`).
|
|
555
|
-
* @param event.forceUpdate - Whether to upsert the channel data anyway (optional, defaults to `false`).
|
|
556
|
-
* @param createQueries - A callback that creates the queries to execute.
|
|
557
|
-
* @returns The list of prepared queries (executed when `execute` is `true`).
|
|
245
|
+
* Runs `createQueries` so they cannot fail on the channel's foreign key. When the channel is
|
|
246
|
+
* not in the DB it is upserted first, from the event's channel or else the one in state. If
|
|
247
|
+
* neither exists the queries are dropped.
|
|
248
|
+
*
|
|
249
|
+
* @param options.event - The WS event we are trying to process.
|
|
250
|
+
* @param options.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
251
|
+
* @param options.forceUpdate - Whether to upsert the channel data even when it is stored.
|
|
252
|
+
* Defaults to `false`.
|
|
253
|
+
* @param createQueries - Builds the queries. It receives the effective `execute` flag and must run
|
|
254
|
+
* its queries when that is `true`, because the fast path relies on that write throwing.
|
|
255
|
+
* @returns The prepared queries.
|
|
558
256
|
*/
|
|
559
257
|
public queriesWithChannelGuard = async (
|
|
560
258
|
{
|
|
@@ -578,24 +276,14 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
578
276
|
return await createQueries(execute);
|
|
579
277
|
}
|
|
580
278
|
|
|
581
|
-
// Fast path:
|
|
582
|
-
//
|
|
583
|
-
//
|
|
584
|
-
// upsert, so re-running them behind the guard is idempotent.
|
|
585
|
-
//
|
|
586
|
-
// Worth the inversion because this runs on EVERY live WS event: the probe it replaces is a native
|
|
587
|
-
// round-trip, which on device costs about as much as the write it was protecting.
|
|
588
|
-
//
|
|
589
|
-
// Only available when we are the ones executing. An `execute: false` caller is collecting queries
|
|
590
|
-
// for someone else's batch, so there is no failure here for us to catch and the channel row has to
|
|
591
|
-
// be secured up front. `forceUpdate` skips it too — that caller wants the channel data written
|
|
592
|
-
// regardless of whether a row already exists.
|
|
279
|
+
// Fast path: write first and probe for the channel only when the write fails (this runs on
|
|
280
|
+
// every live event and the probe is a native round-trip). Safe because the queries are upserts.
|
|
281
|
+
// Not available to `execute: false` (nothing to catch) or `forceUpdate` (always writes it).
|
|
593
282
|
if (execute && !forceUpdate) {
|
|
594
283
|
try {
|
|
595
284
|
return await createQueries(true);
|
|
596
285
|
} catch (error) {
|
|
597
|
-
// A missing channel row is the only failure this guard
|
|
598
|
-
// error and must not be retried.
|
|
286
|
+
// A missing channel row is the only failure this guard repairs.
|
|
599
287
|
if (await this.channelExists({ cid })) throw error;
|
|
600
288
|
}
|
|
601
289
|
return await this.queriesBehindChannelGuard({
|
|
@@ -613,11 +301,8 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
613
301
|
};
|
|
614
302
|
|
|
615
303
|
/**
|
|
616
|
-
* Prepends the channel row to `createQueries`' output
|
|
617
|
-
*
|
|
618
|
-
*
|
|
619
|
-
* Skips the write entirely when neither can supply it: without a channel row the queries would fail
|
|
620
|
-
* anyway, and dropping the event is what the caller expects.
|
|
304
|
+
* Prepends the channel row to `createQueries`' output, sourced from the event or else the
|
|
305
|
+
* in-memory channel. Returns no queries when neither can supply it.
|
|
621
306
|
*/
|
|
622
307
|
private queriesBehindChannelGuard = async ({
|
|
623
308
|
createQueries,
|
|
@@ -637,11 +322,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
637
322
|
|
|
638
323
|
let channelData = channelFromEvent;
|
|
639
324
|
if (!channelData && event_.channel_type && event_.channel_id) {
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
event_.channel_id
|
|
325
|
+
// `get`, not `ensure`: `ensure` would store an empty channel when none is loaded.
|
|
326
|
+
const channelFromState = this.client.channelManager.get(
|
|
327
|
+
`${event_.channel_type}:${event_.channel_id}`,
|
|
643
328
|
);
|
|
644
|
-
if (channelFromState
|
|
329
|
+
if (channelFromState?.initialized && !channelFromState.pendingDisposal) {
|
|
645
330
|
channelData = channelFromState.data as unknown as ChannelResponse;
|
|
646
331
|
}
|
|
647
332
|
}
|
|
@@ -680,13 +365,13 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
680
365
|
};
|
|
681
366
|
|
|
682
367
|
/**
|
|
683
|
-
*
|
|
684
|
-
*
|
|
685
|
-
*
|
|
686
|
-
* the number of unreads.
|
|
368
|
+
* Upserts the message and, when another user sent it, the current user's read (from
|
|
369
|
+
* `channel.state`). The channel guard upserts a missing channel row only with `execute: false`;
|
|
370
|
+
* with `execute: true` (the default) the row is not guarded.
|
|
687
371
|
*
|
|
688
|
-
* @param payload.event -
|
|
689
|
-
* @param payload.execute - Whether to immediately execute the
|
|
372
|
+
* @param payload.event - The WS event we are trying to process.
|
|
373
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
374
|
+
* @returns The prepared queries.
|
|
690
375
|
*/
|
|
691
376
|
public handleNewMessage = async ({
|
|
692
377
|
event,
|
|
@@ -698,27 +383,25 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
698
383
|
const client = this.client;
|
|
699
384
|
const { cid, message, user } = event;
|
|
700
385
|
|
|
701
|
-
if (!message || (message
|
|
386
|
+
if (!message || isThreadOnlyReply(message)) {
|
|
702
387
|
return [];
|
|
703
388
|
}
|
|
704
389
|
|
|
705
|
-
|
|
390
|
+
return await this.queriesWithChannelGuard(
|
|
706
391
|
{ event, execute },
|
|
707
|
-
async () => {
|
|
392
|
+
async (executeOverride) => {
|
|
708
393
|
let queries = await this.upsertMessages({
|
|
709
394
|
execute: false,
|
|
710
395
|
messages: [message],
|
|
711
396
|
});
|
|
712
397
|
if (cid && client.user && client.user.id !== user?.id) {
|
|
713
398
|
const userId = client.user.id;
|
|
714
|
-
const channel = client.
|
|
715
|
-
//
|
|
716
|
-
//
|
|
717
|
-
//
|
|
718
|
-
// a read for those, so state.read[userId] may be absent here; fall back accordingly and rely
|
|
719
|
-
// on countUnread() (the aggregate the local count maintains) for the value.
|
|
399
|
+
const channel = client.channelManager.get(cid);
|
|
400
|
+
// Persists the read state for channels that track reads server-side, and for
|
|
401
|
+
// read-events-disabled channels with a local unread count (their `state.read[userId]`
|
|
402
|
+
// can be absent).
|
|
720
403
|
const tracksReadLocally = channelTracksReadLocally(channel);
|
|
721
|
-
if (channel && (
|
|
404
|
+
if (channel && (channel.readEventsEnabled || tracksReadLocally)) {
|
|
722
405
|
const ownReads = channel.state.read[userId];
|
|
723
406
|
const unreadCount = channel.countUnread();
|
|
724
407
|
const upsertReadsQueries = await this.upsertReads({
|
|
@@ -729,11 +412,9 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
729
412
|
last_read: ownReads?.last_read ?? 0,
|
|
730
413
|
last_read_message_id: ownReads?.last_read_message_id,
|
|
731
414
|
unread_messages: unreadCount,
|
|
732
|
-
// `client.user` is `ClientUser` (
|
|
733
|
-
//
|
|
734
|
-
//
|
|
735
|
-
// omits it when nothing is blocked, while `UserResponse` always carries it
|
|
736
|
-
// (the server sends `[]` for other users).
|
|
415
|
+
// `client.user` is `ClientUser` (all optional but `id`), so the cast holds only
|
|
416
|
+
// after connect. `blocked_user_ids` is supplied because the connect payload
|
|
417
|
+
// omits it when nothing is blocked.
|
|
737
418
|
user: {
|
|
738
419
|
...client.user,
|
|
739
420
|
blocked_user_ids: client.user.blocked_user_ids ?? [],
|
|
@@ -744,23 +425,21 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
744
425
|
queries = [...queries, ...upsertReadsQueries];
|
|
745
426
|
}
|
|
746
427
|
}
|
|
428
|
+
// Executed here so the guard's write-first path can catch a missing channel row.
|
|
429
|
+
if (executeOverride) {
|
|
430
|
+
await this.executeSqlBatch(queries);
|
|
431
|
+
}
|
|
747
432
|
return queries;
|
|
748
433
|
},
|
|
749
434
|
);
|
|
750
|
-
|
|
751
|
-
if (execute) {
|
|
752
|
-
await this.executeSqlBatch(finalQueries);
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
return finalQueries;
|
|
756
435
|
};
|
|
757
436
|
|
|
758
437
|
/**
|
|
759
|
-
*
|
|
760
|
-
* it should hard delete or soft delete the message.
|
|
438
|
+
* Hard or soft deletes the message, depending on `event.hard_delete`.
|
|
761
439
|
*
|
|
762
|
-
* @param payload.event -
|
|
763
|
-
* @param payload.execute - Whether to immediately execute the
|
|
440
|
+
* @param payload.event - The WS event we are trying to process.
|
|
441
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
442
|
+
* @returns The prepared queries.
|
|
764
443
|
*/
|
|
765
444
|
public handleDeleteMessage = async ({
|
|
766
445
|
event,
|
|
@@ -788,12 +467,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
788
467
|
};
|
|
789
468
|
|
|
790
469
|
/**
|
|
791
|
-
*
|
|
792
|
-
* state as well as the DB. We want to drop all pending tasks and finally hard
|
|
793
|
-
* delete the message from the DB.
|
|
470
|
+
* Drops the message's pending tasks and hard deletes it.
|
|
794
471
|
*
|
|
795
472
|
* @param payload.messageId - The ID of the message we want to remove.
|
|
796
|
-
* @param payload.execute - Whether to immediately execute the
|
|
473
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
474
|
+
* @returns The prepared queries.
|
|
797
475
|
*/
|
|
798
476
|
public handleRemoveMessage = async ({
|
|
799
477
|
messageId,
|
|
@@ -820,13 +498,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
820
498
|
};
|
|
821
499
|
|
|
822
500
|
/**
|
|
823
|
-
* Hard
|
|
824
|
-
* {@link hardDeleteMessage} calls: collects each delete's queries (`execute: false`) and runs them
|
|
825
|
-
* as one batch. Used e.g. by destructive reconciliation on reconnect to mirror a set of in-memory
|
|
826
|
-
* removals into the DB. No-op for an empty set.
|
|
501
|
+
* Hard deletes the messages in one transaction.
|
|
827
502
|
*
|
|
828
|
-
* @param payload.ids - The
|
|
829
|
-
* @param payload.execute - Whether to immediately execute the
|
|
503
|
+
* @param payload.ids - The IDs of the messages to hard delete.
|
|
504
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
505
|
+
* @returns The prepared queries.
|
|
830
506
|
*/
|
|
831
507
|
public hardDeleteMessages = async ({
|
|
832
508
|
ids,
|
|
@@ -848,20 +524,14 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
848
524
|
};
|
|
849
525
|
|
|
850
526
|
/**
|
|
851
|
-
*
|
|
852
|
-
*
|
|
853
|
-
*
|
|
854
|
-
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
857
|
-
*
|
|
858
|
-
* The
|
|
859
|
-
* message in a freshly created channel can reach here before that channel has ever been persisted.
|
|
860
|
-
* `upsertMessages` alone is used rather than `updateMessage` because `updateMessage` is an
|
|
861
|
-
* UPDATE — it no-ops when the row does not exist yet, which is exactly the write-ahead case.
|
|
862
|
-
*
|
|
863
|
-
* @param payload.message - The message to persist. Its `cid` decides which channel row is guarded.
|
|
864
|
-
* @param payload.execute - Whether to immediately execute the operation (optional, defaults to `true`).
|
|
527
|
+
* Upserts a message and writes its channel row when that row is missing (the first optimistic
|
|
528
|
+
* write in a new channel can precede the channel's persistence). Upserts because an UPDATE no-ops
|
|
529
|
+
* on a missing row. When the row is missing and the channel is not loaded in memory, or the
|
|
530
|
+
* message has no `cid`, nothing is written.
|
|
531
|
+
*
|
|
532
|
+
* @param payload.message - The message to persist. Its `cid` decides the guarded channel row.
|
|
533
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
534
|
+
* @returns The prepared queries.
|
|
865
535
|
*/
|
|
866
536
|
public upsertMessageWithChannelGuard = async ({
|
|
867
537
|
message,
|
|
@@ -875,9 +545,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
875
545
|
|
|
876
546
|
const messages = [message] as unknown as MessageResponse[];
|
|
877
547
|
|
|
878
|
-
//
|
|
879
|
-
// row when it actually fails. This runs on every optimistic message write, and the probe it
|
|
880
|
-
// replaces is a native round-trip costing a meaningful fraction of the write itself.
|
|
548
|
+
// Write first and probe for the channel only on failure, as in `queriesWithChannelGuard`.
|
|
881
549
|
if (execute) {
|
|
882
550
|
try {
|
|
883
551
|
return await this.upsertMessages({ messages, execute: true });
|
|
@@ -894,11 +562,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
894
562
|
return await this.upsertMessageBehindChannelGuard({ cid, messages, execute });
|
|
895
563
|
};
|
|
896
564
|
|
|
897
|
-
/**
|
|
898
|
-
* Writes the channel row ahead of the messages so the insert cannot fail on the `cid` foreign key.
|
|
899
|
-
* Skips the write when there is nothing to build a channel row from — the message stays correct in
|
|
900
|
-
* memory either way, and an insert that cannot resolve its foreign key is not worth attempting.
|
|
901
|
-
*/
|
|
565
|
+
/** Writes the channel row ahead of the messages. Skips the write when no row can be built. */
|
|
902
566
|
private upsertMessageBehindChannelGuard = async ({
|
|
903
567
|
cid,
|
|
904
568
|
execute,
|
|
@@ -908,7 +572,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
908
572
|
execute: boolean;
|
|
909
573
|
messages: MessageResponse[];
|
|
910
574
|
}): Promise<ExecuteBatchDBQueriesType> => {
|
|
911
|
-
const channel = this.client.
|
|
575
|
+
const channel = this.client.channelManager.get(cid);
|
|
912
576
|
if (!channel?.initialized || channel.pendingDisposal) {
|
|
913
577
|
logger
|
|
914
578
|
.withExtraTags('upsertMessageWithChannelGuard', cid)
|
|
@@ -932,18 +596,12 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
932
596
|
};
|
|
933
597
|
|
|
934
598
|
/**
|
|
935
|
-
* The
|
|
936
|
-
*
|
|
937
|
-
*
|
|
938
|
-
* Concrete, composed from the existing {@link getChannels} primitive, so it adds nothing to
|
|
939
|
-
* {@link OfflineDBApi} that a custom offline DB would have to implement.
|
|
940
|
-
*
|
|
941
|
-
* This is the buffer that lets a failed message survive. v9 recovered such messages by diffing a
|
|
942
|
-
* second, lagging copy of the message list (the React SDK state) against `channel.state`; with a
|
|
943
|
-
* single reactive source of truth the persisted row is the only place that memory can be
|
|
944
|
-
* re-populated from — see `Channel.reload`.
|
|
599
|
+
* The channel's persisted messages with a local `failed` status, which `Channel.reload` and
|
|
600
|
+
* `Thread.reload` re-ingest. Built on {@link getChannels}, so a custom offline DB implements
|
|
601
|
+
* nothing extra.
|
|
945
602
|
*
|
|
946
603
|
* @param payload.cid - The channel whose failed messages to read.
|
|
604
|
+
* @returns The failed messages, formatted.
|
|
947
605
|
*/
|
|
948
606
|
public getFailedMessages = async ({
|
|
949
607
|
cid,
|
|
@@ -962,15 +620,12 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
962
620
|
};
|
|
963
621
|
|
|
964
622
|
/**
|
|
965
|
-
*
|
|
966
|
-
* present in the event, or optionally rely on the hard override in unreadMessages.
|
|
967
|
-
* The unreadMessages argument is useful for cases where we know the exact number of unreads
|
|
968
|
-
* (for example reading an entire channel), but `unread_messages` might not necessarily exist
|
|
969
|
-
* in the event (or it exists with a stale value if we know what we want to ultimately update to).
|
|
623
|
+
* Upserts the reads of a read event.
|
|
970
624
|
*
|
|
971
625
|
* @param payload.event - The WS event we are trying to process.
|
|
972
|
-
* @param payload.unreadMessages - An override of `unread_messages
|
|
973
|
-
* @param payload.execute - Whether to immediately execute the
|
|
626
|
+
* @param payload.unreadMessages - An override of `event.unread_messages`.
|
|
627
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
628
|
+
* @returns The prepared queries.
|
|
974
629
|
*/
|
|
975
630
|
public handleRead = async ({
|
|
976
631
|
event,
|
|
@@ -987,13 +642,20 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
987
642
|
execute?: boolean;
|
|
988
643
|
}) => {
|
|
989
644
|
const {
|
|
990
|
-
received_at: last_read = nowNs(),
|
|
991
645
|
last_read_message_id,
|
|
992
646
|
// @ts-expect-error property missing
|
|
993
647
|
unread_messages = 0,
|
|
994
648
|
user,
|
|
995
649
|
cid,
|
|
996
650
|
} = event;
|
|
651
|
+
// Mirrors what `Channel` writes into `state.read`: mark-unread uses the server's
|
|
652
|
+
// `last_read_at`, other read events its `created_at`. `received_at` is the local clock, so only
|
|
653
|
+
// a fallback.
|
|
654
|
+
const last_read =
|
|
655
|
+
(event.type === 'notification.mark_unread' ? event.last_read_at : undefined) ??
|
|
656
|
+
event.created_at ??
|
|
657
|
+
event.received_at ??
|
|
658
|
+
nowNs();
|
|
997
659
|
|
|
998
660
|
const overriddenUnreadMessages = unreadMessages ?? unread_messages;
|
|
999
661
|
|
|
@@ -1018,12 +680,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1018
680
|
};
|
|
1019
681
|
|
|
1020
682
|
/**
|
|
1021
|
-
*
|
|
1022
|
-
* of each event with a channel guard and also forces an update of member_count
|
|
1023
|
-
* for the respective channel if applicable.
|
|
683
|
+
* Removes or upserts the member, forcing a channel upsert so `member_count` updates.
|
|
1024
684
|
*
|
|
1025
|
-
* @param payload.event -
|
|
1026
|
-
* @param payload.execute - Whether to immediately execute the
|
|
685
|
+
* @param payload.event - The WS event we are trying to process.
|
|
686
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
687
|
+
* @returns The prepared queries.
|
|
1027
688
|
*/
|
|
1028
689
|
public handleMemberEvent = async ({
|
|
1029
690
|
event,
|
|
@@ -1035,7 +696,6 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1035
696
|
const { member, cid, type } = event;
|
|
1036
697
|
|
|
1037
698
|
if (member && cid) {
|
|
1038
|
-
// we force update here so that member_count gets updated
|
|
1039
699
|
// TODO: Although this is more than fine for now, we should look into
|
|
1040
700
|
// changing this to be an actual update to the DB instead.
|
|
1041
701
|
return await this.queriesWithChannelGuard(
|
|
@@ -1058,11 +718,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1058
718
|
};
|
|
1059
719
|
|
|
1060
720
|
/**
|
|
1061
|
-
*
|
|
1062
|
-
* event handler within a channel guard.
|
|
721
|
+
* Updates the message behind the channel guard. Thread replies are ignored.
|
|
1063
722
|
*
|
|
1064
|
-
* @param payload.event -
|
|
1065
|
-
* @param payload.execute - Whether to immediately execute the
|
|
723
|
+
* @param payload.event - The WS event we are trying to process.
|
|
724
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
725
|
+
* @returns The prepared queries.
|
|
1066
726
|
*/
|
|
1067
727
|
public handleMessageUpdatedEvent = async ({
|
|
1068
728
|
event,
|
|
@@ -1085,14 +745,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1085
745
|
};
|
|
1086
746
|
|
|
1087
747
|
/**
|
|
1088
|
-
*
|
|
1089
|
-
* handler because event.channel.hidden does not arrive with the baseline event, so a
|
|
1090
|
-
* simple upsertion is not enough.
|
|
1091
|
-
* It will update the hidden property of a channel to true if handling the `channel.hidden`
|
|
1092
|
-
* event and to false if handling `channel.visible`.
|
|
748
|
+
* Sets `hidden` from the event type, as the event's channel does not carry it.
|
|
1093
749
|
*
|
|
1094
|
-
* @param payload. -
|
|
1095
|
-
* @param payload.execute - Whether to immediately execute the
|
|
750
|
+
* @param payload.event - The WS event we are trying to process.
|
|
751
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
752
|
+
* @returns The prepared queries.
|
|
1096
753
|
*/
|
|
1097
754
|
public handleChannelVisibilityEvent = async ({
|
|
1098
755
|
event,
|
|
@@ -1115,12 +772,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1115
772
|
};
|
|
1116
773
|
|
|
1117
774
|
/**
|
|
1118
|
-
*
|
|
1119
|
-
* removing all messages and relying on truncated_at as well. It will also upsert
|
|
1120
|
-
* reads adequately (and calculate the correct unread messages when truncating).
|
|
775
|
+
* Deletes the channel's messages (before `truncated_at` when set) and upserts the reads.
|
|
1121
776
|
*
|
|
1122
|
-
* @param payload.event -
|
|
1123
|
-
* @param payload.execute - Whether to immediately execute the
|
|
777
|
+
* @param payload.event - The WS event we are trying to process.
|
|
778
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
779
|
+
* @returns The prepared queries.
|
|
1124
780
|
*/
|
|
1125
781
|
public handleChannelTruncatedEvent = async ({
|
|
1126
782
|
event,
|
|
@@ -1141,14 +797,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1141
797
|
|
|
1142
798
|
let finalQueries = [...truncateQueries];
|
|
1143
799
|
|
|
1144
|
-
//
|
|
1145
|
-
// local unread count, also persist it for read-events-disabled channels (e.g. livestreams) so
|
|
1146
|
-
// the client-local count survives a cold start. state.read[userId] may be absent for those, so
|
|
1147
|
-
// fall back accordingly.
|
|
800
|
+
// Same condition as in `handleNewMessage`.
|
|
1148
801
|
const userId = ownUser.id;
|
|
1149
|
-
const activeChannel = this.client.
|
|
802
|
+
const activeChannel = this.client.channelManager.get(cid);
|
|
1150
803
|
const tracksReadLocally = channelTracksReadLocally(activeChannel);
|
|
1151
|
-
if (activeChannel && (
|
|
804
|
+
if (activeChannel && (activeChannel.readEventsEnabled || tracksReadLocally)) {
|
|
1152
805
|
const ownReads = activeChannel.state.read[userId];
|
|
1153
806
|
|
|
1154
807
|
let unreadCount = 0;
|
|
@@ -1164,7 +817,6 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1164
817
|
last_read: ownReads?.last_read ?? 0,
|
|
1165
818
|
last_read_message_id: ownReads?.last_read_message_id,
|
|
1166
819
|
unread_messages: unreadCount,
|
|
1167
|
-
// See the note above: supply `blocked_user_ids`, do not assert it.
|
|
1168
820
|
user: {
|
|
1169
821
|
...ownUser,
|
|
1170
822
|
blocked_user_ids: ownUser.blocked_user_ids ?? [],
|
|
@@ -1186,14 +838,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1186
838
|
};
|
|
1187
839
|
|
|
1188
840
|
/**
|
|
1189
|
-
*
|
|
1190
|
-
* within a channel guard and maps them like so:
|
|
1191
|
-
* - reaction.new -> insertReaction
|
|
1192
|
-
* - reaction.updated -> updateReaction
|
|
1193
|
-
* - reaction.deleted -> deleteReaction
|
|
841
|
+
* Maps `reaction.new`, `.updated` and `.deleted` to the matching reaction write.
|
|
1194
842
|
*
|
|
1195
|
-
* @param payload.event -
|
|
1196
|
-
* @param payload.execute - Whether to immediately execute the
|
|
843
|
+
* @param payload.event - The WS event we are trying to process.
|
|
844
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
845
|
+
* @returns The prepared queries.
|
|
1197
846
|
*/
|
|
1198
847
|
public handleReactionEvent = async ({
|
|
1199
848
|
event,
|
|
@@ -1231,12 +880,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1231
880
|
};
|
|
1232
881
|
|
|
1233
882
|
/**
|
|
1234
|
-
*
|
|
1235
|
-
* - draft.updated -> updateDraft
|
|
1236
|
-
* - draft.deleted -> deleteDraft
|
|
883
|
+
* Maps `draft.updated` and `draft.deleted` to `upsertDraft` and `deleteDraft`.
|
|
1237
884
|
*
|
|
1238
|
-
* @param payload.event -
|
|
1239
|
-
* @param payload.execute - Whether to immediately execute the
|
|
885
|
+
* @param payload.event - The WS event we are trying to process.
|
|
886
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
887
|
+
* @returns The prepared queries.
|
|
1240
888
|
*/
|
|
1241
889
|
handleDraftEvent = async ({
|
|
1242
890
|
event,
|
|
@@ -1270,12 +918,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1270
918
|
};
|
|
1271
919
|
|
|
1272
920
|
/**
|
|
1273
|
-
*
|
|
1274
|
-
* event.type for all events we are currently handling. It is used to both
|
|
1275
|
-
* react on WS events as well as process the sync API events.
|
|
921
|
+
* Routes an event to its handler by type. Used for WS events and for sync events.
|
|
1276
922
|
*
|
|
1277
|
-
* @param payload.event -
|
|
1278
|
-
* @param payload.execute - Whether to immediately execute the
|
|
923
|
+
* @param payload.event - The WS event we are trying to process.
|
|
924
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
925
|
+
* @returns The prepared queries.
|
|
1279
926
|
*/
|
|
1280
927
|
public handleEvent = async ({
|
|
1281
928
|
event,
|
|
@@ -1306,15 +953,17 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1306
953
|
}
|
|
1307
954
|
|
|
1308
955
|
if (type === 'message.read' || type === 'notification.mark_read') {
|
|
956
|
+
// The DB stores channel reads only, so thread read events are skipped.
|
|
957
|
+
if (event.thread) return [];
|
|
1309
958
|
return this.handleRead({ event, unreadMessages: 0, execute });
|
|
1310
959
|
}
|
|
1311
960
|
|
|
1312
|
-
//
|
|
1313
|
-
//
|
|
1314
|
-
// situation it is ever dispatched), so it can never touch channels that track reads server-side.
|
|
961
|
+
// The client-only reset from `Channel.markReadLocally()`: persisted only for channels with a
|
|
962
|
+
// local unread count, never for channels that track reads server-side.
|
|
1315
963
|
if (type === 'message.read_locally') {
|
|
1316
|
-
const localChannel = event.cid
|
|
1317
|
-
|
|
964
|
+
const localChannel = event.cid
|
|
965
|
+
? this.client.channelManager.get(event.cid)
|
|
966
|
+
: undefined;
|
|
1318
967
|
if (channelTracksReadLocally(localChannel)) {
|
|
1319
968
|
return this.handleRead({ event, unreadMessages: 0, execute });
|
|
1320
969
|
}
|
|
@@ -1340,13 +989,8 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1340
989
|
return await this.handleDraftEvent({ event, execute });
|
|
1341
990
|
}
|
|
1342
991
|
|
|
1343
|
-
//
|
|
1344
|
-
//
|
|
1345
|
-
// because we anyway cannot get the messages for a channel until we run
|
|
1346
|
-
// either channel.watch() or channel.query(...) to get them. So, when
|
|
1347
|
-
// receiving the event we only upsert the channel data and we leave the
|
|
1348
|
-
// rest of the entities to be updated whenever we actually start watching
|
|
1349
|
-
// or we at least query.
|
|
992
|
+
// `notification.message_new` only upserts the channel data: its messages are stored when the
|
|
993
|
+
// channel is watched or queried.
|
|
1350
994
|
if (
|
|
1351
995
|
(type === 'channel.updated' ||
|
|
1352
996
|
type === 'notification.message_new' ||
|
|
@@ -1373,36 +1017,16 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1373
1017
|
};
|
|
1374
1018
|
|
|
1375
1019
|
/**
|
|
1376
|
-
*
|
|
1377
|
-
*
|
|
1020
|
+
* Executes the task now. On an {@link isEphemeral} failure the task goes to
|
|
1021
|
+
* `handleAddPendingTask`, and the error is rethrown. With an unhealthy socket it fails with an
|
|
1022
|
+
* `OfflineError` without executing.
|
|
1378
1023
|
*
|
|
1379
|
-
*
|
|
1380
|
-
*
|
|
1381
|
-
*
|
|
1024
|
+
* The payload carries the request options, including an `AbortSignal`. It is live while the task
|
|
1025
|
+
* runs now, but a replay from the table gets an inert `{}` when the store serializes the payload
|
|
1026
|
+
* (always across a restart), and `ApiClient` then runs the request uncancellable.
|
|
1382
1027
|
*
|
|
1383
|
-
*
|
|
1384
|
-
*
|
|
1385
|
-
* A task payload is the argument list of the method being replayed, so it carries that
|
|
1386
|
-
* method's trailing `requestOptions` too. How far an `AbortSignal` in there survives depends on
|
|
1387
|
-
* the path:
|
|
1388
|
-
*
|
|
1389
|
-
* - Step 1 above never leaves memory, so the signal is live and the call is cancellable exactly
|
|
1390
|
-
* as it would be outside the queue. This is the common case.
|
|
1391
|
-
* - A task that reaches the pending-tasks table survives only as far as the concrete
|
|
1392
|
-
* implementation's storage allows. {@link OfflineDBApi.addPendingTask} and
|
|
1393
|
-
* {@link OfflineDBApi.getPendingTasks} make no serialization promise either way: a store that
|
|
1394
|
-
* hands back the object it was given keeps the signal live, while one that encodes the payload
|
|
1395
|
-
* (as a SQLite-backed store does) revives it as an inert `{}`. Across a process restart it is
|
|
1396
|
-
* always the latter.
|
|
1397
|
-
*
|
|
1398
|
-
* `ApiClient` drops a signal it cannot listen to rather than handing it to axios (which would
|
|
1399
|
-
* throw), so a replay that lost its signal simply runs uncancellable.
|
|
1400
|
-
*
|
|
1401
|
-
* Note that cancelling is not a way to drop a queued task after the fact — an aborted request is
|
|
1402
|
-
* a definitive rejection ({@link isEphemeral} is `false` for it), so it is never queued in the
|
|
1403
|
-
* first place.
|
|
1404
|
-
*
|
|
1405
|
-
* @param task - the pending task we want to execute
|
|
1028
|
+
* @param payload.task - The pending task we want to execute.
|
|
1029
|
+
* @returns The response of the task's request.
|
|
1406
1030
|
*/
|
|
1407
1031
|
public queueTask = async <T>({ task }: { task: PendingTask }): Promise<T> => {
|
|
1408
1032
|
const attemptTaskExecution = async () => {
|
|
@@ -1424,109 +1048,62 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1424
1048
|
}
|
|
1425
1049
|
};
|
|
1426
1050
|
|
|
1427
|
-
/**
|
|
1428
|
-
* A utility method that determines if a failed task should be skipped (NOT added to the queue) -
|
|
1429
|
-
* i.e. the failure is a definitive rejection rather than an ephemeral/retryable one. A task is
|
|
1430
|
-
* kept in the queue only when its error is {@link isEphemeral} (connection/network error or a
|
|
1431
|
-
* retryable server code). A non retryable server response (i.e bad request, not allowed etc) is
|
|
1432
|
-
* skipped since retrying it would never succeed, and so is a request the caller cancelled via
|
|
1433
|
-
* `StreamRequestOptions.signal` - replaying it would defeat the point of aborting.
|
|
1434
|
-
*
|
|
1435
|
-
* @param error - The error thrown while executing the failed task.
|
|
1436
|
-
*/
|
|
1051
|
+
/** A definitive rejection (non-retryable server error or a cancelled request) is not queued. */
|
|
1437
1052
|
private shouldSkipQueueingTask = (error: AxiosError<APIError>) => !isEphemeral(error);
|
|
1438
1053
|
|
|
1439
|
-
private mergeFailedMessageUpdateIntoPendingSendMessage = ({
|
|
1440
|
-
editedMessage,
|
|
1441
|
-
pendingMessage,
|
|
1442
|
-
}: {
|
|
1443
|
-
editedMessage: LocalMessage | Partial<MessageResponse>;
|
|
1444
|
-
pendingMessage: MessageRequest;
|
|
1445
|
-
}) => {
|
|
1446
|
-
const normalizedEditedMessage = localMessageToNewMessagePayload(
|
|
1447
|
-
editedMessage as LocalMessage,
|
|
1448
|
-
);
|
|
1449
|
-
const pendingMessageStatus = (pendingMessage as { status?: string }).status;
|
|
1450
|
-
|
|
1451
|
-
return {
|
|
1452
|
-
...pendingMessage,
|
|
1453
|
-
...normalizedEditedMessage,
|
|
1454
|
-
...(typeof pendingMessageStatus !== 'undefined'
|
|
1455
|
-
? { status: pendingMessageStatus }
|
|
1456
|
-
: {}),
|
|
1457
|
-
} as MessageRequest;
|
|
1458
|
-
};
|
|
1459
|
-
|
|
1460
1054
|
private isPendingSendMessageTask = (
|
|
1461
1055
|
task: PendingTask,
|
|
1462
1056
|
): task is Extract<PendingTask, { type: 'send-message' }> =>
|
|
1463
1057
|
task.type === 'send-message';
|
|
1464
1058
|
|
|
1465
|
-
|
|
1059
|
+
/**
|
|
1060
|
+
* Folds an edit of a never-sent message into its queued `send-message` task, so a replay sends
|
|
1061
|
+
* the edited text once. The send task keeps its row and so its place in the queue. With no send
|
|
1062
|
+
* task the edit is queued on its own if `isMessageUpdateReplayable` allows it.
|
|
1063
|
+
*
|
|
1064
|
+
* A queued send is the only reliable sign the server never saw the message (a `failed` status can
|
|
1065
|
+
* also come from a rejected edit). The payloads are already `MessageRequest`s, so they merge by
|
|
1066
|
+
* plain spread: normalizing again would turn a flattened `mentioned_users` into `[undefined]`.
|
|
1067
|
+
*/
|
|
1068
|
+
private handleUpdateMessagePendingTask = async (
|
|
1466
1069
|
task: Extract<PendingTask, { type: 'update-message' }>,
|
|
1467
1070
|
) => {
|
|
1468
|
-
const [{ id, message }] = task.payload;
|
|
1469
|
-
if (!id) {
|
|
1470
|
-
return;
|
|
1471
|
-
}
|
|
1472
|
-
|
|
1071
|
+
const [{ id }, { message }] = task.payload;
|
|
1473
1072
|
const pendingTasks = await this.getPendingTasks({ messageId: id });
|
|
1474
|
-
const
|
|
1073
|
+
const sendTask = pendingTasks.find(this.isPendingSendMessageTask);
|
|
1475
1074
|
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1075
|
+
// No queued send, or one without an ID to rewrite (a task read from the DB always has one).
|
|
1076
|
+
if (sendTask?.id === undefined) {
|
|
1077
|
+
if (!isMessageUpdateReplayable(message)) return;
|
|
1479
1078
|
|
|
1480
|
-
|
|
1481
|
-
{
|
|
1482
|
-
// TODO: this is not good, we have too many message types, should probably only have two (request, response)
|
|
1483
|
-
editedMessage: message as unknown as LocalMessage,
|
|
1484
|
-
pendingMessage: pendingSendMessageTask.payload[0].message as MessageRequest,
|
|
1485
|
-
},
|
|
1486
|
-
);
|
|
1487
|
-
|
|
1488
|
-
const updatedPendingTask: Extract<PendingTask, { type: 'send-message' }> = {
|
|
1489
|
-
...pendingSendMessageTask,
|
|
1490
|
-
payload: [
|
|
1491
|
-
{
|
|
1492
|
-
...pendingSendMessageTask.payload[0],
|
|
1493
|
-
message: updatedPendingSendMessage,
|
|
1494
|
-
},
|
|
1495
|
-
],
|
|
1496
|
-
};
|
|
1497
|
-
|
|
1498
|
-
if (pendingSendMessageTask.id) {
|
|
1499
|
-
await this.updatePendingTask({
|
|
1500
|
-
id: pendingSendMessageTask.id,
|
|
1501
|
-
task: updatedPendingTask,
|
|
1502
|
-
});
|
|
1079
|
+
await this.addPendingTask(task);
|
|
1503
1080
|
return;
|
|
1504
1081
|
}
|
|
1505
1082
|
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1083
|
+
const [sendRequest, ...sendRequestOptions] = sendTask.payload;
|
|
1084
|
+
|
|
1085
|
+
await this.updatePendingTask({
|
|
1086
|
+
id: sendTask.id,
|
|
1087
|
+
task: {
|
|
1088
|
+
...sendTask,
|
|
1089
|
+
payload: [
|
|
1090
|
+
{ ...sendRequest, message: { ...sendRequest.message, ...message } },
|
|
1091
|
+
...sendRequestOptions,
|
|
1092
|
+
],
|
|
1093
|
+
},
|
|
1509
1094
|
});
|
|
1510
1095
|
};
|
|
1511
1096
|
|
|
1512
1097
|
/**
|
|
1513
|
-
*
|
|
1514
|
-
*
|
|
1098
|
+
* Stores the task. An edit of a never-sent message is folded into its queued send instead. With no
|
|
1099
|
+
* queued send, an edit `isMessageUpdateReplayable` refuses (attachments still on local URLs) is
|
|
1100
|
+
* dropped.
|
|
1101
|
+
*
|
|
1102
|
+
* @param payload.task - The pending task to store.
|
|
1515
1103
|
*/
|
|
1516
1104
|
public handleAddPendingTask = async ({ task }: { task: PendingTask }) => {
|
|
1517
|
-
if (
|
|
1518
|
-
task
|
|
1519
|
-
!isMessageUpdateReplayable(task.payload[0].message ?? {})
|
|
1520
|
-
) {
|
|
1521
|
-
return;
|
|
1522
|
-
}
|
|
1523
|
-
|
|
1524
|
-
if (
|
|
1525
|
-
task.type === 'update-message' &&
|
|
1526
|
-
!this.client.wsConnection?.isHealthy &&
|
|
1527
|
-
(task.payload[0].message as { status?: string } | undefined)?.status === 'failed'
|
|
1528
|
-
) {
|
|
1529
|
-
await this.handleOfflineFailedUpdateMessagePendingTask(task);
|
|
1105
|
+
if (task.type === 'update-message') {
|
|
1106
|
+
await this.handleUpdateMessagePendingTask(task);
|
|
1530
1107
|
return;
|
|
1531
1108
|
}
|
|
1532
1109
|
|
|
@@ -1534,30 +1111,8 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1534
1111
|
};
|
|
1535
1112
|
|
|
1536
1113
|
/**
|
|
1537
|
-
*
|
|
1538
|
-
*
|
|
1539
|
-
* - Updating a message
|
|
1540
|
-
* - Deleting a message
|
|
1541
|
-
* - Sending a reaction
|
|
1542
|
-
* - Removing a reaction
|
|
1543
|
-
* - Sending a message
|
|
1544
|
-
* - Creating a draft
|
|
1545
|
-
* - Deleting a draft
|
|
1546
|
-
* It will throw if we try to execute a pending task that is not supported.
|
|
1547
|
-
*
|
|
1548
|
-
* @param task - The task we want to execute
|
|
1549
|
-
* @param isPendingTask - a control value telling us if it's an actual pending task being executed
|
|
1550
|
-
* or delayed execution
|
|
1551
|
-
*/
|
|
1552
|
-
/**
|
|
1553
|
-
* Replays one queued operation, through the {@link QUEUEABLE_OPERATIONS} registry rather than a
|
|
1554
|
-
* per-type branch here — the same registry a first attempt resolves through, so the two cannot
|
|
1555
|
-
* describe different requests.
|
|
1556
|
-
*
|
|
1557
|
-
* @param task - the task to run.
|
|
1558
|
-
* @param isPendingTask - whether this is a replay rather than a first attempt. A first attempt has an
|
|
1559
|
-
* optimistic layer above it that settles local state; a replay does not, so some operations have to
|
|
1560
|
-
* settle it themselves (see `onReplay`).
|
|
1114
|
+
* Runs the task through {@link QUEUEABLE_OPERATIONS}. With `isPendingTask` (a replay) it also
|
|
1115
|
+
* runs the operation's `onReplay`.
|
|
1561
1116
|
*/
|
|
1562
1117
|
private executeTask = async <T extends QueueableType>(
|
|
1563
1118
|
{ task }: { task: PendingTaskOf<T> },
|
|
@@ -1574,10 +1129,8 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1574
1129
|
};
|
|
1575
1130
|
|
|
1576
1131
|
/**
|
|
1577
|
-
*
|
|
1578
|
-
*
|
|
1579
|
-
* it is going to remove the pending task from the DB even if execution fails, otherwise
|
|
1580
|
-
* it will keep it for the next time we try to execute all pending taks.
|
|
1132
|
+
* Replays the pending tasks in order and deletes each one that succeeded or was definitively
|
|
1133
|
+
* rejected. A task that failed for an ephemeral reason stays for the next run.
|
|
1581
1134
|
*/
|
|
1582
1135
|
public executePendingTasks = async () => {
|
|
1583
1136
|
const queue = await this.getPendingTasks();
|
|
@@ -1594,9 +1147,13 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1594
1147
|
true,
|
|
1595
1148
|
);
|
|
1596
1149
|
} catch (e) {
|
|
1150
|
+
const operation = QUEUEABLE_OPERATIONS[task.type] as QueueableOperation<
|
|
1151
|
+
typeof task.type
|
|
1152
|
+
>;
|
|
1153
|
+
operation.onReplayRejected?.({ client: this.client, error: e, task });
|
|
1154
|
+
|
|
1597
1155
|
const error = e as AxiosError<APIError>;
|
|
1598
1156
|
if (!this.shouldSkipQueueingTask(error)) {
|
|
1599
|
-
// executing the pending task has failed, so keep it in the queue
|
|
1600
1157
|
continue;
|
|
1601
1158
|
}
|
|
1602
1159
|
}
|