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
|
@@ -15,12 +15,10 @@ import { StoreBackedItemIndex } from '../../entityStore/StoreBackedItemIndex';
|
|
|
15
15
|
import { makeComparator } from '../sortCompiler';
|
|
16
16
|
import { resolveDotPathValue } from '../utility.normalization';
|
|
17
17
|
|
|
18
|
-
// Reminders are keyed by the message they belong to
|
|
18
|
+
// Reminders are keyed by the message they belong to.
|
|
19
19
|
const getReminderId = (reminder: ReminderResponseData) => reminder.message_id;
|
|
20
20
|
|
|
21
|
-
//
|
|
22
|
-
// (ReminderManager stores reminders in a message_id-keyed Map), but interval storage needs a total
|
|
23
|
-
// order, so default to a deterministic one.
|
|
21
|
+
// Interval storage needs a total order, so default to a deterministic one when no sort is set.
|
|
24
22
|
const DEFAULT_SORT: SortParamRequest[] = [{ direction: 1, field: 'created_at' }];
|
|
25
23
|
|
|
26
24
|
export class ReminderPaginator extends BasePaginator<
|
|
@@ -70,8 +68,7 @@ export class ReminderPaginator extends BasePaginator<
|
|
|
70
68
|
return getReminderId(item);
|
|
71
69
|
}
|
|
72
70
|
|
|
73
|
-
//
|
|
74
|
-
// changes, which also resets the accumulated pages), with a message_id tiebreaker.
|
|
71
|
+
// Total order for interval storage: the requested sort, with a message_id tiebreaker.
|
|
75
72
|
private buildSortComparator() {
|
|
76
73
|
return makeComparator<ReminderResponseData>({
|
|
77
74
|
sort: this._sort ?? DEFAULT_SORT,
|
|
@@ -81,27 +78,27 @@ export class ReminderPaginator extends BasePaginator<
|
|
|
81
78
|
});
|
|
82
79
|
}
|
|
83
80
|
|
|
84
|
-
protected getNextQueryShape({
|
|
85
|
-
direction,
|
|
86
|
-
}: Required<
|
|
87
|
-
Pick<PaginationQueryParams<QueryRemindersRequest>, 'direction'>
|
|
88
|
-
>): QueryRemindersRequest {
|
|
89
|
-
const cursor = this.cursor?.[direction] ?? undefined;
|
|
81
|
+
protected getNextQueryShape(): QueryRemindersRequest {
|
|
90
82
|
return {
|
|
91
83
|
filter: this.filters,
|
|
92
84
|
sort: this.sort,
|
|
93
85
|
limit: this.pageSize,
|
|
94
|
-
// The request names its cursors after the response's `next` / `prev`, not after the direction.
|
|
95
|
-
...(cursor ? { [direction === 'tailward' ? 'next' : 'prev']: cursor } : {}),
|
|
96
86
|
};
|
|
97
87
|
}
|
|
98
88
|
|
|
99
89
|
query = async ({
|
|
90
|
+
direction,
|
|
100
91
|
queryShape,
|
|
101
92
|
}: PaginationQueryParams<QueryRemindersRequest>): Promise<
|
|
102
93
|
PaginationQueryReturnValue<ReminderResponseData>
|
|
103
94
|
> => {
|
|
104
|
-
const
|
|
95
|
+
const cursor = direction ? (this.cursor?.[direction] ?? undefined) : undefined;
|
|
96
|
+
const request: QueryRemindersRequest = {
|
|
97
|
+
...(queryShape ?? this.getNextQueryShape()),
|
|
98
|
+
// The request names its cursors after the response's `next` / `prev`, not after the direction.
|
|
99
|
+
...(cursor ? { [direction === 'tailward' ? 'next' : 'prev']: cursor } : {}),
|
|
100
|
+
};
|
|
101
|
+
const { reminders: items, next, prev } = await this.client.queryReminders(request);
|
|
105
102
|
return { items, headward: prev, tailward: next };
|
|
106
103
|
};
|
|
107
104
|
|
|
@@ -15,7 +15,7 @@ import { isEqual } from '../../utils/mergeWith/mergeWithCore';
|
|
|
15
15
|
|
|
16
16
|
export const DEFAULT_THREAD_PAGE_SIZE = 25;
|
|
17
17
|
|
|
18
|
-
/**
|
|
18
|
+
/** Default query options for every page of the thread list. */
|
|
19
19
|
export const DEFAULT_THREAD_QUERY: QueryThreadsRequest = {
|
|
20
20
|
participant_limit: 10,
|
|
21
21
|
reply_limit: 10,
|
|
@@ -36,18 +36,16 @@ const hasPaginationQueryShapeChanged: PaginationQueryShapeChangeIdentifier<
|
|
|
36
36
|
|
|
37
37
|
export type ThreadPaginatorOptions = {
|
|
38
38
|
client: StreamChat;
|
|
39
|
-
/**
|
|
39
|
+
/** Store holding the list's threads (`client.threads`' thread store). Omitted: a private one. */
|
|
40
40
|
store?: EntityStore<Thread>;
|
|
41
41
|
paginatorOptions?: PaginatorOptions<Thread, QueryThreadsRequest>;
|
|
42
42
|
};
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
|
-
* The thread list, in the order `queryThreads` returns it.
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* interval storage on every mark-read or reply. Pages append in server order and a reload replaces
|
|
50
|
-
* the list ({@link ThreadPaginator.reload}).
|
|
45
|
+
* The thread list, in the order `queryThreads` returns it. There is no client-side sort: the server
|
|
46
|
+
* order (unread first, then latest reply) depends on state a `Thread` mutates in place, so a
|
|
47
|
+
* comparator would corrupt interval storage. Pages append in server order and
|
|
48
|
+
* {@link ThreadPaginator.reload} replaces the list.
|
|
51
49
|
*/
|
|
52
50
|
export class ThreadPaginator extends BasePaginator<Thread, QueryThreadsRequest> {
|
|
53
51
|
private readonly client: StreamChat;
|
|
@@ -73,10 +71,7 @@ export class ThreadPaginator extends BasePaginator<Thread, QueryThreadsRequest>
|
|
|
73
71
|
this.store = store;
|
|
74
72
|
}
|
|
75
73
|
|
|
76
|
-
/**
|
|
77
|
-
* No next page until a load has landed (`master`'s `nextCursor: null`), so `toTail()` is a no-op
|
|
78
|
-
* before the first successful reload, e.g. when an empty list fires `onEndReached` on mount.
|
|
79
|
-
*/
|
|
74
|
+
/** No next page until a load has landed, so `toTail()` is a no-op before the first reload. */
|
|
80
75
|
get initialState(): PaginatorState<Thread> {
|
|
81
76
|
return {
|
|
82
77
|
...super.initialState,
|
|
@@ -111,9 +106,9 @@ export class ThreadPaginator extends BasePaginator<Thread, QueryThreadsRequest>
|
|
|
111
106
|
};
|
|
112
107
|
|
|
113
108
|
/**
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
109
|
+
* Keeps one instance per ID (the index upserts, so a second one would replace the stored one). A
|
|
110
|
+
* stale stored thread is hydrated from the query unless it is active: it reloads itself, and
|
|
111
|
+
* hydrating would replace the replies on screen.
|
|
117
112
|
*/
|
|
118
113
|
private toStoredInstance = (incoming: Thread): Thread => {
|
|
119
114
|
const stored = this.store.get(incoming.id);
|
|
@@ -127,10 +122,10 @@ export class ThreadPaginator extends BasePaginator<Thread, QueryThreadsRequest>
|
|
|
127
122
|
filterQueryResults = (threads: Thread[]) => threads;
|
|
128
123
|
|
|
129
124
|
/**
|
|
130
|
-
* Re-queries the first page and
|
|
131
|
-
* current threads stay until the response lands
|
|
132
|
-
*
|
|
133
|
-
*
|
|
125
|
+
* Re-queries the first page and replaces the list in server order; also the first load. The
|
|
126
|
+
* current threads stay until the response lands. A failed query leaves them and records
|
|
127
|
+
* `lastQueryError`; a success clears it. Only the first load publishes `isLoading` (with items it
|
|
128
|
+
* would mean the next page).
|
|
134
129
|
*/
|
|
135
130
|
reload = async ({ limit = this.pageSize }: { limit?: number } = {}) => {
|
|
136
131
|
const isFirstLoad = !this.hasResults;
|
|
@@ -143,9 +138,7 @@ export class ThreadPaginator extends BasePaginator<Thread, QueryThreadsRequest>
|
|
|
143
138
|
});
|
|
144
139
|
if (!results) return;
|
|
145
140
|
|
|
146
|
-
//
|
|
147
|
-
// and stale ids get replaced (and subsequently evicted from the entity index if no holders
|
|
148
|
-
// exist).
|
|
141
|
+
// Replace rather than merge (the order is server-driven): IDs absent from the response go.
|
|
149
142
|
const nextIds = new Set(results.items.map((thread) => thread.id));
|
|
150
143
|
this.setIntervals([]);
|
|
151
144
|
this.setActiveInterval(undefined);
|
|
@@ -170,10 +163,7 @@ export class ThreadPaginator extends BasePaginator<Thread, QueryThreadsRequest>
|
|
|
170
163
|
}
|
|
171
164
|
};
|
|
172
165
|
|
|
173
|
-
/**
|
|
174
|
-
* Also releases the list's hold on its threads, which the base reset leaves in the index. Released
|
|
175
|
-
* before the base publishes, so a subscriber sees the threads already gone from the registry.
|
|
176
|
-
*/
|
|
166
|
+
/** Also clears the item index (the base reset keeps it), before the base publishes. */
|
|
177
167
|
resetState() {
|
|
178
168
|
this._itemIndex.clear();
|
|
179
169
|
super.resetState();
|
|
@@ -30,12 +30,8 @@ const decodeCursor = <TCursor extends object>(cursor: string | null | undefined)
|
|
|
30
30
|
};
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
|
-
* Paginates
|
|
34
|
-
*
|
|
35
|
-
* This entity only supports forward (tailward) cursor pagination via
|
|
36
|
-
* `created_at_gt` and `id_gt`. Backward (headward) pagination is not available
|
|
37
|
-
* because the API does not expose a backward cursor, so the headward cursor is
|
|
38
|
-
* initialized as exhausted (`null`).
|
|
33
|
+
* Paginates `/usergroups` forward (tailward) only, via `created_at_gt` and `id_gt`. The API has no
|
|
34
|
+
* backward cursor, so the headward cursor starts exhausted (`null`).
|
|
39
35
|
*/
|
|
40
36
|
export class UserGroupPaginator extends BasePaginator<
|
|
41
37
|
UserGroupResponse,
|
|
@@ -57,9 +53,7 @@ export class UserGroupPaginator extends BasePaginator<
|
|
|
57
53
|
...options,
|
|
58
54
|
});
|
|
59
55
|
this.client = client;
|
|
60
|
-
//
|
|
61
|
-
// the forward cursor (`created_at_gt`, `id_gt`), i.e. ascending `created_at` then `id` — mirror
|
|
62
|
-
// that here so the visible order matches the server's.
|
|
56
|
+
// Total order for interval storage, mirroring the server's ascending (`created_at`, `id`).
|
|
63
57
|
this.sortComparator = (a, b) => {
|
|
64
58
|
if (a.created_at !== b.created_at) return a.created_at < b.created_at ? -1 : 1;
|
|
65
59
|
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
|
@@ -89,19 +83,14 @@ export class UserGroupPaginator extends BasePaginator<
|
|
|
89
83
|
if (!lastItem) return undefined;
|
|
90
84
|
|
|
91
85
|
return JSON.stringify({
|
|
92
|
-
//
|
|
93
|
-
//
|
|
94
|
-
// `Date` holds only milliseconds, so flooring the boundary item's timestamp would put the
|
|
95
|
-
// cursor below it and a strict `created_at_gt` could hand that same item back on the next
|
|
96
|
-
// page.
|
|
86
|
+
// `nsToRfc3339` keeps the nanoseconds: a `Date` would floor the boundary timestamp and the
|
|
87
|
+
// strict `created_at_gt` could return that same item on the next page.
|
|
97
88
|
created_at_gt: nsToRfc3339(lastItem.created_at),
|
|
98
89
|
id_gt: lastItem.id,
|
|
99
90
|
} satisfies UserGroupListCursor);
|
|
100
91
|
};
|
|
101
92
|
|
|
102
|
-
// The
|
|
103
|
-
// accumulated list when the query shape changes ('auto' reset policy), so the
|
|
104
|
-
// forward cursor is NOT part of the shape — it is applied per request in `query`.
|
|
93
|
+
// The cursor stays out of the shape (a shape change resets the list): `query` applies it.
|
|
105
94
|
protected getNextQueryShape(): ListUserGroupsOptions {
|
|
106
95
|
return {
|
|
107
96
|
limit: this.pageSize,
|
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Global switch for the message-list `state` publish throttle (see `BasePaginator.scheduleWindowPublish`).
|
|
3
|
-
*
|
|
4
|
-
* Auto-disabled under test runners (Vitest / Jest) so the existing unit suites keep their synchronous,
|
|
5
|
-
* un-throttled behavior with zero per-test changes: with throttling off, every live mutation publishes
|
|
6
|
-
* to `state` immediately, exactly as before this feature. A dedicated throttle test flips it on
|
|
7
|
-
* (around fake timers) via {@link setStateThrottlingEnabled} and restores it afterwards.
|
|
8
|
-
*
|
|
9
|
-
* Production (an app bundle) defaults ON.
|
|
10
|
-
*/
|
|
11
1
|
const isTestRunner = (): boolean => {
|
|
12
2
|
try {
|
|
13
3
|
return (
|
|
@@ -22,6 +12,11 @@ const isTestRunner = (): boolean => {
|
|
|
22
12
|
}
|
|
23
13
|
};
|
|
24
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Global switch for the `state` publish throttle (see `BasePaginator.scheduleWindowPublish`). On by
|
|
17
|
+
* default, off under test runners (Vitest / Jest) so every mutation publishes synchronously. Tests
|
|
18
|
+
* of the throttle itself turn it on with {@link setStateThrottlingEnabled}.
|
|
19
|
+
*/
|
|
25
20
|
let enabled = !isTestRunner();
|
|
26
21
|
|
|
27
22
|
export const isStateThrottlingEnabled = (): boolean => enabled;
|
|
@@ -12,15 +12,10 @@ export type ItemLocation = {
|
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* - a getter `getItemAt(index)` that returns the item (or undefined)
|
|
20
|
-
*
|
|
21
|
-
* It returns:
|
|
22
|
-
* - current: actual index of the item in the array
|
|
23
|
-
* - expected: lower-bound position where the item belongs according to compare function
|
|
15
|
+
* Binary search over an abstract sorted array. Returns the needle's `currentIndex` (`-1` if absent)
|
|
16
|
+
* and the `insertionIndex` where `compare` places it (after items that compare equal). With
|
|
17
|
+
* `plateauScan`, a needle not at the insertion index is looked for by identity, scanning outward
|
|
18
|
+
* from it.
|
|
24
19
|
*/
|
|
25
20
|
export function binarySearch<T>({
|
|
26
21
|
needle,
|
|
@@ -30,43 +25,32 @@ export function binarySearch<T>({
|
|
|
30
25
|
compare,
|
|
31
26
|
plateauScan,
|
|
32
27
|
}: {
|
|
33
|
-
/** Target item in the searched array */
|
|
34
28
|
needle: T;
|
|
35
29
|
length: number;
|
|
36
|
-
/**
|
|
37
|
-
* Therefore, we do not access the array directly but allow to determine, how the item is constructed.
|
|
38
|
-
*/
|
|
30
|
+
/** Reads the item at an index (the array may hold IDs resolved through an index). */
|
|
39
31
|
getItemAt: (index: number) => T | undefined;
|
|
40
|
-
/**
|
|
32
|
+
/** Identity, not equality under the sort order. */
|
|
41
33
|
itemIdentityEquals: (item1: T, item2: T) => boolean;
|
|
42
|
-
/** Used to determine equality from the sort order point of view. */
|
|
43
34
|
compare: Comparator<T>;
|
|
44
35
|
plateauScan?: boolean;
|
|
45
36
|
}): ItemLocation {
|
|
46
|
-
// empty array
|
|
47
37
|
if (length === 0) return { currentIndex: -1, insertionIndex: 0 };
|
|
48
38
|
|
|
49
|
-
// --- 1) Binary search to find lower bound (insertionIndex) ---
|
|
50
39
|
let lo = 0;
|
|
51
40
|
let hi = length;
|
|
52
41
|
|
|
53
42
|
while (lo < hi) {
|
|
54
|
-
const mid = (lo + hi) >> 1;
|
|
43
|
+
const mid = (lo + hi) >> 1;
|
|
55
44
|
const midItem = getItemAt(mid);
|
|
56
45
|
if (!midItem) {
|
|
57
|
-
//
|
|
58
|
-
// Bail out with "not found".
|
|
46
|
+
// An ID without a backing item: report not found.
|
|
59
47
|
return { currentIndex: -1, insertionIndex: -1 };
|
|
60
48
|
}
|
|
61
49
|
|
|
62
50
|
if (itemIdentityEquals(midItem, needle) && mid > 0) {
|
|
63
|
-
// The
|
|
64
|
-
//
|
|
65
|
-
//
|
|
66
|
-
// `Channel` mutated in place before the paginator is told) already carry the new value.
|
|
67
|
-
// Walking right on that 0 reports "insert just after where you are" — read downstream as
|
|
68
|
-
// "already in place" — so the item never moves. The left neighbour is the reliable signal;
|
|
69
|
-
// otherwise fall through, keeping the established end-of-plateau result.
|
|
51
|
+
// The needle's own slot compares 0 and says nothing about where it belongs, and its value may
|
|
52
|
+
// have just changed in place (items held by reference): use the left neighbour, or it never
|
|
53
|
+
// moves.
|
|
70
54
|
const leftItem = getItemAt(mid - 1);
|
|
71
55
|
if (leftItem && compare(leftItem, needle) > 0) {
|
|
72
56
|
hi = mid;
|
|
@@ -75,17 +59,14 @@ export function binarySearch<T>({
|
|
|
75
59
|
}
|
|
76
60
|
|
|
77
61
|
if (compare(midItem, needle) <= 0) {
|
|
78
|
-
// midItem < needle ⇒ go right
|
|
79
62
|
lo = mid + 1;
|
|
80
63
|
} else {
|
|
81
|
-
// midItem ≥ needle ⇒ go left
|
|
82
64
|
hi = mid;
|
|
83
65
|
}
|
|
84
66
|
}
|
|
85
67
|
|
|
86
68
|
const insertionIndex = lo;
|
|
87
69
|
|
|
88
|
-
// item is located where it is expected to be according to the sort
|
|
89
70
|
const itemAtExpectedIndex = getItemAt(insertionIndex);
|
|
90
71
|
if (itemAtExpectedIndex && itemIdentityEquals(itemAtExpectedIndex, needle)) {
|
|
91
72
|
return { currentIndex: insertionIndex, insertionIndex };
|
|
@@ -95,8 +76,6 @@ export function binarySearch<T>({
|
|
|
95
76
|
return { currentIndex: -1, insertionIndex };
|
|
96
77
|
}
|
|
97
78
|
|
|
98
|
-
// --- 2) Plateau scan around insertionIndex ---
|
|
99
|
-
|
|
100
79
|
const checkSide = (atIndex: number) => {
|
|
101
80
|
const result = { exhausted: false, found: false };
|
|
102
81
|
const item = getItemAt(atIndex);
|
|
@@ -105,9 +84,8 @@ export function binarySearch<T>({
|
|
|
105
84
|
return result;
|
|
106
85
|
};
|
|
107
86
|
|
|
108
|
-
// Alternating left/right scan
|
|
109
87
|
let iLeft = insertionIndex - 1;
|
|
110
|
-
let iRight = insertionIndex + 1; //
|
|
88
|
+
let iRight = insertionIndex + 1; // insertionIndex itself is already checked
|
|
111
89
|
let leftDone = iLeft < 0;
|
|
112
90
|
let rightDone = iRight >= length;
|
|
113
91
|
|
|
@@ -125,22 +103,19 @@ export function binarySearch<T>({
|
|
|
125
103
|
}
|
|
126
104
|
}
|
|
127
105
|
|
|
128
|
-
// Not found in plateau; insertion index is still the correct lower bound.
|
|
129
106
|
return { currentIndex: -1, insertionIndex };
|
|
130
107
|
}
|
|
131
108
|
|
|
132
109
|
/**
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
* Zero (0) → leave a and b unchanged relative to each other
|
|
136
|
-
* (but they can still move relative to others — sort in JS is not guaranteed stable in older engines, though modern V8/Node/Chrome/Firefox make it stable)
|
|
137
|
-
*
|
|
138
|
-
* Positive number (> 0) → a comes after b
|
|
110
|
+
* Builds a comparator from a sort spec: terms apply in order, then `tiebreaker`.
|
|
111
|
+
* `null`/`undefined` values sort last whatever the direction.
|
|
139
112
|
*
|
|
140
113
|
* @param params - Comparator configuration.
|
|
141
114
|
* @param params.sort - The sort specification defining fields and directions.
|
|
142
115
|
* @param params.resolvePathValue - Resolver used to read a field value from an item.
|
|
143
|
-
* @param params.tiebreaker - Comparator applied when all sort terms are equal.
|
|
116
|
+
* @param params.tiebreaker - Comparator applied when all sort terms are equal. Defaults to
|
|
117
|
+
* comparing `cid`.
|
|
118
|
+
* @returns The comparator.
|
|
144
119
|
*/
|
|
145
120
|
export function makeComparator<T>({
|
|
146
121
|
sort,
|
|
@@ -165,17 +140,15 @@ export function makeComparator<T>({
|
|
|
165
140
|
comparison = compare(normalized.a, normalized.b);
|
|
166
141
|
break;
|
|
167
142
|
default: {
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
// `{ last_message_at: -1 }`) negates "null last" into "null first" and floats value-less
|
|
171
|
-
// items (e.g. channels with no last message) to the head of the list.
|
|
143
|
+
// Return before the direction flip below, which would turn "null last" into "null first"
|
|
144
|
+
// on a descending sort.
|
|
172
145
|
if (leftValue == null && rightValue == null) {
|
|
173
|
-
comparison = 0;
|
|
146
|
+
comparison = 0;
|
|
174
147
|
break;
|
|
175
148
|
}
|
|
176
|
-
if (leftValue == null) return 1;
|
|
177
|
-
if (rightValue == null) return -1;
|
|
178
|
-
// Both non-null but not normalizable:
|
|
149
|
+
if (leftValue == null) return 1;
|
|
150
|
+
if (rightValue == null) return -1;
|
|
151
|
+
// Both non-null but not normalizable: compare as strings (respects direction).
|
|
179
152
|
const stringLeftValue = String(leftValue),
|
|
180
153
|
stringRightValue = String(rightValue);
|
|
181
154
|
comparison =
|
|
@@ -9,21 +9,12 @@ export function isISODateString(x: any): x is string {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* Nanoseconds rather than milliseconds because the two sides of a comparison come from different
|
|
16
|
-
* places: an item's `created_at` is the wire number, while a filter operand is typed `Date | string`
|
|
17
|
-
* in the generated filter types. Normalizing to ms would have meant halving the pair — a `Date`
|
|
18
|
-
* became ms while a wire number was passed through untouched and *called* ms, so every mixed
|
|
19
|
-
* comparison silently ordered wrong and `normKey` bucketed the two apart.
|
|
20
|
-
*
|
|
21
|
-
* A bare `number` is therefore read as nanoseconds. That is the SDK's unit for a timestamp
|
|
22
|
-
* everywhere else, and it is what a value read off an item will be.
|
|
12
|
+
* Converts a `Date`, ISO string or finite number into unix nanoseconds, the unit item timestamps
|
|
13
|
+
* carry. A bare `number` is read as nanoseconds already. Returns `null` for anything else.
|
|
23
14
|
*/
|
|
24
15
|
export function toEpochNanos(x: any): number | null {
|
|
25
16
|
if (x instanceof Date) return dateToNs(x);
|
|
26
|
-
if (typeof x === 'number' && Number.isFinite(x)) return x;
|
|
17
|
+
if (typeof x === 'number' && Number.isFinite(x)) return x;
|
|
27
18
|
if (isISODateString(x)) return msToNs(Date.parse(x));
|
|
28
19
|
return null;
|
|
29
20
|
}
|
|
@@ -53,8 +44,7 @@ export function normalizeComparedValues(a: any, b: any) {
|
|
|
53
44
|
}
|
|
54
45
|
|
|
55
46
|
export function normKey(x: unknown): string {
|
|
56
|
-
//
|
|
57
|
-
// We can piggyback by normalizing x against itself:
|
|
47
|
+
// Unary form of `normalizeComparedValues`: normalize x against itself.
|
|
58
48
|
const n = normalizeComparedValues(x, x);
|
|
59
49
|
switch (n.kind) {
|
|
60
50
|
case 'date':
|
|
@@ -63,7 +53,6 @@ export function normKey(x: unknown): string {
|
|
|
63
53
|
case 'boolean':
|
|
64
54
|
return `${n.kind}:${String(n.a)}`;
|
|
65
55
|
default:
|
|
66
|
-
// fallback: use JSON-like string with type tag for determinism
|
|
67
56
|
return `other:${String(x)}`;
|
|
68
57
|
}
|
|
69
58
|
}
|
|
@@ -74,7 +63,6 @@ export function compare(a: any, b: any): number {
|
|
|
74
63
|
}
|
|
75
64
|
|
|
76
65
|
export function arraysEqualAsSets(aList: unknown[], bList: unknown[]): boolean {
|
|
77
|
-
// de-duplicate by normalized key
|
|
78
66
|
const aKeys = new Set(aList.map(normKey));
|
|
79
67
|
const bKeys = new Set(bList.map(normKey));
|
|
80
68
|
if (aKeys.size !== bKeys.size) return false;
|
|
@@ -95,23 +83,13 @@ export function normalizeStringAccentInsensitive(s: string): string {
|
|
|
95
83
|
}
|
|
96
84
|
|
|
97
85
|
export function tokenize(s: string): string[] {
|
|
98
|
-
// split on whitespace; keep simple & deterministic
|
|
99
86
|
return normalizeString(s).split(/\s+/).filter(Boolean);
|
|
100
87
|
}
|
|
101
88
|
|
|
102
89
|
/**
|
|
103
|
-
* Reads `a.b.c` off an item, for the filter and sort compilers.
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
* instances (a `Reminder`, a `Poll`) — because a filter path legitimately reaches into all three. That is why
|
|
107
|
-
* this is not `getPath` from `src/utils/objectPath.ts`, which deliberately walks plain records only; the two
|
|
108
|
-
* are documented there as non-interchangeable.
|
|
109
|
-
*
|
|
110
|
-
* Stops at `null` / `undefined`, the only values that cannot be indexed. It used to stop at any *falsy*
|
|
111
|
-
* value, which made the result depend on a string's contents rather than on its shape: `name.length`
|
|
112
|
-
* resolved to `2` for `'ab'` and to `undefined` for `''`. A falsy value at the end of a path was never
|
|
113
|
-
* affected — the guard only ever ran against an intermediate — so `{ count: 0 }` on `'count'` has always
|
|
114
|
-
* returned `0`, and sorting and filtering on scalar fields were never wrong.
|
|
90
|
+
* Reads `a.b.c` off an item, for the filter and sort compilers. Descends through anything indexable
|
|
91
|
+
* (plain objects, arrays, class instances such as `Reminder`), unlike `getPath` in
|
|
92
|
+
* `src/utils/objectPath.ts`, which walks plain records only. Stops at `null` / `undefined`.
|
|
115
93
|
*/
|
|
116
94
|
export function resolveDotPathValue(obj: any, path: string): unknown {
|
|
117
95
|
return path
|
|
@@ -130,5 +108,5 @@ export function isIterableButNotString(v: unknown): v is Iterable<unknown> {
|
|
|
130
108
|
export function toIterableArray(v: unknown): unknown[] {
|
|
131
109
|
if (Array.isArray(v)) return v;
|
|
132
110
|
if (isIterableButNotString(v)) return Array.from(v as Iterable<unknown>);
|
|
133
|
-
return [v];
|
|
111
|
+
return [v];
|
|
134
112
|
}
|
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* `true` for the remaining suffix. Returns the first index at which `predicate(index)` holds, or
|
|
6
|
-
* `length` if it never does. O(log length).
|
|
7
|
-
*
|
|
8
|
-
* Unlike `binarySearch`, this locates a *boundary* defined by a predicate rather than a specific
|
|
9
|
-
* item by identity — e.g. "first message at/after a timestamp" over a chronologically sorted array.
|
|
2
|
+
* Lower-bound binary search over `[0, length)`: the first index where `predicate` holds, or `length`.
|
|
3
|
+
* `predicate` must be monotonic (`false` for a prefix, then `true`), e.g. "first item at or
|
|
4
|
+
* after a timestamp" over a sorted array.
|
|
10
5
|
*/
|
|
11
6
|
export function lowerBound(
|
|
12
7
|
length: number,
|
|
@@ -53,7 +48,7 @@ export function locateOnPlateauAlternating<T>(
|
|
|
53
48
|
const rightOut =
|
|
54
49
|
rightIndex >= items.length || compare(items[rightIndex], needle) !== 0;
|
|
55
50
|
const leftOut = leftIndex < 0 || compare(items[leftIndex], needle) !== 0;
|
|
56
|
-
if (rightOut && leftOut) break;
|
|
51
|
+
if (rightOut && leftOut) break;
|
|
57
52
|
}
|
|
58
53
|
|
|
59
54
|
return -1;
|
|
@@ -68,11 +63,9 @@ export function locateOnPlateauScanOneSide<T>(
|
|
|
68
63
|
): number {
|
|
69
64
|
const targetId = getItemId(needle);
|
|
70
65
|
|
|
71
|
-
// scan left
|
|
72
66
|
for (let i = insertionIndex - 1; i >= 0 && compare(items[i], needle) === 0; i--) {
|
|
73
67
|
if (getItemId(items[i]) === targetId) return i;
|
|
74
68
|
}
|
|
75
|
-
// scan right
|
|
76
69
|
for (let i = insertionIndex; i < items.length && compare(items[i], needle) === 0; i++) {
|
|
77
70
|
if (getItemId(items[i]) === targetId) return i;
|
|
78
71
|
}
|
package/src/poll.ts
CHANGED
|
@@ -72,8 +72,8 @@ export class Poll {
|
|
|
72
72
|
public id: string;
|
|
73
73
|
private client: StreamChat;
|
|
74
74
|
private optimisticVotes: OptimisticPollVotes;
|
|
75
|
-
// Set
|
|
76
|
-
//
|
|
75
|
+
// Set when server data (WS event or rehydration) confirms the close, so a failed optimistic close
|
|
76
|
+
// does not reopen the poll.
|
|
77
77
|
private closeConfirmed = false;
|
|
78
78
|
|
|
79
79
|
constructor({ client, poll }: PollInitOptions) {
|
|
@@ -110,7 +110,7 @@ export class Poll {
|
|
|
110
110
|
};
|
|
111
111
|
};
|
|
112
112
|
|
|
113
|
-
//
|
|
113
|
+
// Persists only the server's votes, never the optimistic ones.
|
|
114
114
|
private upsertOfflineDb = () => {
|
|
115
115
|
const serverState = this.optimisticVotes.withServerVotes(this.data);
|
|
116
116
|
this.client.offlineDb?.executeQuerySafely(
|
|
@@ -126,7 +126,7 @@ export class Poll {
|
|
|
126
126
|
this.state.partialNext(initialState);
|
|
127
127
|
};
|
|
128
128
|
|
|
129
|
-
//
|
|
129
|
+
// The optimistic votes decide whether the event's vote state is shown.
|
|
130
130
|
private updateFromVoteEvent = (
|
|
131
131
|
event: EventPayload<'poll.vote_casted' | 'poll.vote_changed' | 'poll.vote_removed'>,
|
|
132
132
|
ownVotesByOptionId: Record<OptionId, PollVoteResponseData>,
|
|
@@ -275,11 +275,9 @@ export class Poll {
|
|
|
275
275
|
};
|
|
276
276
|
|
|
277
277
|
/**
|
|
278
|
-
* Puts an updated user (new name or image) into the votes, answers and creator
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
* votes don't contain the user keeps its array, so what renders it doesn't re-render. The offline
|
|
282
|
-
* DB gets the updated poll, as with the other poll events.
|
|
278
|
+
* Puts an updated user (new name or image) into the votes, answers and creator of the poll in one
|
|
279
|
+
* state update, then saves the poll to the offline DB. Does nothing when the poll does not
|
|
280
|
+
* contain the user. Option vote arrays without the user keep their identity.
|
|
283
281
|
*/
|
|
284
282
|
public handleUserUpdated = (user: UserResponse) => {
|
|
285
283
|
const currentState = this.data;
|
|
@@ -289,7 +287,7 @@ export class Poll {
|
|
|
289
287
|
changed = true;
|
|
290
288
|
return { ...vote, user };
|
|
291
289
|
};
|
|
292
|
-
// the same array when none of its votes is the user's
|
|
290
|
+
// Returns the same array when none of its votes is the user's.
|
|
293
291
|
const withUserAll = (votes?: PollVoteResponseData[]) => {
|
|
294
292
|
if (!votes) return votes;
|
|
295
293
|
const next = votes.map(withUser);
|
|
@@ -344,9 +342,8 @@ export class Poll {
|
|
|
344
342
|
);
|
|
345
343
|
|
|
346
344
|
/**
|
|
347
|
-
* Closes the poll.
|
|
348
|
-
*
|
|
349
|
-
* Does nothing if the poll is already closed.
|
|
345
|
+
* Closes the poll optimistically. A failed request reverts it, unless the server confirmed the
|
|
346
|
+
* close meanwhile. No-op if the poll is already closed.
|
|
350
347
|
*/
|
|
351
348
|
close = async () => {
|
|
352
349
|
if (this.data.is_closed) return;
|
|
@@ -379,9 +376,8 @@ export class Poll {
|
|
|
379
376
|
await this.client.deletePollOption({ poll_id: this.id as string, option_id });
|
|
380
377
|
|
|
381
378
|
/**
|
|
382
|
-
* Casts a vote
|
|
383
|
-
*
|
|
384
|
-
* If the request fails, the change is reverted.
|
|
379
|
+
* Casts a vote, shown right away as a local placeholder that the server's vote replaces once the
|
|
380
|
+
* WS events of all pending vote changes arrived. A failed request reverts it.
|
|
385
381
|
*/
|
|
386
382
|
castVote = async (optionId: string, messageId: string) => {
|
|
387
383
|
const { max_votes_allowed, ownVotesByOptionId } = this.data;
|
|
@@ -407,9 +403,13 @@ export class Poll {
|
|
|
407
403
|
};
|
|
408
404
|
|
|
409
405
|
/**
|
|
410
|
-
* Removes a vote
|
|
411
|
-
*
|
|
412
|
-
*
|
|
406
|
+
* Removes a vote right away; a failed request restores it. Vote requests are sent in order, so a
|
|
407
|
+
* vote whose cast is not confirmed yet is removed by the ID from the cast's response (if that
|
|
408
|
+
* cast failed, there is nothing to remove).
|
|
409
|
+
*
|
|
410
|
+
* @param voteId - The ID of the vote to remove.
|
|
411
|
+
* @param messageId - The ID of the poll's message.
|
|
412
|
+
* @returns The delete response, or `undefined` when there was nothing to remove.
|
|
413
413
|
*/
|
|
414
414
|
removeVote = async (voteId: string, messageId: string) =>
|
|
415
415
|
await this.optimisticVotes.removeVote(voteId, messageId);
|