@fluidframework/tree 2.61.0 → 2.62.0

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 (388) hide show
  1. package/CHANGELOG.md +162 -0
  2. package/api-report/tree.alpha.api.md +26 -21
  3. package/api-report/tree.beta.api.md +15 -0
  4. package/api-report/tree.legacy.beta.api.md +18 -0
  5. package/dist/alpha.d.ts +8 -8
  6. package/dist/api.d.ts +17 -0
  7. package/dist/api.d.ts.map +1 -0
  8. package/dist/api.js +24 -0
  9. package/dist/api.js.map +1 -0
  10. package/dist/beta.d.ts +5 -0
  11. package/dist/codec/codec.d.ts +3 -5
  12. package/dist/codec/codec.d.ts.map +1 -1
  13. package/dist/codec/codec.js +9 -2
  14. package/dist/codec/codec.js.map +1 -1
  15. package/dist/codec/index.d.ts +0 -1
  16. package/dist/codec/index.d.ts.map +1 -1
  17. package/dist/codec/index.js +1 -3
  18. package/dist/codec/index.js.map +1 -1
  19. package/dist/core/index.d.ts +1 -1
  20. package/dist/core/index.d.ts.map +1 -1
  21. package/dist/core/index.js +2 -1
  22. package/dist/core/index.js.map +1 -1
  23. package/dist/core/rebase/index.d.ts +1 -1
  24. package/dist/core/rebase/index.d.ts.map +1 -1
  25. package/dist/core/rebase/index.js +2 -1
  26. package/dist/core/rebase/index.js.map +1 -1
  27. package/dist/core/rebase/utils.d.ts +10 -0
  28. package/dist/core/rebase/utils.d.ts.map +1 -1
  29. package/dist/core/rebase/utils.js +20 -1
  30. package/dist/core/rebase/utils.js.map +1 -1
  31. package/dist/core/tree/detachedFieldIndex.js +1 -1
  32. package/dist/core/tree/detachedFieldIndex.js.map +1 -1
  33. package/dist/external-utilities/index.d.ts +1 -1
  34. package/dist/external-utilities/index.d.ts.map +1 -1
  35. package/dist/external-utilities/index.js +1 -2
  36. package/dist/external-utilities/index.js.map +1 -1
  37. package/dist/external-utilities/typeboxValidator.d.ts +0 -13
  38. package/dist/external-utilities/typeboxValidator.d.ts.map +1 -1
  39. package/dist/external-utilities/typeboxValidator.js +3 -5
  40. package/dist/external-utilities/typeboxValidator.js.map +1 -1
  41. package/dist/feature-libraries/flex-tree/flexTreeTypes.d.ts +2 -0
  42. package/dist/feature-libraries/flex-tree/flexTreeTypes.d.ts.map +1 -1
  43. package/dist/feature-libraries/flex-tree/flexTreeTypes.js.map +1 -1
  44. package/dist/feature-libraries/flex-tree/index.d.ts +1 -0
  45. package/dist/feature-libraries/flex-tree/index.d.ts.map +1 -1
  46. package/dist/feature-libraries/flex-tree/index.js +4 -1
  47. package/dist/feature-libraries/flex-tree/index.js.map +1 -1
  48. package/dist/feature-libraries/flex-tree/lazyNode.d.ts.map +1 -1
  49. package/dist/feature-libraries/flex-tree/lazyNode.js +15 -8
  50. package/dist/feature-libraries/flex-tree/lazyNode.js.map +1 -1
  51. package/dist/feature-libraries/flex-tree/observer.d.ts +32 -0
  52. package/dist/feature-libraries/flex-tree/observer.d.ts.map +1 -0
  53. package/dist/feature-libraries/flex-tree/observer.js +33 -0
  54. package/dist/feature-libraries/flex-tree/observer.js.map +1 -0
  55. package/dist/feature-libraries/index.d.ts +1 -1
  56. package/dist/feature-libraries/index.d.ts.map +1 -1
  57. package/dist/feature-libraries/index.js +3 -1
  58. package/dist/feature-libraries/index.js.map +1 -1
  59. package/dist/index.d.ts +5 -5
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +9 -8
  62. package/dist/index.js.map +1 -1
  63. package/dist/legacy.d.ts +7 -1
  64. package/dist/packageVersion.d.ts +1 -1
  65. package/dist/packageVersion.js +1 -1
  66. package/dist/packageVersion.js.map +1 -1
  67. package/dist/shared-tree/index.d.ts +2 -2
  68. package/dist/shared-tree/index.d.ts.map +1 -1
  69. package/dist/shared-tree/index.js.map +1 -1
  70. package/dist/shared-tree/schematizingTreeView.js +2 -2
  71. package/dist/shared-tree/schematizingTreeView.js.map +1 -1
  72. package/dist/shared-tree/sharedTree.d.ts +21 -6
  73. package/dist/shared-tree/sharedTree.d.ts.map +1 -1
  74. package/dist/shared-tree/sharedTree.js +76 -37
  75. package/dist/shared-tree/sharedTree.js.map +1 -1
  76. package/dist/shared-tree/treeAlpha.d.ts +114 -1
  77. package/dist/shared-tree/treeAlpha.d.ts.map +1 -1
  78. package/dist/shared-tree/treeAlpha.js +140 -1
  79. package/dist/shared-tree/treeAlpha.js.map +1 -1
  80. package/dist/shared-tree/treeCheckout.d.ts +13 -7
  81. package/dist/shared-tree/treeCheckout.d.ts.map +1 -1
  82. package/dist/shared-tree/treeCheckout.js +115 -85
  83. package/dist/shared-tree/treeCheckout.js.map +1 -1
  84. package/dist/shared-tree-core/branch.d.ts +3 -0
  85. package/dist/shared-tree-core/branch.d.ts.map +1 -1
  86. package/dist/shared-tree-core/branch.js.map +1 -1
  87. package/dist/shared-tree-core/branchIdCodec.d.ts +11 -0
  88. package/dist/shared-tree-core/branchIdCodec.d.ts.map +1 -0
  89. package/dist/shared-tree-core/branchIdCodec.js +18 -0
  90. package/dist/shared-tree-core/branchIdCodec.js.map +1 -0
  91. package/dist/shared-tree-core/editManager.d.ts +39 -64
  92. package/dist/shared-tree-core/editManager.d.ts.map +1 -1
  93. package/dist/shared-tree-core/editManager.js +455 -295
  94. package/dist/shared-tree-core/editManager.js.map +1 -1
  95. package/dist/shared-tree-core/editManagerCodecs.d.ts +1 -1
  96. package/dist/shared-tree-core/editManagerCodecs.d.ts.map +1 -1
  97. package/dist/shared-tree-core/editManagerCodecs.js +7 -96
  98. package/dist/shared-tree-core/editManagerCodecs.js.map +1 -1
  99. package/dist/shared-tree-core/editManagerCodecsCommons.d.ts +17 -0
  100. package/dist/shared-tree-core/editManagerCodecsCommons.d.ts.map +1 -0
  101. package/dist/shared-tree-core/editManagerCodecsCommons.js +139 -0
  102. package/dist/shared-tree-core/editManagerCodecsCommons.js.map +1 -0
  103. package/dist/shared-tree-core/editManagerCodecsV1toV4.d.ts +16 -0
  104. package/dist/shared-tree-core/editManagerCodecsV1toV4.d.ts.map +1 -0
  105. package/dist/shared-tree-core/editManagerCodecsV1toV4.js +39 -0
  106. package/dist/shared-tree-core/editManagerCodecsV1toV4.js.map +1 -0
  107. package/dist/shared-tree-core/editManagerCodecsV5.d.ts +16 -0
  108. package/dist/shared-tree-core/editManagerCodecsV5.d.ts.map +1 -0
  109. package/dist/shared-tree-core/editManagerCodecsV5.js +58 -0
  110. package/dist/shared-tree-core/editManagerCodecsV5.js.map +1 -0
  111. package/dist/shared-tree-core/{editManagerFormat.d.ts → editManagerFormatCommons.d.ts} +31 -7
  112. package/dist/shared-tree-core/editManagerFormatCommons.d.ts.map +1 -0
  113. package/dist/shared-tree-core/{editManagerFormat.js → editManagerFormatCommons.js} +13 -12
  114. package/dist/shared-tree-core/editManagerFormatCommons.js.map +1 -0
  115. package/dist/shared-tree-core/editManagerFormatV1toV4.d.ts +31 -0
  116. package/dist/shared-tree-core/editManagerFormatV1toV4.d.ts.map +1 -0
  117. package/dist/shared-tree-core/editManagerFormatV1toV4.js +24 -0
  118. package/dist/shared-tree-core/editManagerFormatV1toV4.js.map +1 -0
  119. package/dist/shared-tree-core/editManagerFormatV5.d.ts +62 -0
  120. package/dist/shared-tree-core/editManagerFormatV5.d.ts.map +1 -0
  121. package/dist/shared-tree-core/editManagerFormatV5.js +20 -0
  122. package/dist/shared-tree-core/editManagerFormatV5.js.map +1 -0
  123. package/dist/shared-tree-core/index.d.ts +3 -3
  124. package/dist/shared-tree-core/index.d.ts.map +1 -1
  125. package/dist/shared-tree-core/index.js.map +1 -1
  126. package/dist/shared-tree-core/messageCodecV1ToV4.d.ts +11 -0
  127. package/dist/shared-tree-core/messageCodecV1ToV4.d.ts.map +1 -0
  128. package/dist/shared-tree-core/messageCodecV1ToV4.js +59 -0
  129. package/dist/shared-tree-core/messageCodecV1ToV4.js.map +1 -0
  130. package/dist/shared-tree-core/messageCodecV5.d.ts +11 -0
  131. package/dist/shared-tree-core/messageCodecV5.d.ts.map +1 -0
  132. package/dist/shared-tree-core/messageCodecV5.js +78 -0
  133. package/dist/shared-tree-core/messageCodecV5.js.map +1 -0
  134. package/dist/shared-tree-core/messageCodecs.d.ts.map +1 -1
  135. package/dist/shared-tree-core/messageCodecs.js +16 -47
  136. package/dist/shared-tree-core/messageCodecs.js.map +1 -1
  137. package/dist/shared-tree-core/{messageFormat.d.ts → messageFormatV1ToV4.d.ts} +1 -1
  138. package/dist/shared-tree-core/messageFormatV1ToV4.d.ts.map +1 -0
  139. package/dist/shared-tree-core/{messageFormat.js → messageFormatV1ToV4.js} +1 -1
  140. package/dist/shared-tree-core/messageFormatV1ToV4.js.map +1 -0
  141. package/dist/shared-tree-core/messageFormatV5.d.ts +42 -0
  142. package/dist/shared-tree-core/messageFormatV5.d.ts.map +1 -0
  143. package/dist/shared-tree-core/messageFormatV5.js +20 -0
  144. package/dist/shared-tree-core/messageFormatV5.js.map +1 -0
  145. package/dist/shared-tree-core/messageTypes.d.ts +12 -2
  146. package/dist/shared-tree-core/messageTypes.d.ts.map +1 -1
  147. package/dist/shared-tree-core/messageTypes.js.map +1 -1
  148. package/dist/shared-tree-core/sequenceIdUtils.d.ts +1 -1
  149. package/dist/shared-tree-core/sequenceIdUtils.d.ts.map +1 -1
  150. package/dist/shared-tree-core/sequenceIdUtils.js.map +1 -1
  151. package/dist/shared-tree-core/sharedTreeCore.d.ts +19 -5
  152. package/dist/shared-tree-core/sharedTreeCore.d.ts.map +1 -1
  153. package/dist/shared-tree-core/sharedTreeCore.js +182 -58
  154. package/dist/shared-tree-core/sharedTreeCore.js.map +1 -1
  155. package/dist/simple-tree/api/tree.d.ts +17 -0
  156. package/dist/simple-tree/api/tree.d.ts.map +1 -1
  157. package/dist/simple-tree/api/tree.js +2 -0
  158. package/dist/simple-tree/api/tree.js.map +1 -1
  159. package/dist/simple-tree/core/unhydratedFlexTree.d.ts.map +1 -1
  160. package/dist/simple-tree/core/unhydratedFlexTree.js +7 -1
  161. package/dist/simple-tree/core/unhydratedFlexTree.js.map +1 -1
  162. package/dist/treeFactory.d.ts +38 -9
  163. package/dist/treeFactory.d.ts.map +1 -1
  164. package/dist/treeFactory.js +44 -9
  165. package/dist/treeFactory.js.map +1 -1
  166. package/lib/alpha.d.ts +8 -8
  167. package/lib/api.d.ts +17 -0
  168. package/lib/api.d.ts.map +1 -0
  169. package/lib/api.js +22 -0
  170. package/lib/api.js.map +1 -0
  171. package/lib/beta.d.ts +5 -0
  172. package/lib/codec/codec.d.ts +3 -5
  173. package/lib/codec/codec.d.ts.map +1 -1
  174. package/lib/codec/codec.js +8 -1
  175. package/lib/codec/codec.js.map +1 -1
  176. package/lib/codec/index.d.ts +0 -1
  177. package/lib/codec/index.d.ts.map +1 -1
  178. package/lib/codec/index.js +0 -1
  179. package/lib/codec/index.js.map +1 -1
  180. package/lib/core/index.d.ts +1 -1
  181. package/lib/core/index.d.ts.map +1 -1
  182. package/lib/core/index.js +1 -1
  183. package/lib/core/index.js.map +1 -1
  184. package/lib/core/rebase/index.d.ts +1 -1
  185. package/lib/core/rebase/index.d.ts.map +1 -1
  186. package/lib/core/rebase/index.js +1 -1
  187. package/lib/core/rebase/index.js.map +1 -1
  188. package/lib/core/rebase/utils.d.ts +10 -0
  189. package/lib/core/rebase/utils.d.ts.map +1 -1
  190. package/lib/core/rebase/utils.js +18 -0
  191. package/lib/core/rebase/utils.js.map +1 -1
  192. package/lib/core/tree/detachedFieldIndex.js +2 -2
  193. package/lib/core/tree/detachedFieldIndex.js.map +1 -1
  194. package/lib/external-utilities/index.d.ts +1 -1
  195. package/lib/external-utilities/index.d.ts.map +1 -1
  196. package/lib/external-utilities/index.js +1 -1
  197. package/lib/external-utilities/index.js.map +1 -1
  198. package/lib/external-utilities/typeboxValidator.d.ts +0 -13
  199. package/lib/external-utilities/typeboxValidator.d.ts.map +1 -1
  200. package/lib/external-utilities/typeboxValidator.js +1 -3
  201. package/lib/external-utilities/typeboxValidator.js.map +1 -1
  202. package/lib/feature-libraries/flex-tree/flexTreeTypes.d.ts +2 -0
  203. package/lib/feature-libraries/flex-tree/flexTreeTypes.d.ts.map +1 -1
  204. package/lib/feature-libraries/flex-tree/flexTreeTypes.js.map +1 -1
  205. package/lib/feature-libraries/flex-tree/index.d.ts +1 -0
  206. package/lib/feature-libraries/flex-tree/index.d.ts.map +1 -1
  207. package/lib/feature-libraries/flex-tree/index.js +1 -0
  208. package/lib/feature-libraries/flex-tree/index.js.map +1 -1
  209. package/lib/feature-libraries/flex-tree/lazyNode.d.ts.map +1 -1
  210. package/lib/feature-libraries/flex-tree/lazyNode.js +15 -8
  211. package/lib/feature-libraries/flex-tree/lazyNode.js.map +1 -1
  212. package/lib/feature-libraries/flex-tree/observer.d.ts +32 -0
  213. package/lib/feature-libraries/flex-tree/observer.d.ts.map +1 -0
  214. package/lib/feature-libraries/flex-tree/observer.js +40 -0
  215. package/lib/feature-libraries/flex-tree/observer.js.map +1 -0
  216. package/lib/feature-libraries/index.d.ts +1 -1
  217. package/lib/feature-libraries/index.d.ts.map +1 -1
  218. package/lib/feature-libraries/index.js +1 -1
  219. package/lib/feature-libraries/index.js.map +1 -1
  220. package/lib/index.d.ts +5 -5
  221. package/lib/index.d.ts.map +1 -1
  222. package/lib/index.js +3 -3
  223. package/lib/index.js.map +1 -1
  224. package/lib/legacy.d.ts +7 -1
  225. package/lib/packageVersion.d.ts +1 -1
  226. package/lib/packageVersion.js +1 -1
  227. package/lib/packageVersion.js.map +1 -1
  228. package/lib/shared-tree/index.d.ts +2 -2
  229. package/lib/shared-tree/index.d.ts.map +1 -1
  230. package/lib/shared-tree/index.js.map +1 -1
  231. package/lib/shared-tree/schematizingTreeView.js +2 -2
  232. package/lib/shared-tree/schematizingTreeView.js.map +1 -1
  233. package/lib/shared-tree/sharedTree.d.ts +21 -6
  234. package/lib/shared-tree/sharedTree.d.ts.map +1 -1
  235. package/lib/shared-tree/sharedTree.js +78 -39
  236. package/lib/shared-tree/sharedTree.js.map +1 -1
  237. package/lib/shared-tree/treeAlpha.d.ts +114 -1
  238. package/lib/shared-tree/treeAlpha.d.ts.map +1 -1
  239. package/lib/shared-tree/treeAlpha.js +143 -4
  240. package/lib/shared-tree/treeAlpha.js.map +1 -1
  241. package/lib/shared-tree/treeCheckout.d.ts +13 -7
  242. package/lib/shared-tree/treeCheckout.d.ts.map +1 -1
  243. package/lib/shared-tree/treeCheckout.js +117 -87
  244. package/lib/shared-tree/treeCheckout.js.map +1 -1
  245. package/lib/shared-tree-core/branch.d.ts +3 -0
  246. package/lib/shared-tree-core/branch.d.ts.map +1 -1
  247. package/lib/shared-tree-core/branch.js.map +1 -1
  248. package/lib/shared-tree-core/branchIdCodec.d.ts +11 -0
  249. package/lib/shared-tree-core/branchIdCodec.d.ts.map +1 -0
  250. package/lib/shared-tree-core/branchIdCodec.js +13 -0
  251. package/lib/shared-tree-core/branchIdCodec.js.map +1 -0
  252. package/lib/shared-tree-core/editManager.d.ts +39 -64
  253. package/lib/shared-tree-core/editManager.d.ts.map +1 -1
  254. package/lib/shared-tree-core/editManager.js +455 -295
  255. package/lib/shared-tree-core/editManager.js.map +1 -1
  256. package/lib/shared-tree-core/editManagerCodecs.d.ts +1 -1
  257. package/lib/shared-tree-core/editManagerCodecs.d.ts.map +1 -1
  258. package/lib/shared-tree-core/editManagerCodecs.js +4 -93
  259. package/lib/shared-tree-core/editManagerCodecs.js.map +1 -1
  260. package/lib/shared-tree-core/editManagerCodecsCommons.d.ts +17 -0
  261. package/lib/shared-tree-core/editManagerCodecsCommons.d.ts.map +1 -0
  262. package/lib/shared-tree-core/editManagerCodecsCommons.js +134 -0
  263. package/lib/shared-tree-core/editManagerCodecsCommons.js.map +1 -0
  264. package/lib/shared-tree-core/editManagerCodecsV1toV4.d.ts +16 -0
  265. package/lib/shared-tree-core/editManagerCodecsV1toV4.d.ts.map +1 -0
  266. package/lib/shared-tree-core/editManagerCodecsV1toV4.js +35 -0
  267. package/lib/shared-tree-core/editManagerCodecsV1toV4.js.map +1 -0
  268. package/lib/shared-tree-core/editManagerCodecsV5.d.ts +16 -0
  269. package/lib/shared-tree-core/editManagerCodecsV5.d.ts.map +1 -0
  270. package/lib/shared-tree-core/editManagerCodecsV5.js +54 -0
  271. package/lib/shared-tree-core/editManagerCodecsV5.js.map +1 -0
  272. package/lib/shared-tree-core/{editManagerFormat.d.ts → editManagerFormatCommons.d.ts} +31 -7
  273. package/lib/shared-tree-core/editManagerFormatCommons.d.ts.map +1 -0
  274. package/lib/shared-tree-core/{editManagerFormat.js → editManagerFormatCommons.js} +10 -11
  275. package/lib/shared-tree-core/editManagerFormatCommons.js.map +1 -0
  276. package/lib/shared-tree-core/editManagerFormatV1toV4.d.ts +31 -0
  277. package/lib/shared-tree-core/editManagerFormatV1toV4.d.ts.map +1 -0
  278. package/lib/shared-tree-core/editManagerFormatV1toV4.js +20 -0
  279. package/lib/shared-tree-core/editManagerFormatV1toV4.js.map +1 -0
  280. package/lib/shared-tree-core/editManagerFormatV5.d.ts +62 -0
  281. package/lib/shared-tree-core/editManagerFormatV5.d.ts.map +1 -0
  282. package/lib/shared-tree-core/editManagerFormatV5.js +16 -0
  283. package/lib/shared-tree-core/editManagerFormatV5.js.map +1 -0
  284. package/lib/shared-tree-core/index.d.ts +3 -3
  285. package/lib/shared-tree-core/index.d.ts.map +1 -1
  286. package/lib/shared-tree-core/index.js.map +1 -1
  287. package/lib/shared-tree-core/messageCodecV1ToV4.d.ts +11 -0
  288. package/lib/shared-tree-core/messageCodecV1ToV4.d.ts.map +1 -0
  289. package/lib/shared-tree-core/messageCodecV1ToV4.js +55 -0
  290. package/lib/shared-tree-core/messageCodecV1ToV4.js.map +1 -0
  291. package/lib/shared-tree-core/messageCodecV5.d.ts +11 -0
  292. package/lib/shared-tree-core/messageCodecV5.d.ts.map +1 -0
  293. package/lib/shared-tree-core/messageCodecV5.js +74 -0
  294. package/lib/shared-tree-core/messageCodecV5.js.map +1 -0
  295. package/lib/shared-tree-core/messageCodecs.d.ts.map +1 -1
  296. package/lib/shared-tree-core/messageCodecs.js +17 -48
  297. package/lib/shared-tree-core/messageCodecs.js.map +1 -1
  298. package/lib/shared-tree-core/{messageFormat.d.ts → messageFormatV1ToV4.d.ts} +1 -1
  299. package/lib/shared-tree-core/messageFormatV1ToV4.d.ts.map +1 -0
  300. package/lib/shared-tree-core/{messageFormat.js → messageFormatV1ToV4.js} +1 -1
  301. package/lib/shared-tree-core/messageFormatV1ToV4.js.map +1 -0
  302. package/lib/shared-tree-core/messageFormatV5.d.ts +42 -0
  303. package/lib/shared-tree-core/messageFormatV5.d.ts.map +1 -0
  304. package/lib/shared-tree-core/messageFormatV5.js +16 -0
  305. package/lib/shared-tree-core/messageFormatV5.js.map +1 -0
  306. package/lib/shared-tree-core/messageTypes.d.ts +12 -2
  307. package/lib/shared-tree-core/messageTypes.d.ts.map +1 -1
  308. package/lib/shared-tree-core/messageTypes.js.map +1 -1
  309. package/lib/shared-tree-core/sequenceIdUtils.d.ts +1 -1
  310. package/lib/shared-tree-core/sequenceIdUtils.d.ts.map +1 -1
  311. package/lib/shared-tree-core/sequenceIdUtils.js.map +1 -1
  312. package/lib/shared-tree-core/sharedTreeCore.d.ts +19 -5
  313. package/lib/shared-tree-core/sharedTreeCore.d.ts.map +1 -1
  314. package/lib/shared-tree-core/sharedTreeCore.js +183 -59
  315. package/lib/shared-tree-core/sharedTreeCore.js.map +1 -1
  316. package/lib/simple-tree/api/tree.d.ts +17 -0
  317. package/lib/simple-tree/api/tree.d.ts.map +1 -1
  318. package/lib/simple-tree/api/tree.js +2 -0
  319. package/lib/simple-tree/api/tree.js.map +1 -1
  320. package/lib/simple-tree/core/unhydratedFlexTree.d.ts.map +1 -1
  321. package/lib/simple-tree/core/unhydratedFlexTree.js +8 -2
  322. package/lib/simple-tree/core/unhydratedFlexTree.js.map +1 -1
  323. package/lib/treeFactory.d.ts +38 -9
  324. package/lib/treeFactory.d.ts.map +1 -1
  325. package/lib/treeFactory.js +41 -8
  326. package/lib/treeFactory.js.map +1 -1
  327. package/package.json +25 -25
  328. package/src/api.ts +30 -0
  329. package/src/codec/codec.ts +12 -6
  330. package/src/codec/index.ts +0 -1
  331. package/src/core/index.ts +1 -0
  332. package/src/core/rebase/index.ts +1 -0
  333. package/src/core/rebase/utils.ts +27 -0
  334. package/src/core/tree/detachedFieldIndex.ts +2 -2
  335. package/src/external-utilities/index.ts +1 -1
  336. package/src/external-utilities/typeboxValidator.ts +1 -3
  337. package/src/feature-libraries/flex-tree/flexTreeTypes.ts +2 -0
  338. package/src/feature-libraries/flex-tree/index.ts +2 -0
  339. package/src/feature-libraries/flex-tree/lazyNode.ts +13 -3
  340. package/src/feature-libraries/flex-tree/observer.ts +64 -0
  341. package/src/feature-libraries/index.ts +3 -0
  342. package/src/index.ts +6 -4
  343. package/src/packageVersion.ts +1 -1
  344. package/src/shared-tree/index.ts +2 -0
  345. package/src/shared-tree/schematizingTreeView.ts +2 -2
  346. package/src/shared-tree/sharedTree.ts +116 -52
  347. package/src/shared-tree/treeAlpha.ts +309 -4
  348. package/src/shared-tree/treeCheckout.ts +152 -100
  349. package/src/shared-tree-core/branch.ts +7 -0
  350. package/src/shared-tree-core/branchIdCodec.ts +28 -0
  351. package/src/shared-tree-core/editManager.ts +729 -430
  352. package/src/shared-tree-core/editManagerCodecs.ts +4 -164
  353. package/src/shared-tree-core/editManagerCodecsCommons.ts +245 -0
  354. package/src/shared-tree-core/editManagerCodecsV1toV4.ts +108 -0
  355. package/src/shared-tree-core/editManagerCodecsV5.ts +156 -0
  356. package/src/shared-tree-core/{editManagerFormat.ts → editManagerFormatCommons.ts} +17 -13
  357. package/src/shared-tree-core/editManagerFormatV1toV4.ts +42 -0
  358. package/src/shared-tree-core/editManagerFormatV5.ts +35 -0
  359. package/src/shared-tree-core/index.ts +3 -1
  360. package/src/shared-tree-core/messageCodecV1ToV4.ts +104 -0
  361. package/src/shared-tree-core/messageCodecV5.ts +131 -0
  362. package/src/shared-tree-core/messageCodecs.ts +16 -85
  363. package/src/shared-tree-core/messageFormatV5.ts +50 -0
  364. package/src/shared-tree-core/messageTypes.ts +15 -2
  365. package/src/shared-tree-core/sequenceIdUtils.ts +1 -1
  366. package/src/shared-tree-core/sharedTreeCore.ts +281 -85
  367. package/src/simple-tree/api/tree.ts +23 -0
  368. package/src/simple-tree/core/unhydratedFlexTree.ts +11 -2
  369. package/src/treeFactory.ts +48 -8
  370. package/dist/codec/noopValidator.d.ts +0 -13
  371. package/dist/codec/noopValidator.d.ts.map +0 -1
  372. package/dist/codec/noopValidator.js +0 -17
  373. package/dist/codec/noopValidator.js.map +0 -1
  374. package/dist/shared-tree-core/editManagerFormat.d.ts.map +0 -1
  375. package/dist/shared-tree-core/editManagerFormat.js.map +0 -1
  376. package/dist/shared-tree-core/messageFormat.d.ts.map +0 -1
  377. package/dist/shared-tree-core/messageFormat.js.map +0 -1
  378. package/docs/user-facing/schema-evolution.md +0 -309
  379. package/lib/codec/noopValidator.d.ts +0 -13
  380. package/lib/codec/noopValidator.d.ts.map +0 -1
  381. package/lib/codec/noopValidator.js +0 -14
  382. package/lib/codec/noopValidator.js.map +0 -1
  383. package/lib/shared-tree-core/editManagerFormat.d.ts.map +0 -1
  384. package/lib/shared-tree-core/editManagerFormat.js.map +0 -1
  385. package/lib/shared-tree-core/messageFormat.d.ts.map +0 -1
  386. package/lib/shared-tree-core/messageFormat.js.map +0 -1
  387. package/src/codec/noopValidator.ts +0 -18
  388. /package/src/shared-tree-core/{messageFormat.ts → messageFormatV1ToV4.ts} +0 -0
@@ -7,7 +7,7 @@ import { createEmitter } from "@fluid-internal/client-utils";
7
7
  import { BTree } from "@tylerbu/sorted-btree-es6";
8
8
  import { findAncestor, findCommonAncestor, mintCommit, tagChange, } from "../core/index.js";
9
9
  import { brand, getOrCreate, mapIterable } from "../util/index.js";
10
- import { SharedTreeBranch, onForkTransitive } from "./branch.js";
10
+ import { SharedTreeBranch, onForkTransitive, } from "./branch.js";
11
11
  import { getUpperBoundOfPreviousSequenceId, equalSequenceIds, maxSequenceId, minSequenceId, sequenceIdComparator, } from "./sequenceIdUtils.js";
12
12
  import { TelemetryEventBatcher, } from "@fluidframework/telemetry-utils/internal";
13
13
  export const minimumPossibleSequenceNumber = brand(Number.MIN_SAFE_INTEGER);
@@ -34,31 +34,20 @@ export class EditManager {
34
34
  /**
35
35
  * @param changeFamily - the change family of changes on the trunk and local branch
36
36
  * @param localSessionId - the id of the local session that will be used for local commits
37
+ * @param mintRevisionTag - a function which generates globally unique revision tags
38
+ * @param onSharedBranchCreated - called when a new shared branch is created. This is not called for the main branch.
37
39
  */
38
- constructor(changeFamily, localSessionId, mintRevisionTag, logger) {
40
+ constructor(changeFamily, localSessionId, mintRevisionTag, onSharedBranchCreated, logger) {
39
41
  this.changeFamily = changeFamily;
40
42
  this.localSessionId = localSessionId;
41
43
  this.mintRevisionTag = mintRevisionTag;
44
+ this.onSharedBranchCreated = onSharedBranchCreated;
42
45
  this._events = createEmitter();
46
+ this.sharedBranches = new Map();
43
47
  /**
44
- * Records extra data associated with commits in the {@link trunk}.
45
- * This does not include an entry for the {@link trunkBase}.
46
- */
47
- this.trunkMetadata = new Map();
48
- /**
49
- * A map from a sequence id to the commit in the {@link trunk} which has that sequence id.
50
- * This also includes an entry for the {@link trunkBase} which always has the lowest key in the map.
51
- */
52
- this.sequenceMap = new BTree(undefined, sequenceIdComparator);
53
- /**
54
- * Branches are maintained to represent the local change list that the issuing client had
55
- * at the time of submitting the latest known edit on the branch.
56
- * This means the head commit of each branch is always in its original (non-rebased) form.
57
- */
58
- this.peerLocalBranches = new Map();
59
- /**
60
- * Tracks where on the trunk all registered branches are based. Each key is the sequence id of a commit on
61
- * the trunk, and the value is the set of all branches who have that commit as their common ancestor with the trunk.
48
+ * Tracks where on the trunk of the main branch all registered branches are based.
49
+ * Each key is the sequence id of a commit on the trunk,
50
+ * and the value is the set of all branches who have that commit as their common ancestor with the trunk.
62
51
  *
63
52
  * @remarks
64
53
  * This does not include the local branch.
@@ -71,39 +60,24 @@ export class EditManager {
71
60
  * @remarks If there are more than one commit with the same sequence number we assume this refers to the last commit in the batch.
72
61
  */
73
62
  this.minimumSequenceNumber = minimumPossibleSequenceNumber;
74
- /**
75
- * The list of commits (from oldest to most recent) that are on the local branch but not on the trunk.
76
- * When a local commit is sequenced, the first commit in this list shifted onto the tip of the trunk.
77
- */
78
- this.localCommits = [];
79
63
  this.trunkBase = {
80
64
  revision: rootRevision,
81
65
  change: changeFamily.rebaser.compose([]),
82
66
  };
83
- this.sequenceMap.set(minimumPossibleSequenceId, this.trunkBase);
84
67
  if (logger !== undefined) {
85
68
  this.telemetryEventBatcher = new TelemetryEventBatcher({
86
69
  eventName: "rebaseProcessing",
87
70
  category: "performance",
88
71
  }, logger, maxRebaseStatsAggregationCount);
89
72
  }
90
- this.trunk = new SharedTreeBranch(this.trunkBase, changeFamily, mintRevisionTag, this._events, this.telemetryEventBatcher);
91
- this.localBranch = new SharedTreeBranch(this.trunk.getHead(), changeFamily, mintRevisionTag, this._events, this.telemetryEventBatcher);
92
- this.localBranch.events.on("afterChange", (event) => {
93
- if (event.type === "append") {
94
- for (const commit of event.newCommits) {
95
- this.localCommits.push(commit);
96
- }
97
- }
98
- else {
99
- this.localCommits.length = 0;
100
- findCommonAncestor([this.localBranch.getHead(), this.localCommits], this.trunk.getHead());
101
- }
102
- });
103
- // Track all forks of the local branch for purposes of trunk eviction. Unlike the local branch, they have
104
- // an unknown lifetime and rebase frequency, so we can not make any assumptions about which trunk commits
105
- // they require and therefore we monitor them explicitly.
106
- onForkTransitive(this.localBranch, (fork) => this.registerBranch(fork));
73
+ const mainTrunk = new SharedTreeBranch(this.trunkBase, changeFamily, mintRevisionTag, this._events, this.telemetryEventBatcher);
74
+ this.createAndAddSharedBranch("main", undefined, undefined, mainTrunk);
75
+ }
76
+ getLocalBranch(branchId) {
77
+ return this.getSharedBranch(branchId).localBranch;
78
+ }
79
+ getSharedBranch(branchId) {
80
+ return this.sharedBranches.get(branchId) ?? fail(0xc56 /* Branch does not exist */);
107
81
  }
108
82
  /**
109
83
  * Make the given branch known to the `EditManager`. The `EditManager` will ensure that all registered
@@ -125,30 +99,32 @@ export class EditManager {
125
99
  const offAfterRebase = branch.events.on("afterChange", (args) => {
126
100
  if (args.type === "rebase") {
127
101
  this.trackBranch(branch);
128
- this.trimTrunk();
102
+ this.trimHistory();
129
103
  }
130
104
  });
131
105
  // When the branch is disposed, update our branch set and trim the trunk
132
106
  const offDispose = branch.events.on("dispose", () => {
133
107
  this.untrackBranch(branch);
134
- this.trimTrunk();
108
+ this.trimHistory();
135
109
  offBeforeRebase();
136
110
  offAfterRebase();
137
111
  offDispose();
138
112
  });
139
113
  }
140
114
  trackBranch(b) {
141
- const trunkCommit = findCommonAncestor(this.trunk.getHead(), b.getHead()) ??
142
- fail(0xad2 /* Expected branch to be related to trunk */);
143
- const sequenceId = this.getCommitSequenceId(trunkCommit);
115
+ const main = this.getSharedBranch("main");
116
+ const trunkCommit = findCommonAncestor(main.trunk.getHead(), b.getHead()) ??
117
+ fail(0xad2 /* Expected branch to be related to main */);
118
+ const sequenceId = main.getCommitSequenceId(trunkCommit.revision);
144
119
  const branches = getOrCreate(this.trunkBranches, sequenceId, () => new Set());
145
120
  assert(!branches.has(b), 0x670 /* Branch was registered more than once */);
146
121
  branches.add(b);
147
122
  }
148
123
  untrackBranch(b) {
149
- const trunkCommit = findCommonAncestor(this.trunk.getHead(), b.getHead()) ??
150
- fail(0xad3 /* Expected branch to be related to trunk */);
151
- const sequenceId = this.getCommitSequenceId(trunkCommit);
124
+ const main = this.getSharedBranch("main");
125
+ const trunkCommit = findCommonAncestor(main.trunk.getHead(), b.getHead()) ??
126
+ fail(0xad3 /* Expected branch to be related to main */);
127
+ const sequenceId = main.getCommitSequenceId(trunkCommit.revision);
152
128
  const branches = this.trunkBranches.get(sequenceId) ?? fail(0xad4 /* Expected branch to be tracked */);
153
129
  assert(branches.delete(b), 0x671 /* Expected branch to be tracked */);
154
130
  if (branches.size === 0) {
@@ -156,54 +132,17 @@ export class EditManager {
156
132
  }
157
133
  }
158
134
  /**
159
- * Promote the oldest un-sequenced commit on the local branch to the head of the trunk.
160
- * @param sequenceId - The sequence id of the new trunk commit
161
- * @remarks This method is a performance optimization for the scenario where this client receives its own change back after sequencing.
162
- * The normal (not optimized) process in this case would be to apply the new sequenced commit to the trunk and then rebase the local branch over the trunk.
163
- * The first commit will be "the same" (as in, it will have the same revision) as the commit that was just sequenced, so the rebase will be a no-op.
164
- * Because the rebase is a no-op, we can skip it entirely and simply remove the commit from the local branch and append it to the trunk.
165
- * Avoiding the overhead of the rebase process, even when it's a no-op, has real measured performance benefits and is worth the added complexity here.
135
+ * Return the sequenced number of the latest sequenced change.
166
136
  */
167
- fastForwardNextLocalCommit(sequenceId) {
168
- // First, push the local commit to the trunk.
169
- // We are mutating our `localCommits` cache here, but there is no need to actually change the `localBranch` itself because it will simply catch up later if/when it next rebases.
170
- const firstLocalCommit = this.localCommits.shift();
171
- assert(firstLocalCommit !== undefined, 0x6b5 /* Received a sequenced change from the local session despite having no local changes */);
172
- const previousSequenceId = this.getCommitSequenceId(this.trunk.getHead());
173
- this.pushGraphCommitToTrunk(sequenceId, firstLocalCommit, this.localSessionId);
174
- // Next, we need to update the sequence IDs that our local branches (user's branches, not peer branches) are associated with.
175
- // In particular, if a local branch is based on the previous trunk head (the branch's first ancestor in the trunk is the commit that was the head before we pushed the new commit)
176
- // and also branches off of the local branch (it has an ancestor that is part of the local branch), it needs to have its sequence number advanced to be that of the new trunk head.
177
- // Intuitively, this makes sense because:
178
- // 1. The trunk's head just advanced forward by some (sequence) amount.
179
- // 2. The local branch is always rebased to be branching off of the head of the trunk (not literally in this case, because of the optimization, but in effect).
180
- // 3. Therefore, the entire local branch just advanced forward by some (sequence) amount, and any commits downstream of it which track the sequence numbers of their base commits on the trunk should also advance.
181
- // This update is not necessarily required for all local branches, since some may have fallen behind the local branch and are based on older trunk commits (such branches do not need updating).
182
- const currentBranches = this.trunkBranches.get(previousSequenceId);
183
- if (currentBranches !== undefined) {
184
- const newBranches = getOrCreate(this.trunkBranches, sequenceId, () => new Set());
185
- for (const branch of currentBranches) {
186
- // Check every branch associated with the old sequence ID and advance it if it is based on the local branch (specifically, on the local branch as it was before we pushed its first commit to the trunk).
187
- // We validate this by checking if the branch's head is a descendant of the local commit that we just pushed.
188
- if (findAncestor(branch.getHead(), (c) => c === firstLocalCommit) !== undefined) {
189
- newBranches.add(branch);
190
- currentBranches.delete(branch);
191
- }
192
- }
193
- // Clean up our trunk branches map by removing any empty sets.
194
- if (currentBranches.size === 0) {
195
- this.trunkBranches.delete(previousSequenceId);
196
- }
197
- if (newBranches.size === 0) {
198
- this.trunkBranches.delete(sequenceId);
137
+ getLatestSequenceNumber() {
138
+ let maxSequenceNumber;
139
+ for (const branch of this.sharedBranches.values()) {
140
+ const branchMax = branch.getCommitSequenceId(branch.trunk.getHead().revision).sequenceNumber;
141
+ if (maxSequenceNumber === undefined || maxSequenceNumber < branchMax) {
142
+ maxSequenceNumber = branchMax;
199
143
  }
200
144
  }
201
- }
202
- /**
203
- * Return the sequence number at which the given commit was sequenced on the trunk, or undefined if the commit is not part of the trunk.
204
- */
205
- getSequenceNumber(trunkCommit) {
206
- return this.trunkMetadata.get(trunkCommit.revision)?.sequenceId.sequenceNumber;
145
+ return maxSequenceNumber;
207
146
  }
208
147
  /**
209
148
  * Advances the minimum sequence number, and removes all commits from the trunk which lie outside the collaboration window,
@@ -212,21 +151,21 @@ export class EditManager {
212
151
  *
213
152
  * @remarks If there are more than one commit with the same sequence number we assume this refers to the last commit in the batch.
214
153
  */
215
- advanceMinimumSequenceNumber(minimumSequenceNumber, trimTrunk = true) {
154
+ advanceMinimumSequenceNumber(minimumSequenceNumber, trimHistory = true) {
216
155
  if (minimumSequenceNumber === this.minimumSequenceNumber) {
217
156
  return;
218
157
  }
219
158
  assert(minimumSequenceNumber > this.minimumSequenceNumber, 0x476 /* number must be larger or equal to current minimumSequenceNumber. */);
220
159
  this.minimumSequenceNumber = minimumSequenceNumber;
221
- if (trimTrunk) {
222
- this.trimTrunk();
160
+ if (trimHistory) {
161
+ this.trimHistory();
223
162
  }
224
163
  }
225
164
  /**
226
165
  * Examines the latest known minimum sequence number and the trunk bases of any registered branches to determine
227
166
  * if any commits on the trunk are unreferenced and unneeded for future computation; those found are evicted from the trunk.
228
167
  */
229
- trimTrunk() {
168
+ trimHistory() {
230
169
  /** The sequence id of the most recent commit on the trunk that will be trimmed */
231
170
  let trunkTailSequenceId = {
232
171
  sequenceNumber: this.minimumSequenceNumber,
@@ -240,118 +179,72 @@ export class EditManager {
240
179
  const sequenceIdBeforeMinimumBranchBase = getUpperBoundOfPreviousSequenceId(minimumBranchBaseSequenceId);
241
180
  trunkTailSequenceId = minSequenceId(trunkTailSequenceId, sequenceIdBeforeMinimumBranchBase);
242
181
  }
243
- const [sequenceId, latestEvicted] = this.getClosestTrunkCommit(maxSequenceId(trunkTailSequenceId, this.sequenceMap.minKey() ?? minimumPossibleSequenceId));
182
+ const mainBranch = this.getSharedBranch("main");
183
+ const [sequenceId, latestEvicted] = mainBranch.getClosestTrunkCommit(maxSequenceId(trunkTailSequenceId, mainBranch.sequenceIdToCommit.minKey() ?? minimumPossibleSequenceId));
244
184
  // Don't do any work if the commit found by the search is already the tail of the trunk
245
- if (latestEvicted !== this.trunkBase) {
246
- // The minimum sequence number informs us that all peer branches are at least caught up to the tail commit,
247
- // so rebase them accordingly. This is necessary to prevent peer branches from referencing any evicted commits.
248
- for (const [, branch] of this.peerLocalBranches) {
249
- branch.rebaseOnto(this.trunk, latestEvicted);
250
- }
251
- // This mutation is a performance hack. If commits are truly immutable, then changing the trunk's tail requires
252
- // regenerating the entire commit graph. Instead, we can simply chop off the tail like this if we're certain
253
- // that there are no outstanding references to any of the commits being removed (other than the references via
254
- // the trunk). The peer branches have been rebased to the head of the trunk, the local branch is already rebased
255
- // to the head of the trunk, and all other branches are tracked by `trunkBranches` and known to be ahead of or at
256
- // `newTrunkBase`. Therefore, no branches should have unique references to any of the commits being evicted here.
257
- // We mutate the most recent of the evicted commits to become the new trunk base. That way, any other commits that
258
- // have parent pointers to the latest evicted commit will stay linked, even though that it is no longer part of the trunk.
259
- const newTrunkBase = latestEvicted;
260
- // The metadata for new trunk base revision needs to be deleted before modifying it.
261
- this.trunkMetadata.delete(newTrunkBase.revision);
262
- // collect the revisions that will be trimmed to send as part of the branch trimmed event
263
- const trimmedRevisions = getPathFromBase(newTrunkBase, this.trunkBase).map((c) => c.revision);
264
- // Dropping the parent field removes (transitively) all references to the evicted commits so they can be garbage collected.
265
- delete newTrunkBase.parent;
266
- this.trunkBase = newTrunkBase;
267
- // Update any state that is derived from trunk commits
268
- this.sequenceMap.editRange(minimumPossibleSequenceId, sequenceId, true, (s, commit) => {
269
- // Cleanup look-aside data for each evicted commit
270
- this.trunkMetadata.delete(commit.revision);
271
- // Delete all evicted commits from `sequenceMap` except for the latest one, which is the new `trunkBase`
272
- if (equalSequenceIds(s, sequenceId)) {
273
- assert(commit === newTrunkBase, 0x729 /* Expected last evicted commit to be new trunk base */);
274
- }
275
- else {
276
- Reflect.defineProperty(commit, "change", {
277
- get: () => assert(false, 0xa5e /* Should not access 'change' property of an evicted commit */),
278
- });
279
- Reflect.defineProperty(commit, "revision", {
280
- get: () => assert(false, 0xa5f /* Should not access 'revision' property of an evicted commit */),
281
- });
282
- Reflect.defineProperty(commit, "parent", {
283
- get: () => assert(false, 0xa60 /* Should not access 'parent' property of an evicted commit */),
284
- });
285
- return { delete: true };
286
- }
185
+ if (latestEvicted === this.trunkBase) {
186
+ return;
187
+ }
188
+ // This mutation is a performance hack. If commits are truly immutable, then changing the trunk's tail requires
189
+ // regenerating the entire commit graph. Instead, we can simply chop off the tail like this if we're certain
190
+ // that there are no outstanding references to any of the commits being removed (other than the references via
191
+ // the trunk). The peer branches have been rebased to the head of the trunk, the local branch is already rebased
192
+ // to the head of the trunk, and all other branches are tracked by `trunkBranches` and known to be ahead of or at
193
+ // `newTrunkBase`. Therefore, no branches should have unique references to any of the commits being evicted here.
194
+ // We mutate the most recent of the evicted commits to become the new trunk base. That way, any other commits that
195
+ // have parent pointers to the latest evicted commit will stay linked, even though that it is no longer part of the trunk.
196
+ const newTrunkBase = latestEvicted;
197
+ // collect the revisions that will be trimmed to send as part of the branch trimmed event
198
+ const trimmedCommits = getPathFromBase(newTrunkBase, this.trunkBase);
199
+ const trimmedRevisions = trimmedCommits.map((c) => c.revision);
200
+ // The minimum sequence number informs us that all peer branches are at least caught up to the tail commit,
201
+ // so rebase them accordingly. This is necessary to prevent peer branches from referencing any evicted commits.
202
+ mainBranch.trimHistory(latestEvicted, sequenceId);
203
+ // Only the last trimmed commit, which is the new trunk base, should remain accessible.
204
+ for (const commit of trimmedCommits.slice(0, -1)) {
205
+ Reflect.defineProperty(commit, "change", {
206
+ get: () => assert(false, 0xa5e /* Should not access 'change' property of an evicted commit */),
207
+ });
208
+ Reflect.defineProperty(commit, "revision", {
209
+ get: () => assert(false, 0xa5f /* Should not access 'revision' property of an evicted commit */),
210
+ });
211
+ Reflect.defineProperty(commit, "parent", {
212
+ get: () => assert(false, 0xa60 /* Should not access 'parent' property of an evicted commit */),
287
213
  });
288
- const trunkSize = getPathFromBase(this.trunk.getHead(), this.trunkBase).length;
289
- assert(this.sequenceMap.size === trunkSize + 1, 0x744 /* The size of the sequenceMap must have one element more than the trunk */);
290
- assert(this.trunkMetadata.size === trunkSize, 0x745 /* The size of the trunkMetadata must be the same as the trunk */);
291
- this._events.emit("ancestryTrimmed", trimmedRevisions);
292
214
  }
215
+ // Dropping the parent field removes (transitively) all references to the evicted commits so they can be garbage collected.
216
+ delete newTrunkBase.parent;
217
+ this.trunkBase = newTrunkBase;
218
+ this._events.emit("ancestryTrimmed", trimmedRevisions);
293
219
  }
294
220
  isEmpty() {
295
- return (this.trunk.getHead() === this.trunkBase &&
296
- this.peerLocalBranches.size === 0 &&
297
- this.localBranch.getHead() === this.trunk.getHead() &&
298
- this.minimumSequenceNumber === minimumPossibleSequenceNumber);
221
+ for (const branch of this.sharedBranches.values()) {
222
+ if (!branch.isEmpty(this.trunkBase)) {
223
+ return false;
224
+ }
225
+ }
226
+ return this.minimumSequenceNumber === minimumPossibleSequenceNumber;
299
227
  }
300
228
  getSummaryData() {
301
- // The assert below is acceptable at present because summarization only ever occurs on a client with no
302
- // local/in-flight changes.
303
- // In the future we may wish to relax this constraint. For that to work, the current implementation of
304
- // `EditManager` would have to be amended in one of two ways:
305
- // A) Changes made by the local session should be represented by a branch in `EditManager.branches`.
306
- // B) The contents of such a branch should be computed on demand based on the trunk.
307
- // Note that option (A) would be a simple change to `addSequencedChanges` whereas (B) would likely require
308
- // rebasing trunk changes over the inverse of trunk changes.
309
- assert(this.localBranch.getHead() === this.trunk.getHead(), 0x428 /* Clients with local changes cannot be used to generate summaries */);
310
229
  // Trimming the trunk before serializing ensures that the trunk data in the summary is as minimal as possible.
311
- this.trimTrunk();
312
- let oldestCommitInCollabWindow = this.getClosestTrunkCommit(this.minimumSequenceNumber)[1];
313
- assert(oldestCommitInCollabWindow.parent !== undefined ||
314
- oldestCommitInCollabWindow === this.trunkBase, 0x8c7 /* Expected oldest commit in collab window to have a parent or be the trunk base */);
315
- // Path construction is exclusive, so we need to use the parent of the oldest commit in the window if it exists
316
- oldestCommitInCollabWindow =
317
- oldestCommitInCollabWindow.parent ?? oldestCommitInCollabWindow;
318
- const trunk = getPathFromBase(this.trunk.getHead(), oldestCommitInCollabWindow).map((c) => {
319
- assert(c !== this.trunkBase, 0xa61 /* Serialized trunk should not include the trunk base */);
320
- const metadata = this.trunkMetadata.get(c.revision) ??
321
- fail(0xad5 /* Expected metadata for trunk commit */);
322
- const commit = {
323
- change: c.change,
324
- revision: c.revision,
325
- sequenceNumber: metadata.sequenceId.sequenceNumber,
326
- sessionId: metadata.sessionId,
327
- };
328
- if (metadata.sequenceId.indexInBatch !== undefined) {
329
- commit.indexInBatch = metadata.sequenceId.indexInBatch;
230
+ this.trimHistory();
231
+ const minSeqNumberToSummarize = {
232
+ sequenceNumber: brand(this.minimumSequenceNumber + 1),
233
+ };
234
+ let minBaseSeqId = minSeqNumberToSummarize;
235
+ const mainBranch = this.getSharedBranch("main");
236
+ const branches = new Map();
237
+ for (const [branchId, branch] of this.sharedBranches) {
238
+ if (branchId !== "main") {
239
+ const branchSummary = branch.getSummaryData(minSeqNumberToSummarize, this.trunkBase.revision);
240
+ branches.set(branchId, branchSummary);
241
+ assert(branchSummary.base !== undefined, 0xc57 /* Branch summary must have a base */);
242
+ const baseSequenceId = mainBranch.getCommitSequenceId(branchSummary.base);
243
+ minBaseSeqId = minSequenceId(minBaseSeqId, baseSequenceId);
330
244
  }
331
- return commit;
332
- });
333
- const peerLocalBranches = new Map(mapIterable(this.peerLocalBranches.entries(), ([sessionId, branch]) => {
334
- const branchPath = [];
335
- const ancestor = findCommonAncestor([branch.getHead(), branchPath], this.trunk.getHead()) ??
336
- fail(0xad6 /* Expected branch to be based on trunk */);
337
- const base = ancestor === this.trunkBase ? rootRevision : ancestor.revision;
338
- return [
339
- sessionId,
340
- {
341
- base,
342
- commits: branchPath.map((c) => {
343
- assert(c !== this.trunkBase, 0xa62 /* Serialized branch should not include the trunk base */);
344
- const commit = {
345
- change: c.change,
346
- revision: c.revision,
347
- sessionId,
348
- };
349
- return commit;
350
- }),
351
- },
352
- ];
353
- }));
354
- return { trunk, peerLocalBranches };
245
+ }
246
+ const mainSummary = mainBranch.getSummaryData(minBaseSeqId, this.trunkBase.revision);
247
+ return { main: mainSummary, branches, originator: this.localSessionId };
355
248
  }
356
249
  loadSummaryData(data) {
357
250
  assert(this.isEmpty(), 0x68a /* Attempted to load from summary after edit manager was already mutated */);
@@ -359,53 +252,28 @@ export class EditManager {
359
252
  // when hydrating the peer branches below
360
253
  const trunkRevisionCache = new Map();
361
254
  trunkRevisionCache.set(this.trunkBase.revision, this.trunkBase);
362
- this.trunk.setHead(data.trunk.reduce((base, c) => {
363
- const sequenceId = c.indexInBatch === undefined
364
- ? {
365
- sequenceNumber: c.sequenceNumber,
366
- }
367
- : {
368
- sequenceNumber: c.sequenceNumber,
369
- indexInBatch: c.indexInBatch,
370
- };
371
- const commit = mintCommit(base, c);
372
- this.sequenceMap.set(sequenceId, commit);
373
- this.trunkMetadata.set(c.revision, {
374
- sequenceId,
375
- sessionId: c.sessionId,
376
- });
377
- trunkRevisionCache.set(c.revision, commit);
378
- return commit;
379
- }, this.trunkBase));
380
- this.localBranch.setHead(this.trunk.getHead());
381
- for (const [sessionId, branch] of data.peerLocalBranches) {
382
- const commit = trunkRevisionCache.get(branch.base) ??
383
- fail(0xad7 /* Expected summary branch to be based off of a revision in the trunk */);
384
- this.peerLocalBranches.set(sessionId, new SharedTreeBranch(branch.commits.reduce(mintCommit, commit), this.changeFamily, this.mintRevisionTag));
385
- }
386
- }
387
- getCommitSequenceId(trunkCommitOrTrunkBase) {
388
- const id = this.trunkMetadata.get(trunkCommitOrTrunkBase.revision)?.sequenceId;
389
- if (id === undefined) {
390
- assert(trunkCommitOrTrunkBase === this.trunkBase, 0xa63 /* Commit must be either be on the trunk or be the trunk base */);
391
- return minimumPossibleSequenceId;
255
+ const mainBranch = this.sharedBranches.get("main") ?? fail(0xc58 /* Main branch must exist */);
256
+ mainBranch.loadSummaryData(data.main, trunkRevisionCache);
257
+ if (data.branches !== undefined) {
258
+ for (const [branchId, branchData] of data.branches) {
259
+ const branch = this.createSharedBranch(branchId, branchData.session, mainBranch, mainBranch.trunk.fork());
260
+ branch.loadSummaryData(branchData, trunkRevisionCache);
261
+ this.addSharedBranch(branchId, branch);
262
+ }
392
263
  }
393
- return id;
394
- }
395
- getTrunkChanges() {
396
- return this.getTrunkCommits().map((c) => c.change);
397
264
  }
398
- getTrunkCommits() {
399
- return getPathFromBase(this.trunk.getHead(), this.trunkBase);
265
+ getTrunkHead(branchId) {
266
+ return this.getSharedBranch(branchId).trunk.getHead();
400
267
  }
401
- getTrunkHead() {
402
- return this.trunk.getHead();
268
+ getTrunkChanges(branchId) {
269
+ return this.getTrunkCommits(branchId).map((c) => c.change);
403
270
  }
404
- getLocalChanges() {
405
- return this.getLocalCommits().map((c) => c.change);
271
+ getTrunkCommits(branchId) {
272
+ return getPathFromBase(this.getTrunkHead(branchId), this.trunkBase);
406
273
  }
407
- getLocalCommits() {
408
- return this.localCommits;
274
+ getLocalCommits(branchId) {
275
+ const branch = this.getSharedBranch(branchId);
276
+ return branch.getLocalCommits();
409
277
  }
410
278
  /**
411
279
  * Gets the length of the longest branch maintained by this `EditManager`.
@@ -417,15 +285,52 @@ export class EditManager {
417
285
  */
418
286
  getLongestBranchLength() {
419
287
  let max = 0;
420
- const trunkHead = this.trunk.getHead();
421
- for (const branch of this.peerLocalBranches.values()) {
422
- const branchPath = getPathFromBase(branch.getHead(), trunkHead);
423
- if (branchPath.length > max) {
424
- max = branchPath.length;
425
- }
288
+ for (const branch of this.sharedBranches.values()) {
289
+ max = Math.max(max, branch.getLongestBranchLength());
426
290
  }
427
- const localPath = getPathFromBase(this.localBranch.getHead(), trunkHead);
428
- return Math.max(max, localPath.length);
291
+ return max;
292
+ }
293
+ sequenceBranchCreation(sessionId, referenceSequenceNumber, branchId) {
294
+ if (sessionId === this.localSessionId) {
295
+ assert(this.sharedBranches.has(branchId), 0xc59 /* Expected branch to already exist */);
296
+ return;
297
+ }
298
+ const mainBranch = this.getSharedBranch("main");
299
+ const branchTrunk = mainBranch.rebasePeer(sessionId, referenceSequenceNumber).fork();
300
+ this.createAndAddSharedBranch(branchId, sessionId, mainBranch, branchTrunk);
301
+ }
302
+ addNewBranch(branchId) {
303
+ const main = this.getSharedBranch("main") ?? fail(0xc5a /* Main branch must exist */);
304
+ this.createAndAddSharedBranch(branchId, this.localSessionId, main, this.getLocalBranch("main").fork());
305
+ }
306
+ getSharedBranchIds() {
307
+ return Array.from(this.sharedBranches.keys());
308
+ }
309
+ removeBranch(branchId) {
310
+ assert(branchId !== "main", 0xc5b /* Cannot remove main branch */);
311
+ const hadBranch = this.sharedBranches.delete(branchId);
312
+ assert(hadBranch, 0xc5c /* Expected branch to exist */);
313
+ }
314
+ createAndAddSharedBranch(branchId, sessionId, parent, branch) {
315
+ const sharedBranch = this.createSharedBranch(branchId, sessionId, parent, branch);
316
+ this.addSharedBranch(branchId, sharedBranch);
317
+ return sharedBranch;
318
+ }
319
+ addSharedBranch(branchId, branch) {
320
+ assert(!this.sharedBranches.has(branchId), 0xc5d /* A branch with this ID already exists */);
321
+ this.sharedBranches.set(branchId, branch);
322
+ // Track all forks of the local branch for purposes of trunk eviction. Unlike the local branch, they have
323
+ // an unknown lifetime and rebase frequency, so we can not make any assumptions about which trunk commits
324
+ // they require and therefore we monitor them explicitly.
325
+ onForkTransitive(branch.localBranch, (fork) => this.registerBranch(fork));
326
+ if (branchId !== "main") {
327
+ this.registerBranch(branch.localBranch);
328
+ this.onSharedBranchCreated?.(branchId);
329
+ }
330
+ }
331
+ createSharedBranch(branchId, sessionId, parent, branch) {
332
+ const sharedBranch = new SharedBranch(parent, branch, branchId, sessionId, minimumPossibleSequenceId, this.changeFamily, this.mintRevisionTag, this._events, this.telemetryEventBatcher);
333
+ return sharedBranch;
429
334
  }
430
335
  /* eslint-disable jsdoc/check-indentation */
431
336
  /**
@@ -438,34 +343,118 @@ export class EditManager {
438
343
  * - They are not interleaved with messages from other clients.
439
344
  */
440
345
  /* eslint-enable jsdoc/check-indentation */
441
- addSequencedChanges(newCommits, sessionId, sequenceNumber, referenceSequenceNumber) {
346
+ addSequencedChanges(newCommits, sessionId, sequenceNumber, referenceSequenceNumber, branchId) {
442
347
  assert(newCommits.length > 0, 0xad8 /* Expected at least one sequenced change */);
443
348
  assert(sequenceNumber > this.minimumSequenceNumber, 0x713 /* Expected change sequence number to exceed the last known minimum sequence number */);
444
- assert(sequenceNumber >= // This is ">=", not ">" because changes in the same batch will have the same sequence number
445
- (this.sequenceMap.maxKey()?.sequenceNumber ?? minimumPossibleSequenceNumber), 0xa64 /* Attempted to sequence change with an outdated sequence number */);
446
- // Returns the sequence id for the next commit to be processed in the bunch. Since all the commits have the
447
- // same sequence number, only the index in the batch needs to be incremented.
448
- const getNextSequenceId = (sequenceId) => {
449
- return {
450
- sequenceNumber: sequenceId.sequenceNumber,
451
- indexInBatch: (sequenceId.indexInBatch ?? 0) + 1,
452
- };
349
+ const branch = this.getSharedBranch(branchId);
350
+ const onSequenceLocalCommit = (commit, sequenceId, previousSequenceId) => {
351
+ // Next, we need to update the sequence IDs that our local branches (user's branches, not peer branches) are associated with.
352
+ // In particular, if a local branch is based on the previous trunk head (the branch's first ancestor in the trunk is the commit that was the head before we pushed the new commit)
353
+ // and also branches off of the local branch (it has an ancestor that is part of the local branch), it needs to have its sequence number advanced to be that of the new trunk head.
354
+ // Intuitively, this makes sense because:
355
+ // 1. The trunk's head just advanced forward by some (sequence) amount.
356
+ // 2. The local branch is always rebased to be branching off of the head of the trunk (not literally in this case, because of the optimization, but in effect).
357
+ // 3. Therefore, the entire local branch just advanced forward by some (sequence) amount, and any commits downstream of it which track the sequence numbers of their base commits on the trunk should also advance.
358
+ // This update is not necessarily required for all local branches, since some may have fallen behind the local branch and are based on older trunk commits (such branches do not need updating).
359
+ const currentBranches = this.trunkBranches.get(previousSequenceId);
360
+ if (currentBranches !== undefined) {
361
+ const newBranches = getOrCreate(this.trunkBranches, sequenceId, () => new Set());
362
+ for (const forkedBranch of currentBranches) {
363
+ // Check every branch associated with the old sequence ID and advance it if it is based on the local branch (specifically, on the local branch as it was before we pushed its first commit to the trunk).
364
+ // We validate this by checking if the branch's head is a descendant of the local commit that we just pushed.
365
+ if (findAncestor(forkedBranch.getHead(), (c) => c === commit) !== undefined) {
366
+ newBranches.add(forkedBranch);
367
+ currentBranches.delete(forkedBranch);
368
+ }
369
+ }
370
+ // Clean up our trunk branches map by removing any empty sets.
371
+ if (currentBranches.size === 0) {
372
+ this.trunkBranches.delete(previousSequenceId);
373
+ }
374
+ if (newBranches.size === 0) {
375
+ this.trunkBranches.delete(sequenceId);
376
+ }
377
+ }
453
378
  };
454
- const commitsSequenceNumber = this.getBatch(sequenceNumber);
379
+ const areLocalCommits = sessionId === this.localSessionId;
380
+ branch.addSequencedChanges(newCommits, sessionId, sequenceNumber, areLocalCommits, referenceSequenceNumber, onSequenceLocalCommit);
381
+ }
382
+ findLocalCommit(branchId, revision) {
383
+ const commits = [];
384
+ const commit = findAncestor([this.getSharedBranch(branchId).localBranch.getHead(), commits], (c) => c.revision === revision);
385
+ assert(commit !== undefined, 0x599 /* Expected local branch to contain revision */);
386
+ return [commit, commits];
387
+ }
388
+ }
389
+ /**
390
+ * Gets the path from the base of a branch to its head.
391
+ */
392
+ function getPathFromBase(branchHead, baseBranchHead) {
393
+ const path = [];
394
+ assert(findCommonAncestor([branchHead, path], baseBranchHead) !== undefined, 0x573 /* Expected branches to be related */);
395
+ return path;
396
+ }
397
+ class SharedBranch {
398
+ constructor(parentBranch, trunk, id, sessionId, baseCommitSequenceId, changeFamily, mintRevisionTag, branchTrimmer, telemetryEventBatcher) {
399
+ this.parentBranch = parentBranch;
400
+ this.trunk = trunk;
401
+ this.id = id;
402
+ this.sessionId = sessionId;
403
+ this.changeFamily = changeFamily;
404
+ this.mintRevisionTag = mintRevisionTag;
405
+ /**
406
+ * Branches are maintained to represent the local change list that the issuing client had
407
+ * at the time of submitting the latest known edit on the branch.
408
+ * This means the head commit of each branch is always in its original (non-rebased) form.
409
+ */
410
+ this.peerLocalBranches = new Map();
411
+ /**
412
+ * A map from a sequence id to the commit which has that sequence id.
413
+ * This also includes an entry for the {@link trunkBase} which always has the lowest key in the map.
414
+ */
415
+ this.sequenceIdToCommit = new BTree(undefined, sequenceIdComparator);
416
+ /**
417
+ * The list of commits (from oldest to most recent) that are on the local branch but not on the trunk.
418
+ * When a local commit is sequenced, the first commit in this list shifted onto the tip of the trunk.
419
+ */
420
+ this.localCommits = [];
421
+ /**
422
+ * Records extra data associated with sequenced commits.
423
+ * This does not include an entry for the {@link trunkBase}.
424
+ */
425
+ this.commitMetadata = new Map();
426
+ this.localBranch = new SharedTreeBranch(this.trunk.getHead(), changeFamily, mintRevisionTag, branchTrimmer, telemetryEventBatcher);
427
+ this.sequenceIdToCommit.set(baseCommitSequenceId, this.trunk.getHead());
428
+ this.localBranch.events.on("afterChange", (event) => {
429
+ if (event.type === "append") {
430
+ for (const commit of event.newCommits) {
431
+ this.localCommits.push(commit);
432
+ }
433
+ }
434
+ else {
435
+ this.localCommits.length = 0;
436
+ findCommonAncestor([this.localBranch.getHead(), this.localCommits], this.trunk.getHead());
437
+ }
438
+ });
439
+ }
440
+ addSequencedChanges(newCommits, sessionId, sequenceNumber, areLocalCommits, referenceSequenceNumber, onSequenceLocalCommit) {
441
+ assert(sequenceNumber >= // This is ">=", not ">" because changes in the same batch will have the same sequence number
442
+ (this.sequenceIdToCommit.maxKey()?.sequenceNumber ?? minimumPossibleSequenceNumber), 0xa64 /* Attempted to sequence change with an outdated sequence number */);
443
+ const batchSize = this.getBatchSize(sequenceNumber);
455
444
  // The sequence id for the next commit to be processed in the bunch.
456
- let nextSequenceId = commitsSequenceNumber.length === 0
445
+ let nextSequenceId = batchSize === 0
457
446
  ? {
458
447
  sequenceNumber,
459
448
  }
460
449
  : {
461
450
  sequenceNumber,
462
- indexInBatch: commitsSequenceNumber.length,
451
+ indexInBatch: batchSize,
463
452
  };
464
453
  // Local changes, i.e., changes from this client are applied by fast forwarding the local branch commit onto
465
454
  // the trunk.
466
- if (sessionId === this.localSessionId) {
455
+ if (areLocalCommits) {
467
456
  for (const _ of newCommits) {
468
- this.fastForwardNextLocalCommit(nextSequenceId);
457
+ this.sequenceLocalCommit(nextSequenceId, sessionId, onSequenceLocalCommit);
469
458
  nextSequenceId = getNextSequenceId(nextSequenceId);
470
459
  }
471
460
  return;
@@ -475,9 +464,7 @@ export class EditManager {
475
464
  // Get the revision that the remote change is based on and rebase that peer local branch over the part of the
476
465
  // trunk up to the base revision. This will be a no-op if the sending client has not advanced since the last
477
466
  // time we received an edit from it
478
- const [, baseRevisionInTrunk] = this.getClosestTrunkCommit(referenceSequenceNumber);
479
- const peerLocalBranch = getOrCreate(this.peerLocalBranches, sessionId, () => new SharedTreeBranch(baseRevisionInTrunk, this.changeFamily, this.mintRevisionTag));
480
- peerLocalBranch.rebaseOnto(this.trunk, baseRevisionInTrunk);
467
+ const peerLocalBranch = this.rebasePeer(sessionId, referenceSequenceNumber);
481
468
  // Step 2 - Append the changes to the peer branch and rebase the changes to the tip of the trunk.
482
469
  if (peerLocalBranch.getHead() === this.trunk.getHead()) {
483
470
  // If the peer local branch is fully caught up and empty (no changes relative to the trunk) after being
@@ -498,8 +485,8 @@ export class EditManager {
498
485
  // If the merge resulted in any changes to the trunk, update the sequence map and trunk metadata
499
486
  // with the rebased commits.
500
487
  for (const sourceCommit of result.sourceCommits) {
501
- this.sequenceMap.set(nextSequenceId, sourceCommit);
502
- this.trunkMetadata.set(sourceCommit.revision, {
488
+ this.sequenceIdToCommit.set(nextSequenceId, sourceCommit);
489
+ this.commitMetadata.set(sourceCommit.revision, {
503
490
  sequenceId: nextSequenceId,
504
491
  sessionId,
505
492
  });
@@ -510,21 +497,85 @@ export class EditManager {
510
497
  // Step 3 - Rebase the local branch over the updated trunk.
511
498
  this.localBranch.rebaseOnto(this.trunk);
512
499
  }
513
- findLocalCommit(revision) {
514
- const commits = [];
515
- const commit = findAncestor([this.localBranch.getHead(), commits], (c) => c.revision === revision);
516
- assert(commit !== undefined, 0x599 /* Expected local branch to contain revision */);
517
- return [commit, commits];
500
+ isEmpty(baseCommit) {
501
+ return (this.trunk.getHead() === baseCommit &&
502
+ this.peerLocalBranches.size === 0 &&
503
+ this.localBranch.getHead() === this.trunk.getHead());
518
504
  }
519
- pushCommitToTrunk(sequenceId, commit) {
520
- const mintedCommit = mintCommit(this.trunk.getHead(), commit);
521
- this.pushGraphCommitToTrunk(sequenceId, mintedCommit, commit.sessionId);
505
+ rebasePeer(sessionId, referenceSequenceNumber) {
506
+ const [, baseRevisionInTrunk] = this.getClosestTrunkCommit(referenceSequenceNumber);
507
+ const peerLocalBranch = getOrCreate(this.peerLocalBranches, sessionId, () => new SharedTreeBranch(baseRevisionInTrunk, this.changeFamily, this.mintRevisionTag));
508
+ peerLocalBranch.rebaseOnto(this.trunk, baseRevisionInTrunk);
509
+ return peerLocalBranch;
522
510
  }
523
- pushGraphCommitToTrunk(sequenceId, graphCommit, sessionId) {
524
- this.trunk.setHead(graphCommit);
511
+ getPeerBranchOrTrunk(sessionId) {
512
+ return this.peerLocalBranches.get(sessionId) ?? this.trunk;
513
+ }
514
+ /**
515
+ * Gets the length of the longest branch maintained by this `SharedBranch`.
516
+ * This may be the length of a peer branch or the local branch.
517
+ *
518
+ * @remarks
519
+ * The length is counted from the lowest common ancestor with the trunk such that a fully sequenced branch would
520
+ * have length zero.
521
+ */
522
+ getLongestBranchLength() {
523
+ let max = 0;
525
524
  const trunkHead = this.trunk.getHead();
526
- this.sequenceMap.set(sequenceId, trunkHead);
527
- this.trunkMetadata.set(trunkHead.revision, { sequenceId, sessionId });
525
+ for (const branch of this.peerLocalBranches.values()) {
526
+ const branchPath = getPathFromBase(branch.getHead(), trunkHead);
527
+ if (branchPath.length > max) {
528
+ max = branchPath.length;
529
+ }
530
+ }
531
+ const localPath = getPathFromBase(this.localBranch.getHead(), trunkHead);
532
+ return Math.max(max, localPath.length);
533
+ }
534
+ trimHistory(newBase, sequenceId) {
535
+ this.rebasePeers(newBase);
536
+ this.sequenceIdToCommit.editRange(minimumPossibleSequenceId, sequenceId, true, (s, commit) => {
537
+ // Cleanup look-aside data for each evicted commit
538
+ this.commitMetadata.delete(commit.revision);
539
+ // Delete all evicted commits from `sequenceMap` except for the latest one, which is the new `trunkBase`
540
+ if (equalSequenceIds(s, sequenceId)) {
541
+ assert(commit === newBase, 0x729 /* Expected last evicted commit to be new trunk base */);
542
+ }
543
+ else {
544
+ return { delete: true };
545
+ }
546
+ });
547
+ const trunkSize = getPathFromBase(this.trunk.getHead(), newBase).length;
548
+ assert(this.sequenceIdToCommit.size === trunkSize + 1, 0x744 /* The size of the sequenceMap must have one element more than the trunk */);
549
+ assert(this.commitMetadata.size === trunkSize, 0x745 /* The size of the trunkMetadata must be the same as the trunk */);
550
+ }
551
+ rebasePeers(commit) {
552
+ for (const [, branch] of this.peerLocalBranches) {
553
+ branch.rebaseOnto(this.trunk, commit);
554
+ }
555
+ // The metadata for new trunk base revision needs to be deleted before modifying it.
556
+ this.commitMetadata.delete(commit.revision);
557
+ }
558
+ getLocalCommits() {
559
+ return this.localCommits;
560
+ }
561
+ /**
562
+ * Promote the oldest un-sequenced commit on the local branch to the head of the trunk.
563
+ * @param sequenceId - The sequence id of the new trunk commit
564
+ * @remarks This method is a performance optimization for the scenario where this client receives its own change back after sequencing.
565
+ * The normal (not optimized) process in this case would be to apply the new sequenced commit to the trunk and then rebase the local branch over the trunk.
566
+ * The first commit will be "the same" (as in, it will have the same revision) as the commit that was just sequenced, so the rebase will be a no-op.
567
+ * Because the rebase is a no-op, we can skip it entirely and simply remove the commit from the local branch and append it to the trunk.
568
+ * Avoiding the overhead of the rebase process, even when it's a no-op, has real measured performance benefits and is worth the added complexity here.
569
+ */
570
+ sequenceLocalCommit(sequenceId, sessionId, onSequenceLocalCommit) {
571
+ // First, push the local commit to the trunk.
572
+ // We are mutating our `localCommits` cache here,but there is no need to actually change the `localBranch` itself because it will simply catch up later if/when it next rebases.
573
+ const firstLocalCommit = this.localCommits.shift();
574
+ assert(firstLocalCommit !== undefined, 0x6b5 /* Received a sequenced change from the local session despite having no local changes */);
575
+ const prevSequenceId = this.getCommitSequenceId(this.trunk.getHead().revision);
576
+ this.pushGraphCommitToTrunk(sequenceId, firstLocalCommit, sessionId);
577
+ onSequenceLocalCommit(firstLocalCommit, sequenceId, prevSequenceId);
578
+ return firstLocalCommit;
528
579
  }
529
580
  getClosestTrunkCommit(searchBy) {
530
581
  const sequenceId = typeof searchBy === "number"
@@ -536,26 +587,135 @@ export class EditManager {
536
587
  indexInBatch: Number.POSITIVE_INFINITY,
537
588
  }
538
589
  : searchBy;
539
- const commit = this.sequenceMap.getPairOrNextLower(sequenceId);
590
+ const commit = this.sequenceIdToCommit.getPairOrNextLower(sequenceId);
540
591
  assert(commit !== undefined, 0x746 /* sequence id has been evicted */);
541
592
  return commit;
542
593
  }
543
- getBatch(sequenceNumber) {
594
+ pushGraphCommitToTrunk(sequenceId, graphCommit, sessionId) {
595
+ this.trunk.setHead(graphCommit);
596
+ this.registerSequencedCommit(sequenceId, sessionId, graphCommit);
597
+ }
598
+ pushCommitToTrunk(sequenceId, commit) {
599
+ const mintedCommit = mintCommit(this.trunk.getHead(), commit);
600
+ this.pushGraphCommitToTrunk(sequenceId, mintedCommit, commit.sessionId);
601
+ }
602
+ registerSequencedCommit(sequenceId, sessionId, commit) {
603
+ this.sequenceIdToCommit.set(sequenceId, commit);
604
+ this.commitMetadata.set(commit.revision, { sequenceId, sessionId });
605
+ }
606
+ getCommitSequenceId(commitRevision) {
607
+ const id = this.commitMetadata.get(commitRevision)?.sequenceId;
608
+ if (id === undefined) {
609
+ return minimumPossibleSequenceId;
610
+ }
611
+ return id;
612
+ }
613
+ // TODO: Document that this is to handle receiving separate commits with the same sequence ID,
614
+ // as a batch of changes are not guaranteed to be processed as one bunch.
615
+ getBatchSize(sequenceNumber) {
544
616
  const startSequenceId = {
545
617
  sequenceNumber,
546
618
  };
547
619
  const endSequenceId = {
548
620
  sequenceNumber: brand(sequenceNumber + 1),
549
621
  };
550
- return this.sequenceMap.getRange(startSequenceId, endSequenceId, false);
622
+ return this.sequenceIdToCommit.getRange(startSequenceId, endSequenceId, false).length;
623
+ }
624
+ getSummaryData(minSeqNumberToSummarize, trunkBaseRevision) {
625
+ // The assert below is acceptable at present because summarization only ever occurs on a client with no
626
+ // local/in-flight changes.
627
+ // In the future we may wish to relax this constraint. For that to work, the current implementation of
628
+ // `EditManager` would have to be amended in one of two ways:
629
+ // A) Changes made by the local session should be represented by a branch in `EditManager.branches`.
630
+ // B) The contents of such a branch should be computed on demand based on the trunk.
631
+ // Note that option (A) would be a simple change to `addSequencedChanges` whereas (B) would likely require
632
+ // rebasing trunk changes over the inverse of trunk changes.
633
+ assert(this.localBranch.getHead() === this.trunk.getHead(), 0xc5e /* Clients with local changes cannot be used to generate summaries */);
634
+ let parentHead;
635
+ if (this.parentBranch === undefined) {
636
+ const oldestCommitInCollabWindow = this.getClosestTrunkCommit(minSeqNumberToSummarize)[1];
637
+ // Path construction is exclusive, so we need to use the parent of the oldest commit in the window if it exists
638
+ parentHead = oldestCommitInCollabWindow.parent ?? oldestCommitInCollabWindow;
639
+ }
640
+ else {
641
+ parentHead = this.parentBranch.trunk.getHead();
642
+ }
643
+ const childBranchTrunkCommits = [];
644
+ const forkPointFromMainTrunk = findCommonAncestor([this.trunk.getHead(), childBranchTrunkCommits], parentHead);
645
+ assert(forkPointFromMainTrunk !== undefined, 0xc5f /* Expected child branch to be based on main branch */);
646
+ const trunk = childBranchTrunkCommits.map((c) => {
647
+ const metadata = this.commitMetadata.get(c.revision) ??
648
+ fail(0xad5 /* Expected metadata for trunk commit */);
649
+ const commit = {
650
+ change: c.change,
651
+ revision: c.revision,
652
+ sequenceNumber: metadata.sequenceId.sequenceNumber,
653
+ sessionId: metadata.sessionId,
654
+ };
655
+ if (metadata.sequenceId.indexInBatch !== undefined) {
656
+ commit.indexInBatch = metadata.sequenceId.indexInBatch;
657
+ }
658
+ return commit;
659
+ });
660
+ const peerLocalBranches = new Map(mapIterable(this.peerLocalBranches.entries(), ([sessionId, branch]) => {
661
+ const branchPath = [];
662
+ const ancestor = findCommonAncestor([branch.getHead(), branchPath], this.trunk.getHead()) ??
663
+ fail(0xad6 /* Expected branch to be based on trunk */);
664
+ const base = ancestor.revision === trunkBaseRevision ? rootRevision : ancestor.revision;
665
+ return [
666
+ sessionId,
667
+ {
668
+ base,
669
+ commits: branchPath.map((c) => {
670
+ const commit = {
671
+ change: c.change,
672
+ revision: c.revision,
673
+ sessionId,
674
+ };
675
+ return commit;
676
+ }),
677
+ },
678
+ ];
679
+ }));
680
+ const trunkBase = this.parentBranch === undefined ? undefined : forkPointFromMainTrunk.revision;
681
+ return { trunk, peerLocalBranches, base: trunkBase, id: this.id, session: this.sessionId };
682
+ }
683
+ loadSummaryData(data, trunkRevisionCache) {
684
+ assert((this.parentBranch === undefined) === (data.base === undefined), 0xc60 /* Expected branch base to match presence of parent branch */);
685
+ const parentTrunkBase = trunkRevisionCache.get(data.base ?? rootRevision) ??
686
+ fail(0xc61 /* Expected base revision to be in trunk cache */);
687
+ this.trunk.setHead(data.trunk.reduce((base, c) => {
688
+ const sequenceId = c.indexInBatch === undefined
689
+ ? {
690
+ sequenceNumber: c.sequenceNumber,
691
+ }
692
+ : {
693
+ sequenceNumber: c.sequenceNumber,
694
+ indexInBatch: c.indexInBatch,
695
+ };
696
+ const commit = mintCommit(base, c);
697
+ this.sequenceIdToCommit.set(sequenceId, commit);
698
+ this.commitMetadata.set(c.revision, {
699
+ sequenceId,
700
+ sessionId: c.sessionId,
701
+ });
702
+ trunkRevisionCache.set(c.revision, commit);
703
+ return commit;
704
+ }, parentTrunkBase));
705
+ this.localBranch.setHead(this.trunk.getHead());
706
+ for (const [sessionId, branch] of data.peerLocalBranches) {
707
+ const commit = trunkRevisionCache.get(branch.base) ??
708
+ fail(0xad7 /* Expected summary branch to be based off of a revision in the trunk */);
709
+ this.peerLocalBranches.set(sessionId, new SharedTreeBranch(branch.commits.reduce(mintCommit, commit), this.changeFamily, this.mintRevisionTag));
710
+ }
551
711
  }
552
712
  }
553
- /**
554
- * Gets the path from the base of a branch to its head.
555
- */
556
- function getPathFromBase(branchHead, baseBranchHead) {
557
- const path = [];
558
- assert(findCommonAncestor([branchHead, path], baseBranchHead) !== undefined, 0x573 /* Expected branches to be related */);
559
- return path;
713
+ // Returns the sequence id for the next commit to be processed in the bunch. Since all the commits have the
714
+ // same sequence number, only the index in the batch needs to be incremented.
715
+ function getNextSequenceId(sequenceId) {
716
+ return {
717
+ sequenceNumber: sequenceId.sequenceNumber,
718
+ indexInBatch: (sequenceId.indexInBatch ?? 0) + 1,
719
+ };
560
720
  }
561
721
  //# sourceMappingURL=editManager.js.map