stream-chat 10.0.0-rc.21 → 10.0.0-rc.23
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/dist/cjs/index.browser.js +2196 -3902
- package/dist/cjs/index.browser.js.map +3 -3
- package/dist/cjs/index.node.js +2197 -3902
- package/dist/cjs/index.node.js.map +3 -3
- package/dist/esm/index.mjs +2199 -3905
- package/dist/esm/index.mjs.map +4 -4
- package/dist/types/ChannelManager.d.ts +120 -233
- package/dist/types/CooldownTimer.d.ts +8 -23
- package/dist/types/EventHandlerPipeline.d.ts +17 -37
- package/dist/types/LiveLocationManager.d.ts +22 -50
- package/dist/types/api-client.d.ts +9 -14
- package/dist/types/channel.d.ts +253 -448
- package/dist/types/channel_state.d.ts +48 -95
- package/dist/types/client.d.ts +245 -370
- package/dist/types/client_state.d.ts +1 -3
- package/dist/types/configuration/ConfigController.d.ts +30 -124
- package/dist/types/configuration/InstanceConfigurationRegistry.d.ts +31 -123
- 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 +59 -87
- package/dist/types/configuration/utils/applyInstanceConfiguration.d.ts +13 -35
- 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 +18 -40
- package/dist/types/connection/ConnectionRecoveryManager.d.ts +29 -120
- package/dist/types/connection/networkConnection/NetworkConnectionObserver.d.ts +32 -103
- package/dist/types/connection/networkConnection/reporters.d.ts +9 -32
- package/dist/types/connection/networkConnection/types.d.ts +12 -27
- package/dist/types/connection/wsConnection/StableWSConnection.d.ts +37 -116
- package/dist/types/connection/wsConnection/WSConnection.d.ts +41 -104
- package/dist/types/connection/wsConnection/WSConnectionFallback.d.ts +8 -16
- package/dist/types/connection/wsConnection/config.d.ts +8 -28
- package/dist/types/connection/wsConnection/types.d.ts +17 -70
- package/dist/types/entityStore/EntityStore.d.ts +153 -113
- package/dist/types/entityStore/StoreBackedItemIndex.d.ts +13 -46
- package/dist/types/errors.d.ts +7 -19
- package/dist/types/gen/chat/ChannelApi.d.ts +2 -1
- package/dist/types/gen/chat/ChatApi.d.ts +28 -14
- package/dist/types/gen/models/index.d.ts +769 -86
- package/dist/types/gen/moderation/ModerationApi.d.ts +4 -4
- package/dist/types/index.d.ts +2 -4
- package/dist/types/languageNames.d.ts +9 -24
- package/dist/types/logger.d.ts +1 -3
- package/dist/types/messageComposer/LocationComposer.d.ts +1 -1
- package/dist/types/messageComposer/attachmentIdentity.d.ts +1 -6
- package/dist/types/messageComposer/attachmentManager.d.ts +10 -32
- package/dist/types/messageComposer/attachmentUtils.d.ts +3 -14
- package/dist/types/messageComposer/configuration/configuration.d.ts +2 -5
- package/dist/types/messageComposer/configuration/types.d.ts +29 -56
- package/dist/types/messageComposer/linkPreviewsManager.d.ts +6 -9
- package/dist/types/messageComposer/messageComposer.d.ts +30 -164
- package/dist/types/messageComposer/middleware/attachmentManager/postUpload/attachmentEnrichment.d.ts +4 -14
- package/dist/types/messageComposer/middleware/messageComposer/attachments.d.ts +2 -9
- 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/commands.d.ts +9 -0
- package/dist/types/messageComposer/middleware/textComposer/mentions.d.ts +11 -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 +8 -4
- package/dist/types/messageComposer/textComposer.d.ts +3 -9
- package/dist/types/messageComposer/types.d.ts +5 -8
- package/dist/types/messageDelivery/MessageDeliveryReporter.d.ts +25 -79
- package/dist/types/messageDelivery/MessageReceiptsTracker.d.ts +15 -82
- package/dist/types/messageOperations/FailedSendCache.d.ts +4 -9
- package/dist/types/messageOperations/MessageOperationStatePolicy.d.ts +10 -20
- package/dist/types/messageOperations/MessageOperations.d.ts +20 -40
- package/dist/types/messageOperations/applyReactionLocally.d.ts +5 -22
- package/dist/types/messageOperations/createMessageOperations.d.ts +3 -5
- package/dist/types/messageOperations/reflectReactionEvent.d.ts +5 -13
- package/dist/types/messageOperations/sendOrdering.d.ts +4 -7
- package/dist/types/messageOperations/settlePendingAttachmentUploads.d.ts +7 -15
- package/dist/types/messageOperations/types.d.ts +15 -30
- package/dist/types/middleware.d.ts +7 -5
- package/dist/types/moderation.d.ts +21 -14
- package/dist/types/notifications/NotificationManager.d.ts +8 -31
- package/dist/types/notifications/types.d.ts +19 -59
- package/dist/types/offline-support/offline_support_api.d.ts +145 -539
- package/dist/types/offline-support/offline_sync_manager.d.ts +18 -42
- package/dist/types/offline-support/queueableOperations.d.ts +24 -52
- package/dist/types/offline-support/types.d.ts +24 -187
- 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 +2 -4
- package/dist/types/pagination/cursorDerivation/linearPaginationFlags.d.ts +2 -3
- package/dist/types/pagination/paginators/BasePaginator.d.ts +235 -622
- package/dist/types/pagination/paginators/ChannelPaginator.d.ts +67 -125
- package/dist/types/pagination/paginators/MessageIntervalPaginator.d.ts +133 -292
- package/dist/types/pagination/paginators/MessagePaginator.d.ts +69 -118
- package/dist/types/pagination/paginators/PinnedMessagePaginator.d.ts +7 -26
- package/dist/types/pagination/paginators/ReminderPaginator.d.ts +2 -2
- package/dist/types/pagination/paginators/ThreadPaginator.d.ts +15 -23
- package/dist/types/pagination/paginators/UserGroupPaginator.d.ts +2 -6
- 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 +14 -14
- package/dist/types/poll_manager.d.ts +11 -2
- package/dist/types/poll_optimistic_votes.d.ts +10 -14
- package/dist/types/reminders/Reminder.d.ts +4 -4
- package/dist/types/reminders/ReminderManager.d.ts +7 -20
- package/dist/types/search/BaseSearchSource.d.ts +7 -12
- package/dist/types/search/ChannelSearchSource.d.ts +7 -7
- package/dist/types/search/MessageSearchSource.d.ts +7 -9
- package/dist/types/search/SearchController.d.ts +17 -22
- package/dist/types/search/types.d.ts +6 -10
- package/dist/types/signing.d.ts +12 -3
- package/dist/types/thread.d.ts +23 -71
- package/dist/types/thread_manager.d.ts +45 -70
- package/dist/types/token_manager.d.ts +8 -13
- package/dist/types/types.d.ts +64 -125
- package/dist/types/types.utility.d.ts +3 -3
- package/dist/types/upload-utils.d.ts +5 -10
- package/dist/types/uploadManager.d.ts +17 -45
- package/dist/types/utils/FixedSizeQueueCache.d.ts +6 -11
- package/dist/types/utils/WithSubscriptions.d.ts +5 -15
- package/dist/types/utils/concurrency.d.ts +11 -19
- 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 +24 -64
- package/dist/types/utils.d.ts +40 -86
- package/package.json +7 -2
- package/src/ChannelManager.ts +159 -300
- package/src/CooldownTimer.ts +11 -29
- package/src/EventHandlerPipeline.ts +21 -44
- package/src/LiveLocationManager.ts +35 -74
- package/src/api-client.ts +43 -61
- package/src/campaign.ts +1 -1
- package/src/channel.ts +396 -749
- package/src/channel_batch_updater.ts +1 -1
- package/src/channel_state.ts +50 -105
- package/src/client.ts +360 -588
- package/src/client_state.ts +1 -6
- package/src/configuration/ConfigController.ts +41 -157
- package/src/configuration/InstanceConfigurationRegistry.ts +42 -171
- package/src/configuration/index.ts +1 -1
- package/src/configuration/keys.ts +13 -32
- package/src/configuration/shape.ts +67 -70
- package/src/configuration/types.ts +60 -100
- package/src/configuration/utils/applyInstanceConfiguration.ts +21 -53
- 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 +20 -49
- package/src/connection/ConnectionRecoveryManager.ts +51 -178
- package/src/connection/networkConnection/NetworkConnectionObserver.ts +37 -114
- package/src/connection/networkConnection/reporters.ts +9 -36
- package/src/connection/networkConnection/types.ts +12 -27
- package/src/connection/wsConnection/StableWSConnection.ts +73 -193
- package/src/connection/wsConnection/WSConnection.ts +56 -131
- package/src/connection/wsConnection/WSConnectionFallback.ts +22 -31
- package/src/connection/wsConnection/config.ts +13 -48
- package/src/connection/wsConnection/types.ts +17 -70
- package/src/constants.ts +4 -4
- package/src/entityStore/EntityStore.ts +162 -167
- package/src/entityStore/StoreBackedItemIndex.ts +14 -47
- package/src/errors.ts +7 -19
- package/src/formatMessageMemo.ts +3 -9
- package/src/gen/chat/ChannelApi.ts +19 -0
- package/src/gen/chat/ChatApi.ts +87 -13
- package/src/gen/models/index.ts +793 -71
- package/src/gen/moderation/ModerationApi.ts +0 -4
- package/src/index.ts +10 -14
- package/src/languageNames.ts +9 -24
- package/src/logger.ts +1 -3
- package/src/messageComposer/LocationComposer.ts +7 -23
- package/src/messageComposer/MessageComposerEffectHandlers.ts +1 -2
- package/src/messageComposer/attachmentIdentity.ts +2 -13
- package/src/messageComposer/attachmentManager.ts +17 -63
- package/src/messageComposer/attachmentUtils.ts +3 -17
- package/src/messageComposer/configuration/configuration.ts +5 -8
- package/src/messageComposer/configuration/types.ts +29 -56
- package/src/messageComposer/fileUtils.ts +1 -1
- package/src/messageComposer/linkPreviewsManager.ts +8 -20
- package/src/messageComposer/messageComposer.ts +54 -222
- package/src/messageComposer/middleware/attachmentManager/postUpload/attachmentEnrichment.ts +4 -16
- package/src/messageComposer/middleware/attachmentManager/postUpload/uploadErrorHandler.ts +1 -2
- package/src/messageComposer/middleware/messageComposer/attachments.ts +14 -40
- package/src/messageComposer/middleware/messageComposer/linkPreviews.ts +3 -4
- package/src/messageComposer/middleware/messageComposer/textComposer.ts +1 -1
- package/src/messageComposer/middleware/messageComposer/userDataInjection.ts +4 -11
- 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 +1 -6
- package/src/messageComposer/middleware/textComposer/commandUtils.ts +1 -2
- 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 +11 -8
- package/src/messageComposer/textComposer.ts +9 -59
- package/src/messageComposer/types.ts +9 -12
- package/src/messageDelivery/MessageDeliveryReporter.ts +60 -113
- package/src/messageDelivery/MessageReceiptsTracker.ts +21 -105
- package/src/messageOperations/FailedSendCache.ts +4 -9
- package/src/messageOperations/MessageOperationStatePolicy.ts +40 -67
- package/src/messageOperations/MessageOperations.ts +33 -62
- package/src/messageOperations/applyReactionLocally.ts +9 -28
- package/src/messageOperations/createMessageOperations.ts +17 -33
- package/src/messageOperations/reflectReactionEvent.ts +5 -13
- package/src/messageOperations/sendOrdering.ts +4 -7
- package/src/messageOperations/settlePendingAttachmentUploads.ts +10 -21
- package/src/messageOperations/types.ts +15 -30
- package/src/middleware.ts +7 -10
- package/src/moderation.ts +25 -21
- package/src/notifications/NotificationManager.ts +11 -35
- package/src/notifications/types.ts +19 -59
- package/src/offline-support/offline_support_api.ts +178 -608
- package/src/offline-support/offline_sync_manager.ts +25 -69
- package/src/offline-support/queueableOperations.ts +31 -84
- package/src/offline-support/types.ts +25 -188
- package/src/pagination/ItemIndex.ts +4 -12
- package/src/pagination/cursorDerivation/aroundPaginationFlags.ts +29 -0
- package/src/pagination/cursorDerivation/createdAtAroundPaginationFlags.ts +13 -62
- package/src/pagination/cursorDerivation/idAroundPaginationFlags.ts +11 -39
- package/src/pagination/cursorDerivation/linearPaginationFlags.ts +4 -6
- package/src/pagination/filterCompiler.ts +11 -25
- package/src/pagination/paginators/BasePaginator.ts +422 -976
- package/src/pagination/paginators/ChannelPaginator.ts +130 -190
- package/src/pagination/paginators/MessageIntervalPaginator.ts +226 -457
- package/src/pagination/paginators/MessagePaginator.ts +135 -175
- package/src/pagination/paginators/PinnedMessagePaginator.ts +13 -38
- package/src/pagination/paginators/ReminderPaginator.ts +12 -15
- package/src/pagination/paginators/ThreadPaginator.ts +16 -26
- package/src/pagination/paginators/UserGroupPaginator.ts +6 -17
- 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 +19 -19
- package/src/poll_manager.ts +16 -20
- package/src/poll_optimistic_votes.ts +17 -22
- package/src/reminders/Reminder.ts +5 -6
- package/src/reminders/ReminderManager.ts +9 -34
- package/src/reminders/ReminderTimer.ts +1 -2
- package/src/search/BaseSearchSource.ts +14 -29
- package/src/search/ChannelSearchSource.ts +8 -8
- package/src/search/MessageSearchSource.ts +19 -25
- package/src/search/SearchController.ts +18 -24
- package/src/search/types.ts +6 -10
- package/src/segment.ts +1 -1
- package/src/signing.ts +23 -3
- package/src/thread.ts +72 -173
- package/src/thread_manager.ts +59 -76
- package/src/token_manager.ts +11 -21
- package/src/types.ts +78 -126
- package/src/types.utility.ts +3 -3
- package/src/upload-utils.ts +5 -10
- package/src/uploadManager.ts +22 -54
- package/src/utils/FixedSizeQueueCache.ts +13 -21
- package/src/utils/WithSubscriptions.ts +5 -15
- 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 +26 -66
- package/src/utils.ts +72 -128
|
@@ -40,11 +40,10 @@ const logger = chatLoggerSystem.getLogger('offline-db');
|
|
|
40
40
|
import type { WSEvent } from '../gen/models';
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* 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`.
|
|
48
47
|
*
|
|
49
48
|
* @abstract
|
|
50
49
|
*/
|
|
@@ -58,11 +57,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
58
57
|
syncMaxEventCount,
|
|
59
58
|
}: {
|
|
60
59
|
client: StreamChat;
|
|
61
|
-
/**
|
|
62
|
-
* Optional positive cap forwarded to the {@link OfflineDBSyncManager}, which
|
|
63
|
-
* owns sync. `undefined` or any non-positive value means "no limit". See
|
|
64
|
-
* {@link OfflineDBSyncManager.syncMaxEventCount}.
|
|
65
|
-
*/
|
|
60
|
+
/** Forwarded to {@link OfflineDBSyncManager.syncMaxEventCount}. */
|
|
66
61
|
syncMaxEventCount?: number;
|
|
67
62
|
}) {
|
|
68
63
|
this.client = client;
|
|
@@ -77,417 +72,130 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
77
72
|
});
|
|
78
73
|
}
|
|
79
74
|
|
|
80
|
-
/**
|
|
81
|
-
* Inserts a reaction into the DB.
|
|
82
|
-
* Will write to:
|
|
83
|
-
* - The reactions table with the new reaction
|
|
84
|
-
* - The message table with the message containing the new reaction
|
|
85
|
-
* - The users table with any users associated
|
|
86
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
87
|
-
* already executed).
|
|
88
|
-
*
|
|
89
|
-
* @param {DBInsertReactionType} options
|
|
90
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
91
|
-
*/
|
|
75
|
+
/** Writes the reaction, the message carrying it and the associated users. */
|
|
92
76
|
abstract insertReaction: OfflineDBApi['insertReaction'];
|
|
93
77
|
|
|
94
|
-
/**
|
|
95
|
-
* Upserts the list of CIDs for a filter + sort query hash.
|
|
96
|
-
* Will write to only the table containing the cids.
|
|
97
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
98
|
-
* already executed).
|
|
99
|
-
*
|
|
100
|
-
* @param {DBUpsertCidsForQueryType} options
|
|
101
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
102
|
-
*/
|
|
78
|
+
/** Upserts the CIDs for a filter + sort query hash (cids table only). */
|
|
103
79
|
abstract upsertCidsForQuery: OfflineDBApi['upsertCidsForQuery'];
|
|
104
80
|
|
|
105
81
|
/**
|
|
106
|
-
* Upserts the channels
|
|
107
|
-
*
|
|
108
|
-
* tables.
|
|
109
|
-
* Will write to:
|
|
110
|
-
* - The channels table with the channel data
|
|
111
|
-
* - The messages table with each message associated
|
|
112
|
-
* - The reactions table if the messages contain reactions
|
|
113
|
-
* - The users table with all users associated
|
|
114
|
-
* - The members table for membership and members
|
|
115
|
-
* - The polls table for any messages that are polls
|
|
116
|
-
* - The reads table for each user
|
|
117
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
118
|
-
* already executed).
|
|
119
|
-
*
|
|
120
|
-
* @param {DBUpsertChannelsType} options
|
|
121
|
-
* @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.
|
|
122
84
|
*/
|
|
123
85
|
abstract upsertChannels: OfflineDBApi['upsertChannels'];
|
|
124
86
|
|
|
125
|
-
/**
|
|
126
|
-
* Upserts the current active user's sync status.
|
|
127
|
-
* Will only write to the sync status table.
|
|
128
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
129
|
-
* already executed).
|
|
130
|
-
*
|
|
131
|
-
* @param {DBUpsertUserSyncStatusType} options
|
|
132
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
133
|
-
*/
|
|
87
|
+
/** Upserts the current user's sync status (sync status table only). */
|
|
134
88
|
abstract upsertUserSyncStatus: OfflineDBApi['upsertUserSyncStatus'];
|
|
135
89
|
|
|
136
|
-
/**
|
|
137
|
-
* Upserts the app settings for the current Stream App into the DB. It
|
|
138
|
-
* is only intended to be run once per lifecycle of the app.
|
|
139
|
-
* Will only write to the respective app settings table.
|
|
140
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
141
|
-
* already executed).
|
|
142
|
-
*
|
|
143
|
-
* @param {DBUpsertAppSettingsType} options
|
|
144
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
145
|
-
*/
|
|
90
|
+
/** Upserts the app settings (app settings table only). Meant to run once per app lifecycle. */
|
|
146
91
|
abstract upsertAppSettings: OfflineDBApi['upsertAppSettings'];
|
|
147
92
|
|
|
148
|
-
/**
|
|
149
|
-
* Upserts a poll fully in the DB.
|
|
150
|
-
* Will write to the polls table. It should not update the message
|
|
151
|
-
* associated due to how the poll state works.
|
|
152
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
153
|
-
* already executed).
|
|
154
|
-
*
|
|
155
|
-
* @param {DBUpsertPollType} options
|
|
156
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
157
|
-
*/
|
|
93
|
+
/** Upserts a poll (polls table only). It does not update the poll's message. */
|
|
158
94
|
abstract upsertPoll: OfflineDBApi['upsertPoll'];
|
|
159
95
|
|
|
160
|
-
/**
|
|
161
|
-
* Upserts only the channel.data for the provided channels in the DB.
|
|
162
|
-
* Will only write to the channels table.
|
|
163
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
164
|
-
* already executed).
|
|
165
|
-
*
|
|
166
|
-
* @param {DBUpsertChannelDataType} options
|
|
167
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
168
|
-
*/
|
|
96
|
+
/** Upserts only `channel.data` (channels table only). */
|
|
169
97
|
abstract upsertChannelData: OfflineDBApi['upsertChannelData'];
|
|
170
98
|
|
|
171
|
-
/**
|
|
172
|
-
* Upserts the provided reads in the DB.
|
|
173
|
-
* Will write to:
|
|
174
|
-
* - The reads table
|
|
175
|
-
* - The users table for each user associated with a read
|
|
176
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
177
|
-
* already executed).
|
|
178
|
-
*
|
|
179
|
-
* @param {DBUpsertReadsType} options
|
|
180
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
181
|
-
*/
|
|
99
|
+
/** Upserts the reads and the users they reference. */
|
|
182
100
|
abstract upsertReads: OfflineDBApi['upsertReads'];
|
|
183
101
|
|
|
184
|
-
/**
|
|
185
|
-
* Upserts the messages in the DB.
|
|
186
|
-
* Will write to:
|
|
187
|
-
* - The messages table
|
|
188
|
-
* - The reads table
|
|
189
|
-
* - The polls table (if any messages contain polls)
|
|
190
|
-
* - The reactions table (if any messages contain reactions)
|
|
191
|
-
* - The users table
|
|
192
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
193
|
-
* already executed).
|
|
194
|
-
*
|
|
195
|
-
* @param {DBUpsertMessagesType} options
|
|
196
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
197
|
-
*/
|
|
102
|
+
/** Upserts the messages with their reads, polls, reactions and users. */
|
|
198
103
|
abstract upsertMessages: OfflineDBApi['upsertMessages'];
|
|
199
104
|
|
|
200
|
-
/**
|
|
201
|
-
* Upserts the members in the DB.
|
|
202
|
-
* Will write to:
|
|
203
|
-
* - The users table (for each user associated with a member)
|
|
204
|
-
* - The members table
|
|
205
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
206
|
-
* already executed).
|
|
207
|
-
*
|
|
208
|
-
* @param {DBUpsertMembersType} options
|
|
209
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
210
|
-
*/
|
|
105
|
+
/** Upserts the members and their users. */
|
|
211
106
|
abstract upsertMembers: OfflineDBApi['upsertMembers'];
|
|
212
107
|
|
|
213
108
|
/**
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
* Will write to the reactions table.
|
|
218
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
219
|
-
* already executed).
|
|
220
|
-
*
|
|
221
|
-
* @param {DBUpdateReactionType} options
|
|
222
|
-
* @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.
|
|
223
112
|
*/
|
|
224
113
|
abstract updateReaction: OfflineDBApi['updateReaction'];
|
|
225
114
|
|
|
226
|
-
/**
|
|
227
|
-
* Updates a single message in the DB. This is used as a faster
|
|
228
|
-
* alternative to upsertMessages with more optimized queries.
|
|
229
|
-
* Will write to the messages table.
|
|
230
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
231
|
-
* already executed).
|
|
232
|
-
*
|
|
233
|
-
* @param {DBUpdateMessageType} options
|
|
234
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
235
|
-
*/
|
|
115
|
+
/** Updates a single message (messages table only) with leaner queries than `upsertMessages`. */
|
|
236
116
|
abstract updateMessage: OfflineDBApi['updateMessage'];
|
|
237
117
|
|
|
238
|
-
/**
|
|
239
|
-
* Fetches the provided draft from the DB. Should return as close to
|
|
240
|
-
* the server side DraftResponse as possible.
|
|
241
|
-
*
|
|
242
|
-
* @param {DBGetDraftType} options
|
|
243
|
-
* @returns {Promise<DraftResponse | null>}
|
|
244
|
-
*/
|
|
118
|
+
/** Returns the draft as close to the server's `DraftResponse` as possible. */
|
|
245
119
|
abstract getDraft: OfflineDBApi['getDraft'];
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
* Will write to the draft table upserting the draft.
|
|
249
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
250
|
-
* already executed).
|
|
251
|
-
*
|
|
252
|
-
* @param {DBUpsertDraftType} options
|
|
253
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
254
|
-
*/
|
|
120
|
+
|
|
121
|
+
/** Upserts a draft (drafts table only). */
|
|
255
122
|
abstract upsertDraft: OfflineDBApi['upsertDraft'];
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
* Will write to the draft table removing the draft.
|
|
259
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
260
|
-
* already executed).
|
|
261
|
-
*
|
|
262
|
-
* @param {DBDeleteDraftType} options
|
|
263
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
264
|
-
*/
|
|
123
|
+
|
|
124
|
+
/** Deletes a draft (drafts table only). */
|
|
265
125
|
abstract deleteDraft: OfflineDBApi['deleteDraft'];
|
|
266
126
|
|
|
267
127
|
/**
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
* for aggregating and serialization of all of the data. Should return as close to
|
|
271
|
-
* the server side ChannelStateResponseFields as possible.
|
|
272
|
-
*
|
|
273
|
-
* @param {DBGetChannelsType} options
|
|
274
|
-
* @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.
|
|
275
130
|
*/
|
|
276
131
|
abstract getChannels: OfflineDBApi['getChannels'];
|
|
277
132
|
|
|
278
133
|
/**
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
* The implementation itself is responsible for aggregating and serialization of all of the data.
|
|
282
|
-
* Should return as close to the server side ChannelStateResponseFields as possible.
|
|
283
|
-
*
|
|
284
|
-
* @param {DBGetChannelsForQueryType} options
|
|
285
|
-
* @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`.
|
|
286
136
|
*/
|
|
287
137
|
abstract getChannelsForQuery: OfflineDBApi['getChannelsForQuery'];
|
|
288
138
|
|
|
289
|
-
/**
|
|
290
|
-
* Will return a list of all available CIDs in the DB. The same can be achieved
|
|
291
|
-
* by fetching all channels, however this is meant to be much faster as a query.
|
|
292
|
-
*
|
|
293
|
-
* @returns {Promise<string[]>}
|
|
294
|
-
*/
|
|
139
|
+
/** All CIDs in the DB. Much cheaper than fetching all channels. */
|
|
295
140
|
abstract getAllChannelCids: OfflineDBApi['getAllChannelCids'];
|
|
296
141
|
|
|
297
|
-
/**
|
|
298
|
-
* Fetches the timestamp of the last sync of the DB.
|
|
299
|
-
*
|
|
300
|
-
* @param {DBGetLastSyncedAtType} options
|
|
301
|
-
* @returns {Promise<string | undefined>}
|
|
302
|
-
*/
|
|
303
142
|
abstract getLastSyncedAt: OfflineDBApi['getLastSyncedAt'];
|
|
304
143
|
|
|
305
|
-
/**
|
|
306
|
-
* Fetches all pending tasks from the DB. It will return them in an
|
|
307
|
-
* ordered fashion by the time they were created.
|
|
308
|
-
*
|
|
309
|
-
* @param {DBGetPendingTasksType} [conditions]
|
|
310
|
-
* @returns {Promise<PendingTask[]>}
|
|
311
|
-
*/
|
|
144
|
+
/** Pending tasks ordered by creation time, optionally only one message's. */
|
|
312
145
|
abstract getPendingTasks: OfflineDBApi['getPendingTasks'];
|
|
313
146
|
|
|
314
|
-
/**
|
|
315
|
-
* Fetches the app settings stored in the DB. Is mainly meant to be used
|
|
316
|
-
* only while offline and opening the application, as we only update the
|
|
317
|
-
* app settings whenever they are fetched again so it has the potential to
|
|
318
|
-
* be stale.
|
|
319
|
-
*
|
|
320
|
-
* @param {DBGetAppSettingsType} options
|
|
321
|
-
* @returns {Promise<GetApplicationResponse | null>}
|
|
322
|
-
*/
|
|
147
|
+
/** The stored app settings. They only refresh when fetched again, so they can be stale. */
|
|
323
148
|
abstract getAppSettings: OfflineDBApi['getAppSettings'];
|
|
324
149
|
|
|
325
|
-
/**
|
|
326
|
-
* Fetches reactions from the DB for a given filter & sort hash and
|
|
327
|
-
* for a given message ID.
|
|
328
|
-
*
|
|
329
|
-
* @param {DBGetReactionsType} options
|
|
330
|
-
* @returns {Promise<ReactionResponse[] | null>}
|
|
331
|
-
*/
|
|
332
150
|
abstract getReactions: OfflineDBApi['getReactions'];
|
|
333
151
|
|
|
334
|
-
/**
|
|
335
|
-
* Executes multiple queries in a batched fashion. It will also be done
|
|
336
|
-
* within a transaction.
|
|
337
|
-
*
|
|
338
|
-
* @param {ExecuteBatchDBQueriesType} queries
|
|
339
|
-
* @returns {Promise<unknown>}
|
|
340
|
-
*/
|
|
152
|
+
/** Runs the queries in one transaction. */
|
|
341
153
|
abstract executeSqlBatch: OfflineDBApi['executeSqlBatch'];
|
|
342
154
|
|
|
343
|
-
/**
|
|
344
|
-
* Adds a pending task to the pending tasks table. Can only be one of the
|
|
345
|
-
* supported types of pending tasks, otherwise its execution will throw.
|
|
346
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
347
|
-
* already executed).
|
|
348
|
-
*
|
|
349
|
-
* @param {PendingTask} task
|
|
350
|
-
* @returns {Promise<() => Promise<void>>}
|
|
351
|
-
*/
|
|
155
|
+
/** Adds a pending task. Executing a task of an unsupported type throws. */
|
|
352
156
|
abstract addPendingTask: OfflineDBApi['addPendingTask'];
|
|
353
157
|
|
|
354
|
-
/**
|
|
355
|
-
* Updates a pending task in the DB, given its ID.
|
|
356
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
357
|
-
* already executed).
|
|
358
|
-
*
|
|
359
|
-
* @param {DBUpdatePendingTaskType} options
|
|
360
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
361
|
-
*/
|
|
362
158
|
abstract updatePendingTask: OfflineDBApi['updatePendingTask'];
|
|
363
159
|
|
|
364
|
-
/**
|
|
365
|
-
* Deletes a pending task from the DB, given its ID.
|
|
366
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
367
|
-
* already executed).
|
|
368
|
-
*
|
|
369
|
-
* @param {DBDeletePendingTaskType} options
|
|
370
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
371
|
-
*/
|
|
372
160
|
abstract deletePendingTask: OfflineDBApi['deletePendingTask'];
|
|
373
161
|
|
|
374
|
-
/**
|
|
375
|
-
* Deletes a reaction from the DB.
|
|
376
|
-
* Will write to the reactions table removing the reaction.
|
|
377
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
378
|
-
* already executed).
|
|
379
|
-
*
|
|
380
|
-
* @param {DBDeleteReactionType} options
|
|
381
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
382
|
-
*/
|
|
383
162
|
abstract deleteReaction: OfflineDBApi['deleteReaction'];
|
|
384
163
|
|
|
385
|
-
/**
|
|
386
|
-
* Deletes a member from the DB.
|
|
387
|
-
* Will only write to the members table.
|
|
388
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
389
|
-
* already executed).
|
|
390
|
-
*
|
|
391
|
-
* @param {DBDeleteMemberType} options
|
|
392
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
393
|
-
*/
|
|
394
164
|
abstract deleteMember: OfflineDBApi['deleteMember'];
|
|
395
165
|
|
|
396
|
-
/**
|
|
397
|
-
* Deletes a channel from the DB.
|
|
398
|
-
* It will also delete all other entities associated with the channel in
|
|
399
|
-
* a cascading fashion (messages, reactions, members etc.).
|
|
400
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
401
|
-
* already executed).
|
|
402
|
-
*
|
|
403
|
-
* @param {DBDeleteChannelType} options
|
|
404
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
405
|
-
*/
|
|
166
|
+
/** Deletes the channel and everything associated with it (messages, reactions, members, ...). */
|
|
406
167
|
abstract deleteChannel: OfflineDBApi['deleteChannel'];
|
|
407
168
|
|
|
408
|
-
/**
|
|
409
|
-
* Deletes multiple messages for a given channel. Works as `channel.truncated` would.
|
|
410
|
-
* Should remove entities primarily from the messages table and then from all associated
|
|
411
|
-
* tables in a cascading fashion (reactions, polls etc.).
|
|
412
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
413
|
-
* already executed).
|
|
414
|
-
*
|
|
415
|
-
* @param {DBDeleteMessagesForChannelType} options
|
|
416
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
417
|
-
*/
|
|
169
|
+
/** Deletes a channel's messages as `channel.truncated` would, cascading to their reactions. */
|
|
418
170
|
abstract deleteMessagesForChannel: OfflineDBApi['deleteMessagesForChannel'];
|
|
419
171
|
|
|
420
|
-
/**
|
|
421
|
-
* Deletes all pending tasks from the DB.
|
|
422
|
-
* Will only update the pending tasks table.
|
|
423
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
424
|
-
* already executed).
|
|
425
|
-
*
|
|
426
|
-
* @param {DBDropPendingTasksType} options
|
|
427
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
428
|
-
*/
|
|
172
|
+
/** Deletes all pending tasks of a message (pending tasks table only). */
|
|
429
173
|
abstract dropPendingTasks: OfflineDBApi['dropPendingTasks'];
|
|
430
174
|
|
|
431
|
-
/**
|
|
432
|
-
* Deletes a message from the DB.
|
|
433
|
-
* All other entities associated with the message will also be deleted
|
|
434
|
-
* in a cascading fashion (reactions, polls etc.).
|
|
435
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
436
|
-
* already executed).
|
|
437
|
-
*
|
|
438
|
-
* @param {DBDeleteMessageType} options
|
|
439
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
440
|
-
*/
|
|
175
|
+
/** Deletes the message and cascades to its reactions, polls, etc. */
|
|
441
176
|
abstract hardDeleteMessage: OfflineDBApi['hardDeleteMessage'];
|
|
442
177
|
|
|
443
|
-
/**
|
|
444
|
-
* Updates a message with a deleted_at value in the DB.
|
|
445
|
-
* Will only update the messages table, as the message is simply marked
|
|
446
|
-
* as deleted and not removed from the DB.
|
|
447
|
-
* Will return the prepared queries for delayed execution (even if they are
|
|
448
|
-
* already executed).
|
|
449
|
-
*
|
|
450
|
-
* @param {DBDeleteMessageType} options
|
|
451
|
-
* @returns {Promise<ExecuteBatchDBQueriesType>}
|
|
452
|
-
*/
|
|
178
|
+
/** Sets `deleted_at` on the message (messages table only) instead of removing it. */
|
|
453
179
|
abstract softDeleteMessage: OfflineDBApi['softDeleteMessage'];
|
|
454
180
|
|
|
455
|
-
/**
|
|
456
|
-
* Drops all tables and reinitializes the connection to the DB.
|
|
457
|
-
*
|
|
458
|
-
* @returns {Promise<unknown>}
|
|
459
|
-
*/
|
|
181
|
+
/** Drops all tables and reinitializes the DB connection. */
|
|
460
182
|
abstract resetDB: OfflineDBApi['resetDB'];
|
|
461
183
|
|
|
462
|
-
/**
|
|
463
|
-
* A utility query that checks whether a specific channel exists in the DB.
|
|
464
|
-
* Technically the same as actually fetching that channel through other queries,
|
|
465
|
-
* but much faster.
|
|
466
|
-
*
|
|
467
|
-
* @param {DBChannelExistsType} options
|
|
468
|
-
* @returns {Promise<boolean>}
|
|
469
|
-
*/
|
|
184
|
+
/** Whether the channel is in the DB. Much cheaper than fetching it. */
|
|
470
185
|
abstract channelExists: OfflineDBApi['channelExists'];
|
|
471
186
|
|
|
472
|
-
/**
|
|
473
|
-
* Initializes the DB (typically creating a simple file handle as a connection pointer for
|
|
474
|
-
* SQLite and likely similar for other DBs).
|
|
475
|
-
*
|
|
476
|
-
* @returns {Promise<boolean>}
|
|
477
|
-
*/
|
|
187
|
+
/** Opens the DB connection (for SQLite, typically a file handle). */
|
|
478
188
|
abstract initializeDB: OfflineDBApi['initializeDB'];
|
|
479
189
|
|
|
480
190
|
/**
|
|
481
|
-
* Initializes the DB
|
|
482
|
-
* It will update the DBs reactive state with initialization values.
|
|
191
|
+
* Initializes the DB and its sync manager for a user, and updates `state`.
|
|
483
192
|
*
|
|
484
|
-
* @param userId -
|
|
193
|
+
* @param userId - The user ID for which we want to initialize.
|
|
485
194
|
*/
|
|
486
195
|
public init = async (userId: string) => {
|
|
487
196
|
try {
|
|
488
197
|
if (!this.shouldInitialize(userId)) {
|
|
489
|
-
//
|
|
490
|
-
// This will change in the next major release.
|
|
198
|
+
// `initializeDB` has to resolve to a boolean: changing it is a breaking change.
|
|
491
199
|
const initialized = await this.initializeDB();
|
|
492
200
|
if (initialized) {
|
|
493
201
|
await this.syncManager.init();
|
|
@@ -505,9 +213,10 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
505
213
|
};
|
|
506
214
|
|
|
507
215
|
/**
|
|
508
|
-
* Checks whether the DB
|
|
216
|
+
* Checks whether the DB is already initialized for a user.
|
|
509
217
|
*
|
|
510
|
-
* @param userId -
|
|
218
|
+
* @param userId - The user ID for which we want to check initialization.
|
|
219
|
+
* @returns Whether the DB is initialized for `userId`.
|
|
511
220
|
*/
|
|
512
221
|
public shouldInitialize(userId: string): boolean {
|
|
513
222
|
const { userId: userIdFromState, initialized } = this.state.getLatestValue();
|
|
@@ -515,14 +224,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
515
224
|
}
|
|
516
225
|
|
|
517
226
|
/**
|
|
518
|
-
*
|
|
519
|
-
*
|
|
520
|
-
* and silently. Only really meant to be used for write queries that need to
|
|
521
|
-
* 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.
|
|
522
229
|
*
|
|
523
|
-
* @param queryCallback -
|
|
524
|
-
* @param method -
|
|
525
|
-
* 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.
|
|
526
232
|
*/
|
|
527
233
|
public executeQuerySafely = <T>(
|
|
528
234
|
queryCallback: (db: AbstractOfflineDB) => Promise<T>,
|
|
@@ -536,23 +242,17 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
536
242
|
};
|
|
537
243
|
|
|
538
244
|
/**
|
|
539
|
-
*
|
|
540
|
-
*
|
|
541
|
-
*
|
|
542
|
-
*
|
|
543
|
-
*
|
|
544
|
-
*
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
*
|
|
550
|
-
*
|
|
551
|
-
* @param event - The WS event we are trying to process.
|
|
552
|
-
* @param event.execute - Whether to immediately execute the operation (optional, defaults to `true`).
|
|
553
|
-
* @param event.forceUpdate - Whether to upsert the channel data anyway (optional, defaults to `false`).
|
|
554
|
-
* @param createQueries - A callback that creates the queries to execute.
|
|
555
|
-
* @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.
|
|
556
256
|
*/
|
|
557
257
|
public queriesWithChannelGuard = async (
|
|
558
258
|
{
|
|
@@ -576,24 +276,14 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
576
276
|
return await createQueries(execute);
|
|
577
277
|
}
|
|
578
278
|
|
|
579
|
-
// Fast path:
|
|
580
|
-
//
|
|
581
|
-
//
|
|
582
|
-
// upsert, so re-running them behind the guard is idempotent.
|
|
583
|
-
//
|
|
584
|
-
// Worth the inversion because this runs on EVERY live WS event: the probe it replaces is a native
|
|
585
|
-
// round-trip, which on device costs about as much as the write it was protecting.
|
|
586
|
-
//
|
|
587
|
-
// Only available when we are the ones executing. An `execute: false` caller is collecting queries
|
|
588
|
-
// for someone else's batch, so there is no failure here for us to catch and the channel row has to
|
|
589
|
-
// be secured up front. `forceUpdate` skips it too — that caller wants the channel data written
|
|
590
|
-
// 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).
|
|
591
282
|
if (execute && !forceUpdate) {
|
|
592
283
|
try {
|
|
593
284
|
return await createQueries(true);
|
|
594
285
|
} catch (error) {
|
|
595
|
-
// A missing channel row is the only failure this guard
|
|
596
|
-
// error and must not be retried.
|
|
286
|
+
// A missing channel row is the only failure this guard repairs.
|
|
597
287
|
if (await this.channelExists({ cid })) throw error;
|
|
598
288
|
}
|
|
599
289
|
return await this.queriesBehindChannelGuard({
|
|
@@ -611,11 +301,8 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
611
301
|
};
|
|
612
302
|
|
|
613
303
|
/**
|
|
614
|
-
* Prepends the channel row to `createQueries`' output
|
|
615
|
-
*
|
|
616
|
-
*
|
|
617
|
-
* Skips the write entirely when neither can supply it: without a channel row the queries would fail
|
|
618
|
-
* 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.
|
|
619
306
|
*/
|
|
620
307
|
private queriesBehindChannelGuard = async ({
|
|
621
308
|
createQueries,
|
|
@@ -635,8 +322,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
635
322
|
|
|
636
323
|
let channelData = channelFromEvent;
|
|
637
324
|
if (!channelData && event_.channel_type && event_.channel_id) {
|
|
638
|
-
// `get`, not `ensure`:
|
|
639
|
-
// store an empty channel nothing uses.
|
|
325
|
+
// `get`, not `ensure`: `ensure` would store an empty channel when none is loaded.
|
|
640
326
|
const channelFromState = this.client.channelManager.get(
|
|
641
327
|
`${event_.channel_type}:${event_.channel_id}`,
|
|
642
328
|
);
|
|
@@ -679,13 +365,13 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
679
365
|
};
|
|
680
366
|
|
|
681
367
|
/**
|
|
682
|
-
*
|
|
683
|
-
*
|
|
684
|
-
*
|
|
685
|
-
* 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.
|
|
686
371
|
*
|
|
687
|
-
* @param payload.event -
|
|
688
|
-
* @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.
|
|
689
375
|
*/
|
|
690
376
|
public handleNewMessage = async ({
|
|
691
377
|
event,
|
|
@@ -701,9 +387,9 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
701
387
|
return [];
|
|
702
388
|
}
|
|
703
389
|
|
|
704
|
-
|
|
390
|
+
return await this.queriesWithChannelGuard(
|
|
705
391
|
{ event, execute },
|
|
706
|
-
async () => {
|
|
392
|
+
async (executeOverride) => {
|
|
707
393
|
let queries = await this.upsertMessages({
|
|
708
394
|
execute: false,
|
|
709
395
|
messages: [message],
|
|
@@ -711,11 +397,9 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
711
397
|
if (cid && client.user && client.user.id !== user?.id) {
|
|
712
398
|
const userId = client.user.id;
|
|
713
399
|
const channel = client.channelManager.get(cid);
|
|
714
|
-
//
|
|
715
|
-
//
|
|
716
|
-
//
|
|
717
|
-
// a read for those, so state.read[userId] may be absent here; fall back accordingly and rely
|
|
718
|
-
// on countUnread() (the aggregate the local count maintains) for the value.
|
|
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).
|
|
719
403
|
const tracksReadLocally = channelTracksReadLocally(channel);
|
|
720
404
|
if (channel && (channelHasReadEvents(channel) || tracksReadLocally)) {
|
|
721
405
|
const ownReads = channel.state.read[userId];
|
|
@@ -728,11 +412,9 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
728
412
|
last_read: ownReads?.last_read ?? 0,
|
|
729
413
|
last_read_message_id: ownReads?.last_read_message_id,
|
|
730
414
|
unread_messages: unreadCount,
|
|
731
|
-
// `client.user` is `ClientUser` (
|
|
732
|
-
//
|
|
733
|
-
//
|
|
734
|
-
// omits it when nothing is blocked, while `UserResponse` always carries it
|
|
735
|
-
// (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.
|
|
736
418
|
user: {
|
|
737
419
|
...client.user,
|
|
738
420
|
blocked_user_ids: client.user.blocked_user_ids ?? [],
|
|
@@ -743,23 +425,21 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
743
425
|
queries = [...queries, ...upsertReadsQueries];
|
|
744
426
|
}
|
|
745
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
|
+
}
|
|
746
432
|
return queries;
|
|
747
433
|
},
|
|
748
434
|
);
|
|
749
|
-
|
|
750
|
-
if (execute) {
|
|
751
|
-
await this.executeSqlBatch(finalQueries);
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
return finalQueries;
|
|
755
435
|
};
|
|
756
436
|
|
|
757
437
|
/**
|
|
758
|
-
*
|
|
759
|
-
* it should hard delete or soft delete the message.
|
|
438
|
+
* Hard or soft deletes the message, depending on `event.hard_delete`.
|
|
760
439
|
*
|
|
761
|
-
* @param payload.event -
|
|
762
|
-
* @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.
|
|
763
443
|
*/
|
|
764
444
|
public handleDeleteMessage = async ({
|
|
765
445
|
event,
|
|
@@ -787,12 +467,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
787
467
|
};
|
|
788
468
|
|
|
789
469
|
/**
|
|
790
|
-
*
|
|
791
|
-
* state as well as the DB. We want to drop all pending tasks and finally hard
|
|
792
|
-
* delete the message from the DB.
|
|
470
|
+
* Drops the message's pending tasks and hard deletes it.
|
|
793
471
|
*
|
|
794
472
|
* @param payload.messageId - The ID of the message we want to remove.
|
|
795
|
-
* @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.
|
|
796
475
|
*/
|
|
797
476
|
public handleRemoveMessage = async ({
|
|
798
477
|
messageId,
|
|
@@ -819,13 +498,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
819
498
|
};
|
|
820
499
|
|
|
821
500
|
/**
|
|
822
|
-
* Hard
|
|
823
|
-
* {@link hardDeleteMessage} calls: collects each delete's queries (`execute: false`) and runs them
|
|
824
|
-
* as one batch. Used e.g. by destructive reconciliation on reconnect to mirror a set of in-memory
|
|
825
|
-
* removals into the DB. No-op for an empty set.
|
|
501
|
+
* Hard deletes the messages in one transaction.
|
|
826
502
|
*
|
|
827
|
-
* @param payload.ids - The
|
|
828
|
-
* @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.
|
|
829
506
|
*/
|
|
830
507
|
public hardDeleteMessages = async ({
|
|
831
508
|
ids,
|
|
@@ -847,20 +524,14 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
847
524
|
};
|
|
848
525
|
|
|
849
526
|
/**
|
|
850
|
-
*
|
|
851
|
-
*
|
|
852
|
-
*
|
|
853
|
-
*
|
|
854
|
-
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
857
|
-
* The
|
|
858
|
-
* message in a freshly created channel can reach here before that channel has ever been persisted.
|
|
859
|
-
* `upsertMessages` alone is used rather than `updateMessage` because `updateMessage` is an
|
|
860
|
-
* UPDATE — it no-ops when the row does not exist yet, which is exactly the write-ahead case.
|
|
861
|
-
*
|
|
862
|
-
* @param payload.message - The message to persist. Its `cid` decides which channel row is guarded.
|
|
863
|
-
* @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.
|
|
864
535
|
*/
|
|
865
536
|
public upsertMessageWithChannelGuard = async ({
|
|
866
537
|
message,
|
|
@@ -874,9 +545,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
874
545
|
|
|
875
546
|
const messages = [message] as unknown as MessageResponse[];
|
|
876
547
|
|
|
877
|
-
//
|
|
878
|
-
// row when it actually fails. This runs on every optimistic message write, and the probe it
|
|
879
|
-
// 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`.
|
|
880
549
|
if (execute) {
|
|
881
550
|
try {
|
|
882
551
|
return await this.upsertMessages({ messages, execute: true });
|
|
@@ -893,11 +562,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
893
562
|
return await this.upsertMessageBehindChannelGuard({ cid, messages, execute });
|
|
894
563
|
};
|
|
895
564
|
|
|
896
|
-
/**
|
|
897
|
-
* Writes the channel row ahead of the messages so the insert cannot fail on the `cid` foreign key.
|
|
898
|
-
* Skips the write when there is nothing to build a channel row from — the message stays correct in
|
|
899
|
-
* memory either way, and an insert that cannot resolve its foreign key is not worth attempting.
|
|
900
|
-
*/
|
|
565
|
+
/** Writes the channel row ahead of the messages. Skips the write when no row can be built. */
|
|
901
566
|
private upsertMessageBehindChannelGuard = async ({
|
|
902
567
|
cid,
|
|
903
568
|
execute,
|
|
@@ -931,18 +596,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
931
596
|
};
|
|
932
597
|
|
|
933
598
|
/**
|
|
934
|
-
* The
|
|
935
|
-
*
|
|
936
|
-
*
|
|
937
|
-
* Concrete, composed from the existing {@link getChannels} primitive, so it adds nothing to
|
|
938
|
-
* {@link OfflineDBApi} that a custom offline DB would have to implement.
|
|
939
|
-
*
|
|
940
|
-
* This is the buffer that lets a failed message survive. v9 recovered such messages by diffing a
|
|
941
|
-
* second, lagging copy of the message list (the React SDK state) against `channel.state`; with a
|
|
942
|
-
* single reactive source of truth the persisted row is the only place that memory can be
|
|
943
|
-
* re-populated from — see `Channel.reload`.
|
|
599
|
+
* The channel's persisted messages with a local `failed` status, which `Channel.reload`
|
|
600
|
+
* re-ingests. Built on {@link getChannels}, so a custom offline DB implements nothing extra.
|
|
944
601
|
*
|
|
945
602
|
* @param payload.cid - The channel whose failed messages to read.
|
|
603
|
+
* @returns The failed messages, formatted.
|
|
946
604
|
*/
|
|
947
605
|
public getFailedMessages = async ({
|
|
948
606
|
cid,
|
|
@@ -961,15 +619,12 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
961
619
|
};
|
|
962
620
|
|
|
963
621
|
/**
|
|
964
|
-
*
|
|
965
|
-
* present in the event, or optionally rely on the hard override in unreadMessages.
|
|
966
|
-
* The unreadMessages argument is useful for cases where we know the exact number of unreads
|
|
967
|
-
* (for example reading an entire channel), but `unread_messages` might not necessarily exist
|
|
968
|
-
* in the event (or it exists with a stale value if we know what we want to ultimately update to).
|
|
622
|
+
* Upserts the reads of a read event.
|
|
969
623
|
*
|
|
970
624
|
* @param payload.event - The WS event we are trying to process.
|
|
971
|
-
* @param payload.unreadMessages - An override of `unread_messages
|
|
972
|
-
* @param payload.execute - Whether to immediately execute the
|
|
625
|
+
* @param payload.unreadMessages - An override of `event.unread_messages`.
|
|
626
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
627
|
+
* @returns The prepared queries.
|
|
973
628
|
*/
|
|
974
629
|
public handleRead = async ({
|
|
975
630
|
event,
|
|
@@ -992,11 +647,9 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
992
647
|
user,
|
|
993
648
|
cid,
|
|
994
649
|
} = event;
|
|
995
|
-
//
|
|
996
|
-
//
|
|
997
|
-
//
|
|
998
|
-
// the local clock at receipt — skewed against server time, and for a mark-unread plain wrong,
|
|
999
|
-
// since "now" is after the messages the user just marked unread — so it is only a fallback.
|
|
650
|
+
// Mirrors what `Channel` writes into `state.read`: mark-unread uses the server's
|
|
651
|
+
// `last_read_at`, other read events its `created_at`. `received_at` is the local clock, so only
|
|
652
|
+
// a fallback.
|
|
1000
653
|
const last_read =
|
|
1001
654
|
(event.type === 'notification.mark_unread' ? event.last_read_at : undefined) ??
|
|
1002
655
|
event.created_at ??
|
|
@@ -1026,12 +679,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1026
679
|
};
|
|
1027
680
|
|
|
1028
681
|
/**
|
|
1029
|
-
*
|
|
1030
|
-
* of each event with a channel guard and also forces an update of member_count
|
|
1031
|
-
* for the respective channel if applicable.
|
|
682
|
+
* Removes or upserts the member, forcing a channel upsert so `member_count` updates.
|
|
1032
683
|
*
|
|
1033
|
-
* @param payload.event -
|
|
1034
|
-
* @param payload.execute - Whether to immediately execute the
|
|
684
|
+
* @param payload.event - The WS event we are trying to process.
|
|
685
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
686
|
+
* @returns The prepared queries.
|
|
1035
687
|
*/
|
|
1036
688
|
public handleMemberEvent = async ({
|
|
1037
689
|
event,
|
|
@@ -1043,7 +695,6 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1043
695
|
const { member, cid, type } = event;
|
|
1044
696
|
|
|
1045
697
|
if (member && cid) {
|
|
1046
|
-
// we force update here so that member_count gets updated
|
|
1047
698
|
// TODO: Although this is more than fine for now, we should look into
|
|
1048
699
|
// changing this to be an actual update to the DB instead.
|
|
1049
700
|
return await this.queriesWithChannelGuard(
|
|
@@ -1066,11 +717,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1066
717
|
};
|
|
1067
718
|
|
|
1068
719
|
/**
|
|
1069
|
-
*
|
|
1070
|
-
* event handler within a channel guard.
|
|
720
|
+
* Updates the message behind the channel guard. Thread replies are ignored.
|
|
1071
721
|
*
|
|
1072
|
-
* @param payload.event -
|
|
1073
|
-
* @param payload.execute - Whether to immediately execute the
|
|
722
|
+
* @param payload.event - The WS event we are trying to process.
|
|
723
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
724
|
+
* @returns The prepared queries.
|
|
1074
725
|
*/
|
|
1075
726
|
public handleMessageUpdatedEvent = async ({
|
|
1076
727
|
event,
|
|
@@ -1093,14 +744,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1093
744
|
};
|
|
1094
745
|
|
|
1095
746
|
/**
|
|
1096
|
-
*
|
|
1097
|
-
* handler because event.channel.hidden does not arrive with the baseline event, so a
|
|
1098
|
-
* simple upsertion is not enough.
|
|
1099
|
-
* It will update the hidden property of a channel to true if handling the `channel.hidden`
|
|
1100
|
-
* event and to false if handling `channel.visible`.
|
|
747
|
+
* Sets `hidden` from the event type, as the event's channel does not carry it.
|
|
1101
748
|
*
|
|
1102
|
-
* @param payload. -
|
|
1103
|
-
* @param payload.execute - Whether to immediately execute the
|
|
749
|
+
* @param payload.event - The WS event we are trying to process.
|
|
750
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
751
|
+
* @returns The prepared queries.
|
|
1104
752
|
*/
|
|
1105
753
|
public handleChannelVisibilityEvent = async ({
|
|
1106
754
|
event,
|
|
@@ -1123,12 +771,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1123
771
|
};
|
|
1124
772
|
|
|
1125
773
|
/**
|
|
1126
|
-
*
|
|
1127
|
-
* removing all messages and relying on truncated_at as well. It will also upsert
|
|
1128
|
-
* reads adequately (and calculate the correct unread messages when truncating).
|
|
774
|
+
* Deletes the channel's messages (before `truncated_at` when set) and upserts the reads.
|
|
1129
775
|
*
|
|
1130
|
-
* @param payload.event -
|
|
1131
|
-
* @param payload.execute - Whether to immediately execute the
|
|
776
|
+
* @param payload.event - The WS event we are trying to process.
|
|
777
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
778
|
+
* @returns The prepared queries.
|
|
1132
779
|
*/
|
|
1133
780
|
public handleChannelTruncatedEvent = async ({
|
|
1134
781
|
event,
|
|
@@ -1149,10 +796,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1149
796
|
|
|
1150
797
|
let finalQueries = [...truncateQueries];
|
|
1151
798
|
|
|
1152
|
-
//
|
|
1153
|
-
// local unread count, also persist it for read-events-disabled channels (e.g. livestreams) so
|
|
1154
|
-
// the client-local count survives a cold start. state.read[userId] may be absent for those, so
|
|
1155
|
-
// fall back accordingly.
|
|
799
|
+
// Same condition as in `handleNewMessage`.
|
|
1156
800
|
const userId = ownUser.id;
|
|
1157
801
|
const activeChannel = this.client.channelManager.get(cid);
|
|
1158
802
|
const tracksReadLocally = channelTracksReadLocally(activeChannel);
|
|
@@ -1172,7 +816,6 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1172
816
|
last_read: ownReads?.last_read ?? 0,
|
|
1173
817
|
last_read_message_id: ownReads?.last_read_message_id,
|
|
1174
818
|
unread_messages: unreadCount,
|
|
1175
|
-
// See the note above: supply `blocked_user_ids`, do not assert it.
|
|
1176
819
|
user: {
|
|
1177
820
|
...ownUser,
|
|
1178
821
|
blocked_user_ids: ownUser.blocked_user_ids ?? [],
|
|
@@ -1194,14 +837,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1194
837
|
};
|
|
1195
838
|
|
|
1196
839
|
/**
|
|
1197
|
-
*
|
|
1198
|
-
* within a channel guard and maps them like so:
|
|
1199
|
-
* - reaction.new -> insertReaction
|
|
1200
|
-
* - reaction.updated -> updateReaction
|
|
1201
|
-
* - reaction.deleted -> deleteReaction
|
|
840
|
+
* Maps `reaction.new`, `.updated` and `.deleted` to the matching reaction write.
|
|
1202
841
|
*
|
|
1203
|
-
* @param payload.event -
|
|
1204
|
-
* @param payload.execute - Whether to immediately execute the
|
|
842
|
+
* @param payload.event - The WS event we are trying to process.
|
|
843
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
844
|
+
* @returns The prepared queries.
|
|
1205
845
|
*/
|
|
1206
846
|
public handleReactionEvent = async ({
|
|
1207
847
|
event,
|
|
@@ -1239,12 +879,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1239
879
|
};
|
|
1240
880
|
|
|
1241
881
|
/**
|
|
1242
|
-
*
|
|
1243
|
-
* - draft.updated -> updateDraft
|
|
1244
|
-
* - draft.deleted -> deleteDraft
|
|
882
|
+
* Maps `draft.updated` and `draft.deleted` to `upsertDraft` and `deleteDraft`.
|
|
1245
883
|
*
|
|
1246
|
-
* @param payload.event -
|
|
1247
|
-
* @param payload.execute - Whether to immediately execute the
|
|
884
|
+
* @param payload.event - The WS event we are trying to process.
|
|
885
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
886
|
+
* @returns The prepared queries.
|
|
1248
887
|
*/
|
|
1249
888
|
handleDraftEvent = async ({
|
|
1250
889
|
event,
|
|
@@ -1278,12 +917,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1278
917
|
};
|
|
1279
918
|
|
|
1280
919
|
/**
|
|
1281
|
-
*
|
|
1282
|
-
* event.type for all events we are currently handling. It is used to both
|
|
1283
|
-
* react on WS events as well as process the sync API events.
|
|
920
|
+
* Routes an event to its handler by type. Used for WS events and for sync events.
|
|
1284
921
|
*
|
|
1285
|
-
* @param payload.event -
|
|
1286
|
-
* @param payload.execute - Whether to immediately execute the
|
|
922
|
+
* @param payload.event - The WS event we are trying to process.
|
|
923
|
+
* @param payload.execute - Whether to immediately execute the queries. Defaults to `true`.
|
|
924
|
+
* @returns The prepared queries.
|
|
1287
925
|
*/
|
|
1288
926
|
public handleEvent = async ({
|
|
1289
927
|
event,
|
|
@@ -1314,21 +952,17 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1314
952
|
}
|
|
1315
953
|
|
|
1316
954
|
if (type === 'message.read' || type === 'notification.mark_read') {
|
|
1317
|
-
//
|
|
1318
|
-
// the offline DB in any case) whenever we receive a read event for a
|
|
1319
|
-
// a thread specifically.
|
|
955
|
+
// The DB stores channel reads only, so thread read events are skipped.
|
|
1320
956
|
if (event.thread) return [];
|
|
1321
957
|
return this.handleRead({ event, unreadMessages: 0, execute });
|
|
1322
958
|
}
|
|
1323
959
|
|
|
1324
|
-
//
|
|
1325
|
-
//
|
|
1326
|
-
// situation it is ever dispatched), so it can never touch channels that track reads server-side.
|
|
960
|
+
// The client-only reset from `Channel.markReadLocally()`: persisted only for channels with a
|
|
961
|
+
// local unread count, never for channels that track reads server-side.
|
|
1327
962
|
if (type === 'message.read_locally') {
|
|
1328
963
|
const localChannel = event.cid
|
|
1329
964
|
? this.client.channelManager.get(event.cid)
|
|
1330
965
|
: undefined;
|
|
1331
|
-
// channelTracksReadLocally returns false when localChannel is undefined, so no extra guard.
|
|
1332
966
|
if (channelTracksReadLocally(localChannel)) {
|
|
1333
967
|
return this.handleRead({ event, unreadMessages: 0, execute });
|
|
1334
968
|
}
|
|
@@ -1354,13 +988,8 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1354
988
|
return await this.handleDraftEvent({ event, execute });
|
|
1355
989
|
}
|
|
1356
990
|
|
|
1357
|
-
//
|
|
1358
|
-
//
|
|
1359
|
-
// because we anyway cannot get the messages for a channel until we run
|
|
1360
|
-
// either channel.watch() or channel.query(...) to get them. So, when
|
|
1361
|
-
// receiving the event we only upsert the channel data and we leave the
|
|
1362
|
-
// rest of the entities to be updated whenever we actually start watching
|
|
1363
|
-
// or we at least query.
|
|
991
|
+
// `notification.message_new` only upserts the channel data: its messages are stored when the
|
|
992
|
+
// channel is watched or queried.
|
|
1364
993
|
if (
|
|
1365
994
|
(type === 'channel.updated' ||
|
|
1366
995
|
type === 'notification.message_new' ||
|
|
@@ -1387,36 +1016,16 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1387
1016
|
};
|
|
1388
1017
|
|
|
1389
1018
|
/**
|
|
1390
|
-
*
|
|
1391
|
-
*
|
|
1392
|
-
*
|
|
1393
|
-
* 1. Execute the task immediately
|
|
1394
|
-
* 2. If this fails, checks if the failure was due to something valid for a pending task
|
|
1395
|
-
* 3. If it is, it will insert the task in the pending tasks table
|
|
1396
|
-
*
|
|
1397
|
-
* It will return the response from the execution if it succeeded.
|
|
1019
|
+
* Executes the task now. On an {@link isEphemeral} failure the task goes to
|
|
1020
|
+
* `handleAddPendingTask`, and the error is rethrown. With an unhealthy socket it fails with an
|
|
1021
|
+
* `OfflineError` without executing.
|
|
1398
1022
|
*
|
|
1399
|
-
*
|
|
1400
|
-
*
|
|
1401
|
-
* the
|
|
1023
|
+
* The payload carries the request options, including an `AbortSignal`. It is live while the task
|
|
1024
|
+
* runs now, but a replay from the table gets an inert `{}` when the store serializes the payload
|
|
1025
|
+
* (always across a restart), and `ApiClient` then runs the request uncancellable.
|
|
1402
1026
|
*
|
|
1403
|
-
*
|
|
1404
|
-
*
|
|
1405
|
-
* - A task that reaches the pending-tasks table survives only as far as the concrete
|
|
1406
|
-
* implementation's storage allows. {@link OfflineDBApi.addPendingTask} and
|
|
1407
|
-
* {@link OfflineDBApi.getPendingTasks} make no serialization promise either way: a store that
|
|
1408
|
-
* hands back the object it was given keeps the signal live, while one that encodes the payload
|
|
1409
|
-
* (as a SQLite-backed store does) revives it as an inert `{}`. Across a process restart it is
|
|
1410
|
-
* always the latter.
|
|
1411
|
-
*
|
|
1412
|
-
* `ApiClient` drops a signal it cannot listen to rather than handing it to axios (which would
|
|
1413
|
-
* throw), so a replay that lost its signal simply runs uncancellable.
|
|
1414
|
-
*
|
|
1415
|
-
* Note that cancelling is not a way to drop a queued task after the fact — an aborted request is
|
|
1416
|
-
* a definitive rejection ({@link isEphemeral} is `false` for it), so it is never queued in the
|
|
1417
|
-
* first place.
|
|
1418
|
-
*
|
|
1419
|
-
* @param task - the pending task we want to execute
|
|
1027
|
+
* @param payload.task - The pending task we want to execute.
|
|
1028
|
+
* @returns The response of the task's request.
|
|
1420
1029
|
*/
|
|
1421
1030
|
public queueTask = async <T>({ task }: { task: PendingTask }): Promise<T> => {
|
|
1422
1031
|
const attemptTaskExecution = async () => {
|
|
@@ -1438,16 +1047,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1438
1047
|
}
|
|
1439
1048
|
};
|
|
1440
1049
|
|
|
1441
|
-
/**
|
|
1442
|
-
* A utility method that determines if a failed task should be skipped (NOT added to the queue) -
|
|
1443
|
-
* i.e. the failure is a definitive rejection rather than an ephemeral/retryable one. A task is
|
|
1444
|
-
* kept in the queue only when its error is {@link isEphemeral} (connection/network error or a
|
|
1445
|
-
* retryable server code). A non retryable server response (i.e bad request, not allowed etc) is
|
|
1446
|
-
* skipped since retrying it would never succeed, and so is a request the caller cancelled via
|
|
1447
|
-
* `StreamRequestOptions.signal` - replaying it would defeat the point of aborting.
|
|
1448
|
-
*
|
|
1449
|
-
* @param error - The error thrown while executing the failed task.
|
|
1450
|
-
*/
|
|
1050
|
+
/** A definitive rejection (non-retryable server error or a cancelled request) is not queued. */
|
|
1451
1051
|
private shouldSkipQueueingTask = (error: AxiosError<APIError>) => !isEphemeral(error);
|
|
1452
1052
|
|
|
1453
1053
|
private isPendingSendMessageTask = (
|
|
@@ -1456,20 +1056,13 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1456
1056
|
task.type === 'send-message';
|
|
1457
1057
|
|
|
1458
1058
|
/**
|
|
1459
|
-
* Folds an edit of a
|
|
1460
|
-
*
|
|
1461
|
-
*
|
|
1462
|
-
* message ordered against everything queued after it.
|
|
1463
|
-
*
|
|
1464
|
-
* Keyed on a queued `send-message` task existing for this id - the only reliable statement of "the
|
|
1465
|
-
* server has never seen this message". NOT on the edited message's status: `failed` is also what a
|
|
1466
|
-
* rejected edit of a message the server has carries.
|
|
1059
|
+
* Folds an edit of a never-sent message into its queued `send-message` task, so a replay sends
|
|
1060
|
+
* the edited text once. The send task keeps its row and so its place in the queue. With no send
|
|
1061
|
+
* task the edit is queued on its own if `isMessageUpdateReplayable` allows it.
|
|
1467
1062
|
*
|
|
1468
|
-
*
|
|
1469
|
-
*
|
|
1470
|
-
* would
|
|
1471
|
-
*
|
|
1472
|
-
* With nothing to fold into, the edit is queued on its own and replays as an ordinary update.
|
|
1063
|
+
* A queued send is the only reliable sign the server never saw the message (a `failed` status can
|
|
1064
|
+
* also come from a rejected edit). The payloads are already `MessageRequest`s, so they merge by
|
|
1065
|
+
* plain spread: normalizing again would turn a flattened `mentioned_users` into `[undefined]`.
|
|
1473
1066
|
*/
|
|
1474
1067
|
private handleUpdateMessagePendingTask = async (
|
|
1475
1068
|
task: Extract<PendingTask, { type: 'update-message' }>,
|
|
@@ -1478,8 +1071,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1478
1071
|
const pendingTasks = await this.getPendingTasks({ messageId: id });
|
|
1479
1072
|
const sendTask = pendingTasks.find(this.isPendingSendMessageTask);
|
|
1480
1073
|
|
|
1481
|
-
// No queued send
|
|
1482
|
-
// Either way there is nothing to fold into, so the edit goes in as its own task.
|
|
1074
|
+
// No queued send, or one without an ID to rewrite (a task read from the DB always has one).
|
|
1483
1075
|
if (sendTask?.id === undefined) {
|
|
1484
1076
|
if (!isMessageUpdateReplayable(message)) return;
|
|
1485
1077
|
|
|
@@ -1502,8 +1094,11 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1502
1094
|
};
|
|
1503
1095
|
|
|
1504
1096
|
/**
|
|
1505
|
-
*
|
|
1506
|
-
*
|
|
1097
|
+
* Stores the task. An edit of a never-sent message is folded into its queued send instead. With no
|
|
1098
|
+
* queued send, an edit `isMessageUpdateReplayable` refuses (attachments still on local URLs) is
|
|
1099
|
+
* dropped.
|
|
1100
|
+
*
|
|
1101
|
+
* @param payload.task - The pending task to store.
|
|
1507
1102
|
*/
|
|
1508
1103
|
public handleAddPendingTask = async ({ task }: { task: PendingTask }) => {
|
|
1509
1104
|
if (task.type === 'update-message') {
|
|
@@ -1515,30 +1110,8 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1515
1110
|
};
|
|
1516
1111
|
|
|
1517
1112
|
/**
|
|
1518
|
-
*
|
|
1519
|
-
*
|
|
1520
|
-
* - Updating a message
|
|
1521
|
-
* - Deleting a message
|
|
1522
|
-
* - Sending a reaction
|
|
1523
|
-
* - Removing a reaction
|
|
1524
|
-
* - Sending a message
|
|
1525
|
-
* - Creating a draft
|
|
1526
|
-
* - Deleting a draft
|
|
1527
|
-
* It will throw if we try to execute a pending task that is not supported.
|
|
1528
|
-
*
|
|
1529
|
-
* @param task - The task we want to execute
|
|
1530
|
-
* @param isPendingTask - a control value telling us if it's an actual pending task being executed
|
|
1531
|
-
* or delayed execution
|
|
1532
|
-
*/
|
|
1533
|
-
/**
|
|
1534
|
-
* Replays one queued operation, through the {@link QUEUEABLE_OPERATIONS} registry rather than a
|
|
1535
|
-
* per-type branch here — the same registry a first attempt resolves through, so the two cannot
|
|
1536
|
-
* describe different requests.
|
|
1537
|
-
*
|
|
1538
|
-
* @param task - the task to run.
|
|
1539
|
-
* @param isPendingTask - whether this is a replay rather than a first attempt. A first attempt has an
|
|
1540
|
-
* optimistic layer above it that settles local state; a replay does not, so some operations have to
|
|
1541
|
-
* settle it themselves (see `onReplay`).
|
|
1113
|
+
* Runs the task through {@link QUEUEABLE_OPERATIONS}. With `isPendingTask` (a replay) it also
|
|
1114
|
+
* runs the operation's `onReplay`.
|
|
1542
1115
|
*/
|
|
1543
1116
|
private executeTask = async <T extends QueueableType>(
|
|
1544
1117
|
{ task }: { task: PendingTaskOf<T> },
|
|
@@ -1555,10 +1128,8 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1555
1128
|
};
|
|
1556
1129
|
|
|
1557
1130
|
/**
|
|
1558
|
-
*
|
|
1559
|
-
*
|
|
1560
|
-
* it is going to remove the pending task from the DB even if execution fails, otherwise
|
|
1561
|
-
* it will keep it for the next time we try to execute all pending taks.
|
|
1131
|
+
* Replays the pending tasks in order and deletes each one that succeeded or was definitively
|
|
1132
|
+
* rejected. A task that failed for an ephemeral reason stays for the next run.
|
|
1562
1133
|
*/
|
|
1563
1134
|
public executePendingTasks = async () => {
|
|
1564
1135
|
const queue = await this.getPendingTasks();
|
|
@@ -1577,7 +1148,6 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
|
|
|
1577
1148
|
} catch (e) {
|
|
1578
1149
|
const error = e as AxiosError<APIError>;
|
|
1579
1150
|
if (!this.shouldSkipQueueingTask(error)) {
|
|
1580
|
-
// executing the pending task has failed, so keep it in the queue
|
|
1581
1151
|
continue;
|
|
1582
1152
|
}
|
|
1583
1153
|
}
|