@fluidframework/merge-tree 1.2.1 → 2.0.0-internal.1.0.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 (231) hide show
  1. package/DEV.md +2 -2
  2. package/README.md +1 -1
  3. package/REFERENCEPOSITIONS.md +2 -2
  4. package/dist/MergeTreeTextHelper.d.ts +23 -0
  5. package/dist/MergeTreeTextHelper.d.ts.map +1 -0
  6. package/dist/MergeTreeTextHelper.js +136 -0
  7. package/dist/MergeTreeTextHelper.js.map +1 -0
  8. package/dist/base.d.ts +2 -26
  9. package/dist/base.d.ts.map +1 -1
  10. package/dist/base.js.map +1 -1
  11. package/dist/client.d.ts +21 -12
  12. package/dist/client.d.ts.map +1 -1
  13. package/dist/client.js +87 -27
  14. package/dist/client.js.map +1 -1
  15. package/dist/collections/heap.d.ts +28 -0
  16. package/dist/collections/heap.d.ts.map +1 -0
  17. package/dist/collections/heap.js +65 -0
  18. package/dist/collections/heap.js.map +1 -0
  19. package/dist/collections/index.d.ts +11 -0
  20. package/dist/collections/index.d.ts.map +1 -0
  21. package/dist/collections/index.js +23 -0
  22. package/dist/collections/index.js.map +1 -0
  23. package/dist/collections/intervalTree.d.ts +60 -0
  24. package/dist/collections/intervalTree.d.ts.map +1 -0
  25. package/dist/collections/intervalTree.js +99 -0
  26. package/dist/collections/intervalTree.js.map +1 -0
  27. package/dist/collections/list.d.ts +39 -0
  28. package/dist/collections/list.d.ts.map +1 -0
  29. package/dist/collections/list.js +155 -0
  30. package/dist/collections/list.js.map +1 -0
  31. package/dist/collections/rbTree.d.ts +154 -0
  32. package/dist/collections/rbTree.d.ts.map +1 -0
  33. package/dist/{collections.js → collections/rbTree.js} +10 -448
  34. package/dist/collections/rbTree.js.map +1 -0
  35. package/dist/collections/stack.d.ts +16 -0
  36. package/dist/collections/stack.d.ts.map +1 -0
  37. package/dist/collections/stack.js +30 -0
  38. package/dist/collections/stack.js.map +1 -0
  39. package/dist/collections/tst.d.ts +55 -0
  40. package/dist/collections/tst.d.ts.map +1 -0
  41. package/dist/collections/tst.js +171 -0
  42. package/dist/collections/tst.js.map +1 -0
  43. package/dist/index.d.ts +3 -1
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +4 -2
  46. package/dist/index.js.map +1 -1
  47. package/dist/localReference.d.ts +48 -99
  48. package/dist/localReference.d.ts.map +1 -1
  49. package/dist/localReference.js +132 -169
  50. package/dist/localReference.js.map +1 -1
  51. package/dist/mergeTree.d.ts +38 -299
  52. package/dist/mergeTree.d.ts.map +1 -1
  53. package/dist/mergeTree.js +214 -598
  54. package/dist/mergeTree.js.map +1 -1
  55. package/dist/mergeTreeDeltaCallback.d.ts +1 -1
  56. package/dist/mergeTreeDeltaCallback.d.ts.map +1 -1
  57. package/dist/mergeTreeDeltaCallback.js.map +1 -1
  58. package/dist/mergeTreeNodes.d.ts +269 -0
  59. package/dist/mergeTreeNodes.d.ts.map +1 -0
  60. package/dist/mergeTreeNodes.js +383 -0
  61. package/dist/mergeTreeNodes.js.map +1 -0
  62. package/dist/mergeTreeTracking.d.ts +1 -1
  63. package/dist/mergeTreeTracking.d.ts.map +1 -1
  64. package/dist/mergeTreeTracking.js.map +1 -1
  65. package/dist/opBuilder.d.ts +1 -1
  66. package/dist/opBuilder.d.ts.map +1 -1
  67. package/dist/opBuilder.js.map +1 -1
  68. package/dist/partialLengths.d.ts +130 -15
  69. package/dist/partialLengths.d.ts.map +1 -1
  70. package/dist/partialLengths.js +230 -138
  71. package/dist/partialLengths.js.map +1 -1
  72. package/dist/properties.d.ts.map +1 -1
  73. package/dist/properties.js.map +1 -1
  74. package/dist/referencePositions.d.ts +6 -26
  75. package/dist/referencePositions.d.ts.map +1 -1
  76. package/dist/referencePositions.js.map +1 -1
  77. package/dist/segmentGroupCollection.d.ts +2 -1
  78. package/dist/segmentGroupCollection.d.ts.map +1 -1
  79. package/dist/segmentGroupCollection.js +3 -0
  80. package/dist/segmentGroupCollection.js.map +1 -1
  81. package/dist/segmentPropertiesManager.d.ts +10 -1
  82. package/dist/segmentPropertiesManager.d.ts.map +1 -1
  83. package/dist/segmentPropertiesManager.js +41 -6
  84. package/dist/segmentPropertiesManager.js.map +1 -1
  85. package/dist/snapshotLoader.d.ts.map +1 -1
  86. package/dist/snapshotLoader.js.map +1 -1
  87. package/dist/snapshotV1.d.ts +1 -1
  88. package/dist/snapshotV1.d.ts.map +1 -1
  89. package/dist/snapshotV1.js.map +1 -1
  90. package/dist/snapshotlegacy.d.ts +5 -1
  91. package/dist/snapshotlegacy.d.ts.map +1 -1
  92. package/dist/snapshotlegacy.js +4 -0
  93. package/dist/snapshotlegacy.js.map +1 -1
  94. package/dist/sortedSegmentSet.d.ts +1 -1
  95. package/dist/sortedSegmentSet.d.ts.map +1 -1
  96. package/dist/sortedSegmentSet.js.map +1 -1
  97. package/dist/textSegment.d.ts +7 -7
  98. package/dist/textSegment.d.ts.map +1 -1
  99. package/dist/textSegment.js +3 -125
  100. package/dist/textSegment.js.map +1 -1
  101. package/lib/MergeTreeTextHelper.d.ts +23 -0
  102. package/lib/MergeTreeTextHelper.d.ts.map +1 -0
  103. package/lib/MergeTreeTextHelper.js +132 -0
  104. package/lib/MergeTreeTextHelper.js.map +1 -0
  105. package/lib/base.d.ts +2 -26
  106. package/lib/base.d.ts.map +1 -1
  107. package/lib/base.js.map +1 -1
  108. package/lib/client.d.ts +21 -12
  109. package/lib/client.d.ts.map +1 -1
  110. package/lib/client.js +85 -25
  111. package/lib/client.js.map +1 -1
  112. package/lib/collections/heap.d.ts +28 -0
  113. package/lib/collections/heap.d.ts.map +1 -0
  114. package/lib/collections/heap.js +61 -0
  115. package/lib/collections/heap.js.map +1 -0
  116. package/lib/collections/index.d.ts +11 -0
  117. package/lib/collections/index.d.ts.map +1 -0
  118. package/lib/collections/index.js +11 -0
  119. package/lib/collections/index.js.map +1 -0
  120. package/lib/collections/intervalTree.d.ts +60 -0
  121. package/lib/collections/intervalTree.d.ts.map +1 -0
  122. package/lib/collections/intervalTree.js +94 -0
  123. package/lib/collections/intervalTree.js.map +1 -0
  124. package/lib/collections/list.d.ts +39 -0
  125. package/lib/collections/list.d.ts.map +1 -0
  126. package/lib/collections/list.js +149 -0
  127. package/lib/collections/list.js.map +1 -0
  128. package/lib/collections/rbTree.d.ts +154 -0
  129. package/lib/collections/rbTree.d.ts.map +1 -0
  130. package/lib/{collections.js → collections/rbTree.js} +9 -439
  131. package/lib/collections/rbTree.js.map +1 -0
  132. package/lib/collections/stack.d.ts +16 -0
  133. package/lib/collections/stack.d.ts.map +1 -0
  134. package/lib/collections/stack.js +26 -0
  135. package/lib/collections/stack.js.map +1 -0
  136. package/lib/collections/tst.d.ts +55 -0
  137. package/lib/collections/tst.d.ts.map +1 -0
  138. package/lib/collections/tst.js +167 -0
  139. package/lib/collections/tst.js.map +1 -0
  140. package/lib/index.d.ts +3 -1
  141. package/lib/index.d.ts.map +1 -1
  142. package/lib/index.js +3 -1
  143. package/lib/index.js.map +1 -1
  144. package/lib/localReference.d.ts +48 -99
  145. package/lib/localReference.d.ts.map +1 -1
  146. package/lib/localReference.js +132 -170
  147. package/lib/localReference.js.map +1 -1
  148. package/lib/mergeTree.d.ts +38 -299
  149. package/lib/mergeTree.d.ts.map +1 -1
  150. package/lib/mergeTree.js +190 -563
  151. package/lib/mergeTree.js.map +1 -1
  152. package/lib/mergeTreeDeltaCallback.d.ts +1 -1
  153. package/lib/mergeTreeDeltaCallback.d.ts.map +1 -1
  154. package/lib/mergeTreeDeltaCallback.js.map +1 -1
  155. package/lib/mergeTreeNodes.d.ts +269 -0
  156. package/lib/mergeTreeNodes.d.ts.map +1 -0
  157. package/lib/mergeTreeNodes.js +369 -0
  158. package/lib/mergeTreeNodes.js.map +1 -0
  159. package/lib/mergeTreeTracking.d.ts +1 -1
  160. package/lib/mergeTreeTracking.d.ts.map +1 -1
  161. package/lib/mergeTreeTracking.js.map +1 -1
  162. package/lib/opBuilder.d.ts +1 -1
  163. package/lib/opBuilder.d.ts.map +1 -1
  164. package/lib/opBuilder.js.map +1 -1
  165. package/lib/partialLengths.d.ts +130 -15
  166. package/lib/partialLengths.d.ts.map +1 -1
  167. package/lib/partialLengths.js +227 -135
  168. package/lib/partialLengths.js.map +1 -1
  169. package/lib/properties.d.ts.map +1 -1
  170. package/lib/properties.js.map +1 -1
  171. package/lib/referencePositions.d.ts +6 -26
  172. package/lib/referencePositions.d.ts.map +1 -1
  173. package/lib/referencePositions.js.map +1 -1
  174. package/lib/segmentGroupCollection.d.ts +2 -1
  175. package/lib/segmentGroupCollection.d.ts.map +1 -1
  176. package/lib/segmentGroupCollection.js +3 -0
  177. package/lib/segmentGroupCollection.js.map +1 -1
  178. package/lib/segmentPropertiesManager.d.ts +10 -1
  179. package/lib/segmentPropertiesManager.d.ts.map +1 -1
  180. package/lib/segmentPropertiesManager.js +41 -6
  181. package/lib/segmentPropertiesManager.js.map +1 -1
  182. package/lib/snapshotLoader.d.ts.map +1 -1
  183. package/lib/snapshotLoader.js.map +1 -1
  184. package/lib/snapshotV1.d.ts +1 -1
  185. package/lib/snapshotV1.d.ts.map +1 -1
  186. package/lib/snapshotV1.js.map +1 -1
  187. package/lib/snapshotlegacy.d.ts +5 -1
  188. package/lib/snapshotlegacy.d.ts.map +1 -1
  189. package/lib/snapshotlegacy.js +4 -0
  190. package/lib/snapshotlegacy.js.map +1 -1
  191. package/lib/sortedSegmentSet.d.ts +1 -1
  192. package/lib/sortedSegmentSet.d.ts.map +1 -1
  193. package/lib/sortedSegmentSet.js.map +1 -1
  194. package/lib/textSegment.d.ts +7 -7
  195. package/lib/textSegment.d.ts.map +1 -1
  196. package/lib/textSegment.js +1 -122
  197. package/lib/textSegment.js.map +1 -1
  198. package/package.json +93 -17
  199. package/src/MergeTreeTextHelper.ts +172 -0
  200. package/src/base.ts +2 -35
  201. package/src/client.ts +114 -30
  202. package/src/collections/heap.ts +75 -0
  203. package/src/collections/index.ts +11 -0
  204. package/src/collections/intervalTree.ts +140 -0
  205. package/src/collections/list.ts +165 -0
  206. package/src/{collections.ts → collections/rbTree.ts} +79 -538
  207. package/src/collections/stack.ts +27 -0
  208. package/src/collections/tst.ts +212 -0
  209. package/src/index.ts +8 -2
  210. package/src/localReference.ts +152 -203
  211. package/src/mergeTree.ts +265 -868
  212. package/src/mergeTreeDeltaCallback.ts +1 -1
  213. package/src/mergeTreeNodes.ts +676 -0
  214. package/src/mergeTreeTracking.ts +1 -1
  215. package/src/opBuilder.ts +1 -1
  216. package/src/partialLengths.ts +295 -150
  217. package/src/properties.ts +1 -0
  218. package/src/referencePositions.ts +7 -27
  219. package/src/segmentGroupCollection.ts +5 -1
  220. package/src/segmentPropertiesManager.ts +45 -6
  221. package/src/snapshotLoader.ts +2 -1
  222. package/src/snapshotV1.ts +2 -2
  223. package/src/snapshotlegacy.ts +6 -2
  224. package/src/sortedSegmentSet.ts +1 -1
  225. package/src/textSegment.ts +10 -157
  226. package/dist/collections.d.ts +0 -197
  227. package/dist/collections.d.ts.map +0 -1
  228. package/dist/collections.js.map +0 -1
  229. package/lib/collections.d.ts +0 -197
  230. package/lib/collections.d.ts.map +0 -1
  231. package/lib/collections.js.map +0 -1
@@ -7,7 +7,7 @@ import { UsageError } from "@fluidframework/container-utils";
7
7
  import { ListMakeHead, ListRemoveEntry } from "./collections";
8
8
  import { ReferenceType } from "./ops";
9
9
  import { addProperties } from "./properties";
10
- import { minReferencePosition, maxReferencePosition, compareReferencePositions, refHasTileLabels, refHasRangeLabels, refGetRangeLabels, refGetTileLabels, refHasRangeLabel, refHasTileLabel, refTypeIncludesFlag, } from "./referencePositions";
10
+ import { refHasTileLabels, refHasRangeLabels, refTypeIncludesFlag, } from "./referencePositions";
11
11
  /**
12
12
  * @internal
13
13
  */
@@ -27,83 +27,25 @@ export function _validateReferenceType(refType) {
27
27
  }
28
28
  }
29
29
  /**
30
- * @deprecated - Use LocalReferencePosition
30
+ * @privateRemarks This should not be exported outside merge tree.
31
+ * @internal
31
32
  */
32
- export class LocalReference {
33
- /**
34
- * @deprecated - use createReferencePosition
35
- */
36
- constructor(client, initSegment,
37
- /**
38
- * @deprecated - use getOffset
39
- */
40
- offset = 0, refType = ReferenceType.Simple, properties) {
41
- this.client = client;
42
- this.offset = offset;
33
+ class LocalReference {
34
+ constructor(refType = ReferenceType.Simple, properties) {
43
35
  this.refType = refType;
36
+ this.offset = 0;
44
37
  _validateReferenceType(refType);
45
- this.segment = initSegment;
46
38
  this.properties = properties;
47
39
  }
48
- /**
49
- * @deprecated - use minReferencePosition
50
- */
51
- min(b) {
52
- return minReferencePosition(this, b);
53
- }
54
- /**
55
- * @deprecated - use maxReferencePosition
56
- */
57
- max(b) {
58
- return maxReferencePosition(this, b);
59
- }
60
- /**
61
- * @deprecated - use compareReferencePositions
62
- */
63
- compare(b) {
64
- return compareReferencePositions(this, b);
65
- }
66
- /**
67
- * @deprecated - use getLocalReferencePosition
68
- */
69
- toPosition() {
70
- return this.getClient().localReferencePositionToPosition(this);
71
- }
72
- /**
73
- * @deprecated - use refHasTileLabels
74
- */
75
- hasTileLabels() {
76
- return refHasTileLabels(this);
77
- }
78
- /**
79
- * @deprecated - use refHasRangeLabels
80
- */
81
- hasRangeLabels() {
82
- return refHasRangeLabels(this);
83
- }
84
- /**
85
- * @deprecated - use refHasTileLabel
86
- */
87
- hasTileLabel(label) {
88
- return refHasTileLabel(this, label);
89
- }
90
- /**
91
- * @deprecated - use refHasRangeLabel
92
- */
93
- hasRangeLabel(label) {
94
- return refHasRangeLabel(this, label);
95
- }
96
- /**
97
- * @deprecated - use refGetTileLabels
98
- */
99
- getTileLabels() {
100
- return refGetTileLabels(this);
101
- }
102
- /**
103
- * @deprecated - use refGetRangeLabels
104
- */
105
- getRangeLabels() {
106
- return refGetRangeLabels(this);
40
+ link(segment, offset, listNode) {
41
+ var _a, _b;
42
+ if (listNode !== this.listNode
43
+ && this.listNode !== undefined) {
44
+ (_b = (_a = this.segment) === null || _a === void 0 ? void 0 : _a.localRefs) === null || _b === void 0 ? void 0 : _b.removeLocalRef(this);
45
+ }
46
+ this.segment = segment;
47
+ this.offset = offset;
48
+ this.listNode = listNode;
107
49
  }
108
50
  isLeaf() {
109
51
  return false;
@@ -111,44 +53,40 @@ export class LocalReference {
111
53
  addProperties(newProps, op) {
112
54
  this.properties = addProperties(this.properties, newProps, op);
113
55
  }
114
- /**
115
- * @deprecated - no longer supported
116
- */
117
- getClient() {
118
- return this.client;
119
- }
120
56
  getSegment() {
121
57
  return this.segment;
122
58
  }
123
59
  getOffset() {
124
60
  return this.offset;
125
61
  }
62
+ getListNode() {
63
+ return this.listNode;
64
+ }
126
65
  getProperties() {
127
66
  return this.properties;
128
67
  }
129
68
  }
130
- /**
131
- * @deprecated - use DetachedReferencePosition
132
- */
133
- LocalReference.DetachedPosition = -1;
134
- function assertLocalReferences(lref) {
69
+ export function createDetachedLocalReferencePosition(refType) {
70
+ return new LocalReference(refType, undefined);
71
+ }
72
+ export function assertLocalReferences(lref) {
135
73
  assert(lref instanceof LocalReference, 0x2e0 /* "lref not a Local Reference" */);
136
74
  }
137
75
  /**
138
- * Represents a collection of {@link ReferencePosition}s associated with one segment in a merge-tree.
76
+ * Represents a collection of {@link LocalReferencePosition}s associated with one segment in a merge-tree.
139
77
  */
140
78
  export class LocalReferenceCollection {
141
79
  /**
142
80
  *
143
- * @internal - this method should only be called by mergeTree
81
+ * @internal
144
82
  */
145
83
  constructor(
146
84
  /** Segment this `LocalReferenceCollection` is associated to. */
147
85
  segment, initialRefsByfOffset = new Array(segment.cachedLength)) {
148
86
  this.segment = segment;
149
87
  /**
150
- *
151
- * @internal - this method should only be called by mergeTree
88
+ * @remarks This method should only be called by mergeTree.
89
+ * @internal
152
90
  */
153
91
  this.hierRefCount = 0;
154
92
  this.refCount = 0;
@@ -172,8 +110,8 @@ export class LocalReferenceCollection {
172
110
  }
173
111
  }
174
112
  /**
175
- *
176
- * @internal - this method should only be called by mergeTree
113
+ * @remarks This method should only be called by mergeTree.
114
+ * @internal
177
115
  */
178
116
  [Symbol.iterator]() {
179
117
  const subiterators = [];
@@ -210,8 +148,8 @@ export class LocalReferenceCollection {
210
148
  return iterator;
211
149
  }
212
150
  /**
213
- *
214
- * @internal - this method should only be called by mergeTree
151
+ * @remarks This method should only be called by mergeTree.
152
+ * @internal
215
153
  */
216
154
  clear() {
217
155
  this.refCount = 0;
@@ -219,9 +157,7 @@ export class LocalReferenceCollection {
219
157
  const detachSegments = (refs) => {
220
158
  if (refs) {
221
159
  for (const r of refs) {
222
- if (r.segment === this.segment) {
223
- r.segment = undefined;
224
- }
160
+ this.removeLocalRef(r);
225
161
  }
226
162
  }
227
163
  };
@@ -230,94 +166,77 @@ export class LocalReferenceCollection {
230
166
  if (refsAtOffset) {
231
167
  detachSegments(refsAtOffset.before);
232
168
  detachSegments(refsAtOffset.at);
233
- detachSegments(refsAtOffset.before);
169
+ detachSegments(refsAtOffset.after);
234
170
  this.refsByOffset[i] = undefined;
235
171
  }
236
172
  }
237
173
  }
238
174
  /**
239
- *
240
- * @internal - this method should only be called by mergeTree
175
+ * @remarks This method should only be called by mergeTree.
176
+ * @internal
241
177
  */
242
178
  get empty() {
243
179
  return this.refCount === 0;
244
180
  }
245
181
  /**
246
- *
247
- * @internal - this method should only be called by mergeTree
182
+ * @remarks This method should only be called by mergeTree.
183
+ * @internal
248
184
  */
249
- createLocalRef(offset, refType, properties, client) {
250
- const ref = new LocalReference(client, this.segment, offset, refType, properties);
185
+ createLocalRef(offset, refType, properties) {
186
+ const ref = new LocalReference(refType, properties);
187
+ ref.link(this.segment, offset, undefined);
251
188
  if (!refTypeIncludesFlag(ref, ReferenceType.Transient)) {
252
- this.addLocalRef(ref);
189
+ this.addLocalRef(ref, offset);
253
190
  }
254
191
  return ref;
255
192
  }
256
193
  /**
257
- *
258
- * @internal - this method should only be called by mergeTree
194
+ * @remarks This method should only be called by mergeTree.
195
+ * @internal
259
196
  */
260
- addLocalRef(lref) {
197
+ addLocalRef(lref, offset) {
261
198
  var _a, _b;
262
199
  assert(!refTypeIncludesFlag(lref, ReferenceType.Transient), 0x2df /* "transient references cannot be bound to segments" */);
263
200
  assertLocalReferences(lref);
264
- const refsAtOffset = this.refsByOffset[lref.offset] =
265
- (_a = this.refsByOffset[lref.offset]) !== null && _a !== void 0 ? _a : { at: ListMakeHead() };
201
+ assert(offset < this.segment.cachedLength, 0x348 /* offset cannot be beyond segment length */);
202
+ const refsAtOffset = this.refsByOffset[offset] =
203
+ (_a = this.refsByOffset[offset]) !== null && _a !== void 0 ? _a : { at: ListMakeHead() };
266
204
  const atRefs = refsAtOffset.at =
267
205
  (_b = refsAtOffset.at) !== null && _b !== void 0 ? _b : ListMakeHead();
268
- atRefs.enqueue(lref);
206
+ lref.link(this.segment, offset, atRefs.enqueue(lref));
269
207
  if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {
270
208
  this.hierRefCount++;
271
209
  }
272
210
  this.refCount++;
273
211
  }
274
212
  /**
275
- *
276
- * @internal - this method should only be called by mergeTree
213
+ * @remarks This method should only be called by mergeTree.
214
+ * @internal
277
215
  */
278
216
  removeLocalRef(lref) {
279
- assertLocalReferences(lref);
280
- const tryRemoveRef = (refs) => {
281
- if (refs) {
282
- let node = refs;
283
- do {
284
- node = node.next;
285
- if (node.data === lref) {
286
- ListRemoveEntry(node);
287
- if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {
288
- this.hierRefCount--;
289
- }
290
- this.refCount--;
291
- return lref;
292
- }
293
- } while (!node.isHead);
294
- }
295
- };
296
- const refAtOffset = this.refsByOffset[lref.offset];
297
- if (refAtOffset !== undefined) {
298
- let ref = tryRemoveRef(refAtOffset.before);
299
- if (ref) {
300
- return ref;
301
- }
302
- ref = tryRemoveRef(refAtOffset.at);
303
- if (ref) {
304
- return ref;
305
- }
306
- ref = tryRemoveRef(refAtOffset.after);
307
- if (ref) {
308
- return ref;
217
+ if (this.has(lref)) {
218
+ assertLocalReferences(lref);
219
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
220
+ ListRemoveEntry(lref.getListNode());
221
+ lref.link(lref.getSegment(), lref.getOffset(), undefined);
222
+ if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {
223
+ this.hierRefCount--;
309
224
  }
225
+ this.refCount--;
226
+ return lref;
310
227
  }
311
228
  }
312
229
  /**
313
- * @internal - this method should only be called by mergeTree
314
230
  *
315
231
  * Called by 'append()' implementations to append local refs from the given 'other' segment to the
316
232
  * end of 'this' segment.
317
233
  *
318
234
  * Note: This method should be invoked after the caller has ensured that segments can be merged,
319
- * but before 'this' segment's cachedLength has changed, or the adjustment to the local refs
320
- * will be incorrect.
235
+ * but before 'this' segment's cachedLength has changed, or the adjustment to the local refs
236
+ * will be incorrect.
237
+ *
238
+ * @remarks This method should only be called by mergeTree.
239
+ * @internal
321
240
  */
322
241
  append(other) {
323
242
  if (!other || other.empty) {
@@ -327,14 +246,52 @@ export class LocalReferenceCollection {
327
246
  this.refCount += other.refCount;
328
247
  other.hierRefCount = 0;
329
248
  for (const lref of other) {
330
- lref.segment = this.segment;
331
- lref.offset += this.refsByOffset.length;
249
+ assertLocalReferences(lref);
250
+ lref.link(this.segment, lref.getOffset() + this.refsByOffset.length, lref.getListNode());
332
251
  }
333
252
  this.refsByOffset.push(...other.refsByOffset);
334
253
  }
335
254
  /**
336
- * @internal - this method should only be called by mergeTree
255
+ * Returns true of the local reference is in the collection, otherwise false.
337
256
  *
257
+ * @remarks This method should only be called by mergeTree.
258
+ * @internal
259
+ */
260
+ has(lref) {
261
+ if (!(lref instanceof LocalReference)
262
+ || refTypeIncludesFlag(lref, ReferenceType.Transient)) {
263
+ return false;
264
+ }
265
+ const seg = lref.getSegment();
266
+ if (seg !== this.segment) {
267
+ return false;
268
+ }
269
+ // we should be able to optimize finding the
270
+ // list head
271
+ const listNode = lref.getListNode();
272
+ if (listNode === undefined) {
273
+ return false;
274
+ }
275
+ let prev = listNode;
276
+ let next = listNode;
277
+ while ((prev === null || prev === void 0 ? void 0 : prev.isHead) !== true && (next === null || next === void 0 ? void 0 : next.isHead) !== true) {
278
+ prev = prev === null || prev === void 0 ? void 0 : prev.prev;
279
+ next = next === null || next === void 0 ? void 0 : next.next;
280
+ }
281
+ const headNode = (prev === null || prev === void 0 ? void 0 : prev.isHead) === true ? prev : next;
282
+ if ((headNode === null || headNode === void 0 ? void 0 : headNode.isHead) !== true || headNode.empty()) {
283
+ return false;
284
+ }
285
+ const offset = lref.getOffset();
286
+ const refsAtOffset = this.refsByOffset[offset];
287
+ if ((refsAtOffset === null || refsAtOffset === void 0 ? void 0 : refsAtOffset.before) === headNode
288
+ || (refsAtOffset === null || refsAtOffset === void 0 ? void 0 : refsAtOffset.at) === headNode
289
+ || (refsAtOffset === null || refsAtOffset === void 0 ? void 0 : refsAtOffset.after) === headNode) {
290
+ return true;
291
+ }
292
+ return false;
293
+ }
294
+ /**
338
295
  * Splits this `LocalReferenceCollection` into the intervals [0, offset) and [offset, originalLength).
339
296
  * Local references in the former half of this split will remain associated with the segment used on construction.
340
297
  * Local references in the latter half of this split will be transferred to `splitSeg`,
@@ -342,14 +299,17 @@ export class LocalReferenceCollection {
342
299
  * @param offset - Offset into the original segment at which the collection should be split
343
300
  * @param splitSeg - Split segment which originally corresponded to the indices [offset, originalLength)
344
301
  * before splitting.
302
+ *
303
+ * @remarks This method should only be called by mergeTree.
304
+ * @internal
345
305
  */
346
306
  split(offset, splitSeg) {
347
307
  if (!this.empty) {
348
308
  const localRefs = new LocalReferenceCollection(splitSeg, this.refsByOffset.splice(offset, this.refsByOffset.length - offset));
349
309
  splitSeg.localRefs = localRefs;
350
310
  for (const lref of localRefs) {
351
- lref.segment = splitSeg;
352
- lref.offset -= offset;
311
+ assertLocalReferences(lref);
312
+ lref.link(splitSeg, lref.getOffset() - offset, lref.getListNode());
353
313
  if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {
354
314
  this.hierRefCount--;
355
315
  localRefs.hierRefCount++;
@@ -364,57 +324,59 @@ export class LocalReferenceCollection {
364
324
  }
365
325
  }
366
326
  addBeforeTombstones(...refs) {
367
- var _a, _b, _c, _d, _e;
327
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
328
+ var _k;
368
329
  const beforeRefs = (_b = (_a = this.refsByOffset[0]) === null || _a === void 0 ? void 0 : _a.before) !== null && _b !== void 0 ? _b : ListMakeHead();
330
+ if (((_c = this.refsByOffset[0]) === null || _c === void 0 ? void 0 : _c.before) === undefined) {
331
+ const refsAtOffset = (_d = (_k = this.refsByOffset)[0]) !== null && _d !== void 0 ? _d : (_k[0] = { before: beforeRefs });
332
+ (_e = refsAtOffset.before) !== null && _e !== void 0 ? _e : (refsAtOffset.before = beforeRefs);
333
+ }
369
334
  for (const iterable of refs) {
370
335
  for (const lref of iterable) {
371
336
  assertLocalReferences(lref);
372
337
  if (refTypeIncludesFlag(lref, ReferenceType.SlideOnRemove)) {
373
- beforeRefs.push(lref);
374
- lref.segment = this.segment;
375
- lref.offset = 0;
338
+ (_g = (_f = lref.callbacks) === null || _f === void 0 ? void 0 : _f.beforeSlide) === null || _g === void 0 ? void 0 : _g.call(_f);
339
+ beforeRefs.unshift(lref);
340
+ lref.link(this.segment, 0, beforeRefs.next);
376
341
  if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {
377
342
  this.hierRefCount++;
378
343
  }
379
344
  this.refCount++;
345
+ (_j = (_h = lref.callbacks) === null || _h === void 0 ? void 0 : _h.afterSlide) === null || _j === void 0 ? void 0 : _j.call(_h);
380
346
  }
381
347
  else {
382
- lref.segment = undefined;
348
+ lref.link(undefined, 0, undefined);
383
349
  }
384
350
  }
385
351
  }
386
- if (!beforeRefs.empty() && ((_c = this.refsByOffset[0]) === null || _c === void 0 ? void 0 : _c.before) === undefined) {
387
- const refsAtOffset = this.refsByOffset[0] =
388
- (_d = this.refsByOffset[0]) !== null && _d !== void 0 ? _d : { before: beforeRefs };
389
- refsAtOffset.before = (_e = refsAtOffset.before) !== null && _e !== void 0 ? _e : beforeRefs;
390
- }
391
352
  }
392
353
  addAfterTombstones(...refs) {
393
- var _a, _b, _c, _d, _e;
394
- const lastOffset = this.refsByOffset.length - 1;
354
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
355
+ var _k;
356
+ const lastOffset = this.segment.cachedLength - 1;
395
357
  const afterRefs = (_b = (_a = this.refsByOffset[lastOffset]) === null || _a === void 0 ? void 0 : _a.after) !== null && _b !== void 0 ? _b : ListMakeHead();
358
+ if (((_c = this.refsByOffset[lastOffset]) === null || _c === void 0 ? void 0 : _c.after) === undefined) {
359
+ const refsAtOffset = (_d = (_k = this.refsByOffset)[lastOffset]) !== null && _d !== void 0 ? _d : (_k[lastOffset] = { after: afterRefs });
360
+ (_e = refsAtOffset.after) !== null && _e !== void 0 ? _e : (refsAtOffset.after = afterRefs);
361
+ }
396
362
  for (const iterable of refs) {
397
363
  for (const lref of iterable) {
398
364
  assertLocalReferences(lref);
399
365
  if (refTypeIncludesFlag(lref, ReferenceType.SlideOnRemove)) {
400
- afterRefs.push(lref);
401
- lref.segment = this.segment;
402
- lref.offset = this.segment.cachedLength - 1;
366
+ (_g = (_f = lref.callbacks) === null || _f === void 0 ? void 0 : _f.beforeSlide) === null || _g === void 0 ? void 0 : _g.call(_f);
367
+ afterRefs.enqueue(lref);
368
+ lref.link(this.segment, lastOffset, afterRefs.prev);
403
369
  if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {
404
370
  this.hierRefCount++;
405
371
  }
406
372
  this.refCount++;
373
+ (_j = (_h = lref.callbacks) === null || _h === void 0 ? void 0 : _h.afterSlide) === null || _j === void 0 ? void 0 : _j.call(_h);
407
374
  }
408
375
  else {
409
- lref.segment = undefined;
376
+ lref.link(undefined, 0, undefined);
410
377
  }
411
378
  }
412
379
  }
413
- if (!afterRefs.empty() && ((_c = this.refsByOffset[lastOffset]) === null || _c === void 0 ? void 0 : _c.after) === undefined) {
414
- const refsAtOffset = this.refsByOffset[lastOffset] =
415
- (_d = this.refsByOffset[lastOffset]) !== null && _d !== void 0 ? _d : { after: afterRefs };
416
- refsAtOffset.after = (_e = refsAtOffset.after) !== null && _e !== void 0 ? _e : afterRefs;
417
- }
418
380
  }
419
381
  }
420
382
  //# sourceMappingURL=localReference.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"localReference.js","sourceRoot":"","sources":["../src/localReference.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,8BAA8B,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAE7D,OAAO,EAAQ,YAAY,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAIpE,OAAO,EAAgB,aAAa,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,aAAa,EAAe,MAAM,cAAc,CAAC;AAC1D,OAAO,EACH,oBAAoB,EACpB,oBAAoB,EACpB,yBAAyB,EACzB,gBAAgB,EAChB,iBAAiB,EAEjB,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,mBAAmB,GACtB,MAAM,sBAAsB,CAAC;AAE9B;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,OAAsB;IACzD,IAAI,cAAc,GAAG,CAAC,CAAC;IACvB,IAAI,mBAAmB,CAAC,OAAO,EAAE,aAAa,CAAC,SAAS,CAAC,EAAE;QACvD,EAAE,cAAc,CAAC;KACpB;IACD,IAAI,mBAAmB,CAAC,OAAO,EAAE,aAAa,CAAC,aAAa,CAAC,EAAE;QAC3D,EAAE,cAAc,CAAC;KACpB;IACD,IAAI,mBAAmB,CAAC,OAAO,EAAE,aAAa,CAAC,YAAY,CAAC,EAAE;QAC1D,EAAE,cAAc,CAAC;KACpB;IACD,IAAI,cAAc,GAAG,CAAC,EAAE;QACpB,MAAM,IAAI,UAAU,CAChB,+EAA+E,CAAC,CAAC;KACxF;AACL,CAAC;AAMD;;GAEG;AACH,MAAM,OAAO,cAAc;IAkBvB;;OAEG;IACH,YACqB,MAAc,EAC/B,WAAqB;IACrB;;OAEG;IACI,SAAiB,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C5B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,GAAG,CAAC,CAAC;oBAC5C,IAAI,iBAAiB,CAAC,IAAI,CAAC,IAAI,gBAAgB,CAAC,IAAI,CAAC,EAAE;wBACnD,IAAI,CAAC,YAAY,EAAE,CAAC;qBACvB;oBACD,IAAI,CAAC,QAAQ,EAAE,CAAC;iBACnB;qBAAM;oBACH,IAAI,CAAC,OAAO,GAAG,SAAS,CAAC;iBAC5B;aACJ;SACJ;QACD,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,CAAA,MAAA,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,0CAAE,KAAK,MAAK,SAAS,EAAE;YAC1E,MAAM,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC;gBAC9C,MAAA,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,mCAC1B,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;YAC5B,YAAY,CAAC,KAAK,GAAG,MAAA,YAAY,CAAC,KAAK,mCAAI,SAAS,CAAC;SACxD;IACL,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from \"@fluidframework/common-utils\";\nimport { UsageError } from \"@fluidframework/container-utils\";\nimport { Client } from \"./client\";\nimport { List, ListMakeHead, ListRemoveEntry } from \"./collections\";\nimport {\n ISegment,\n} from \"./mergeTree\";\nimport { ICombiningOp, ReferenceType } from \"./ops\";\nimport { addProperties, PropertySet } from \"./properties\";\nimport {\n minReferencePosition,\n maxReferencePosition,\n compareReferencePositions,\n refHasTileLabels,\n refHasRangeLabels,\n ReferencePosition,\n refGetRangeLabels,\n refGetTileLabels,\n refHasRangeLabel,\n refHasTileLabel,\n refTypeIncludesFlag,\n} from \"./referencePositions\";\n\n/**\n * @internal\n */\nexport function _validateReferenceType(refType: ReferenceType) {\n let exclusiveCount = 0;\n if (refTypeIncludesFlag(refType, ReferenceType.Transient)) {\n ++exclusiveCount;\n }\n if (refTypeIncludesFlag(refType, ReferenceType.SlideOnRemove)) {\n ++exclusiveCount;\n }\n if (refTypeIncludesFlag(refType, ReferenceType.StayOnRemove)) {\n ++exclusiveCount;\n }\n if (exclusiveCount > 1) {\n throw new UsageError(\n \"Reference types can only be one of Transient, SlideOnRemove, and StayOnRemove\");\n }\n}\n\nexport interface LocalReferencePosition extends ReferencePosition {\n callbacks?: Partial<Record<\"beforeSlide\" | \"afterSlide\", () => void>>;\n}\n\n/**\n * @deprecated - Use LocalReferencePosition\n */\nexport class LocalReference implements LocalReferencePosition {\n /**\n * @deprecated - use DetachedReferencePosition\n */\n public static readonly DetachedPosition: number = -1;\n\n public properties: PropertySet | undefined;\n /**\n * @deprecated - use properties to store pair\n */\n public pairedRef?: LocalReference;\n /**\n * @deprecated - use getSegment\n */\n public segment: ISegment | undefined;\n\n public callbacks?: Partial<Record<\"beforeSlide\" | \"afterSlide\", () => void>> | undefined;\n\n /**\n * @deprecated - use createReferencePosition\n */\n constructor(\n private readonly client: Client,\n initSegment: ISegment,\n /**\n * @deprecated - use getOffset\n */\n public offset: number = 0,\n public refType = ReferenceType.Simple,\n properties?: PropertySet,\n ) {\n _validateReferenceType(refType);\n this.segment = initSegment;\n this.properties = properties;\n }\n\n /**\n * @deprecated - use minReferencePosition\n */\n public min(b: LocalReference) {\n return minReferencePosition(this, b);\n }\n /**\n * @deprecated - use maxReferencePosition\n */\n public max(b: LocalReference) {\n return maxReferencePosition(this, b);\n }\n /**\n * @deprecated - use compareReferencePositions\n */\n public compare(b: LocalReference) {\n return compareReferencePositions(this, b);\n }\n\n /**\n * @deprecated - use getLocalReferencePosition\n */\n public toPosition() {\n return this.getClient().localReferencePositionToPosition(this);\n }\n\n /**\n * @deprecated - use refHasTileLabels\n */\n public hasTileLabels(): boolean {\n return refHasTileLabels(this);\n }\n /**\n * @deprecated - use refHasRangeLabels\n */\n public hasRangeLabels(): boolean {\n return refHasRangeLabels(this);\n }\n /**\n * @deprecated - use refHasTileLabel\n */\n public hasTileLabel(label: string): boolean {\n return refHasTileLabel(this, label);\n }\n /**\n * @deprecated - use refHasRangeLabel\n */\n public hasRangeLabel(label: string): boolean {\n return refHasRangeLabel(this, label);\n }\n /**\n * @deprecated - use refGetTileLabels\n */\n public getTileLabels(): string[] | undefined {\n return refGetTileLabels(this);\n }\n /**\n * @deprecated - use refGetRangeLabels\n */\n public getRangeLabels(): string[] | undefined {\n return refGetRangeLabels(this);\n }\n\n public isLeaf() {\n return false;\n }\n\n public addProperties(newProps: PropertySet, op?: ICombiningOp) {\n this.properties = addProperties(this.properties, newProps, op);\n }\n\n /**\n * @deprecated - no longer supported\n */\n public getClient() {\n return this.client;\n }\n\n public getSegment() {\n return this.segment;\n }\n\n public getOffset() {\n return this.offset;\n }\n\n public getProperties() {\n return this.properties;\n }\n}\n\ninterface IRefsAtOffset {\n before?: List<LocalReference>;\n at?: List<LocalReference>;\n after?: List<LocalReference>;\n}\n\nfunction assertLocalReferences(lref: ReferencePosition | LocalReference): asserts lref is LocalReference {\n assert(lref instanceof LocalReference, 0x2e0 /* \"lref not a Local Reference\" */);\n}\n\n/**\n * Represents a collection of {@link ReferencePosition}s associated with one segment in a merge-tree.\n */\nexport class LocalReferenceCollection {\n public static append(seg1: ISegment, seg2: ISegment) {\n if (seg2.localRefs && !seg2.localRefs.empty) {\n if (!seg1.localRefs) {\n seg1.localRefs = new LocalReferenceCollection(seg1);\n }\n assert(seg1.localRefs.refsByOffset.length === seg1.cachedLength,\n 0x2be /* \"LocalReferences array contains a gap\" */);\n seg1.localRefs.append(seg2.localRefs);\n } else if (seg1.localRefs) {\n // Since creating the LocalReferenceCollection, we may have appended\n // segments that had no local references. Account for them now by padding the array.\n seg1.localRefs.refsByOffset.length += seg2.cachedLength;\n }\n }\n\n /**\n *\n * @internal - this method should only be called by mergeTree\n */\n public hierRefCount: number = 0;\n private readonly refsByOffset: (IRefsAtOffset | undefined)[];\n private refCount: number = 0;\n\n /**\n *\n * @internal - this method should only be called by mergeTree\n */\n constructor(\n /** Segment this `LocalReferenceCollection` is associated to. */\n private readonly segment: ISegment,\n initialRefsByfOffset = new Array<IRefsAtOffset | undefined>(segment.cachedLength)) {\n // Since javascript arrays are sparse the above won't populate any of the\n // indices, but it will ensure the length property of the array matches\n // the length of the segment.\n this.refsByOffset = initialRefsByfOffset;\n }\n\n /**\n *\n * @internal - this method should only be called by mergeTree\n */\n public [Symbol.iterator]() {\n const subiterators: IterableIterator<LocalReference>[] = [];\n for (const refs of this.refsByOffset) {\n if (refs) {\n if (refs.before) {\n subiterators.push(refs.before[Symbol.iterator]());\n }\n if (refs.at) {\n subiterators.push(refs.at[Symbol.iterator]());\n }\n if (refs.after) {\n subiterators.push(refs.after[Symbol.iterator]());\n }\n }\n }\n\n const iterator = {\n next(): IteratorResult<LocalReference> {\n while (subiterators.length > 0) {\n const next = subiterators[0].next();\n if (next.done === true) {\n subiterators.shift();\n } else {\n return next;\n }\n }\n\n return { value: undefined, done: true };\n },\n [Symbol.iterator]() {\n return this;\n },\n };\n return iterator;\n }\n\n /**\n *\n * @internal - this method should only be called by mergeTree\n */\n public clear() {\n this.refCount = 0;\n this.hierRefCount = 0;\n const detachSegments = (refs: List<LocalReference> | undefined) => {\n if (refs) {\n for (const r of refs) {\n if (r.segment === this.segment) {\n r.segment = undefined;\n }\n }\n }\n };\n for (let i = 0; i < this.refsByOffset.length; i++) {\n const refsAtOffset = this.refsByOffset[i];\n if (refsAtOffset) {\n detachSegments(refsAtOffset.before);\n detachSegments(refsAtOffset.at);\n detachSegments(refsAtOffset.before);\n this.refsByOffset[i] = undefined;\n }\n }\n }\n\n /**\n *\n * @internal - this method should only be called by mergeTree\n */\n public get empty() {\n return this.refCount === 0;\n }\n\n /**\n *\n * @internal - this method should only be called by mergeTree\n */\n public createLocalRef(\n offset: number | undefined,\n refType: ReferenceType,\n properties: PropertySet | undefined,\n client: Client): ReferencePosition {\n const ref = new LocalReference(\n client,\n this.segment,\n offset,\n refType,\n properties,\n );\n if (!refTypeIncludesFlag(ref, ReferenceType.Transient)) {\n this.addLocalRef(ref);\n }\n return ref;\n }\n\n /**\n *\n * @internal - this method should only be called by mergeTree\n */\n public addLocalRef(lref: LocalReference | ReferencePosition) {\n assert(\n !refTypeIncludesFlag(lref, ReferenceType.Transient),\n 0x2df /* \"transient references cannot be bound to segments\" */);\n assertLocalReferences(lref);\n const refsAtOffset = this.refsByOffset[lref.offset] =\n this.refsByOffset[lref.offset]\n ?? { at: ListMakeHead() };\n const atRefs = refsAtOffset.at =\n refsAtOffset.at\n ?? ListMakeHead();\n\n atRefs.enqueue(lref);\n\n if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {\n this.hierRefCount++;\n }\n this.refCount++;\n }\n\n /**\n *\n * @internal - this method should only be called by mergeTree\n */\n public removeLocalRef(lref: LocalReference | ReferencePosition) {\n assertLocalReferences(lref);\n const tryRemoveRef = (refs: List<LocalReference> | undefined) => {\n if (refs) {\n let node = refs;\n do {\n node = node.next;\n if (node.data === lref) {\n ListRemoveEntry(node);\n if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {\n this.hierRefCount--;\n }\n this.refCount--;\n return lref;\n }\n } while (!node.isHead);\n }\n };\n const refAtOffset = this.refsByOffset[lref.offset];\n if (refAtOffset !== undefined) {\n let ref = tryRemoveRef(refAtOffset.before);\n if (ref) {\n return ref;\n }\n\n ref = tryRemoveRef(refAtOffset.at);\n if (ref) {\n return ref;\n }\n\n ref = tryRemoveRef(refAtOffset.after);\n if (ref) {\n return ref;\n }\n }\n }\n\n /**\n * @internal - this method should only be called by mergeTree\n *\n * Called by 'append()' implementations to append local refs from the given 'other' segment to the\n * end of 'this' segment.\n *\n * Note: This method should be invoked after the caller has ensured that segments can be merged,\n * but before 'this' segment's cachedLength has changed, or the adjustment to the local refs\n * will be incorrect.\n */\n public append(other: LocalReferenceCollection) {\n if (!other || other.empty) {\n return;\n }\n this.hierRefCount += other.hierRefCount;\n this.refCount += other.refCount;\n other.hierRefCount = 0;\n for (const lref of other) {\n lref.segment = this.segment;\n lref.offset += this.refsByOffset.length;\n }\n\n this.refsByOffset.push(...other.refsByOffset);\n }\n\n /**\n * @internal - this method should only be called by mergeTree\n *\n * Splits this `LocalReferenceCollection` into the intervals [0, offset) and [offset, originalLength).\n * Local references in the former half of this split will remain associated with the segment used on construction.\n * Local references in the latter half of this split will be transferred to `splitSeg`,\n * and its `localRefs` field will be set.\n * @param offset - Offset into the original segment at which the collection should be split\n * @param splitSeg - Split segment which originally corresponded to the indices [offset, originalLength)\n * before splitting.\n */\n public split(offset: number, splitSeg: ISegment) {\n if (!this.empty) {\n const localRefs =\n new LocalReferenceCollection(\n splitSeg,\n this.refsByOffset.splice(offset, this.refsByOffset.length - offset));\n\n splitSeg.localRefs = localRefs;\n for (const lref of localRefs) {\n lref.segment = splitSeg;\n lref.offset -= offset;\n if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {\n this.hierRefCount--;\n localRefs.hierRefCount++;\n }\n this.refCount--;\n localRefs.refCount++;\n }\n } else {\n // shrink the offset array when empty and splitting\n this.refsByOffset.length = offset;\n }\n }\n\n public addBeforeTombstones(...refs: Iterable<LocalReference | ReferencePosition>[]) {\n const beforeRefs = this.refsByOffset[0]?.before ?? ListMakeHead();\n\n for (const iterable of refs) {\n for (const lref of iterable) {\n assertLocalReferences(lref);\n if (refTypeIncludesFlag(lref, ReferenceType.SlideOnRemove)) {\n beforeRefs.push(lref);\n lref.segment = this.segment;\n lref.offset = 0;\n if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {\n this.hierRefCount++;\n }\n this.refCount++;\n } else {\n lref.segment = undefined;\n }\n }\n }\n if (!beforeRefs.empty() && this.refsByOffset[0]?.before === undefined) {\n const refsAtOffset = this.refsByOffset[0] =\n this.refsByOffset[0]\n ?? { before: beforeRefs };\n refsAtOffset.before = refsAtOffset.before ?? beforeRefs;\n }\n }\n\n public addAfterTombstones(...refs: Iterable<LocalReference | ReferencePosition>[]) {\n const lastOffset = this.refsByOffset.length - 1;\n const afterRefs =\n this.refsByOffset[lastOffset]?.after ?? ListMakeHead();\n\n for (const iterable of refs) {\n for (const lref of iterable) {\n assertLocalReferences(lref);\n if (refTypeIncludesFlag(lref, ReferenceType.SlideOnRemove)) {\n afterRefs.push(lref);\n lref.segment = this.segment;\n lref.offset = this.segment.cachedLength - 1;\n if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {\n this.hierRefCount++;\n }\n this.refCount++;\n } else {\n lref.segment = undefined;\n }\n }\n }\n if (!afterRefs.empty() && this.refsByOffset[lastOffset]?.after === undefined) {\n const refsAtOffset = this.refsByOffset[lastOffset] =\n this.refsByOffset[lastOffset]\n ?? { after: afterRefs };\n refsAtOffset.after = refsAtOffset.after ?? afterRefs;\n }\n }\n}\n"]}
1
+ {"version":3,"file":"localReference.js","sourceRoot":"","sources":["../src/localReference.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,8BAA8B,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAQ,YAAY,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAIpE,OAAO,EAAgB,aAAa,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,aAAa,EAAe,MAAM,cAAc,CAAC;AAC1D,OAAO,EACH,gBAAgB,EAChB,iBAAiB,EAEjB,mBAAmB,GACtB,MAAM,sBAAsB,CAAC;AAE9B;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,OAAsB;IACzD,IAAI,cAAc,GAAG,CAAC,CAAC;IACvB,IAAI,mBAAmB,CAAC,OAAO,EAAE,aAAa,CAAC,SAAS,CAAC,EAAE;QACvD,EAAE,cAAc,CAAC;KACpB;IACD,IAAI,mBAAmB,CAAC,OAAO,EAAE,aAAa,CAAC,aAAa,CAAC,EAAE;QAC3D,EAAE,cAAc,CAAC;KACpB;IACD,IAA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IAAI,CAAC,CAAC;oBACxB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC;oBACpD,IAAI,iBAAiB,CAAC,IAAI,CAAC,IAAI,gBAAgB,CAAC,IAAI,CAAC,EAAE;wBACnD,IAAI,CAAC,YAAY,EAAE,CAAC;qBACvB;oBACD,IAAI,CAAC,QAAQ,EAAE,CAAC;oBAChB,MAAA,MAAA,IAAI,CAAC,SAAS,0CAAE,UAAU,kDAAI,CAAC;iBAClC;qBAAM;oBACH,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC;iBACtC;aACJ;SACJ;IACL,CAAC;CACJ","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from \"@fluidframework/common-utils\";\nimport { UsageError } from \"@fluidframework/container-utils\";\nimport { List, ListMakeHead, ListRemoveEntry } from \"./collections\";\nimport {\n ISegment,\n} from \"./mergeTreeNodes\";\nimport { ICombiningOp, ReferenceType } from \"./ops\";\nimport { addProperties, PropertySet } from \"./properties\";\nimport {\n refHasTileLabels,\n refHasRangeLabels,\n ReferencePosition,\n refTypeIncludesFlag,\n} from \"./referencePositions\";\n\n/**\n * @internal\n */\nexport function _validateReferenceType(refType: ReferenceType) {\n let exclusiveCount = 0;\n if (refTypeIncludesFlag(refType, ReferenceType.Transient)) {\n ++exclusiveCount;\n }\n if (refTypeIncludesFlag(refType, ReferenceType.SlideOnRemove)) {\n ++exclusiveCount;\n }\n if (refTypeIncludesFlag(refType, ReferenceType.StayOnRemove)) {\n ++exclusiveCount;\n }\n if (exclusiveCount > 1) {\n throw new UsageError(\n \"Reference types can only be one of Transient, SlideOnRemove, and StayOnRemove\");\n }\n}\n\nexport interface LocalReferencePosition extends ReferencePosition {\n callbacks?: Partial<Record<\"beforeSlide\" | \"afterSlide\", () => void>>;\n}\n\n/**\n * @privateRemarks This should not be exported outside merge tree.\n * @internal\n */\nclass LocalReference implements LocalReferencePosition {\n public properties: PropertySet | undefined;\n\n private segment: ISegment | undefined;\n private offset: number = 0;\n private listNode: List<LocalReference> | undefined;\n\n public callbacks?: Partial<Record<\"beforeSlide\" | \"afterSlide\", () => void>> | undefined;\n\n constructor(\n public refType = ReferenceType.Simple,\n properties?: PropertySet,\n ) {\n _validateReferenceType(refType);\n this.properties = properties;\n }\n\n public link(segment: ISegment | undefined, offset: number, listNode: List<LocalReference> | undefined) {\n if (listNode !== this.listNode\n && this.listNode !== undefined) {\n this.segment?.localRefs?.removeLocalRef(this);\n }\n this.segment = segment;\n this.offset = offset;\n this.listNode = listNode;\n }\n\n public isLeaf() {\n return false;\n }\n\n public addProperties(newProps: PropertySet, op?: ICombiningOp) {\n this.properties = addProperties(this.properties, newProps, op);\n }\n\n public getSegment() {\n return this.segment;\n }\n\n public getOffset() {\n return this.offset;\n }\n\n public getListNode() {\n return this.listNode;\n }\n\n public getProperties() {\n return this.properties;\n }\n}\n\nexport function createDetachedLocalReferencePosition(refType?: ReferenceType): LocalReferencePosition {\n return new LocalReference(refType, undefined);\n}\n\ninterface IRefsAtOffset {\n before?: List<LocalReference>;\n at?: List<LocalReference>;\n after?: List<LocalReference>;\n}\n\nexport function assertLocalReferences(\n lref: any,\n): asserts lref is LocalReference {\n assert(lref instanceof LocalReference, 0x2e0 /* \"lref not a Local Reference\" */);\n}\n\n/**\n * Represents a collection of {@link LocalReferencePosition}s associated with one segment in a merge-tree.\n */\nexport class LocalReferenceCollection {\n public static append(seg1: ISegment, seg2: ISegment) {\n if (seg2.localRefs && !seg2.localRefs.empty) {\n if (!seg1.localRefs) {\n seg1.localRefs = new LocalReferenceCollection(seg1);\n }\n assert(seg1.localRefs.refsByOffset.length === seg1.cachedLength,\n 0x2be /* \"LocalReferences array contains a gap\" */);\n seg1.localRefs.append(seg2.localRefs);\n } else if (seg1.localRefs) {\n // Since creating the LocalReferenceCollection, we may have appended\n // segments that had no local references. Account for them now by padding the array.\n seg1.localRefs.refsByOffset.length += seg2.cachedLength;\n }\n }\n\n /**\n * @remarks This method should only be called by mergeTree.\n * @internal\n */\n public hierRefCount: number = 0;\n private readonly refsByOffset: (IRefsAtOffset | undefined)[];\n private refCount: number = 0;\n\n /**\n *\n * @internal\n */\n constructor(\n /** Segment this `LocalReferenceCollection` is associated to. */\n private readonly segment: ISegment,\n initialRefsByfOffset = new Array<IRefsAtOffset | undefined>(segment.cachedLength)) {\n // Since javascript arrays are sparse the above won't populate any of the\n // indices, but it will ensure the length property of the array matches\n // the length of the segment.\n this.refsByOffset = initialRefsByfOffset;\n }\n\n /**\n * @remarks This method should only be called by mergeTree.\n * @internal\n */\n public [Symbol.iterator]() {\n const subiterators: IterableIterator<LocalReferencePosition>[] = [];\n for (const refs of this.refsByOffset) {\n if (refs) {\n if (refs.before) {\n subiterators.push(refs.before[Symbol.iterator]());\n }\n if (refs.at) {\n subiterators.push(refs.at[Symbol.iterator]());\n }\n if (refs.after) {\n subiterators.push(refs.after[Symbol.iterator]());\n }\n }\n }\n\n const iterator = {\n next(): IteratorResult<LocalReferencePosition> {\n while (subiterators.length > 0) {\n const next = subiterators[0].next();\n if (next.done === true) {\n subiterators.shift();\n } else {\n return next;\n }\n }\n\n return { value: undefined, done: true };\n },\n [Symbol.iterator]() {\n return this;\n },\n };\n return iterator;\n }\n\n /**\n * @remarks This method should only be called by mergeTree.\n * @internal\n */\n public clear() {\n this.refCount = 0;\n this.hierRefCount = 0;\n const detachSegments = (refs: List<LocalReference> | undefined) => {\n if (refs) {\n for (const r of refs) {\n this.removeLocalRef(r);\n }\n }\n };\n for (let i = 0; i < this.refsByOffset.length; i++) {\n const refsAtOffset = this.refsByOffset[i];\n if (refsAtOffset) {\n detachSegments(refsAtOffset.before);\n detachSegments(refsAtOffset.at);\n detachSegments(refsAtOffset.after);\n this.refsByOffset[i] = undefined;\n }\n }\n }\n\n /**\n * @remarks This method should only be called by mergeTree.\n * @internal\n */\n public get empty() {\n return this.refCount === 0;\n }\n\n /**\n * @remarks This method should only be called by mergeTree.\n * @internal\n */\n public createLocalRef(\n offset: number,\n refType: ReferenceType,\n properties: PropertySet | undefined): LocalReferencePosition {\n const ref = new LocalReference(\n refType,\n properties,\n );\n ref.link(this.segment, offset, undefined);\n if (!refTypeIncludesFlag(ref, ReferenceType.Transient)) {\n this.addLocalRef(ref, offset);\n }\n return ref;\n }\n\n /**\n * @remarks This method should only be called by mergeTree.\n * @internal\n */\n public addLocalRef(lref: LocalReferencePosition, offset: number) {\n assert(\n !refTypeIncludesFlag(lref, ReferenceType.Transient),\n 0x2df /* \"transient references cannot be bound to segments\" */);\n assertLocalReferences(lref);\n assert(offset < this.segment.cachedLength, 0x348 /* offset cannot be beyond segment length */);\n const refsAtOffset = this.refsByOffset[offset] =\n this.refsByOffset[offset]\n ?? { at: ListMakeHead() };\n const atRefs = refsAtOffset.at =\n refsAtOffset.at\n ?? ListMakeHead();\n\n lref.link(this.segment, offset, atRefs.enqueue(lref));\n\n if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {\n this.hierRefCount++;\n }\n this.refCount++;\n }\n\n /**\n * @remarks This method should only be called by mergeTree.\n * @internal\n */\n public removeLocalRef(lref: LocalReferencePosition): LocalReferencePosition | undefined {\n if (this.has(lref)) {\n assertLocalReferences(lref);\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n ListRemoveEntry(lref.getListNode()!);\n lref.link(\n lref.getSegment(),\n lref.getOffset(),\n undefined);\n if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {\n this.hierRefCount--;\n }\n this.refCount--;\n return lref;\n }\n }\n\n /**\n *\n * Called by 'append()' implementations to append local refs from the given 'other' segment to the\n * end of 'this' segment.\n *\n * Note: This method should be invoked after the caller has ensured that segments can be merged,\n * but before 'this' segment's cachedLength has changed, or the adjustment to the local refs\n * will be incorrect.\n *\n * @remarks This method should only be called by mergeTree.\n * @internal\n */\n public append(other: LocalReferenceCollection) {\n if (!other || other.empty) {\n return;\n }\n this.hierRefCount += other.hierRefCount;\n this.refCount += other.refCount;\n other.hierRefCount = 0;\n for (const lref of other) {\n assertLocalReferences(lref);\n lref.link(\n this.segment,\n lref.getOffset() + this.refsByOffset.length,\n lref.getListNode());\n }\n\n this.refsByOffset.push(...other.refsByOffset);\n }\n /**\n * Returns true of the local reference is in the collection, otherwise false.\n *\n * @remarks This method should only be called by mergeTree.\n * @internal\n */\n public has(lref: ReferencePosition): boolean {\n if (!(lref instanceof LocalReference)\n || refTypeIncludesFlag(lref, ReferenceType.Transient)) {\n return false;\n }\n const seg = lref.getSegment();\n if (seg !== this.segment) {\n return false;\n }\n // we should be able to optimize finding the\n // list head\n const listNode = lref.getListNode();\n if (listNode === undefined) {\n return false;\n }\n let prev = listNode;\n let next = listNode;\n while (prev?.isHead !== true && next?.isHead !== true) {\n prev = prev?.prev;\n next = next?.next;\n }\n\n const headNode = prev?.isHead === true ? prev : next;\n if (headNode?.isHead !== true || headNode.empty()) {\n return false;\n }\n const offset = lref.getOffset();\n const refsAtOffset = this.refsByOffset[offset];\n if (refsAtOffset?.before === headNode\n || refsAtOffset?.at === headNode\n || refsAtOffset?.after === headNode) {\n return true;\n }\n return false;\n }\n\n /**\n * Splits this `LocalReferenceCollection` into the intervals [0, offset) and [offset, originalLength).\n * Local references in the former half of this split will remain associated with the segment used on construction.\n * Local references in the latter half of this split will be transferred to `splitSeg`,\n * and its `localRefs` field will be set.\n * @param offset - Offset into the original segment at which the collection should be split\n * @param splitSeg - Split segment which originally corresponded to the indices [offset, originalLength)\n * before splitting.\n *\n * @remarks This method should only be called by mergeTree.\n * @internal\n */\n public split(offset: number, splitSeg: ISegment) {\n if (!this.empty) {\n const localRefs =\n new LocalReferenceCollection(\n splitSeg,\n this.refsByOffset.splice(offset, this.refsByOffset.length - offset));\n\n splitSeg.localRefs = localRefs;\n for (const lref of localRefs) {\n assertLocalReferences(lref);\n lref.link(\n splitSeg,\n lref.getOffset() - offset,\n lref.getListNode());\n if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {\n this.hierRefCount--;\n localRefs.hierRefCount++;\n }\n this.refCount--;\n localRefs.refCount++;\n }\n } else {\n // shrink the offset array when empty and splitting\n this.refsByOffset.length = offset;\n }\n }\n\n public addBeforeTombstones(...refs: Iterable<LocalReferencePosition>[]) {\n const beforeRefs = this.refsByOffset[0]?.before ?? ListMakeHead();\n\n if (this.refsByOffset[0]?.before === undefined) {\n const refsAtOffset = this.refsByOffset[0] ??= { before: beforeRefs };\n refsAtOffset.before ??= beforeRefs;\n }\n\n for (const iterable of refs) {\n for (const lref of iterable) {\n assertLocalReferences(lref);\n if (refTypeIncludesFlag(lref, ReferenceType.SlideOnRemove)) {\n lref.callbacks?.beforeSlide?.();\n beforeRefs.unshift(lref);\n lref.link(this.segment, 0, beforeRefs.next);\n if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {\n this.hierRefCount++;\n }\n this.refCount++;\n lref.callbacks?.afterSlide?.();\n } else {\n lref.link(undefined, 0, undefined);\n }\n }\n }\n }\n\n public addAfterTombstones(...refs: Iterable<LocalReferencePosition>[]) {\n const lastOffset = this.segment.cachedLength - 1;\n const afterRefs = this.refsByOffset[lastOffset]?.after ?? ListMakeHead();\n\n if (this.refsByOffset[lastOffset]?.after === undefined) {\n const refsAtOffset = this.refsByOffset[lastOffset] ??= { after: afterRefs };\n refsAtOffset.after ??= afterRefs;\n }\n\n for (const iterable of refs) {\n for (const lref of iterable) {\n assertLocalReferences(lref);\n if (refTypeIncludesFlag(lref, ReferenceType.SlideOnRemove)) {\n lref.callbacks?.beforeSlide?.();\n afterRefs.enqueue(lref);\n lref.link(this.segment, lastOffset, afterRefs.prev);\n if (refHasRangeLabels(lref) || refHasTileLabels(lref)) {\n this.hierRefCount++;\n }\n this.refCount++;\n lref.callbacks?.afterSlide?.();\n } else {\n lref.link(undefined, 0, undefined);\n }\n }\n }\n }\n}\n"]}