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
|
@@ -1,122 +1,69 @@
|
|
|
1
1
|
import type { Unsubscribe } from '@stream-io/state-store';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* `changedIds` are the ids the subscriber watches whose canonical object changed reference this
|
|
7
|
-
* flush (an upsert, or a removal — a removal makes `store.get(id)` return `undefined`).
|
|
4
|
+
* The IDs a subscriber watches whose canonical object changed reference in this flush (an upsert or
|
|
5
|
+
* a removal; after a removal `store.get(id)` returns `undefined`).
|
|
8
6
|
*/
|
|
9
7
|
export type EntityStoreChangeBatch = {
|
|
10
8
|
changedIds: ReadonlySet<string>;
|
|
11
9
|
};
|
|
12
10
|
|
|
13
11
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* intervals; a thread watches its single parent id). It is notified at most once
|
|
19
|
-
* per store transaction with the subset of its watched ids that changed. While linked, it also
|
|
20
|
-
* holds those entities; see {@link EntityStoreClaim} for holding without being notified.
|
|
12
|
+
* Observes entities in an {@link EntityStore}, linked to them by ID ({@link EntityStore.link}). It
|
|
13
|
+
* is notified at most once per store transaction with the subset of its watched IDs that changed,
|
|
14
|
+
* and while linked it holds those entities (see {@link EntityStoreClaim} to hold without being
|
|
15
|
+
* notified).
|
|
21
16
|
*/
|
|
22
17
|
export type EntityStoreSubscriber = {
|
|
23
18
|
onEntitiesChanged: (batch: EntityStoreChangeBatch) => void;
|
|
24
19
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* renders without throttle delay. A paginator implements this by flushing its throttled window
|
|
28
|
-
* publish.
|
|
20
|
+
* Emits any throttled pending state immediately. Called by {@link EntityStore.flushSubscribers}
|
|
21
|
+
* after an optimistic (local-user) write.
|
|
29
22
|
*/
|
|
30
23
|
flushState?: () => void;
|
|
31
24
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* and the entity would never be released.
|
|
25
|
+
* Called by {@link EntityStore.changeId} when this subscriber's link moves from `oldId` to
|
|
26
|
+
* `newId`, before the change notification. A subscriber keeping its own list of linked IDs renames
|
|
27
|
+
* the entry here, or its later `unlink(oldId)` misses the moved link.
|
|
36
28
|
*/
|
|
37
29
|
onIdChanged?: (oldId: string, newId: string) => void;
|
|
38
30
|
/**
|
|
39
|
-
*
|
|
40
|
-
* {@link EntityStore.
|
|
41
|
-
*
|
|
42
|
-
* and drop it on its side, and its unlink changes nothing. Not called when the last holder
|
|
43
|
-
* unlinks: nobody holds the entity then.
|
|
31
|
+
* Called when the store removes an entity this subscriber holds ({@link EntityStore.remove},
|
|
32
|
+
* {@link EntityStore.clear}), after its holders are dropped but while the entity can still be
|
|
33
|
+
* read. Not called when the last holder unlinks.
|
|
44
34
|
*/
|
|
45
35
|
onEntityRemoved?: (id: string, entity: unknown) => void;
|
|
46
|
-
/**
|
|
47
|
-
* Optional: what this subscriber is, for debugging tools that list who holds an entity. A
|
|
48
|
-
* kebab-case identifier with no spaces, e.g. `channel-paginator`.
|
|
49
|
-
*/
|
|
36
|
+
/** What this subscriber is, for debugging tools: a kebab-case ID, e.g. `channel-paginator`. */
|
|
50
37
|
name?: string;
|
|
51
38
|
};
|
|
52
39
|
|
|
53
40
|
/**
|
|
54
|
-
* A standing claim on entities
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* held, so the claim never reports a change.
|
|
41
|
+
* A standing claim on entities, added with {@link EntityStore.addClaim}. Unlike a subscriber it is
|
|
42
|
+
* not linked per ID and never notified: the store asks {@link EntityStoreClaim.heldBy} whenever it
|
|
43
|
+
* needs to know what is held.
|
|
58
44
|
*/
|
|
59
45
|
export type EntityStoreClaim<T> = {
|
|
60
|
-
/** The entities held by this claim right now
|
|
46
|
+
/** The entities held by this claim right now, matched by identity against the stored ones. */
|
|
61
47
|
heldBy: () => Iterable<T>;
|
|
62
|
-
/**
|
|
63
|
-
* Optional: what makes the claim, for debugging tools that list who holds an entity. A kebab-case
|
|
64
|
-
* identifier with no spaces, e.g. `message-composer-cache`.
|
|
65
|
-
*/
|
|
48
|
+
/** What makes the claim, for debugging tools: a kebab-case ID, e.g. `message-composer-cache`. */
|
|
66
49
|
name?: string;
|
|
67
50
|
};
|
|
68
51
|
|
|
69
52
|
export type EntityStoreOptions<T> = {
|
|
70
|
-
/** Extracts the canonical id an entity is stored and addressed under. */
|
|
71
53
|
getEntityId: (entity: T) => string;
|
|
72
54
|
/**
|
|
73
|
-
* Called with an entity
|
|
74
|
-
*
|
|
75
|
-
* (unsubscribe listeners, stop timers). Optional.
|
|
55
|
+
* Called with an entity removed because its last holder unlinked or {@link EntityStore.clear} ran.
|
|
56
|
+
* A store of class instances uses it to tear them down (listeners, timers).
|
|
76
57
|
*/
|
|
77
58
|
onRelease?: (entity: T) => void;
|
|
78
59
|
/**
|
|
79
|
-
* Whether an entry is removed when its last holder unlinks (the default).
|
|
80
|
-
*
|
|
81
|
-
*
|
|
60
|
+
* Whether an entry is removed when its last holder unlinks (the default). Set `false` when the
|
|
61
|
+
* store decides itself: entries then leave through {@link EntityStore.remove} or
|
|
62
|
+
* {@link EntityStore.clear}, and holders only receive notifications.
|
|
82
63
|
*/
|
|
83
64
|
releaseOnLastUnlink?: boolean;
|
|
84
65
|
};
|
|
85
66
|
|
|
86
|
-
/**
|
|
87
|
-
* A client-global, normalized store for domain entities addressed by id.
|
|
88
|
-
*
|
|
89
|
-
* The store holds exactly **one** canonical `T` per id and lets any number of entities
|
|
90
|
-
* (paginators, threads, ad-hoc consumers) subscribe to individual ids. Every mutation of a
|
|
91
|
-
* stored entity becomes a single `upsert`, and the store fans the change out to exactly the
|
|
92
|
-
* subscribers holding that id — replacing the manual copy-to-copy fan-out that keeping N
|
|
93
|
-
* per-consumer copies in sync required.
|
|
94
|
-
*
|
|
95
|
-
* The store is entity-agnostic: it learns how to extract an id from a `T` via the `getEntityId`
|
|
96
|
-
* function passed to its constructor. The client holds one as `client.messageStore`, an
|
|
97
|
-
* `EntityStore<LocalMessage>`, for message content.
|
|
98
|
-
*
|
|
99
|
-
* ## Design
|
|
100
|
-
*
|
|
101
|
-
* - **Content:** `byId` (`Map<id, T>`) is the single source of truth. Objects are immutable
|
|
102
|
-
* snapshots: `upsert` *replaces*, never mutates in place, so the reference-equality
|
|
103
|
-
* short-circuit (mirroring {@link StateStore.next}) and every downstream selector keep working.
|
|
104
|
-
* - **Subscription registry / refcount:** `subscribers` (`Map<id, Set<subscriber>>`) is both the
|
|
105
|
-
* per-id notification list and the reference count — when the last subscriber of an id unlinks,
|
|
106
|
-
* the canonical copy is garbage-collected.
|
|
107
|
-
* - **Batching:** `transaction` coalesces a bulk write (e.g. a page of N entities) so each affected
|
|
108
|
-
* subscriber is notified **once** with the full changed-id set, instead of N times.
|
|
109
|
-
* - **Signal-then-pull:** subscribers receive the set of changed ids and pull the new content via
|
|
110
|
-
* `get`; the store never pushes objects.
|
|
111
|
-
*
|
|
112
|
-
* This is deliberately a hand-rolled per-id registry rather than a {@link StateStore}: a single
|
|
113
|
-
* `StateStore` would run every subscriber's selector on every change (O(all subscribers) per emit),
|
|
114
|
-
* and a store-per-entity would allocate a full `StateStore` per id with no cross-id batching. The
|
|
115
|
-
* `Map<id, Set>` gives O(subscribers-of-changed-id) fan-out — the same idiom as `PollManager`'s
|
|
116
|
-
* cache.
|
|
117
|
-
*
|
|
118
|
-
* @template T The domain entity type held by the store.
|
|
119
|
-
*/
|
|
120
67
|
/**
|
|
121
68
|
* Runs callbacks so one that throws doesn't stop the ones after it, and rethrows the first error once
|
|
122
69
|
* they all ran.
|
|
@@ -137,6 +84,16 @@ class RunnerWithErrorCollector {
|
|
|
137
84
|
}
|
|
138
85
|
}
|
|
139
86
|
|
|
87
|
+
/**
|
|
88
|
+
* A normalized store of entities addressed by ID: one canonical `T` per ID (`upsert` replaces the
|
|
89
|
+
* reference; `getOrCreate` hydrates the stored one in place), shared by any number of subscribers
|
|
90
|
+
* linked to individual IDs.
|
|
91
|
+
* A write notifies exactly the subscribers holding that ID, with the changed IDs only (they pull
|
|
92
|
+
* content via `get`). Links are also the refcount: by default an entity is dropped when its last
|
|
93
|
+
* holder unlinks. {@link EntityStore.transaction} coalesces notifications into one flush.
|
|
94
|
+
*
|
|
95
|
+
* @template T The domain entity type held by the store.
|
|
96
|
+
*/
|
|
140
97
|
export class EntityStore<T> {
|
|
141
98
|
private byId = new Map<string, T>();
|
|
142
99
|
private subscribers = new Map<string, Set<EntityStoreSubscriber>>();
|
|
@@ -147,7 +104,7 @@ export class EntityStore<T> {
|
|
|
147
104
|
|
|
148
105
|
private transactionDepth = 0;
|
|
149
106
|
private pendingChanged = new Map<EntityStoreSubscriber, Set<string>>();
|
|
150
|
-
/**
|
|
107
|
+
/** IDs whose {@link EntityStore.flushSubscribers} was requested while a transaction was open. */
|
|
151
108
|
private pendingFlushIds?: Set<string>;
|
|
152
109
|
|
|
153
110
|
constructor({
|
|
@@ -160,22 +117,43 @@ export class EntityStore<T> {
|
|
|
160
117
|
this.releaseOnLastUnlink = releaseOnLastUnlink;
|
|
161
118
|
}
|
|
162
119
|
|
|
163
|
-
|
|
164
|
-
|
|
120
|
+
/**
|
|
121
|
+
* Reads the canonical copy stored under an ID.
|
|
122
|
+
*
|
|
123
|
+
* @param id - The ID to read. `undefined` reads nothing.
|
|
124
|
+
* @returns The stored entity, or `undefined` when none is stored under `id`.
|
|
125
|
+
*/
|
|
165
126
|
get(id: string | undefined): T | undefined {
|
|
166
127
|
return typeof id === 'string' ? this.byId.get(id) : undefined;
|
|
167
128
|
}
|
|
168
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Checks whether an entity is stored under an ID.
|
|
132
|
+
*
|
|
133
|
+
* @param id - The ID to look up.
|
|
134
|
+
* @returns Whether an entity is stored under `id`.
|
|
135
|
+
*/
|
|
169
136
|
has(id: string): boolean {
|
|
170
137
|
return this.byId.has(id);
|
|
171
138
|
}
|
|
172
139
|
|
|
173
|
-
/**
|
|
140
|
+
/**
|
|
141
|
+
* Checks whether a subscriber is linked to an ID.
|
|
142
|
+
*
|
|
143
|
+
* @param id - The ID to look up.
|
|
144
|
+
* @param subscriber - The subscriber to look for.
|
|
145
|
+
* @returns Whether `subscriber` is linked to `id`.
|
|
146
|
+
*/
|
|
174
147
|
isHeldBy(id: string, subscriber: EntityStoreSubscriber): boolean {
|
|
175
148
|
return this.subscribers.get(id)?.has(subscriber) ?? false;
|
|
176
149
|
}
|
|
177
150
|
|
|
178
|
-
/**
|
|
151
|
+
/**
|
|
152
|
+
* Checks whether anything holds the entity stored under an ID.
|
|
153
|
+
*
|
|
154
|
+
* @param id - The ID to look up.
|
|
155
|
+
* @returns Whether a subscriber linked to `id`, or a claim listing its entity, holds it.
|
|
156
|
+
*/
|
|
179
157
|
isHeld(id: string): boolean {
|
|
180
158
|
if (this.subscribers.has(id)) return true;
|
|
181
159
|
const entity = this.byId.get(id);
|
|
@@ -183,8 +161,11 @@ export class EntityStore<T> {
|
|
|
183
161
|
}
|
|
184
162
|
|
|
185
163
|
/**
|
|
186
|
-
*
|
|
187
|
-
*
|
|
164
|
+
* Lists what holds the entity stored under an ID, for debugging.
|
|
165
|
+
*
|
|
166
|
+
* @param id - The ID to look up.
|
|
167
|
+
* @returns The names of the named subscribers linked to `id` and of the named claims listing its
|
|
168
|
+
* entity.
|
|
188
169
|
*/
|
|
189
170
|
holderNames(id: string): string[] {
|
|
190
171
|
const names: string[] = [];
|
|
@@ -200,8 +181,10 @@ export class EntityStore<T> {
|
|
|
200
181
|
}
|
|
201
182
|
|
|
202
183
|
/**
|
|
203
|
-
*
|
|
204
|
-
*
|
|
184
|
+
* Lists the stored entities nothing holds. Each claim is asked once for the whole list.
|
|
185
|
+
*
|
|
186
|
+
* @returns Every stored entity that neither a linked subscriber nor a claim holds, with the ID it
|
|
187
|
+
* is stored under.
|
|
205
188
|
*/
|
|
206
189
|
unheldEntries(): [string, T][] {
|
|
207
190
|
const claimed = this.claimsByEntity();
|
|
@@ -211,9 +194,11 @@ export class EntityStore<T> {
|
|
|
211
194
|
}
|
|
212
195
|
|
|
213
196
|
/**
|
|
214
|
-
* Adds
|
|
215
|
-
*
|
|
216
|
-
*
|
|
197
|
+
* Adds a claim. Entities are matched by identity, so one stored under an ID other than its own (a
|
|
198
|
+
* temporary ID) is still found.
|
|
199
|
+
*
|
|
200
|
+
* @param claim - The claim to add.
|
|
201
|
+
* @returns A function that removes the claim.
|
|
217
202
|
*/
|
|
218
203
|
addClaim(claim: EntityStoreClaim<T>): () => void {
|
|
219
204
|
this.claims.add(claim);
|
|
@@ -222,10 +207,7 @@ export class EntityStore<T> {
|
|
|
222
207
|
};
|
|
223
208
|
}
|
|
224
209
|
|
|
225
|
-
/**
|
|
226
|
-
* Each entity the claims list, with the claims listing it, each once: a claim may list an entity
|
|
227
|
-
* several times, e.g. the threads claim lists a channel once per thread.
|
|
228
|
-
*/
|
|
210
|
+
/** Maps each claimed entity to the claims listing it, each once (a claim may repeat an entity). */
|
|
229
211
|
private claimsByEntity() {
|
|
230
212
|
const claimed = new Map<T, EntityStoreClaim<T>[]>();
|
|
231
213
|
for (const claim of this.claims) {
|
|
@@ -238,28 +220,34 @@ export class EntityStore<T> {
|
|
|
238
220
|
return claimed;
|
|
239
221
|
}
|
|
240
222
|
|
|
241
|
-
/**
|
|
223
|
+
/**
|
|
224
|
+
* Lists the stored entities.
|
|
225
|
+
*
|
|
226
|
+
* @returns Every stored entity.
|
|
227
|
+
*/
|
|
242
228
|
values(): T[] {
|
|
243
229
|
return Array.from(this.byId.values());
|
|
244
230
|
}
|
|
245
231
|
|
|
246
|
-
/**
|
|
232
|
+
/**
|
|
233
|
+
* Lists the stored entities with their keys.
|
|
234
|
+
*
|
|
235
|
+
* @returns Every stored entity with the ID it is stored under, which can differ from its own ID.
|
|
236
|
+
*/
|
|
247
237
|
entries(): [string, T][] {
|
|
248
238
|
return Array.from(this.byId.entries());
|
|
249
239
|
}
|
|
250
240
|
|
|
251
|
-
// ---- writes ----
|
|
252
|
-
|
|
253
241
|
/**
|
|
254
|
-
* Replaces the canonical copy of `entity` and notifies
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
242
|
+
* Replaces the canonical copy of `entity` and notifies the subscribers watching its ID.
|
|
243
|
+
*
|
|
244
|
+
* @param entity - The new canonical copy, stored under its own ID.
|
|
245
|
+
* @param subscriber - The writer, which updates itself and is not notified.
|
|
258
246
|
*/
|
|
259
247
|
upsert(entity: T, subscriber?: EntityStoreSubscriber): void {
|
|
260
248
|
const id = this.getEntityId(entity);
|
|
261
249
|
const previous = this.byId.get(id);
|
|
262
|
-
//
|
|
250
|
+
// No-op on an unchanged reference, like `StateStore.next`.
|
|
263
251
|
if (previous === entity) return;
|
|
264
252
|
this.byId.set(id, entity);
|
|
265
253
|
this.markDirty(id, subscriber);
|
|
@@ -267,13 +255,15 @@ export class EntityStore<T> {
|
|
|
267
255
|
}
|
|
268
256
|
|
|
269
257
|
/**
|
|
270
|
-
* Returns the entity stored under `id`,
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
258
|
+
* Returns the entity stored under `id`, or stores a new one. A stored entity is never replaced, so
|
|
259
|
+
* every caller gets the same instance and fresh data goes in through `hydrate`. Storing a new
|
|
260
|
+
* entity notifies the subscribers already linked to `id`; hydrating does not, since the reference
|
|
261
|
+
* is unchanged.
|
|
274
262
|
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
263
|
+
* @param id - The ID to read or store under.
|
|
264
|
+
* @param create - Builds the entity when none is stored under `id`.
|
|
265
|
+
* @param hydrate - Receives an already stored entity, to update it in place.
|
|
266
|
+
* @returns The stored entity, or the new one from `create()`.
|
|
277
267
|
*/
|
|
278
268
|
getOrCreate(id: string, create: () => T, hydrate?: (stored: T) => void): T {
|
|
279
269
|
if (this.byId.has(id)) {
|
|
@@ -289,15 +279,19 @@ export class EntityStore<T> {
|
|
|
289
279
|
}
|
|
290
280
|
|
|
291
281
|
/**
|
|
292
|
-
* Moves an entity and its holders from `oldId` to `newId`, for
|
|
293
|
-
* holders get {@link EntityStoreSubscriber.onIdChanged}, then holders of both
|
|
294
|
-
* Returns `false` without changes when `oldId` is not stored or the ids are equal.
|
|
282
|
+
* Moves an entity and its holders from `oldId` to `newId`, for IDs assigned after storing. Moved
|
|
283
|
+
* holders get {@link EntityStoreSubscriber.onIdChanged}, then holders of both IDs are notified.
|
|
295
284
|
*
|
|
296
|
-
* When `newId` holds another entity, nothing changes unless `replace` is set
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
*
|
|
285
|
+
* When `newId` holds another entity, nothing changes unless `replace` is set. With `replace`, the
|
|
286
|
+
* moved entity takes its place: the holders of `newId` stay linked and hold the moved one, and
|
|
287
|
+
* the replaced entity goes to `onRelease`. Its holders are not told it was removed, so a list can
|
|
288
|
+
* swap the item in place.
|
|
300
289
|
*
|
|
290
|
+
* @param oldId - The ID the entity is stored under.
|
|
291
|
+
* @param newId - The ID to move it to.
|
|
292
|
+
* @param options.replace - Take the place of an entity already stored under `newId`.
|
|
293
|
+
* @returns Whether the entry moved: `false`, with nothing changed, when `oldId` is not stored, the
|
|
294
|
+
* IDs are equal, or `newId` is taken and `replace` is not set.
|
|
301
295
|
* @example
|
|
302
296
|
* // A channel created from members has no id until the server creates it, so it is stored under a
|
|
303
297
|
* // temporary cid built from its member ids.
|
|
@@ -320,10 +314,10 @@ export class EntityStore<T> {
|
|
|
320
314
|
|
|
321
315
|
const moved = this.subscribers.get(oldId);
|
|
322
316
|
if (moved) {
|
|
323
|
-
//
|
|
317
|
+
// Holders of `oldId` learn it resolves to `undefined`.
|
|
324
318
|
this.markDirty(oldId);
|
|
325
319
|
this.subscribers.delete(oldId);
|
|
326
|
-
//
|
|
320
|
+
// Subscribers may link an ID before an entity is stored under it.
|
|
327
321
|
const existing = this.subscribers.get(newId);
|
|
328
322
|
if (existing) for (const subscriber of moved) existing.add(subscriber);
|
|
329
323
|
else this.subscribers.set(newId, moved);
|
|
@@ -331,7 +325,7 @@ export class EntityStore<T> {
|
|
|
331
325
|
this.markDirty(newId);
|
|
332
326
|
|
|
333
327
|
if (this.pendingFlushIds?.delete(oldId)) this.pendingFlushIds.add(newId);
|
|
334
|
-
//
|
|
328
|
+
// Re-keyed before `autoFlush`, so a holder handling the notification already reads `newId`.
|
|
335
329
|
if (moved) for (const subscriber of moved) subscriber.onIdChanged?.(oldId, newId);
|
|
336
330
|
this.autoFlush();
|
|
337
331
|
if (replaced !== undefined) this.onRelease?.(replaced);
|
|
@@ -339,10 +333,12 @@ export class EntityStore<T> {
|
|
|
339
333
|
}
|
|
340
334
|
|
|
341
335
|
/**
|
|
342
|
-
* Stores `entity` in place of another
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
336
|
+
* Stores `entity` in place of another stored under the same ID. The holders of that ID stay linked
|
|
337
|
+
* and hold `entity`; they are notified of a change, not a removal, so a list can swap the item
|
|
338
|
+
* in place. The replaced entity goes to `onRelease`. With nothing under the ID it stores like
|
|
339
|
+
* {@link EntityStore.upsert}.
|
|
340
|
+
*
|
|
341
|
+
* @param entity - The entity to store under its own ID.
|
|
346
342
|
*/
|
|
347
343
|
replace(entity: T): void {
|
|
348
344
|
const id = this.getEntityId(entity);
|
|
@@ -355,21 +351,22 @@ export class EntityStore<T> {
|
|
|
355
351
|
}
|
|
356
352
|
|
|
357
353
|
/**
|
|
358
|
-
* Removes the entry stored under `id` whatever holds it, drops its holders and calls `onRelease
|
|
359
|
-
*
|
|
360
|
-
*
|
|
354
|
+
* Removes the entry stored under `id` whatever holds it, drops its holders and calls `onRelease`.
|
|
355
|
+
* Each holder gets {@link EntityStoreSubscriber.onEntityRemoved} and a change notification for
|
|
356
|
+
* `id`. Does nothing when `id` is not stored. A throwing holder or `onRelease` does not stop the
|
|
357
|
+
* removal: the first error is rethrown at the end.
|
|
361
358
|
*
|
|
362
|
-
*
|
|
363
|
-
* deleted and released, and then the first error is rethrown.
|
|
359
|
+
* @param id - The ID to remove.
|
|
364
360
|
*/
|
|
365
361
|
remove(id: string): void {
|
|
366
362
|
this.dropEntry(id, { release: true });
|
|
367
363
|
}
|
|
368
364
|
|
|
369
365
|
/**
|
|
370
|
-
* Like {@link EntityStore.remove},
|
|
371
|
-
*
|
|
372
|
-
*
|
|
366
|
+
* Like {@link EntityStore.remove}, but the entity is not passed to `onRelease`: it is still in use
|
|
367
|
+
* elsewhere, so whoever detaches it decides when it is released.
|
|
368
|
+
*
|
|
369
|
+
* @param id - The ID to remove.
|
|
373
370
|
*/
|
|
374
371
|
detach(id: string): void {
|
|
375
372
|
this.dropEntry(id, { release: false });
|
|
@@ -395,11 +392,8 @@ export class EntityStore<T> {
|
|
|
395
392
|
|
|
396
393
|
/**
|
|
397
394
|
* Removes every entry and holder. Each holder gets {@link EntityStoreSubscriber.onEntityRemoved}
|
|
398
|
-
* for every entity it held, then `onRelease` is called for each removed entity.
|
|
399
|
-
*
|
|
400
|
-
*
|
|
401
|
-
* A holder or `onRelease` that throws doesn't stop the clear: every holder is told, the store is
|
|
402
|
-
* emptied and every entity released, and then the first error is rethrown.
|
|
395
|
+
* for every entity it held, then `onRelease` is called for each removed entity. A throwing holder
|
|
396
|
+
* or `onRelease` does not stop the clear: the first error is rethrown at the end.
|
|
403
397
|
*/
|
|
404
398
|
clear(): void {
|
|
405
399
|
const holdersById = this.subscribers;
|
|
@@ -407,7 +401,7 @@ export class EntityStore<T> {
|
|
|
407
401
|
this.pendingChanged.clear();
|
|
408
402
|
this.pendingFlushIds = undefined;
|
|
409
403
|
const runner = new RunnerWithErrorCollector();
|
|
410
|
-
//
|
|
404
|
+
// Holders are dropped first, so a list removing the item on its side unlinks nothing.
|
|
411
405
|
for (const [id, holders] of holdersById) {
|
|
412
406
|
const entity = this.byId.get(id);
|
|
413
407
|
if (entity === undefined) continue;
|
|
@@ -416,15 +410,18 @@ export class EntityStore<T> {
|
|
|
416
410
|
}
|
|
417
411
|
const released = [...this.byId.values()];
|
|
418
412
|
this.byId = new Map();
|
|
419
|
-
//
|
|
413
|
+
// Released after the store is emptied, so an `onRelease` reading the store sees the final state.
|
|
420
414
|
const { onRelease } = this;
|
|
421
415
|
if (onRelease) for (const entity of released) runner.run(() => onRelease(entity));
|
|
422
416
|
runner.rethrowFirst();
|
|
423
417
|
}
|
|
424
418
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
419
|
+
/**
|
|
420
|
+
* Registers a subscriber as a holder of an ID (notification target and refcount).
|
|
421
|
+
*
|
|
422
|
+
* @param id - The ID to hold.
|
|
423
|
+
* @param subscriber - The holder.
|
|
424
|
+
*/
|
|
428
425
|
link(id: string, subscriber: EntityStoreSubscriber): void {
|
|
429
426
|
let subscribers = this.subscribers.get(id);
|
|
430
427
|
if (!subscribers) {
|
|
@@ -435,9 +432,11 @@ export class EntityStore<T> {
|
|
|
435
432
|
}
|
|
436
433
|
|
|
437
434
|
/**
|
|
438
|
-
* Drops `subscriber` from `id
|
|
439
|
-
* `onRelease`, unless
|
|
440
|
-
*
|
|
435
|
+
* Drops `subscriber` from `id`. When none remain, the canonical copy is dropped and passed to
|
|
436
|
+
* `onRelease`, unless `releaseOnLastUnlink` is `false`. Does nothing if it is not linked.
|
|
437
|
+
*
|
|
438
|
+
* @param id - The ID to release.
|
|
439
|
+
* @param subscriber - The holder to drop.
|
|
441
440
|
*/
|
|
442
441
|
unlink(id: string, subscriber: EntityStoreSubscriber): void {
|
|
443
442
|
const subscribers = this.subscribers.get(id);
|
|
@@ -445,7 +444,6 @@ export class EntityStore<T> {
|
|
|
445
444
|
if (subscribers.size === 0) {
|
|
446
445
|
this.subscribers.delete(id);
|
|
447
446
|
if (!this.releaseOnLastUnlink) return;
|
|
448
|
-
// refcount GC: nobody holds this entity any longer.
|
|
449
447
|
const released = this.byId.get(id);
|
|
450
448
|
const wasStored = this.byId.delete(id);
|
|
451
449
|
if (wasStored) this.onRelease?.(released as T);
|
|
@@ -453,9 +451,12 @@ export class EntityStore<T> {
|
|
|
453
451
|
}
|
|
454
452
|
|
|
455
453
|
/**
|
|
456
|
-
*
|
|
457
|
-
*
|
|
458
|
-
*
|
|
454
|
+
* Watches a single ID. Fires `handler` immediately with the current value (like
|
|
455
|
+
* {@link StateStore.subscribe}) and on every change.
|
|
456
|
+
*
|
|
457
|
+
* @param id - The ID to watch.
|
|
458
|
+
* @param handler - Called with the entity stored under `id`, or `undefined`.
|
|
459
|
+
* @returns A function that stops watching.
|
|
459
460
|
*/
|
|
460
461
|
subscribe(id: string, handler: (entity: T | undefined) => void): Unsubscribe {
|
|
461
462
|
const subscriber: EntityStoreSubscriber = {
|
|
@@ -466,11 +467,12 @@ export class EntityStore<T> {
|
|
|
466
467
|
return () => this.unlink(id, subscriber);
|
|
467
468
|
}
|
|
468
469
|
|
|
469
|
-
// ---- batching ----
|
|
470
|
-
|
|
471
470
|
/**
|
|
472
|
-
* Runs `fn`, coalescing
|
|
473
|
-
*
|
|
471
|
+
* Runs `fn`, coalescing the notifications of writes inside it into a single flush on exit. Nested
|
|
472
|
+
* transactions flush only when the outermost exits.
|
|
473
|
+
*
|
|
474
|
+
* @param fn - The writes to batch.
|
|
475
|
+
* @returns What `fn` returns.
|
|
474
476
|
*/
|
|
475
477
|
transaction<R>(fn: () => R): R {
|
|
476
478
|
this.transactionDepth += 1;
|
|
@@ -480,8 +482,7 @@ export class EntityStore<T> {
|
|
|
480
482
|
this.transactionDepth -= 1;
|
|
481
483
|
if (this.transactionDepth === 0) {
|
|
482
484
|
this.flush();
|
|
483
|
-
//
|
|
484
|
-
// has pending and markDirty accumulates until flush() hands it over.
|
|
485
|
+
// Drained after `flush()`: `flushState` only emits what a subscriber already has pending.
|
|
485
486
|
const ids = this.pendingFlushIds;
|
|
486
487
|
this.pendingFlushIds = undefined;
|
|
487
488
|
if (ids) for (const id of ids) this.flushSubscribersNow(id);
|
|
@@ -490,14 +491,12 @@ export class EntityStore<T> {
|
|
|
490
491
|
}
|
|
491
492
|
|
|
492
493
|
/**
|
|
493
|
-
* Immediately flushes any throttled
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
494
|
+
* Immediately flushes any throttled state publish ({@link EntityStoreSubscriber.flushState}) on
|
|
495
|
+
* the subscribers of `id`, so an optimistic (local-user) write renders without the throttle delay.
|
|
496
|
+
* Inside a transaction it is deferred to the outermost exit, since inline it would be a no-op
|
|
497
|
+
* (nobody has been notified yet).
|
|
497
498
|
*
|
|
498
|
-
*
|
|
499
|
-
* since `markDirty` has not notified anyone yet. Getting this wrong costs the siblings (pinned
|
|
500
|
-
* list, open thread) up to 500ms on an optimistic write, while the writer updates inline.
|
|
499
|
+
* @param id - The ID whose subscribers flush.
|
|
501
500
|
*/
|
|
502
501
|
flushSubscribers(id: string): void {
|
|
503
502
|
if (this.transactionDepth > 0) {
|
|
@@ -513,10 +512,7 @@ export class EntityStore<T> {
|
|
|
513
512
|
for (const subscriber of subscribers) subscriber.flushState?.();
|
|
514
513
|
}
|
|
515
514
|
|
|
516
|
-
/**
|
|
517
|
-
* Records `id` as changed for every subscriber watching it, except `subscriber` — the one that
|
|
518
|
-
* wrote it and is responsible for updating itself (see {@link EntityStore.upsert}).
|
|
519
|
-
*/
|
|
515
|
+
/** Records `id` as changed for every subscriber watching it, except the writer `subscriber`. */
|
|
520
516
|
private markDirty(id: string, subscriber?: EntityStoreSubscriber): void {
|
|
521
517
|
const subscribers = this.subscribers.get(id);
|
|
522
518
|
if (!subscribers) return;
|
|
@@ -537,8 +533,7 @@ export class EntityStore<T> {
|
|
|
537
533
|
|
|
538
534
|
private flush(): void {
|
|
539
535
|
if (this.pendingChanged.size === 0) return;
|
|
540
|
-
//
|
|
541
|
-
// subscriber accumulate into the next flush rather than mutating this one.
|
|
536
|
+
// Swapped out first, so writes made by a subscriber accumulate into the next flush.
|
|
542
537
|
const changedBySubscriber = this.pendingChanged;
|
|
543
538
|
this.pendingChanged = new Map();
|
|
544
539
|
for (const [subscriber, changedIds] of changedBySubscriber) {
|