@fluidframework/container-runtime 2.0.0-internal.3.0.5 → 2.0.0-internal.3.1.1

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 (329) hide show
  1. package/.eslintrc.js +19 -19
  2. package/.mocharc.js +2 -2
  3. package/api-extractor.json +2 -2
  4. package/dist/batchTracker.d.ts.map +1 -1
  5. package/dist/batchTracker.js +2 -1
  6. package/dist/batchTracker.js.map +1 -1
  7. package/dist/blobManager.d.ts +9 -2
  8. package/dist/blobManager.d.ts.map +1 -1
  9. package/dist/blobManager.js +80 -33
  10. package/dist/blobManager.js.map +1 -1
  11. package/dist/connectionTelemetry.d.ts.map +1 -1
  12. package/dist/connectionTelemetry.js +11 -9
  13. package/dist/connectionTelemetry.js.map +1 -1
  14. package/dist/containerHandleContext.d.ts.map +1 -1
  15. package/dist/containerHandleContext.js +3 -1
  16. package/dist/containerHandleContext.js.map +1 -1
  17. package/dist/containerRuntime.d.ts +11 -1
  18. package/dist/containerRuntime.d.ts.map +1 -1
  19. package/dist/containerRuntime.js +116 -72
  20. package/dist/containerRuntime.js.map +1 -1
  21. package/dist/dataStore.d.ts.map +1 -1
  22. package/dist/dataStore.js +11 -9
  23. package/dist/dataStore.js.map +1 -1
  24. package/dist/dataStoreContext.d.ts +18 -13
  25. package/dist/dataStoreContext.d.ts.map +1 -1
  26. package/dist/dataStoreContext.js +68 -55
  27. package/dist/dataStoreContext.js.map +1 -1
  28. package/dist/dataStoreContexts.d.ts.map +1 -1
  29. package/dist/dataStoreContexts.js +7 -3
  30. package/dist/dataStoreContexts.js.map +1 -1
  31. package/dist/dataStoreRegistry.d.ts.map +1 -1
  32. package/dist/dataStoreRegistry.js +3 -1
  33. package/dist/dataStoreRegistry.js.map +1 -1
  34. package/dist/dataStores.d.ts +26 -1
  35. package/dist/dataStores.d.ts.map +1 -1
  36. package/dist/dataStores.js +103 -18
  37. package/dist/dataStores.js.map +1 -1
  38. package/dist/deltaScheduler.d.ts.map +1 -1
  39. package/dist/deltaScheduler.js +8 -3
  40. package/dist/deltaScheduler.js.map +1 -1
  41. package/dist/garbageCollection.d.ts +33 -14
  42. package/dist/garbageCollection.d.ts.map +1 -1
  43. package/dist/garbageCollection.js +178 -92
  44. package/dist/garbageCollection.js.map +1 -1
  45. package/dist/garbageCollectionConstants.d.ts +1 -0
  46. package/dist/garbageCollectionConstants.d.ts.map +1 -1
  47. package/dist/garbageCollectionConstants.js +4 -1
  48. package/dist/garbageCollectionConstants.js.map +1 -1
  49. package/dist/garbageCollectionHelpers.d.ts +26 -0
  50. package/dist/garbageCollectionHelpers.d.ts.map +1 -0
  51. package/dist/garbageCollectionHelpers.js +45 -0
  52. package/dist/garbageCollectionHelpers.js.map +1 -0
  53. package/dist/gcSweepReadyUsageDetection.d.ts +5 -5
  54. package/dist/gcSweepReadyUsageDetection.d.ts.map +1 -1
  55. package/dist/gcSweepReadyUsageDetection.js +14 -10
  56. package/dist/gcSweepReadyUsageDetection.js.map +1 -1
  57. package/dist/index.d.ts +2 -2
  58. package/dist/index.d.ts.map +1 -1
  59. package/dist/index.js.map +1 -1
  60. package/dist/opLifecycle/batchManager.d.ts +5 -5
  61. package/dist/opLifecycle/batchManager.d.ts.map +1 -1
  62. package/dist/opLifecycle/batchManager.js +19 -12
  63. package/dist/opLifecycle/batchManager.js.map +1 -1
  64. package/dist/opLifecycle/definitions.d.ts.map +1 -1
  65. package/dist/opLifecycle/definitions.js.map +1 -1
  66. package/dist/opLifecycle/index.d.ts.map +1 -1
  67. package/dist/opLifecycle/index.js.map +1 -1
  68. package/dist/opLifecycle/opCompressor.d.ts.map +1 -1
  69. package/dist/opLifecycle/opCompressor.js.map +1 -1
  70. package/dist/opLifecycle/opDecompressor.d.ts +0 -4
  71. package/dist/opLifecycle/opDecompressor.d.ts.map +1 -1
  72. package/dist/opLifecycle/opDecompressor.js +7 -43
  73. package/dist/opLifecycle/opDecompressor.js.map +1 -1
  74. package/dist/opLifecycle/opSplitter.d.ts.map +1 -1
  75. package/dist/opLifecycle/opSplitter.js +4 -1
  76. package/dist/opLifecycle/opSplitter.js.map +1 -1
  77. package/dist/opLifecycle/outbox.d.ts.map +1 -1
  78. package/dist/opLifecycle/outbox.js +20 -19
  79. package/dist/opLifecycle/outbox.js.map +1 -1
  80. package/dist/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
  81. package/dist/opLifecycle/remoteMessageProcessor.js.map +1 -1
  82. package/dist/opProperties.d.ts.map +1 -1
  83. package/dist/opProperties.js +1 -3
  84. package/dist/opProperties.js.map +1 -1
  85. package/dist/orderedClientElection.d.ts.map +1 -1
  86. package/dist/orderedClientElection.js +10 -4
  87. package/dist/orderedClientElection.js.map +1 -1
  88. package/dist/packageVersion.d.ts +1 -1
  89. package/dist/packageVersion.js +1 -1
  90. package/dist/packageVersion.js.map +1 -1
  91. package/dist/pendingStateManager.d.ts +7 -0
  92. package/dist/pendingStateManager.d.ts.map +1 -1
  93. package/dist/pendingStateManager.js +7 -4
  94. package/dist/pendingStateManager.js.map +1 -1
  95. package/dist/runWhileConnectedCoordinator.d.ts.map +1 -1
  96. package/dist/runWhileConnectedCoordinator.js.map +1 -1
  97. package/dist/runningSummarizer.d.ts.map +1 -1
  98. package/dist/runningSummarizer.js +34 -21
  99. package/dist/runningSummarizer.js.map +1 -1
  100. package/dist/scheduleManager.d.ts.map +1 -1
  101. package/dist/scheduleManager.js +3 -2
  102. package/dist/scheduleManager.js.map +1 -1
  103. package/dist/serializedSnapshotStorage.d.ts +2 -2
  104. package/dist/serializedSnapshotStorage.d.ts.map +1 -1
  105. package/dist/serializedSnapshotStorage.js +5 -3
  106. package/dist/serializedSnapshotStorage.js.map +1 -1
  107. package/dist/summarizer.d.ts +2 -2
  108. package/dist/summarizer.d.ts.map +1 -1
  109. package/dist/summarizer.js +37 -17
  110. package/dist/summarizer.js.map +1 -1
  111. package/dist/summarizerClientElection.d.ts.map +1 -1
  112. package/dist/summarizerClientElection.js.map +1 -1
  113. package/dist/summarizerHandle.d.ts.map +1 -1
  114. package/dist/summarizerHandle.js.map +1 -1
  115. package/dist/summarizerHeuristics.d.ts.map +1 -1
  116. package/dist/summarizerHeuristics.js +6 -9
  117. package/dist/summarizerHeuristics.js.map +1 -1
  118. package/dist/summarizerTypes.d.ts +21 -21
  119. package/dist/summarizerTypes.d.ts.map +1 -1
  120. package/dist/summarizerTypes.js.map +1 -1
  121. package/dist/summaryCollection.d.ts.map +1 -1
  122. package/dist/summaryCollection.js +18 -8
  123. package/dist/summaryCollection.js.map +1 -1
  124. package/dist/summaryFormat.d.ts +5 -2
  125. package/dist/summaryFormat.d.ts.map +1 -1
  126. package/dist/summaryFormat.js +18 -10
  127. package/dist/summaryFormat.js.map +1 -1
  128. package/dist/summaryGenerator.d.ts.map +1 -1
  129. package/dist/summaryGenerator.js +35 -16
  130. package/dist/summaryGenerator.js.map +1 -1
  131. package/dist/summaryManager.d.ts.map +1 -1
  132. package/dist/summaryManager.js +21 -9
  133. package/dist/summaryManager.js.map +1 -1
  134. package/dist/throttler.d.ts +2 -2
  135. package/dist/throttler.d.ts.map +1 -1
  136. package/dist/throttler.js +4 -4
  137. package/dist/throttler.js.map +1 -1
  138. package/garbageCollection.md +15 -2
  139. package/lib/batchTracker.d.ts.map +1 -1
  140. package/lib/batchTracker.js +2 -1
  141. package/lib/batchTracker.js.map +1 -1
  142. package/lib/blobManager.d.ts +9 -2
  143. package/lib/blobManager.d.ts.map +1 -1
  144. package/lib/blobManager.js +82 -35
  145. package/lib/blobManager.js.map +1 -1
  146. package/lib/connectionTelemetry.d.ts.map +1 -1
  147. package/lib/connectionTelemetry.js +11 -9
  148. package/lib/connectionTelemetry.js.map +1 -1
  149. package/lib/containerHandleContext.d.ts.map +1 -1
  150. package/lib/containerHandleContext.js +3 -1
  151. package/lib/containerHandleContext.js.map +1 -1
  152. package/lib/containerRuntime.d.ts +11 -1
  153. package/lib/containerRuntime.d.ts.map +1 -1
  154. package/lib/containerRuntime.js +122 -78
  155. package/lib/containerRuntime.js.map +1 -1
  156. package/lib/dataStore.d.ts.map +1 -1
  157. package/lib/dataStore.js +11 -9
  158. package/lib/dataStore.js.map +1 -1
  159. package/lib/dataStoreContext.d.ts +18 -13
  160. package/lib/dataStoreContext.d.ts.map +1 -1
  161. package/lib/dataStoreContext.js +71 -58
  162. package/lib/dataStoreContext.js.map +1 -1
  163. package/lib/dataStoreContexts.d.ts.map +1 -1
  164. package/lib/dataStoreContexts.js +7 -3
  165. package/lib/dataStoreContexts.js.map +1 -1
  166. package/lib/dataStoreRegistry.d.ts.map +1 -1
  167. package/lib/dataStoreRegistry.js +3 -1
  168. package/lib/dataStoreRegistry.js.map +1 -1
  169. package/lib/dataStores.d.ts +26 -1
  170. package/lib/dataStores.d.ts.map +1 -1
  171. package/lib/dataStores.js +109 -24
  172. package/lib/dataStores.js.map +1 -1
  173. package/lib/deltaScheduler.d.ts.map +1 -1
  174. package/lib/deltaScheduler.js +9 -4
  175. package/lib/deltaScheduler.js.map +1 -1
  176. package/lib/garbageCollection.d.ts +33 -14
  177. package/lib/garbageCollection.d.ts.map +1 -1
  178. package/lib/garbageCollection.js +180 -94
  179. package/lib/garbageCollection.js.map +1 -1
  180. package/lib/garbageCollectionConstants.d.ts +1 -0
  181. package/lib/garbageCollectionConstants.d.ts.map +1 -1
  182. package/lib/garbageCollectionConstants.js +3 -0
  183. package/lib/garbageCollectionConstants.js.map +1 -1
  184. package/lib/garbageCollectionHelpers.d.ts +26 -0
  185. package/lib/garbageCollectionHelpers.d.ts.map +1 -0
  186. package/lib/garbageCollectionHelpers.js +40 -0
  187. package/lib/garbageCollectionHelpers.js.map +1 -0
  188. package/lib/gcSweepReadyUsageDetection.d.ts +5 -5
  189. package/lib/gcSweepReadyUsageDetection.d.ts.map +1 -1
  190. package/lib/gcSweepReadyUsageDetection.js +14 -10
  191. package/lib/gcSweepReadyUsageDetection.js.map +1 -1
  192. package/lib/index.d.ts +2 -2
  193. package/lib/index.d.ts.map +1 -1
  194. package/lib/index.js +1 -1
  195. package/lib/index.js.map +1 -1
  196. package/lib/opLifecycle/batchManager.d.ts +5 -5
  197. package/lib/opLifecycle/batchManager.d.ts.map +1 -1
  198. package/lib/opLifecycle/batchManager.js +19 -12
  199. package/lib/opLifecycle/batchManager.js.map +1 -1
  200. package/lib/opLifecycle/definitions.d.ts.map +1 -1
  201. package/lib/opLifecycle/definitions.js.map +1 -1
  202. package/lib/opLifecycle/index.d.ts.map +1 -1
  203. package/lib/opLifecycle/index.js.map +1 -1
  204. package/lib/opLifecycle/opCompressor.d.ts.map +1 -1
  205. package/lib/opLifecycle/opCompressor.js.map +1 -1
  206. package/lib/opLifecycle/opDecompressor.d.ts +0 -4
  207. package/lib/opLifecycle/opDecompressor.d.ts.map +1 -1
  208. package/lib/opLifecycle/opDecompressor.js +7 -43
  209. package/lib/opLifecycle/opDecompressor.js.map +1 -1
  210. package/lib/opLifecycle/opSplitter.d.ts.map +1 -1
  211. package/lib/opLifecycle/opSplitter.js +5 -2
  212. package/lib/opLifecycle/opSplitter.js.map +1 -1
  213. package/lib/opLifecycle/outbox.d.ts.map +1 -1
  214. package/lib/opLifecycle/outbox.js +20 -19
  215. package/lib/opLifecycle/outbox.js.map +1 -1
  216. package/lib/opLifecycle/remoteMessageProcessor.d.ts.map +1 -1
  217. package/lib/opLifecycle/remoteMessageProcessor.js.map +1 -1
  218. package/lib/opProperties.d.ts.map +1 -1
  219. package/lib/opProperties.js +1 -3
  220. package/lib/opProperties.js.map +1 -1
  221. package/lib/orderedClientElection.d.ts.map +1 -1
  222. package/lib/orderedClientElection.js +10 -4
  223. package/lib/orderedClientElection.js.map +1 -1
  224. package/lib/packageVersion.d.ts +1 -1
  225. package/lib/packageVersion.js +1 -1
  226. package/lib/packageVersion.js.map +1 -1
  227. package/lib/pendingStateManager.d.ts +7 -0
  228. package/lib/pendingStateManager.d.ts.map +1 -1
  229. package/lib/pendingStateManager.js +7 -4
  230. package/lib/pendingStateManager.js.map +1 -1
  231. package/lib/runWhileConnectedCoordinator.d.ts.map +1 -1
  232. package/lib/runWhileConnectedCoordinator.js.map +1 -1
  233. package/lib/runningSummarizer.d.ts.map +1 -1
  234. package/lib/runningSummarizer.js +35 -22
  235. package/lib/runningSummarizer.js.map +1 -1
  236. package/lib/scheduleManager.d.ts.map +1 -1
  237. package/lib/scheduleManager.js +3 -2
  238. package/lib/scheduleManager.js.map +1 -1
  239. package/lib/serializedSnapshotStorage.d.ts +2 -2
  240. package/lib/serializedSnapshotStorage.d.ts.map +1 -1
  241. package/lib/serializedSnapshotStorage.js +5 -3
  242. package/lib/serializedSnapshotStorage.js.map +1 -1
  243. package/lib/summarizer.d.ts +2 -2
  244. package/lib/summarizer.d.ts.map +1 -1
  245. package/lib/summarizer.js +37 -17
  246. package/lib/summarizer.js.map +1 -1
  247. package/lib/summarizerClientElection.d.ts.map +1 -1
  248. package/lib/summarizerClientElection.js.map +1 -1
  249. package/lib/summarizerHandle.d.ts.map +1 -1
  250. package/lib/summarizerHandle.js.map +1 -1
  251. package/lib/summarizerHeuristics.d.ts.map +1 -1
  252. package/lib/summarizerHeuristics.js +6 -9
  253. package/lib/summarizerHeuristics.js.map +1 -1
  254. package/lib/summarizerTypes.d.ts +21 -21
  255. package/lib/summarizerTypes.d.ts.map +1 -1
  256. package/lib/summarizerTypes.js.map +1 -1
  257. package/lib/summaryCollection.d.ts.map +1 -1
  258. package/lib/summaryCollection.js +18 -8
  259. package/lib/summaryCollection.js.map +1 -1
  260. package/lib/summaryFormat.d.ts +5 -2
  261. package/lib/summaryFormat.d.ts.map +1 -1
  262. package/lib/summaryFormat.js +20 -12
  263. package/lib/summaryFormat.js.map +1 -1
  264. package/lib/summaryGenerator.d.ts.map +1 -1
  265. package/lib/summaryGenerator.js +35 -16
  266. package/lib/summaryGenerator.js.map +1 -1
  267. package/lib/summaryManager.d.ts.map +1 -1
  268. package/lib/summaryManager.js +21 -9
  269. package/lib/summaryManager.js.map +1 -1
  270. package/lib/throttler.d.ts +2 -2
  271. package/lib/throttler.d.ts.map +1 -1
  272. package/lib/throttler.js +4 -4
  273. package/lib/throttler.js.map +1 -1
  274. package/package.json +121 -115
  275. package/prettier.config.cjs +1 -1
  276. package/src/batchTracker.ts +54 -49
  277. package/src/blobManager.ts +793 -672
  278. package/src/connectionTelemetry.ts +280 -249
  279. package/src/containerHandleContext.ts +27 -29
  280. package/src/containerRuntime.ts +3168 -2988
  281. package/src/dataStore.ts +172 -159
  282. package/src/dataStoreContext.ts +1098 -1055
  283. package/src/dataStoreContexts.ts +178 -161
  284. package/src/dataStoreRegistry.ts +25 -20
  285. package/src/dataStores.ts +884 -728
  286. package/src/deltaScheduler.ts +158 -150
  287. package/src/garbageCollection.ts +1860 -1688
  288. package/src/garbageCollectionConstants.ts +3 -0
  289. package/src/garbageCollectionHelpers.ts +61 -0
  290. package/src/gcSweepReadyUsageDetection.ts +89 -83
  291. package/src/index.ts +67 -66
  292. package/src/opLifecycle/README.md +152 -0
  293. package/src/opLifecycle/batchManager.ts +145 -141
  294. package/src/opLifecycle/definitions.ts +29 -29
  295. package/src/opLifecycle/index.ts +5 -5
  296. package/src/opLifecycle/opCompressor.ts +54 -53
  297. package/src/opLifecycle/opDecompressor.ts +100 -128
  298. package/src/opLifecycle/opSplitter.ts +214 -188
  299. package/src/opLifecycle/outbox.ts +204 -195
  300. package/src/opLifecycle/remoteMessageProcessor.ts +62 -62
  301. package/src/opProperties.ts +11 -9
  302. package/src/orderedClientElection.ts +489 -457
  303. package/src/packageVersion.ts +1 -1
  304. package/src/pendingStateManager.ts +384 -338
  305. package/src/runWhileConnectedCoordinator.ts +78 -71
  306. package/src/runningSummarizer.ts +619 -581
  307. package/src/scheduleManager.ts +299 -269
  308. package/src/serializedSnapshotStorage.ts +126 -112
  309. package/src/summarizer.ts +417 -381
  310. package/src/summarizerClientElection.ts +107 -100
  311. package/src/summarizerHandle.ts +11 -9
  312. package/src/summarizerHeuristics.ts +183 -186
  313. package/src/summarizerTypes.ts +344 -330
  314. package/src/summaryCollection.ts +378 -349
  315. package/src/summaryFormat.ts +165 -143
  316. package/src/summaryGenerator.ts +465 -410
  317. package/src/summaryManager.ts +377 -348
  318. package/src/throttler.ts +131 -122
  319. package/tsconfig.esnext.json +6 -6
  320. package/tsconfig.json +9 -13
  321. package/dist/garbageCollectionTombstoneUtils.d.ts +0 -14
  322. package/dist/garbageCollectionTombstoneUtils.d.ts.map +0 -1
  323. package/dist/garbageCollectionTombstoneUtils.js +0 -23
  324. package/dist/garbageCollectionTombstoneUtils.js.map +0 -1
  325. package/lib/garbageCollectionTombstoneUtils.d.ts +0 -14
  326. package/lib/garbageCollectionTombstoneUtils.d.ts.map +0 -1
  327. package/lib/garbageCollectionTombstoneUtils.js +0 -19
  328. package/lib/garbageCollectionTombstoneUtils.js.map +0 -1
  329. package/src/garbageCollectionTombstoneUtils.ts +0 -28
@@ -7,9 +7,7 @@ import { IDisposable } from "@fluidframework/common-definitions";
7
7
  import { assert, Lazy } from "@fluidframework/common-utils";
8
8
  import { ICriticalContainerError } from "@fluidframework/container-definitions";
9
9
  import { DataProcessingError } from "@fluidframework/container-utils";
10
- import {
11
- ISequencedDocumentMessage,
12
- } from "@fluidframework/protocol-definitions";
10
+ import { ISequencedDocumentMessage } from "@fluidframework/protocol-definitions";
13
11
  import Deque from "double-ended-queue";
14
12
  import { ContainerMessageType } from "./containerRuntime";
15
13
  import { pkgVersion } from "./packageVersion";
@@ -17,15 +15,16 @@ import { pkgVersion } from "./packageVersion";
17
15
  /**
18
16
  * This represents a message that has been submitted and is added to the pending queue when `submit` is called on the
19
17
  * ContainerRuntime. This message has either not been ack'd by the server or has not been submitted to the server yet.
18
+ * @deprecated - This interface will no longer be exported in a future version
20
19
  */
21
20
  export interface IPendingMessage {
22
- type: "message";
23
- messageType: ContainerMessageType;
24
- clientSequenceNumber: number;
25
- referenceSequenceNumber: number;
26
- content: any;
27
- localOpMetadata: unknown;
28
- opMetadata: Record<string, unknown> | undefined;
21
+ type: "message";
22
+ messageType: ContainerMessageType;
23
+ clientSequenceNumber: number;
24
+ referenceSequenceNumber: number;
25
+ content: any;
26
+ localOpMetadata: unknown;
27
+ opMetadata: Record<string, unknown> | undefined;
29
28
  }
30
29
 
31
30
  /**
@@ -34,33 +33,40 @@ export interface IPendingMessage {
34
33
  * @deprecated Use batch metadata on IPendingMessage instead. To be removed in 2.0.0-internal.4.0.0 (AB#2496)
35
34
  */
36
35
  export interface IPendingFlush {
37
- type: "flush";
36
+ type: "flush";
38
37
  }
39
38
 
39
+ /**
40
+ * @deprecated - This interface will no longer be exported in a future version
41
+ */
40
42
  export type IPendingState = IPendingMessage | IPendingFlush;
41
43
 
44
+ /**
45
+ * @deprecated - This interface will no longer be exported in a future version
46
+ */
42
47
  export interface IPendingLocalState {
43
- /**
44
- * list of pending states, including ops and batch information
45
- */
46
- pendingStates: IPendingState[];
48
+ /**
49
+ * list of pending states, including ops and batch information
50
+ */
51
+ pendingStates: IPendingState[];
47
52
  }
48
53
 
49
54
  export interface IRuntimeStateHandler {
50
- connected(): boolean;
51
- clientId(): string | undefined;
52
- close(error?: ICriticalContainerError): void;
53
- applyStashedOp: (type: ContainerMessageType, content: ISequencedDocumentMessage) => Promise<unknown>;
54
- reSubmit(
55
- type: ContainerMessageType,
56
- content: any,
57
- localOpMetadata: unknown,
58
- opMetadata: Record<string, unknown> | undefined): void;
59
- rollback(
60
- type: ContainerMessageType,
61
- content: any,
62
- localOpMetadata: unknown): void;
63
- orderSequentially(callback: () => void): void;
55
+ connected(): boolean;
56
+ clientId(): string | undefined;
57
+ close(error?: ICriticalContainerError): void;
58
+ applyStashedOp: (
59
+ type: ContainerMessageType,
60
+ content: ISequencedDocumentMessage,
61
+ ) => Promise<unknown>;
62
+ reSubmit(
63
+ type: ContainerMessageType,
64
+ content: any,
65
+ localOpMetadata: unknown,
66
+ opMetadata: Record<string, unknown> | undefined,
67
+ ): void;
68
+ rollback(type: ContainerMessageType, content: any, localOpMetadata: unknown): void;
69
+ orderSequentially(callback: () => void): void;
64
70
  }
65
71
 
66
72
  /**
@@ -73,313 +79,353 @@ export interface IRuntimeStateHandler {
73
79
  * It verifies that all the ops are acked, are received in the right order and batch information is correct.
74
80
  */
75
81
  export class PendingStateManager implements IDisposable {
76
- private readonly pendingMessages = new Deque<IPendingMessage>();
77
- private readonly initialMessages = new Deque<IPendingMessage>();
78
- private readonly disposeOnce = new Lazy<void>(() => {
79
- this.initialMessages.clear();
80
- this.pendingMessages.clear();
81
- });
82
-
83
- public get pendingMessagesCount(): number {
84
- return this.pendingMessages.length;
85
- }
86
-
87
- // Indicates whether we are processing a batch.
88
- private isProcessingBatch: boolean = false;
89
-
90
- // This stores the first message in the batch that we are processing. This is used to verify that we get
91
- // the correct batch metadata.
92
- private pendingBatchBeginMessage: ISequencedDocumentMessage | undefined;
93
-
94
- private clientId: string | undefined;
95
-
96
- /**
97
- * Called to check if there are any pending messages in the pending message queue.
98
- * @returns A boolean indicating whether there are messages or not.
99
- */
100
- public hasPendingMessages(): boolean {
101
- return !this.pendingMessages.isEmpty() || !this.initialMessages.isEmpty();
102
- }
103
-
104
- public getLocalState(): IPendingLocalState | undefined {
105
- assert(this.initialMessages.isEmpty(), 0x2e9 /* "Must call getLocalState() after applying initial states" */);
106
- if (!this.pendingMessages.isEmpty()) {
107
- return {
108
- pendingStates: this.pendingMessages.toArray().reduce((arr, message) => {
109
- // delete localOpMetadata since it may not be serializable
110
- // and will be regenerated by applyStashedOp()
111
- arr.push({ ...message, localOpMetadata: undefined });
112
-
113
- // TODO: Remove in 2.0.0-internal.4.0.0 (AB#2496)
114
- if (message.opMetadata?.batch === false) {
115
- arr.push({ type: "flush" });
116
- }
117
- return arr;
118
- }, new Array<IPendingState>()),
119
- };
120
- }
121
- }
122
-
123
- constructor(
124
- private readonly stateHandler: IRuntimeStateHandler,
125
- initialLocalState: IPendingLocalState | undefined,
126
- ) {
127
- /**
128
- * Convert old local state format to the new format
129
- * The old format contained "flush" messages as the indicator of batch ends
130
- * The new format instead uses batch metadata on the last message to indicate batch ends
131
- * ! TODO: Remove this conversion in "2.0.0-internal.4.0.0" as rollback from future version will be new format
132
- * AB#2496 tracks removal
133
- */
134
- if (initialLocalState?.pendingStates) {
135
- const pendingStates = initialLocalState?.pendingStates;
136
- let currentlyBatching = false;
137
- for (let i = 0; i < pendingStates.length; i++) {
138
- const initialState = pendingStates[i];
139
-
140
- // Skip over "flush" messages
141
- if (initialState.type === "message") {
142
- if (initialState.opMetadata?.batch) {
143
- currentlyBatching = true;
144
- } else if (initialState.opMetadata?.batch === false) {
145
- currentlyBatching = false;
146
- } else if (
147
- // End of batch if we are currently batching and this is last message or next message is flush
148
- currentlyBatching
149
- && (i === pendingStates.length - 1 || pendingStates[i + 1].type === "flush")
150
- ) {
151
- currentlyBatching = false;
152
- initialState.opMetadata = { ...initialState.opMetadata, batch: false };
153
- }
154
- this.initialMessages.push(initialState);
155
- }
156
- }
157
- }
158
- }
159
-
160
- public get disposed() { return this.disposeOnce.evaluated; }
161
- public readonly dispose = () => this.disposeOnce.value;
162
-
163
- /**
164
- * Called when a message is submitted locally. Adds the message and the associated details to the pending state
165
- * queue.
166
- * @param type - The container message type.
167
- * @param clientSequenceNumber - The clientSequenceNumber associated with the message.
168
- * @param content - The message content.
169
- * @param localOpMetadata - The local metadata associated with the message.
170
- */
171
- public onSubmitMessage(
172
- type: ContainerMessageType,
173
- clientSequenceNumber: number,
174
- referenceSequenceNumber: number,
175
- content: any,
176
- localOpMetadata: unknown,
177
- opMetadata: Record<string, unknown> | undefined,
178
- ) {
179
- const pendingMessage: IPendingMessage = {
180
- type: "message",
181
- messageType: type,
182
- clientSequenceNumber,
183
- referenceSequenceNumber,
184
- content,
185
- localOpMetadata,
186
- opMetadata,
187
- };
188
-
189
- this.pendingMessages.push(pendingMessage);
190
- }
191
-
192
- /**
193
- * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted
194
- * @param seqNum - Sequence number at which to apply ops. Will apply all ops if seqNum is undefined.
195
- */
196
- public async applyStashedOpsAt(seqNum?: number) {
197
- // apply stashed ops at sequence number
198
- while (!this.initialMessages.isEmpty()) {
199
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
200
- const nextMessage = this.initialMessages.peekFront()!;
201
- if (seqNum !== undefined) {
202
- if (nextMessage.referenceSequenceNumber > seqNum) {
203
- break; // nothing left to do at this sequence number
204
- }
205
- if (nextMessage.referenceSequenceNumber < seqNum) {
206
- throw new Error("loaded from snapshot too recent to apply stashed ops");
207
- }
208
- }
209
-
210
- // applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it
211
- const localOpMetadata =
212
- await this.stateHandler.applyStashedOp(nextMessage.messageType, nextMessage.content);
213
- nextMessage.localOpMetadata = localOpMetadata;
214
-
215
- // then we push onto pendingMessages which will cause PendingStateManager to resubmit when we connect
216
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
217
- this.pendingMessages.push(this.initialMessages.shift()!);
218
- }
219
- }
220
-
221
- /**
222
- * Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that
223
- * the batch information was preserved for batch messages.
224
- * @param message - The message that got ack'd and needs to be processed.
225
- */
226
- public processPendingLocalMessage(message: ISequencedDocumentMessage): unknown {
227
- // Pre-processing part - This may be the start of a batch.
228
- this.maybeProcessBatchBegin(message);
229
-
230
- // Get the next message from the pending queue. Verify a message exists.
231
- const pendingMessage = this.pendingMessages.peekFront();
232
- assert(pendingMessage !== undefined, 0x169 /* "No pending message found for this remote message" */);
233
- this.pendingMessages.shift();
234
-
235
- // Processing part - Verify that there has been no data corruption.
236
- // The clientSequenceNumber of the incoming message must match that of the pending message.
237
- if (pendingMessage.clientSequenceNumber !== message.clientSequenceNumber) {
238
- // Close the container because this could indicate data corruption.
239
- const error = DataProcessingError.create(
240
- "pending local message clientSequenceNumber mismatch",
241
- "unexpectedAckReceived",
242
- message,
243
- { expectedClientSequenceNumber: pendingMessage.clientSequenceNumber },
244
- );
245
-
246
- this.stateHandler.close(error);
247
- return;
248
- }
249
-
250
- // Post-processing part - If we are processing a batch then this could be the last message in the batch.
251
- this.maybeProcessBatchEnd(message);
252
-
253
- return pendingMessage.localOpMetadata;
254
- }
255
-
256
- /**
257
- * This message could be the first message in batch. If so, set batch state marking the beginning of a batch.
258
- * @param message - The message that is being processed.
259
- */
260
- private maybeProcessBatchBegin(message: ISequencedDocumentMessage) {
261
- // This message is the first in a batch if the "batch" property on the metadata is set to true
262
- if (message.metadata?.batch) {
263
- // We should not already be processing a batch and there should be no pending batch begin message.
264
- assert(!this.isProcessingBatch && this.pendingBatchBeginMessage === undefined,
265
- 0x16b /* "The pending batch state indicates we are already processing a batch" */);
266
-
267
- // Set the pending batch state indicating we have started processing a batch.
268
- this.pendingBatchBeginMessage = message;
269
- this.isProcessingBatch = true;
270
- }
271
- }
272
-
273
- /**
274
- * This message could be the last message in batch. If so, clear batch state since the batch is complete.
275
- * @param message - The message that is being processed.
276
- */
277
- private maybeProcessBatchEnd(message: ISequencedDocumentMessage) {
278
- if (!this.isProcessingBatch) {
279
- return;
280
- }
281
-
282
- // There should be a pending batch begin message.
283
- assert(this.pendingBatchBeginMessage !== undefined, 0x16d /* "There is no pending batch begin message" */);
284
-
285
- const batchEndMetadata = message.metadata?.batch;
286
- if (this.pendingMessages.isEmpty() || batchEndMetadata === false) {
287
- // Get the batch begin metadata from the first message in the batch.
288
- const batchBeginMetadata = this.pendingBatchBeginMessage.metadata?.batch;
289
-
290
- // There could be just a single message in the batch. If so, it should not have any batch metadata. If there
291
- // are multiple messages in the batch, verify that we got the correct batch begin and end metadata.
292
- if (this.pendingBatchBeginMessage === message) {
293
- assert(batchBeginMetadata === undefined,
294
- 0x16e /* "Batch with single message should not have batch metadata" */);
295
- } else {
296
- if (batchBeginMetadata !== true || batchEndMetadata !== false) {
297
- this.stateHandler.close(DataProcessingError.create(
298
- "Pending batch inconsistency", // Formerly known as asserts 0x16f and 0x170
299
- "processPendingLocalMessage",
300
- message,
301
- {
302
- runtimeVersion: pkgVersion,
303
- batchClientId: this.pendingBatchBeginMessage.clientId,
304
- clientId: this.stateHandler.clientId(),
305
- hasBatchStart: batchBeginMetadata === true,
306
- hasBatchEnd: batchEndMetadata === false,
307
- messageType: message.type,
308
- batchStartSequenceNumber: this.pendingBatchBeginMessage.clientSequenceNumber,
309
- pendingMessagesCount: this.pendingMessagesCount,
310
- }));
311
- }
312
- }
313
-
314
- // Clear the pending batch state now that we have processed the entire batch.
315
- this.pendingBatchBeginMessage = undefined;
316
- this.isProcessingBatch = false;
317
- }
318
- }
319
-
320
- /**
321
- * Called when the Container's connection state changes. If the Container gets connected, it replays all the pending
322
- * states in its queue. This includes triggering resubmission of unacked ops.
323
- */
324
- public replayPendingStates() {
325
- assert(this.stateHandler.connected(), 0x172 /* "The connection state is not consistent with the runtime" */);
326
-
327
- // This assert suggests we are about to send same ops twice, which will result in data loss.
328
- assert(this.clientId !== this.stateHandler.clientId(),
329
- 0x173 /* "replayPendingStates called twice for same clientId!" */);
330
- this.clientId = this.stateHandler.clientId();
331
-
332
- assert(this.initialMessages.isEmpty(), 0x174 /* "initial states should be empty before replaying pending" */);
333
-
334
- let pendingMessagesCount = this.pendingMessages.length;
335
- if (pendingMessagesCount === 0) {
336
- return;
337
- }
338
-
339
- // Process exactly `pendingMessagesCount` items in the queue as it represents the number of messages that were
340
- // pending when we connected. This is important because the `reSubmitFn` might add more items in the queue
341
- // which must not be replayed.
342
- while (pendingMessagesCount > 0) {
343
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
344
- let pendingMessage = this.pendingMessages.shift()!;
345
- pendingMessagesCount--;
346
- assert(pendingMessage.opMetadata?.batch !== false, 0x41b /* We cannot process batches in chunks */);
347
-
348
- /**
349
- * We want to ensure grouped messages get processed in a batch.
350
- * Note: It is not possible for the PendingStateManager to receive a partially acked batch. It will
351
- * either receive the whole batch ack or nothing at all.
352
- */
353
- if (pendingMessage.opMetadata?.batch) {
354
- assert(pendingMessagesCount > 0, 0x554 /* Last pending message cannot be a batch begin */);
355
-
356
- this.stateHandler.orderSequentially(() => {
357
- while (pendingMessagesCount >= 0) { // check is >= because batch end may be last pending message
358
- this.stateHandler.reSubmit(
359
- pendingMessage.messageType,
360
- pendingMessage.content,
361
- pendingMessage.localOpMetadata,
362
- pendingMessage.opMetadata);
363
-
364
- if (pendingMessage.opMetadata?.batch === false) {
365
- break;
366
- }
367
- assert(pendingMessagesCount > 0, 0x555 /* No batch end found */);
368
-
369
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
370
- pendingMessage = this.pendingMessages.shift()!;
371
- pendingMessagesCount--;
372
- assert(pendingMessage.opMetadata?.batch !== true,
373
- 0x556 /* Batch start needs a corresponding batch end */);
374
- }
375
- });
376
- } else {
377
- this.stateHandler.reSubmit(
378
- pendingMessage.messageType,
379
- pendingMessage.content,
380
- pendingMessage.localOpMetadata,
381
- pendingMessage.opMetadata);
382
- }
383
- }
384
- }
82
+ private readonly pendingMessages = new Deque<IPendingMessage>();
83
+ private readonly initialMessages = new Deque<IPendingMessage>();
84
+ private readonly disposeOnce = new Lazy<void>(() => {
85
+ this.initialMessages.clear();
86
+ this.pendingMessages.clear();
87
+ });
88
+
89
+ public get pendingMessagesCount(): number {
90
+ return this.pendingMessages.length;
91
+ }
92
+
93
+ // Indicates whether we are processing a batch.
94
+ private isProcessingBatch: boolean = false;
95
+
96
+ // This stores the first message in the batch that we are processing. This is used to verify that we get
97
+ // the correct batch metadata.
98
+ private pendingBatchBeginMessage: ISequencedDocumentMessage | undefined;
99
+
100
+ private clientId: string | undefined;
101
+
102
+ /**
103
+ * Called to check if there are any pending messages in the pending message queue.
104
+ * @returns A boolean indicating whether there are messages or not.
105
+ */
106
+ public hasPendingMessages(): boolean {
107
+ return !this.pendingMessages.isEmpty() || !this.initialMessages.isEmpty();
108
+ }
109
+
110
+ public getLocalState(): IPendingLocalState | undefined {
111
+ assert(
112
+ this.initialMessages.isEmpty(),
113
+ 0x2e9 /* "Must call getLocalState() after applying initial states" */,
114
+ );
115
+ if (!this.pendingMessages.isEmpty()) {
116
+ return {
117
+ pendingStates: this.pendingMessages.toArray().reduce((arr, message) => {
118
+ // delete localOpMetadata since it may not be serializable
119
+ // and will be regenerated by applyStashedOp()
120
+ arr.push({ ...message, localOpMetadata: undefined });
121
+
122
+ // TODO: Remove in 2.0.0-internal.4.0.0 (AB#2496)
123
+ if (message.opMetadata?.batch === false) {
124
+ arr.push({ type: "flush" });
125
+ }
126
+ return arr;
127
+ }, new Array<IPendingState>()),
128
+ };
129
+ }
130
+ }
131
+
132
+ constructor(
133
+ private readonly stateHandler: IRuntimeStateHandler,
134
+ initialLocalState: IPendingLocalState | undefined,
135
+ ) {
136
+ /**
137
+ * Convert old local state format to the new format
138
+ * The old format contained "flush" messages as the indicator of batch ends
139
+ * The new format instead uses batch metadata on the last message to indicate batch ends
140
+ * ! TODO: Remove this conversion in "2.0.0-internal.4.0.0" as rollback from future version will be new format
141
+ * AB#2496 tracks removal
142
+ */
143
+ if (initialLocalState?.pendingStates) {
144
+ const pendingStates = initialLocalState?.pendingStates;
145
+ let currentlyBatching = false;
146
+ for (let i = 0; i < pendingStates.length; i++) {
147
+ const initialState = pendingStates[i];
148
+
149
+ // Skip over "flush" messages
150
+ if (initialState.type === "message") {
151
+ if (initialState.opMetadata?.batch) {
152
+ currentlyBatching = true;
153
+ } else if (initialState.opMetadata?.batch === false) {
154
+ currentlyBatching = false;
155
+ } else if (
156
+ // End of batch if we are currently batching and this is last message or next message is flush
157
+ currentlyBatching &&
158
+ (i === pendingStates.length - 1 || pendingStates[i + 1].type === "flush")
159
+ ) {
160
+ currentlyBatching = false;
161
+ initialState.opMetadata = { ...initialState.opMetadata, batch: false };
162
+ }
163
+ this.initialMessages.push(initialState);
164
+ }
165
+ }
166
+ }
167
+ }
168
+
169
+ public get disposed() {
170
+ return this.disposeOnce.evaluated;
171
+ }
172
+ public readonly dispose = () => this.disposeOnce.value;
173
+
174
+ /**
175
+ * Called when a message is submitted locally. Adds the message and the associated details to the pending state
176
+ * queue.
177
+ * @param type - The container message type.
178
+ * @param clientSequenceNumber - The clientSequenceNumber associated with the message.
179
+ * @param content - The message content.
180
+ * @param localOpMetadata - The local metadata associated with the message.
181
+ */
182
+ public onSubmitMessage(
183
+ type: ContainerMessageType,
184
+ clientSequenceNumber: number,
185
+ referenceSequenceNumber: number,
186
+ content: any,
187
+ localOpMetadata: unknown,
188
+ opMetadata: Record<string, unknown> | undefined,
189
+ ) {
190
+ const pendingMessage: IPendingMessage = {
191
+ type: "message",
192
+ messageType: type,
193
+ clientSequenceNumber,
194
+ referenceSequenceNumber,
195
+ content,
196
+ localOpMetadata,
197
+ opMetadata,
198
+ };
199
+
200
+ this.pendingMessages.push(pendingMessage);
201
+ }
202
+
203
+ /**
204
+ * Applies stashed ops at their reference sequence number so they are ready to be ACKed or resubmitted
205
+ * @param seqNum - Sequence number at which to apply ops. Will apply all ops if seqNum is undefined.
206
+ */
207
+ public async applyStashedOpsAt(seqNum?: number) {
208
+ // apply stashed ops at sequence number
209
+ while (!this.initialMessages.isEmpty()) {
210
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
211
+ const nextMessage = this.initialMessages.peekFront()!;
212
+ if (seqNum !== undefined) {
213
+ if (nextMessage.referenceSequenceNumber > seqNum) {
214
+ break; // nothing left to do at this sequence number
215
+ }
216
+ if (nextMessage.referenceSequenceNumber < seqNum) {
217
+ throw new Error("loaded from snapshot too recent to apply stashed ops");
218
+ }
219
+ }
220
+
221
+ // applyStashedOp will cause the DDS to behave as if it has sent the op but not actually send it
222
+ const localOpMetadata = await this.stateHandler.applyStashedOp(
223
+ nextMessage.messageType,
224
+ nextMessage.content,
225
+ );
226
+ nextMessage.localOpMetadata = localOpMetadata;
227
+
228
+ // then we push onto pendingMessages which will cause PendingStateManager to resubmit when we connect
229
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
230
+ this.pendingMessages.push(this.initialMessages.shift()!);
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Processes a local message once its ack'd by the server. It verifies that there was no data corruption and that
236
+ * the batch information was preserved for batch messages.
237
+ * @param message - The message that got ack'd and needs to be processed.
238
+ */
239
+ public processPendingLocalMessage(message: ISequencedDocumentMessage): unknown {
240
+ // Pre-processing part - This may be the start of a batch.
241
+ this.maybeProcessBatchBegin(message);
242
+
243
+ // Get the next message from the pending queue. Verify a message exists.
244
+ const pendingMessage = this.pendingMessages.peekFront();
245
+ assert(
246
+ pendingMessage !== undefined,
247
+ 0x169 /* "No pending message found for this remote message" */,
248
+ );
249
+ this.pendingMessages.shift();
250
+
251
+ // Processing part - Verify that there has been no data corruption.
252
+ // The clientSequenceNumber of the incoming message must match that of the pending message.
253
+ if (pendingMessage.clientSequenceNumber !== message.clientSequenceNumber) {
254
+ // Close the container because this could indicate data corruption.
255
+ const error = DataProcessingError.create(
256
+ "pending local message clientSequenceNumber mismatch",
257
+ "unexpectedAckReceived",
258
+ message,
259
+ { expectedClientSequenceNumber: pendingMessage.clientSequenceNumber },
260
+ );
261
+
262
+ this.stateHandler.close(error);
263
+ return;
264
+ }
265
+
266
+ // Post-processing part - If we are processing a batch then this could be the last message in the batch.
267
+ this.maybeProcessBatchEnd(message);
268
+
269
+ return pendingMessage.localOpMetadata;
270
+ }
271
+
272
+ /**
273
+ * This message could be the first message in batch. If so, set batch state marking the beginning of a batch.
274
+ * @param message - The message that is being processed.
275
+ */
276
+ private maybeProcessBatchBegin(message: ISequencedDocumentMessage) {
277
+ // This message is the first in a batch if the "batch" property on the metadata is set to true
278
+ if (message.metadata?.batch) {
279
+ // We should not already be processing a batch and there should be no pending batch begin message.
280
+ assert(
281
+ !this.isProcessingBatch && this.pendingBatchBeginMessage === undefined,
282
+ 0x16b /* "The pending batch state indicates we are already processing a batch" */,
283
+ );
284
+
285
+ // Set the pending batch state indicating we have started processing a batch.
286
+ this.pendingBatchBeginMessage = message;
287
+ this.isProcessingBatch = true;
288
+ }
289
+ }
290
+
291
+ /**
292
+ * This message could be the last message in batch. If so, clear batch state since the batch is complete.
293
+ * @param message - The message that is being processed.
294
+ */
295
+ private maybeProcessBatchEnd(message: ISequencedDocumentMessage) {
296
+ if (!this.isProcessingBatch) {
297
+ return;
298
+ }
299
+
300
+ // There should be a pending batch begin message.
301
+ assert(
302
+ this.pendingBatchBeginMessage !== undefined,
303
+ 0x16d /* "There is no pending batch begin message" */,
304
+ );
305
+
306
+ const batchEndMetadata = message.metadata?.batch;
307
+ if (this.pendingMessages.isEmpty() || batchEndMetadata === false) {
308
+ // Get the batch begin metadata from the first message in the batch.
309
+ const batchBeginMetadata = this.pendingBatchBeginMessage.metadata?.batch;
310
+
311
+ // There could be just a single message in the batch. If so, it should not have any batch metadata. If there
312
+ // are multiple messages in the batch, verify that we got the correct batch begin and end metadata.
313
+ if (this.pendingBatchBeginMessage === message) {
314
+ assert(
315
+ batchBeginMetadata === undefined,
316
+ 0x16e /* "Batch with single message should not have batch metadata" */,
317
+ );
318
+ } else {
319
+ if (batchBeginMetadata !== true || batchEndMetadata !== false) {
320
+ this.stateHandler.close(
321
+ DataProcessingError.create(
322
+ "Pending batch inconsistency", // Formerly known as asserts 0x16f and 0x170
323
+ "processPendingLocalMessage",
324
+ message,
325
+ {
326
+ runtimeVersion: pkgVersion,
327
+ batchClientId: this.pendingBatchBeginMessage.clientId,
328
+ clientId: this.stateHandler.clientId(),
329
+ hasBatchStart: batchBeginMetadata === true,
330
+ hasBatchEnd: batchEndMetadata === false,
331
+ messageType: message.type,
332
+ batchStartSequenceNumber:
333
+ this.pendingBatchBeginMessage.clientSequenceNumber,
334
+ pendingMessagesCount: this.pendingMessagesCount,
335
+ },
336
+ ),
337
+ );
338
+ }
339
+ }
340
+
341
+ // Clear the pending batch state now that we have processed the entire batch.
342
+ this.pendingBatchBeginMessage = undefined;
343
+ this.isProcessingBatch = false;
344
+ }
345
+ }
346
+
347
+ /**
348
+ * Called when the Container's connection state changes. If the Container gets connected, it replays all the pending
349
+ * states in its queue. This includes triggering resubmission of unacked ops.
350
+ */
351
+ public replayPendingStates() {
352
+ assert(
353
+ this.stateHandler.connected(),
354
+ 0x172 /* "The connection state is not consistent with the runtime" */,
355
+ );
356
+
357
+ // This assert suggests we are about to send same ops twice, which will result in data loss.
358
+ assert(
359
+ this.clientId !== this.stateHandler.clientId(),
360
+ 0x173 /* "replayPendingStates called twice for same clientId!" */,
361
+ );
362
+ this.clientId = this.stateHandler.clientId();
363
+
364
+ assert(
365
+ this.initialMessages.isEmpty(),
366
+ 0x174 /* "initial states should be empty before replaying pending" */,
367
+ );
368
+
369
+ let pendingMessagesCount = this.pendingMessages.length;
370
+ if (pendingMessagesCount === 0) {
371
+ return;
372
+ }
373
+
374
+ // Process exactly `pendingMessagesCount` items in the queue as it represents the number of messages that were
375
+ // pending when we connected. This is important because the `reSubmitFn` might add more items in the queue
376
+ // which must not be replayed.
377
+ while (pendingMessagesCount > 0) {
378
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
379
+ let pendingMessage = this.pendingMessages.shift()!;
380
+ pendingMessagesCount--;
381
+ assert(
382
+ pendingMessage.opMetadata?.batch !== false,
383
+ 0x41b /* We cannot process batches in chunks */,
384
+ );
385
+
386
+ /**
387
+ * We want to ensure grouped messages get processed in a batch.
388
+ * Note: It is not possible for the PendingStateManager to receive a partially acked batch. It will
389
+ * either receive the whole batch ack or nothing at all.
390
+ */
391
+ if (pendingMessage.opMetadata?.batch) {
392
+ assert(
393
+ pendingMessagesCount > 0,
394
+ 0x554 /* Last pending message cannot be a batch begin */,
395
+ );
396
+
397
+ this.stateHandler.orderSequentially(() => {
398
+ while (pendingMessagesCount >= 0) {
399
+ // check is >= because batch end may be last pending message
400
+ this.stateHandler.reSubmit(
401
+ pendingMessage.messageType,
402
+ pendingMessage.content,
403
+ pendingMessage.localOpMetadata,
404
+ pendingMessage.opMetadata,
405
+ );
406
+
407
+ if (pendingMessage.opMetadata?.batch === false) {
408
+ break;
409
+ }
410
+ assert(pendingMessagesCount > 0, 0x555 /* No batch end found */);
411
+
412
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
413
+ pendingMessage = this.pendingMessages.shift()!;
414
+ pendingMessagesCount--;
415
+ assert(
416
+ pendingMessage.opMetadata?.batch !== true,
417
+ 0x556 /* Batch start needs a corresponding batch end */,
418
+ );
419
+ }
420
+ });
421
+ } else {
422
+ this.stateHandler.reSubmit(
423
+ pendingMessage.messageType,
424
+ pendingMessage.content,
425
+ pendingMessage.localOpMetadata,
426
+ pendingMessage.opMetadata,
427
+ );
428
+ }
429
+ }
430
+ }
385
431
  }