@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
@@ -4,7 +4,7 @@
4
4
  * Licensed under the MIT License.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.LocalReferenceCollection = exports.LocalReference = exports._validateReferenceType = void 0;
7
+ exports.LocalReferenceCollection = exports.assertLocalReferences = exports.createDetachedLocalReferencePosition = exports._validateReferenceType = void 0;
8
8
  const common_utils_1 = require("@fluidframework/common-utils");
9
9
  const container_utils_1 = require("@fluidframework/container-utils");
10
10
  const collections_1 = require("./collections");
@@ -31,83 +31,25 @@ function _validateReferenceType(refType) {
31
31
  }
32
32
  exports._validateReferenceType = _validateReferenceType;
33
33
  /**
34
- * @deprecated - Use LocalReferencePosition
34
+ * @privateRemarks This should not be exported outside merge tree.
35
+ * @internal
35
36
  */
36
37
  class LocalReference {
37
- /**
38
- * @deprecated - use createReferencePosition
39
- */
40
- constructor(client, initSegment,
41
- /**
42
- * @deprecated - use getOffset
43
- */
44
- offset = 0, refType = ops_1.ReferenceType.Simple, properties) {
45
- this.client = client;
46
- this.offset = offset;
38
+ constructor(refType = ops_1.ReferenceType.Simple, properties) {
47
39
  this.refType = refType;
40
+ this.offset = 0;
48
41
  _validateReferenceType(refType);
49
- this.segment = initSegment;
50
42
  this.properties = properties;
51
43
  }
52
- /**
53
- * @deprecated - use minReferencePosition
54
- */
55
- min(b) {
56
- return (0, referencePositions_1.minReferencePosition)(this, b);
57
- }
58
- /**
59
- * @deprecated - use maxReferencePosition
60
- */
61
- max(b) {
62
- return (0, referencePositions_1.maxReferencePosition)(this, b);
63
- }
64
- /**
65
- * @deprecated - use compareReferencePositions
66
- */
67
- compare(b) {
68
- return (0, referencePositions_1.compareReferencePositions)(this, b);
69
- }
70
- /**
71
- * @deprecated - use getLocalReferencePosition
72
- */
73
- toPosition() {
74
- return this.getClient().localReferencePositionToPosition(this);
75
- }
76
- /**
77
- * @deprecated - use refHasTileLabels
78
- */
79
- hasTileLabels() {
80
- return (0, referencePositions_1.refHasTileLabels)(this);
81
- }
82
- /**
83
- * @deprecated - use refHasRangeLabels
84
- */
85
- hasRangeLabels() {
86
- return (0, referencePositions_1.refHasRangeLabels)(this);
87
- }
88
- /**
89
- * @deprecated - use refHasTileLabel
90
- */
91
- hasTileLabel(label) {
92
- return (0, referencePositions_1.refHasTileLabel)(this, label);
93
- }
94
- /**
95
- * @deprecated - use refHasRangeLabel
96
- */
97
- hasRangeLabel(label) {
98
- return (0, referencePositions_1.refHasRangeLabel)(this, label);
99
- }
100
- /**
101
- * @deprecated - use refGetTileLabels
102
- */
103
- getTileLabels() {
104
- return (0, referencePositions_1.refGetTileLabels)(this);
105
- }
106
- /**
107
- * @deprecated - use refGetRangeLabels
108
- */
109
- getRangeLabels() {
110
- return (0, referencePositions_1.refGetRangeLabels)(this);
44
+ link(segment, offset, listNode) {
45
+ var _a, _b;
46
+ if (listNode !== this.listNode
47
+ && this.listNode !== undefined) {
48
+ (_b = (_a = this.segment) === null || _a === void 0 ? void 0 : _a.localRefs) === null || _b === void 0 ? void 0 : _b.removeLocalRef(this);
49
+ }
50
+ this.segment = segment;
51
+ this.offset = offset;
52
+ this.listNode = listNode;
111
53
  }
112
54
  isLeaf() {
113
55
  return false;
@@ -115,45 +57,42 @@ class LocalReference {
115
57
  addProperties(newProps, op) {
116
58
  this.properties = (0, properties_1.addProperties)(this.properties, newProps, op);
117
59
  }
118
- /**
119
- * @deprecated - no longer supported
120
- */
121
- getClient() {
122
- return this.client;
123
- }
124
60
  getSegment() {
125
61
  return this.segment;
126
62
  }
127
63
  getOffset() {
128
64
  return this.offset;
129
65
  }
66
+ getListNode() {
67
+ return this.listNode;
68
+ }
130
69
  getProperties() {
131
70
  return this.properties;
132
71
  }
133
72
  }
134
- exports.LocalReference = LocalReference;
135
- /**
136
- * @deprecated - use DetachedReferencePosition
137
- */
138
- LocalReference.DetachedPosition = -1;
73
+ function createDetachedLocalReferencePosition(refType) {
74
+ return new LocalReference(refType, undefined);
75
+ }
76
+ exports.createDetachedLocalReferencePosition = createDetachedLocalReferencePosition;
139
77
  function assertLocalReferences(lref) {
140
78
  (0, common_utils_1.assert)(lref instanceof LocalReference, 0x2e0 /* "lref not a Local Reference" */);
141
79
  }
80
+ exports.assertLocalReferences = assertLocalReferences;
142
81
  /**
143
- * Represents a collection of {@link ReferencePosition}s associated with one segment in a merge-tree.
82
+ * Represents a collection of {@link LocalReferencePosition}s associated with one segment in a merge-tree.
144
83
  */
145
84
  class LocalReferenceCollection {
146
85
  /**
147
86
  *
148
- * @internal - this method should only be called by mergeTree
87
+ * @internal
149
88
  */
150
89
  constructor(
151
90
  /** Segment this `LocalReferenceCollection` is associated to. */
152
91
  segment, initialRefsByfOffset = new Array(segment.cachedLength)) {
153
92
  this.segment = segment;
154
93
  /**
155
- *
156
- * @internal - this method should only be called by mergeTree
94
+ * @remarks This method should only be called by mergeTree.
95
+ * @internal
157
96
  */
158
97
  this.hierRefCount = 0;
159
98
  this.refCount = 0;
@@ -177,8 +116,8 @@ class LocalReferenceCollection {
177
116
  }
178
117
  }
179
118
  /**
180
- *
181
- * @internal - this method should only be called by mergeTree
119
+ * @remarks This method should only be called by mergeTree.
120
+ * @internal
182
121
  */
183
122
  [Symbol.iterator]() {
184
123
  const subiterators = [];
@@ -215,8 +154,8 @@ class LocalReferenceCollection {
215
154
  return iterator;
216
155
  }
217
156
  /**
218
- *
219
- * @internal - this method should only be called by mergeTree
157
+ * @remarks This method should only be called by mergeTree.
158
+ * @internal
220
159
  */
221
160
  clear() {
222
161
  this.refCount = 0;
@@ -224,9 +163,7 @@ class LocalReferenceCollection {
224
163
  const detachSegments = (refs) => {
225
164
  if (refs) {
226
165
  for (const r of refs) {
227
- if (r.segment === this.segment) {
228
- r.segment = undefined;
229
- }
166
+ this.removeLocalRef(r);
230
167
  }
231
168
  }
232
169
  };
@@ -235,94 +172,77 @@ class LocalReferenceCollection {
235
172
  if (refsAtOffset) {
236
173
  detachSegments(refsAtOffset.before);
237
174
  detachSegments(refsAtOffset.at);
238
- detachSegments(refsAtOffset.before);
175
+ detachSegments(refsAtOffset.after);
239
176
  this.refsByOffset[i] = undefined;
240
177
  }
241
178
  }
242
179
  }
243
180
  /**
244
- *
245
- * @internal - this method should only be called by mergeTree
181
+ * @remarks This method should only be called by mergeTree.
182
+ * @internal
246
183
  */
247
184
  get empty() {
248
185
  return this.refCount === 0;
249
186
  }
250
187
  /**
251
- *
252
- * @internal - this method should only be called by mergeTree
188
+ * @remarks This method should only be called by mergeTree.
189
+ * @internal
253
190
  */
254
- createLocalRef(offset, refType, properties, client) {
255
- const ref = new LocalReference(client, this.segment, offset, refType, properties);
191
+ createLocalRef(offset, refType, properties) {
192
+ const ref = new LocalReference(refType, properties);
193
+ ref.link(this.segment, offset, undefined);
256
194
  if (!(0, referencePositions_1.refTypeIncludesFlag)(ref, ops_1.ReferenceType.Transient)) {
257
- this.addLocalRef(ref);
195
+ this.addLocalRef(ref, offset);
258
196
  }
259
197
  return ref;
260
198
  }
261
199
  /**
262
- *
263
- * @internal - this method should only be called by mergeTree
200
+ * @remarks This method should only be called by mergeTree.
201
+ * @internal
264
202
  */
265
- addLocalRef(lref) {
203
+ addLocalRef(lref, offset) {
266
204
  var _a, _b;
267
205
  (0, common_utils_1.assert)(!(0, referencePositions_1.refTypeIncludesFlag)(lref, ops_1.ReferenceType.Transient), 0x2df /* "transient references cannot be bound to segments" */);
268
206
  assertLocalReferences(lref);
269
- const refsAtOffset = this.refsByOffset[lref.offset] =
270
- (_a = this.refsByOffset[lref.offset]) !== null && _a !== void 0 ? _a : { at: (0, collections_1.ListMakeHead)() };
207
+ (0, common_utils_1.assert)(offset < this.segment.cachedLength, 0x348 /* offset cannot be beyond segment length */);
208
+ const refsAtOffset = this.refsByOffset[offset] =
209
+ (_a = this.refsByOffset[offset]) !== null && _a !== void 0 ? _a : { at: (0, collections_1.ListMakeHead)() };
271
210
  const atRefs = refsAtOffset.at =
272
211
  (_b = refsAtOffset.at) !== null && _b !== void 0 ? _b : (0, collections_1.ListMakeHead)();
273
- atRefs.enqueue(lref);
212
+ lref.link(this.segment, offset, atRefs.enqueue(lref));
274
213
  if ((0, referencePositions_1.refHasRangeLabels)(lref) || (0, referencePositions_1.refHasTileLabels)(lref)) {
275
214
  this.hierRefCount++;
276
215
  }
277
216
  this.refCount++;
278
217
  }
279
218
  /**
280
- *
281
- * @internal - this method should only be called by mergeTree
219
+ * @remarks This method should only be called by mergeTree.
220
+ * @internal
282
221
  */
283
222
  removeLocalRef(lref) {
284
- assertLocalReferences(lref);
285
- const tryRemoveRef = (refs) => {
286
- if (refs) {
287
- let node = refs;
288
- do {
289
- node = node.next;
290
- if (node.data === lref) {
291
- (0, collections_1.ListRemoveEntry)(node);
292
- if ((0, referencePositions_1.refHasRangeLabels)(lref) || (0, referencePositions_1.refHasTileLabels)(lref)) {
293
- this.hierRefCount--;
294
- }
295
- this.refCount--;
296
- return lref;
297
- }
298
- } while (!node.isHead);
299
- }
300
- };
301
- const refAtOffset = this.refsByOffset[lref.offset];
302
- if (refAtOffset !== undefined) {
303
- let ref = tryRemoveRef(refAtOffset.before);
304
- if (ref) {
305
- return ref;
306
- }
307
- ref = tryRemoveRef(refAtOffset.at);
308
- if (ref) {
309
- return ref;
310
- }
311
- ref = tryRemoveRef(refAtOffset.after);
312
- if (ref) {
313
- return ref;
223
+ if (this.has(lref)) {
224
+ assertLocalReferences(lref);
225
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
226
+ (0, collections_1.ListRemoveEntry)(lref.getListNode());
227
+ lref.link(lref.getSegment(), lref.getOffset(), undefined);
228
+ if ((0, referencePositions_1.refHasRangeLabels)(lref) || (0, referencePositions_1.refHasTileLabels)(lref)) {
229
+ this.hierRefCount--;
314
230
  }
231
+ this.refCount--;
232
+ return lref;
315
233
  }
316
234
  }
317
235
  /**
318
- * @internal - this method should only be called by mergeTree
319
236
  *
320
237
  * Called by 'append()' implementations to append local refs from the given 'other' segment to the
321
238
  * end of 'this' segment.
322
239
  *
323
240
  * Note: This method should be invoked after the caller has ensured that segments can be merged,
324
- * but before 'this' segment's cachedLength has changed, or the adjustment to the local refs
325
- * will be incorrect.
241
+ * but before 'this' segment's cachedLength has changed, or the adjustment to the local refs
242
+ * will be incorrect.
243
+ *
244
+ * @remarks This method should only be called by mergeTree.
245
+ * @internal
326
246
  */
327
247
  append(other) {
328
248
  if (!other || other.empty) {
@@ -332,14 +252,52 @@ class LocalReferenceCollection {
332
252
  this.refCount += other.refCount;
333
253
  other.hierRefCount = 0;
334
254
  for (const lref of other) {
335
- lref.segment = this.segment;
336
- lref.offset += this.refsByOffset.length;
255
+ assertLocalReferences(lref);
256
+ lref.link(this.segment, lref.getOffset() + this.refsByOffset.length, lref.getListNode());
337
257
  }
338
258
  this.refsByOffset.push(...other.refsByOffset);
339
259
  }
340
260
  /**
341
- * @internal - this method should only be called by mergeTree
261
+ * Returns true of the local reference is in the collection, otherwise false.
342
262
  *
263
+ * @remarks This method should only be called by mergeTree.
264
+ * @internal
265
+ */
266
+ has(lref) {
267
+ if (!(lref instanceof LocalReference)
268
+ || (0, referencePositions_1.refTypeIncludesFlag)(lref, ops_1.ReferenceType.Transient)) {
269
+ return false;
270
+ }
271
+ const seg = lref.getSegment();
272
+ if (seg !== this.segment) {
273
+ return false;
274
+ }
275
+ // we should be able to optimize finding the
276
+ // list head
277
+ const listNode = lref.getListNode();
278
+ if (listNode === undefined) {
279
+ return false;
280
+ }
281
+ let prev = listNode;
282
+ let next = listNode;
283
+ while ((prev === null || prev === void 0 ? void 0 : prev.isHead) !== true && (next === null || next === void 0 ? void 0 : next.isHead) !== true) {
284
+ prev = prev === null || prev === void 0 ? void 0 : prev.prev;
285
+ next = next === null || next === void 0 ? void 0 : next.next;
286
+ }
287
+ const headNode = (prev === null || prev === void 0 ? void 0 : prev.isHead) === true ? prev : next;
288
+ if ((headNode === null || headNode === void 0 ? void 0 : headNode.isHead) !== true || headNode.empty()) {
289
+ return false;
290
+ }
291
+ const offset = lref.getOffset();
292
+ const refsAtOffset = this.refsByOffset[offset];
293
+ if ((refsAtOffset === null || refsAtOffset === void 0 ? void 0 : refsAtOffset.before) === headNode
294
+ || (refsAtOffset === null || refsAtOffset === void 0 ? void 0 : refsAtOffset.at) === headNode
295
+ || (refsAtOffset === null || refsAtOffset === void 0 ? void 0 : refsAtOffset.after) === headNode) {
296
+ return true;
297
+ }
298
+ return false;
299
+ }
300
+ /**
343
301
  * Splits this `LocalReferenceCollection` into the intervals [0, offset) and [offset, originalLength).
344
302
  * Local references in the former half of this split will remain associated with the segment used on construction.
345
303
  * Local references in the latter half of this split will be transferred to `splitSeg`,
@@ -347,14 +305,17 @@ class LocalReferenceCollection {
347
305
  * @param offset - Offset into the original segment at which the collection should be split
348
306
  * @param splitSeg - Split segment which originally corresponded to the indices [offset, originalLength)
349
307
  * before splitting.
308
+ *
309
+ * @remarks This method should only be called by mergeTree.
310
+ * @internal
350
311
  */
351
312
  split(offset, splitSeg) {
352
313
  if (!this.empty) {
353
314
  const localRefs = new LocalReferenceCollection(splitSeg, this.refsByOffset.splice(offset, this.refsByOffset.length - offset));
354
315
  splitSeg.localRefs = localRefs;
355
316
  for (const lref of localRefs) {
356
- lref.segment = splitSeg;
357
- lref.offset -= offset;
317
+ assertLocalReferences(lref);
318
+ lref.link(splitSeg, lref.getOffset() - offset, lref.getListNode());
358
319
  if ((0, referencePositions_1.refHasRangeLabels)(lref) || (0, referencePositions_1.refHasTileLabels)(lref)) {
359
320
  this.hierRefCount--;
360
321
  localRefs.hierRefCount++;
@@ -369,57 +330,59 @@ class LocalReferenceCollection {
369
330
  }
370
331
  }
371
332
  addBeforeTombstones(...refs) {
372
- var _a, _b, _c, _d, _e;
333
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
334
+ var _k;
373
335
  const beforeRefs = (_b = (_a = this.refsByOffset[0]) === null || _a === void 0 ? void 0 : _a.before) !== null && _b !== void 0 ? _b : (0, collections_1.ListMakeHead)();
336
+ if (((_c = this.refsByOffset[0]) === null || _c === void 0 ? void 0 : _c.before) === undefined) {
337
+ const refsAtOffset = (_d = (_k = this.refsByOffset)[0]) !== null && _d !== void 0 ? _d : (_k[0] = { before: beforeRefs });
338
+ (_e = refsAtOffset.before) !== null && _e !== void 0 ? _e : (refsAtOffset.before = beforeRefs);
339
+ }
374
340
  for (const iterable of refs) {
375
341
  for (const lref of iterable) {
376
342
  assertLocalReferences(lref);
377
343
  if ((0, referencePositions_1.refTypeIncludesFlag)(lref, ops_1.ReferenceType.SlideOnRemove)) {
378
- beforeRefs.push(lref);
379
- lref.segment = this.segment;
380
- lref.offset = 0;
344
+ (_g = (_f = lref.callbacks) === null || _f === void 0 ? void 0 : _f.beforeSlide) === null || _g === void 0 ? void 0 : _g.call(_f);
345
+ beforeRefs.unshift(lref);
346
+ lref.link(this.segment, 0, beforeRefs.next);
381
347
  if ((0, referencePositions_1.refHasRangeLabels)(lref) || (0, referencePositions_1.refHasTileLabels)(lref)) {
382
348
  this.hierRefCount++;
383
349
  }
384
350
  this.refCount++;
351
+ (_j = (_h = lref.callbacks) === null || _h === void 0 ? void 0 : _h.afterSlide) === null || _j === void 0 ? void 0 : _j.call(_h);
385
352
  }
386
353
  else {
387
- lref.segment = undefined;
354
+ lref.link(undefined, 0, undefined);
388
355
  }
389
356
  }
390
357
  }
391
- if (!beforeRefs.empty() && ((_c = this.refsByOffset[0]) === null || _c === void 0 ? void 0 : _c.before) === undefined) {
392
- const refsAtOffset = this.refsByOffset[0] =
393
- (_d = this.refsByOffset[0]) !== null && _d !== void 0 ? _d : { before: beforeRefs };
394
- refsAtOffset.before = (_e = refsAtOffset.before) !== null && _e !== void 0 ? _e : beforeRefs;
395
- }
396
358
  }
397
359
  addAfterTombstones(...refs) {
398
- var _a, _b, _c, _d, _e;
399
- const lastOffset = this.refsByOffset.length - 1;
360
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
361
+ var _k;
362
+ const lastOffset = this.segment.cachedLength - 1;
400
363
  const afterRefs = (_b = (_a = this.refsByOffset[lastOffset]) === null || _a === void 0 ? void 0 : _a.after) !== null && _b !== void 0 ? _b : (0, collections_1.ListMakeHead)();
364
+ if (((_c = this.refsByOffset[lastOffset]) === null || _c === void 0 ? void 0 : _c.after) === undefined) {
365
+ const refsAtOffset = (_d = (_k = this.refsByOffset)[lastOffset]) !== null && _d !== void 0 ? _d : (_k[lastOffset] = { after: afterRefs });
366
+ (_e = refsAtOffset.after) !== null && _e !== void 0 ? _e : (refsAtOffset.after = afterRefs);
367
+ }
401
368
  for (const iterable of refs) {
402
369
  for (const lref of iterable) {
403
370
  assertLocalReferences(lref);
404
371
  if ((0, referencePositions_1.refTypeIncludesFlag)(lref, ops_1.ReferenceType.SlideOnRemove)) {
405
- afterRefs.push(lref);
406
- lref.segment = this.segment;
407
- lref.offset = this.segment.cachedLength - 1;
372
+ (_g = (_f = lref.callbacks) === null || _f === void 0 ? void 0 : _f.beforeSlide) === null || _g === void 0 ? void 0 : _g.call(_f);
373
+ afterRefs.enqueue(lref);
374
+ lref.link(this.segment, lastOffset, afterRefs.prev);
408
375
  if ((0, referencePositions_1.refHasRangeLabels)(lref) || (0, referencePositions_1.refHasTileLabels)(lref)) {
409
376
  this.hierRefCount++;
410
377
  }
411
378
  this.refCount++;
379
+ (_j = (_h = lref.callbacks) === null || _h === void 0 ? void 0 : _h.afterSlide) === null || _j === void 0 ? void 0 : _j.call(_h);
412
380
  }
413
381
  else {
414
- lref.segment = undefined;
382
+ lref.link(undefined, 0, undefined);
415
383
  }
416
384
  }
417
385
  }
418
- if (!afterRefs.empty() && ((_c = this.refsByOffset[lastOffset]) === null || _c === void 0 ? void 0 : _c.after) === undefined) {
419
- const refsAtOffset = this.refsByOffset[lastOffset] =
420
- (_d = this.refsByOffset[lastOffset]) !== null && _d !== void 0 ? _d : { after: afterRefs };
421
- refsAtOffset.after = (_e = refsAtOffset.after) !== null && _e !== void 0 ? _e : afterRefs;
422
- }
423
386
  }
424
387
  }
425
388
  exports.LocalReferenceCollection = LocalReferenceCollection;
@@ -1 +1 @@
1
- {"version":3,"file":"localReference.js","sourceRoot":"","sources":["../src/localReference.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,+DAAsD;AACtD,qEAA6D;AAE7D,+CAAoE;AAIpE,+BAAoD;AACpD,6CAA0D;AAC1D,6DAY8B;AAE9B;;GAEG;AACH,SAAgB,sBAAsB,CAAC,OAAsB;IACzD,IAAI,cAAc,GAAG,CAAC,CAAC;IACvB,IAAI,IAAA,wCAAmB,EAAC,OAAO,EAAE,mBAAa,CAAC,SAAS,CAAC,EAAE;QACvD,EAAE,cAAc,CAAC;KACpB;IACD,IAAI,IAAA,wCAAmB,EAAC,OAAO,EAAE,mBAAa,CAAC,aAAa,CAAC,EAAE;QAC3D,EAAE,cAAc,CAAC;KACpB;IACD,IAAI,IAAA,wCAAmB,EAAC,OAAO,EAAE,mBAAa,CAAC,YAAY,CAAC,EAA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ved.\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,+DAAsD;AACtD,qEAA6D;AAC7D,+CAAoE;AAIpE,+BAAoD;AACpD,6CAA0D;AAC1D,6DAK8B;AAE9B;;GAEG;AACH,SAAgB,sBAAsB,CAAC,OAAsB;IACzD,IAAI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,UAAU,IAAM,EAAE,KAAK,EAAE,SAAS,EAAE,CAAA,CAAC;YAC5E,MAAA,YAAY,CAAC,KAAK,oCAAlB,YAAY,CAAC,KAAK,GAAK,SAAS,EAAC;SACpC;QAED,KAAK,MAAM,QAAQ,IAAI,IAAI,EAAE;YACzB,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE;gBACzB,qBAAqB,CAAC,IAAI,CAAC,CAAC;gBAC5B,IAAI,IAAA,wCAAmB,EAAC,IAAI,EAAE,mBAAa,CAAC,aAAa,CAAC,EAAE;oBACxD,MAAA,MAAA,IAAI,CAAC,SAAS,0CAAE,WAAW,kDAAI,CAAC;oBAChC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;oBACxB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC;oBACpD,IAAI,IAAA,sCAAiB,EAAC,IAAI,CAAC,IAAI,IAAA,qCAAgB,EAAC,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;AApVD,4DAoVC","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"]}