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.
Files changed (269) hide show
  1. package/dist/cjs/index.browser.js +2196 -3902
  2. package/dist/cjs/index.browser.js.map +3 -3
  3. package/dist/cjs/index.node.js +2197 -3902
  4. package/dist/cjs/index.node.js.map +3 -3
  5. package/dist/esm/index.mjs +2199 -3905
  6. package/dist/esm/index.mjs.map +4 -4
  7. package/dist/types/ChannelManager.d.ts +120 -233
  8. package/dist/types/CooldownTimer.d.ts +8 -23
  9. package/dist/types/EventHandlerPipeline.d.ts +17 -37
  10. package/dist/types/LiveLocationManager.d.ts +22 -50
  11. package/dist/types/api-client.d.ts +9 -14
  12. package/dist/types/channel.d.ts +253 -448
  13. package/dist/types/channel_state.d.ts +48 -95
  14. package/dist/types/client.d.ts +245 -370
  15. package/dist/types/client_state.d.ts +1 -3
  16. package/dist/types/configuration/ConfigController.d.ts +30 -124
  17. package/dist/types/configuration/InstanceConfigurationRegistry.d.ts +31 -123
  18. package/dist/types/configuration/keys.d.ts +5 -19
  19. package/dist/types/configuration/shape.d.ts +12 -29
  20. package/dist/types/configuration/types.d.ts +59 -87
  21. package/dist/types/configuration/utils/applyInstanceConfiguration.d.ts +13 -35
  22. package/dist/types/configuration/utils/copyConfigPatch.d.ts +8 -0
  23. package/dist/types/configuration/utils/declarativeSlices.d.ts +5 -16
  24. package/dist/types/configuration/utils/deepFreezeConfig.d.ts +3 -17
  25. package/dist/types/configuration/utils/serverAuthority.d.ts +9 -29
  26. package/dist/types/connection/ConnectionIdManager.d.ts +18 -40
  27. package/dist/types/connection/ConnectionRecoveryManager.d.ts +29 -120
  28. package/dist/types/connection/networkConnection/NetworkConnectionObserver.d.ts +32 -103
  29. package/dist/types/connection/networkConnection/reporters.d.ts +9 -32
  30. package/dist/types/connection/networkConnection/types.d.ts +12 -27
  31. package/dist/types/connection/wsConnection/StableWSConnection.d.ts +37 -116
  32. package/dist/types/connection/wsConnection/WSConnection.d.ts +41 -104
  33. package/dist/types/connection/wsConnection/WSConnectionFallback.d.ts +8 -16
  34. package/dist/types/connection/wsConnection/config.d.ts +8 -28
  35. package/dist/types/connection/wsConnection/types.d.ts +17 -70
  36. package/dist/types/entityStore/EntityStore.d.ts +153 -113
  37. package/dist/types/entityStore/StoreBackedItemIndex.d.ts +13 -46
  38. package/dist/types/errors.d.ts +7 -19
  39. package/dist/types/gen/chat/ChannelApi.d.ts +2 -1
  40. package/dist/types/gen/chat/ChatApi.d.ts +28 -14
  41. package/dist/types/gen/models/index.d.ts +769 -86
  42. package/dist/types/gen/moderation/ModerationApi.d.ts +4 -4
  43. package/dist/types/index.d.ts +2 -4
  44. package/dist/types/languageNames.d.ts +9 -24
  45. package/dist/types/logger.d.ts +1 -3
  46. package/dist/types/messageComposer/LocationComposer.d.ts +1 -1
  47. package/dist/types/messageComposer/attachmentIdentity.d.ts +1 -6
  48. package/dist/types/messageComposer/attachmentManager.d.ts +10 -32
  49. package/dist/types/messageComposer/attachmentUtils.d.ts +3 -14
  50. package/dist/types/messageComposer/configuration/configuration.d.ts +2 -5
  51. package/dist/types/messageComposer/configuration/types.d.ts +29 -56
  52. package/dist/types/messageComposer/linkPreviewsManager.d.ts +6 -9
  53. package/dist/types/messageComposer/messageComposer.d.ts +30 -164
  54. package/dist/types/messageComposer/middleware/attachmentManager/postUpload/attachmentEnrichment.d.ts +4 -14
  55. package/dist/types/messageComposer/middleware/messageComposer/attachments.d.ts +2 -9
  56. package/dist/types/messageComposer/middleware/pollComposer/types.d.ts +2 -4
  57. package/dist/types/messageComposer/middleware/pollComposer/validation.d.ts +7 -19
  58. package/dist/types/messageComposer/middleware/textComposer/commands.d.ts +9 -0
  59. package/dist/types/messageComposer/middleware/textComposer/mentions.d.ts +11 -18
  60. package/dist/types/messageComposer/middleware/textComposer/textMiddlewareUtils.d.ts +9 -3
  61. package/dist/types/messageComposer/middleware/textComposer/types.d.ts +1 -6
  62. package/dist/types/messageComposer/pollComposer.d.ts +8 -4
  63. package/dist/types/messageComposer/textComposer.d.ts +3 -9
  64. package/dist/types/messageComposer/types.d.ts +5 -8
  65. package/dist/types/messageDelivery/MessageDeliveryReporter.d.ts +25 -79
  66. package/dist/types/messageDelivery/MessageReceiptsTracker.d.ts +15 -82
  67. package/dist/types/messageOperations/FailedSendCache.d.ts +4 -9
  68. package/dist/types/messageOperations/MessageOperationStatePolicy.d.ts +10 -20
  69. package/dist/types/messageOperations/MessageOperations.d.ts +20 -40
  70. package/dist/types/messageOperations/applyReactionLocally.d.ts +5 -22
  71. package/dist/types/messageOperations/createMessageOperations.d.ts +3 -5
  72. package/dist/types/messageOperations/reflectReactionEvent.d.ts +5 -13
  73. package/dist/types/messageOperations/sendOrdering.d.ts +4 -7
  74. package/dist/types/messageOperations/settlePendingAttachmentUploads.d.ts +7 -15
  75. package/dist/types/messageOperations/types.d.ts +15 -30
  76. package/dist/types/middleware.d.ts +7 -5
  77. package/dist/types/moderation.d.ts +21 -14
  78. package/dist/types/notifications/NotificationManager.d.ts +8 -31
  79. package/dist/types/notifications/types.d.ts +19 -59
  80. package/dist/types/offline-support/offline_support_api.d.ts +145 -539
  81. package/dist/types/offline-support/offline_sync_manager.d.ts +18 -42
  82. package/dist/types/offline-support/queueableOperations.d.ts +24 -52
  83. package/dist/types/offline-support/types.d.ts +24 -187
  84. package/dist/types/pagination/ItemIndex.d.ts +4 -12
  85. package/dist/types/pagination/cursorDerivation/aroundPaginationFlags.d.ts +13 -0
  86. package/dist/types/pagination/cursorDerivation/createdAtAroundPaginationFlags.d.ts +2 -4
  87. package/dist/types/pagination/cursorDerivation/linearPaginationFlags.d.ts +2 -3
  88. package/dist/types/pagination/paginators/BasePaginator.d.ts +235 -622
  89. package/dist/types/pagination/paginators/ChannelPaginator.d.ts +67 -125
  90. package/dist/types/pagination/paginators/MessageIntervalPaginator.d.ts +133 -292
  91. package/dist/types/pagination/paginators/MessagePaginator.d.ts +69 -118
  92. package/dist/types/pagination/paginators/PinnedMessagePaginator.d.ts +7 -26
  93. package/dist/types/pagination/paginators/ReminderPaginator.d.ts +2 -2
  94. package/dist/types/pagination/paginators/ThreadPaginator.d.ts +15 -23
  95. package/dist/types/pagination/paginators/UserGroupPaginator.d.ts +2 -6
  96. package/dist/types/pagination/sortCompiler.d.ts +11 -22
  97. package/dist/types/pagination/utility.normalization.d.ts +5 -23
  98. package/dist/types/pagination/utility.search.d.ts +3 -8
  99. package/dist/types/poll.d.ts +14 -14
  100. package/dist/types/poll_manager.d.ts +11 -2
  101. package/dist/types/poll_optimistic_votes.d.ts +10 -14
  102. package/dist/types/reminders/Reminder.d.ts +4 -4
  103. package/dist/types/reminders/ReminderManager.d.ts +7 -20
  104. package/dist/types/search/BaseSearchSource.d.ts +7 -12
  105. package/dist/types/search/ChannelSearchSource.d.ts +7 -7
  106. package/dist/types/search/MessageSearchSource.d.ts +7 -9
  107. package/dist/types/search/SearchController.d.ts +17 -22
  108. package/dist/types/search/types.d.ts +6 -10
  109. package/dist/types/signing.d.ts +12 -3
  110. package/dist/types/thread.d.ts +23 -71
  111. package/dist/types/thread_manager.d.ts +45 -70
  112. package/dist/types/token_manager.d.ts +8 -13
  113. package/dist/types/types.d.ts +64 -125
  114. package/dist/types/types.utility.d.ts +3 -3
  115. package/dist/types/upload-utils.d.ts +5 -10
  116. package/dist/types/uploadManager.d.ts +17 -45
  117. package/dist/types/utils/FixedSizeQueueCache.d.ts +6 -11
  118. package/dist/types/utils/WithSubscriptions.d.ts +5 -15
  119. package/dist/types/utils/concurrency.d.ts +11 -19
  120. package/dist/types/utils/mergeWith/mergeWith.d.ts +8 -11
  121. package/dist/types/utils/mergeWith/mergeWithCore.d.ts +5 -13
  122. package/dist/types/utils/mergeWith/mergeWithDiff.d.ts +22 -0
  123. package/dist/types/utils/objectPath.d.ts +6 -41
  124. package/dist/types/utils/retryable.d.ts +8 -12
  125. package/dist/types/utils/throttling/throttle.d.ts +8 -23
  126. package/dist/types/utils/time.d.ts +24 -64
  127. package/dist/types/utils.d.ts +40 -86
  128. package/package.json +7 -2
  129. package/src/ChannelManager.ts +159 -300
  130. package/src/CooldownTimer.ts +11 -29
  131. package/src/EventHandlerPipeline.ts +21 -44
  132. package/src/LiveLocationManager.ts +35 -74
  133. package/src/api-client.ts +43 -61
  134. package/src/campaign.ts +1 -1
  135. package/src/channel.ts +396 -749
  136. package/src/channel_batch_updater.ts +1 -1
  137. package/src/channel_state.ts +50 -105
  138. package/src/client.ts +360 -588
  139. package/src/client_state.ts +1 -6
  140. package/src/configuration/ConfigController.ts +41 -157
  141. package/src/configuration/InstanceConfigurationRegistry.ts +42 -171
  142. package/src/configuration/index.ts +1 -1
  143. package/src/configuration/keys.ts +13 -32
  144. package/src/configuration/shape.ts +67 -70
  145. package/src/configuration/types.ts +60 -100
  146. package/src/configuration/utils/applyInstanceConfiguration.ts +21 -53
  147. package/src/configuration/utils/copyConfigPatch.ts +11 -32
  148. package/src/configuration/utils/declarativeSlices.ts +9 -39
  149. package/src/configuration/utils/deepFreezeConfig.ts +3 -17
  150. package/src/configuration/utils/index.ts +2 -4
  151. package/src/configuration/utils/serverAuthority.ts +20 -68
  152. package/src/connection/ConnectionIdManager.ts +20 -49
  153. package/src/connection/ConnectionRecoveryManager.ts +51 -178
  154. package/src/connection/networkConnection/NetworkConnectionObserver.ts +37 -114
  155. package/src/connection/networkConnection/reporters.ts +9 -36
  156. package/src/connection/networkConnection/types.ts +12 -27
  157. package/src/connection/wsConnection/StableWSConnection.ts +73 -193
  158. package/src/connection/wsConnection/WSConnection.ts +56 -131
  159. package/src/connection/wsConnection/WSConnectionFallback.ts +22 -31
  160. package/src/connection/wsConnection/config.ts +13 -48
  161. package/src/connection/wsConnection/types.ts +17 -70
  162. package/src/constants.ts +4 -4
  163. package/src/entityStore/EntityStore.ts +162 -167
  164. package/src/entityStore/StoreBackedItemIndex.ts +14 -47
  165. package/src/errors.ts +7 -19
  166. package/src/formatMessageMemo.ts +3 -9
  167. package/src/gen/chat/ChannelApi.ts +19 -0
  168. package/src/gen/chat/ChatApi.ts +87 -13
  169. package/src/gen/models/index.ts +793 -71
  170. package/src/gen/moderation/ModerationApi.ts +0 -4
  171. package/src/index.ts +10 -14
  172. package/src/languageNames.ts +9 -24
  173. package/src/logger.ts +1 -3
  174. package/src/messageComposer/LocationComposer.ts +7 -23
  175. package/src/messageComposer/MessageComposerEffectHandlers.ts +1 -2
  176. package/src/messageComposer/attachmentIdentity.ts +2 -13
  177. package/src/messageComposer/attachmentManager.ts +17 -63
  178. package/src/messageComposer/attachmentUtils.ts +3 -17
  179. package/src/messageComposer/configuration/configuration.ts +5 -8
  180. package/src/messageComposer/configuration/types.ts +29 -56
  181. package/src/messageComposer/fileUtils.ts +1 -1
  182. package/src/messageComposer/linkPreviewsManager.ts +8 -20
  183. package/src/messageComposer/messageComposer.ts +54 -222
  184. package/src/messageComposer/middleware/attachmentManager/postUpload/attachmentEnrichment.ts +4 -16
  185. package/src/messageComposer/middleware/attachmentManager/postUpload/uploadErrorHandler.ts +1 -2
  186. package/src/messageComposer/middleware/messageComposer/attachments.ts +14 -40
  187. package/src/messageComposer/middleware/messageComposer/linkPreviews.ts +3 -4
  188. package/src/messageComposer/middleware/messageComposer/textComposer.ts +1 -1
  189. package/src/messageComposer/middleware/messageComposer/userDataInjection.ts +4 -11
  190. package/src/messageComposer/middleware/pollComposer/state.ts +2 -2
  191. package/src/messageComposer/middleware/pollComposer/types.ts +2 -4
  192. package/src/messageComposer/middleware/pollComposer/validation.ts +8 -26
  193. package/src/messageComposer/middleware/textComposer/TextComposerMiddlewareExecutor.ts +1 -6
  194. package/src/messageComposer/middleware/textComposer/commandUtils.ts +1 -2
  195. package/src/messageComposer/middleware/textComposer/commands.ts +11 -16
  196. package/src/messageComposer/middleware/textComposer/mentions.ts +32 -70
  197. package/src/messageComposer/middleware/textComposer/textMiddlewareUtils.ts +10 -4
  198. package/src/messageComposer/middleware/textComposer/types.ts +1 -6
  199. package/src/messageComposer/pollComposer.ts +11 -8
  200. package/src/messageComposer/textComposer.ts +9 -59
  201. package/src/messageComposer/types.ts +9 -12
  202. package/src/messageDelivery/MessageDeliveryReporter.ts +60 -113
  203. package/src/messageDelivery/MessageReceiptsTracker.ts +21 -105
  204. package/src/messageOperations/FailedSendCache.ts +4 -9
  205. package/src/messageOperations/MessageOperationStatePolicy.ts +40 -67
  206. package/src/messageOperations/MessageOperations.ts +33 -62
  207. package/src/messageOperations/applyReactionLocally.ts +9 -28
  208. package/src/messageOperations/createMessageOperations.ts +17 -33
  209. package/src/messageOperations/reflectReactionEvent.ts +5 -13
  210. package/src/messageOperations/sendOrdering.ts +4 -7
  211. package/src/messageOperations/settlePendingAttachmentUploads.ts +10 -21
  212. package/src/messageOperations/types.ts +15 -30
  213. package/src/middleware.ts +7 -10
  214. package/src/moderation.ts +25 -21
  215. package/src/notifications/NotificationManager.ts +11 -35
  216. package/src/notifications/types.ts +19 -59
  217. package/src/offline-support/offline_support_api.ts +178 -608
  218. package/src/offline-support/offline_sync_manager.ts +25 -69
  219. package/src/offline-support/queueableOperations.ts +31 -84
  220. package/src/offline-support/types.ts +25 -188
  221. package/src/pagination/ItemIndex.ts +4 -12
  222. package/src/pagination/cursorDerivation/aroundPaginationFlags.ts +29 -0
  223. package/src/pagination/cursorDerivation/createdAtAroundPaginationFlags.ts +13 -62
  224. package/src/pagination/cursorDerivation/idAroundPaginationFlags.ts +11 -39
  225. package/src/pagination/cursorDerivation/linearPaginationFlags.ts +4 -6
  226. package/src/pagination/filterCompiler.ts +11 -25
  227. package/src/pagination/paginators/BasePaginator.ts +422 -976
  228. package/src/pagination/paginators/ChannelPaginator.ts +130 -190
  229. package/src/pagination/paginators/MessageIntervalPaginator.ts +226 -457
  230. package/src/pagination/paginators/MessagePaginator.ts +135 -175
  231. package/src/pagination/paginators/PinnedMessagePaginator.ts +13 -38
  232. package/src/pagination/paginators/ReminderPaginator.ts +12 -15
  233. package/src/pagination/paginators/ThreadPaginator.ts +16 -26
  234. package/src/pagination/paginators/UserGroupPaginator.ts +6 -17
  235. package/src/pagination/paginators/stateThrottling.ts +5 -10
  236. package/src/pagination/sortCompiler.ts +23 -50
  237. package/src/pagination/utility.normalization.ts +8 -30
  238. package/src/pagination/utility.search.ts +4 -11
  239. package/src/poll.ts +19 -19
  240. package/src/poll_manager.ts +16 -20
  241. package/src/poll_optimistic_votes.ts +17 -22
  242. package/src/reminders/Reminder.ts +5 -6
  243. package/src/reminders/ReminderManager.ts +9 -34
  244. package/src/reminders/ReminderTimer.ts +1 -2
  245. package/src/search/BaseSearchSource.ts +14 -29
  246. package/src/search/ChannelSearchSource.ts +8 -8
  247. package/src/search/MessageSearchSource.ts +19 -25
  248. package/src/search/SearchController.ts +18 -24
  249. package/src/search/types.ts +6 -10
  250. package/src/segment.ts +1 -1
  251. package/src/signing.ts +23 -3
  252. package/src/thread.ts +72 -173
  253. package/src/thread_manager.ts +59 -76
  254. package/src/token_manager.ts +11 -21
  255. package/src/types.ts +78 -126
  256. package/src/types.utility.ts +3 -3
  257. package/src/upload-utils.ts +5 -10
  258. package/src/uploadManager.ts +22 -54
  259. package/src/utils/FixedSizeQueueCache.ts +13 -21
  260. package/src/utils/WithSubscriptions.ts +5 -15
  261. package/src/utils/concurrency.ts +16 -55
  262. package/src/utils/mergeWith/mergeWith.ts +10 -13
  263. package/src/utils/mergeWith/mergeWithCore.ts +24 -81
  264. package/src/utils/mergeWith/mergeWithDiff.ts +15 -33
  265. package/src/utils/objectPath.ts +6 -41
  266. package/src/utils/retryable.ts +10 -14
  267. package/src/utils/throttling/throttle.ts +9 -25
  268. package/src/utils/time.ts +26 -66
  269. 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
- * A batch of entity-store changes delivered to a subscriber in a single notification.
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
- * Anything that observes entities held in an {@link EntityStore}, linked to them by id
15
- * ({@link EntityStore.link}).
16
- *
17
- * A subscriber watches a *set* of entity ids (a paginator watches all ids in its
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
- * Optional: emit any throttled/pending state notification immediately. Called by
26
- * {@link EntityStore.flushSubscribers} after an optimistic (local-user) write so the change
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
- * Optional: called by {@link EntityStore.changeId} when this subscriber's link moves from `oldId`
33
- * to `newId`, before the change notification. A subscriber that keeps its own list of linked ids
34
- * implements it to rename the entry; otherwise its later `unlink(oldId)` would miss the moved link
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
- * Optional: called when the store removes an entity this subscriber holds, through
40
- * {@link EntityStore.remove} or {@link EntityStore.clear}, with the removed entity. Called after
41
- * its holders are dropped but while the entity can still be read, so a list can locate the item
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 in an {@link EntityStore}, added once with
55
- * {@link EntityStore.addClaim}. Unlike a subscriber it isn't linked per id and gets no
56
- * notifications: the store asks {@link EntityStoreClaim.heldBy} whenever it needs to know what is
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. Matched by identity against the stored ones. */
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 after its entry is removed because its last holder unlinked, or because
74
- * {@link EntityStore.clear} ran. A store of class instances uses it to tear the instance down
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). A store that decides
80
- * on its own when to remove entries sets it to `false`; holders then only receive notifications,
81
- * and entries leave through {@link EntityStore.remove} or {@link EntityStore.clear}.
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
- /** Ids whose {@link EntityStore.flushSubscribers} was requested while a transaction was open. */
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
- // ---- reads ----
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
- /** Whether `subscriber` is one of the holders of `id`. */
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
- /** Whether a subscriber linked to `id`, or a claim listing its entity, holds it. */
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
- * The names of what holds `id`: the subscribers linked to it and the claims listing its entity;
187
- * unnamed ones are left out. For debugging tools.
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
- * Every stored entity that neither a linked subscriber nor a claim holds, with the id it is stored
204
- * under. Each claim is asked once for the whole list.
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 `claim`, which holds the entities its {@link EntityStoreClaim.heldBy} lists, until the
215
- * returned function removes it. Entities are matched by identity, so one stored under an id other
216
- * than its own (a temporary id) is still found.
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
- /** Every entity currently in the store. */
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
- /** Every stored entity with the id it is stored under, which can differ from its own id. */
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 every subscriber watching its id —
255
- * except `subscriber`, the one that performed the write, which is expected to re-render itself
256
- * (the paginator that performed the write already emits its own window inline, so it must not be
257
- * notified a second time through the subscription).
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
- // do not notify if the value hasn't changed (mirrors StateStore.next)
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`, passing it to `hydrate` first, or stores and returns the
271
- * result of `create` when `id` is not stored. A stored entity is never replaced, which is what a
272
- * store of class instances needs: every caller asking for `id` gets the same instance, and fresh
273
- * data is applied to it through `hydrate`.
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
- * Storing a new entity notifies the subscribers already linked to `id`. Hydrating does not — the
276
- * stored reference is unchanged, and the entity publishes its own state changes.
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 ids assigned after storing. Moved
293
- * holders get {@link EntityStoreSubscriber.onIdChanged}, then holders of both ids are notified.
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 (merging is the
297
- * caller's decision). With `replace`, the moved entity takes that entity's place: the holders of
298
- * `newId` stay linked and now hold the moved one, and the replaced entity is passed to
299
- * `onRelease`. Its holders are not told it was removed, so a list can swap the item in place.
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
- // holders of oldId learn that it now resolves to undefined
317
+ // Holders of `oldId` learn it resolves to `undefined`.
324
318
  this.markDirty(oldId);
325
319
  this.subscribers.delete(oldId);
326
- // subscribers may link an id before an entity is stored under it
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
- // re-keyed before autoFlush, so a holder handling the notification already reads newId
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 entity stored under the same id. The holders of that id stay
343
- * linked and now hold `entity`; they are notified of a change, not told of a removal, so a list
344
- * can swap the item in place. The replaced entity is passed to `onRelease`. Storing an entity
345
- * under an id nothing holds yet stores it, as {@link EntityStore.upsert} does.
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
- * with the entity. Each holder gets {@link EntityStoreSubscriber.onEntityRemoved}, and is notified
360
- * that `id` changed (it now resolves to `undefined`). Removing an ID that isn't stored does nothing.
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
- * A holder or `onRelease` that throws doesn't stop the removal: every holder is told, the entry is
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}, except the entity is not passed to `onRelease`: it left this
371
- * entry but is still in use elsewhere, so whoever detaches it decides when it is released.
372
- * Detaching an ID that isn't stored does nothing.
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. Calling it on an
399
- * empty store does nothing.
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
- // holders are dropped first, so a list removing the item on its side unlinks nothing
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
- // released after the store is emptied, so an onRelease reading the store sees the final state
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
- // ---- subscription registry / refcount ----
426
-
427
- /** Registers `subscriber` for `id` (notification target + refcount). */
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`; GCs the canonical copy when none remain and passes it to
439
- * `onRelease`, unless the store keeps unheld entries (`releaseOnLastUnlink: false`). Unlinking a
440
- * subscriber that is not linked does nothing.
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
- * Sugar for atomic / ad-hoc consumers that watch a single id (e.g. a thread watching its parent
457
- * message). Fires `handler` immediately with the current value (mirroring
458
- * {@link StateStore.subscribe}) and on every subsequent change.
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 all notifications produced by writes inside it into a single flush on
473
- * exit. Re-entrant: nested transactions flush only when the outermost exits.
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
- // drained after flush(), because flushState only emits what a subscriber already
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/pending state publish on the subscribers of `id` (via
494
- * {@link EntityStoreSubscriber.flushState}). Called after an optimistic (local-user) write to
495
- * `id` so it renders without the throttle delay. Only that id's own subscribers are flushed — the
496
- * write touched no other id — and flushing a subscriber with nothing pending is a no-op.
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
- * Inside a transaction it is deferred to the outermost exit — inline there it would be a no-op,
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
- // swap out the pending map before notifying so writes made from within a
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) {