@fluidframework/sequence 2.0.0-internal.5.3.2 → 2.0.0-internal.5.4.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 (179) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/index.d.ts +4 -3
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +12 -9
  5. package/dist/index.js.map +1 -1
  6. package/dist/intervalCollection.d.ts +46 -410
  7. package/dist/intervalCollection.d.ts.map +1 -1
  8. package/dist/intervalCollection.js +44 -732
  9. package/dist/intervalCollection.js.map +1 -1
  10. package/dist/intervalIndex/endpointInRangeIndex.d.ts +20 -0
  11. package/dist/intervalIndex/endpointInRangeIndex.d.ts.map +1 -0
  12. package/dist/intervalIndex/endpointInRangeIndex.js +60 -0
  13. package/dist/intervalIndex/endpointInRangeIndex.js.map +1 -0
  14. package/dist/intervalIndex/endpointIndex.d.ts +21 -0
  15. package/dist/intervalIndex/endpointIndex.d.ts.map +1 -0
  16. package/dist/intervalIndex/endpointIndex.js +42 -0
  17. package/dist/intervalIndex/endpointIndex.js.map +1 -0
  18. package/dist/intervalIndex/idIntervalIndex.d.ts +12 -0
  19. package/dist/intervalIndex/idIntervalIndex.d.ts.map +1 -0
  20. package/dist/intervalIndex/idIntervalIndex.js +41 -0
  21. package/dist/intervalIndex/idIntervalIndex.js.map +1 -0
  22. package/dist/intervalIndex/index.d.ts +5 -0
  23. package/dist/intervalIndex/index.d.ts.map +1 -1
  24. package/dist/intervalIndex/index.js +9 -1
  25. package/dist/intervalIndex/index.js.map +1 -1
  26. package/dist/intervalIndex/intervalIndex.d.ts +29 -0
  27. package/dist/intervalIndex/intervalIndex.d.ts.map +1 -0
  28. package/dist/intervalIndex/intervalIndex.js +7 -0
  29. package/dist/intervalIndex/intervalIndex.js.map +1 -0
  30. package/dist/intervalIndex/intervalIndexUtils.d.ts +17 -0
  31. package/dist/intervalIndex/intervalIndexUtils.d.ts.map +1 -0
  32. package/dist/intervalIndex/intervalIndexUtils.js +23 -0
  33. package/dist/intervalIndex/intervalIndexUtils.js.map +1 -0
  34. package/dist/intervalIndex/overlappingIntervalsIndex.d.ts +2 -1
  35. package/dist/intervalIndex/overlappingIntervalsIndex.d.ts.map +1 -1
  36. package/dist/intervalIndex/overlappingIntervalsIndex.js +3 -3
  37. package/dist/intervalIndex/overlappingIntervalsIndex.js.map +1 -1
  38. package/dist/intervalIndex/overlappingSequenceIntervalsIndex.js +5 -5
  39. package/dist/intervalIndex/overlappingSequenceIntervalsIndex.js.map +1 -1
  40. package/dist/intervalIndex/sequenceIntervalIndexes.d.ts +1 -1
  41. package/dist/intervalIndex/sequenceIntervalIndexes.d.ts.map +1 -1
  42. package/dist/intervalIndex/sequenceIntervalIndexes.js.map +1 -1
  43. package/dist/intervalIndex/startpointInRangeIndex.d.ts +20 -0
  44. package/dist/intervalIndex/startpointInRangeIndex.d.ts.map +1 -0
  45. package/dist/intervalIndex/startpointInRangeIndex.js +62 -0
  46. package/dist/intervalIndex/startpointInRangeIndex.js.map +1 -0
  47. package/dist/intervalTree.d.ts +1 -47
  48. package/dist/intervalTree.d.ts.map +1 -1
  49. package/dist/intervalTree.js.map +1 -1
  50. package/dist/intervals/index.d.ts +8 -0
  51. package/dist/intervals/index.d.ts.map +1 -0
  52. package/dist/intervals/index.js +23 -0
  53. package/dist/intervals/index.js.map +1 -0
  54. package/dist/intervals/interval.d.ts +88 -0
  55. package/dist/intervals/interval.d.ts.map +1 -0
  56. package/dist/intervals/interval.js +182 -0
  57. package/dist/intervals/interval.js.map +1 -0
  58. package/dist/intervals/intervalUtils.d.ts +194 -0
  59. package/dist/intervals/intervalUtils.d.ts.map +1 -0
  60. package/dist/intervals/intervalUtils.js +79 -0
  61. package/dist/intervals/intervalUtils.js.map +1 -0
  62. package/dist/intervals/sequenceInterval.d.ts +132 -0
  63. package/dist/intervals/sequenceInterval.d.ts.map +1 -0
  64. package/dist/intervals/sequenceInterval.js +315 -0
  65. package/dist/intervals/sequenceInterval.js.map +1 -0
  66. package/dist/packageVersion.d.ts +1 -1
  67. package/dist/packageVersion.js +1 -1
  68. package/dist/packageVersion.js.map +1 -1
  69. package/dist/revertibles.d.ts +1 -1
  70. package/dist/revertibles.d.ts.map +1 -1
  71. package/dist/revertibles.js +36 -29
  72. package/dist/revertibles.js.map +1 -1
  73. package/dist/sequence.d.ts +2 -1
  74. package/dist/sequence.d.ts.map +1 -1
  75. package/dist/sequence.js +4 -1
  76. package/dist/sequence.js.map +1 -1
  77. package/dist/sharedIntervalCollection.d.ts +2 -1
  78. package/dist/sharedIntervalCollection.d.ts.map +1 -1
  79. package/dist/sharedIntervalCollection.js.map +1 -1
  80. package/lib/index.d.ts +4 -3
  81. package/lib/index.d.ts.map +1 -1
  82. package/lib/index.js +3 -2
  83. package/lib/index.js.map +1 -1
  84. package/lib/intervalCollection.d.ts +46 -410
  85. package/lib/intervalCollection.d.ts.map +1 -1
  86. package/lib/intervalCollection.js +25 -705
  87. package/lib/intervalCollection.js.map +1 -1
  88. package/lib/intervalIndex/endpointInRangeIndex.d.ts +20 -0
  89. package/lib/intervalIndex/endpointInRangeIndex.d.ts.map +1 -0
  90. package/lib/intervalIndex/endpointInRangeIndex.js +56 -0
  91. package/lib/intervalIndex/endpointInRangeIndex.js.map +1 -0
  92. package/lib/intervalIndex/endpointIndex.d.ts +21 -0
  93. package/lib/intervalIndex/endpointIndex.d.ts.map +1 -0
  94. package/lib/intervalIndex/endpointIndex.js +38 -0
  95. package/lib/intervalIndex/endpointIndex.js.map +1 -0
  96. package/lib/intervalIndex/idIntervalIndex.d.ts +12 -0
  97. package/lib/intervalIndex/idIntervalIndex.d.ts.map +1 -0
  98. package/lib/intervalIndex/idIntervalIndex.js +37 -0
  99. package/lib/intervalIndex/idIntervalIndex.js.map +1 -0
  100. package/lib/intervalIndex/index.d.ts +5 -0
  101. package/lib/intervalIndex/index.d.ts.map +1 -1
  102. package/lib/intervalIndex/index.js +4 -0
  103. package/lib/intervalIndex/index.js.map +1 -1
  104. package/lib/intervalIndex/intervalIndex.d.ts +29 -0
  105. package/lib/intervalIndex/intervalIndex.d.ts.map +1 -0
  106. package/lib/intervalIndex/intervalIndex.js +6 -0
  107. package/lib/intervalIndex/intervalIndex.js.map +1 -0
  108. package/lib/intervalIndex/intervalIndexUtils.d.ts +17 -0
  109. package/lib/intervalIndex/intervalIndexUtils.d.ts.map +1 -0
  110. package/lib/intervalIndex/intervalIndexUtils.js +19 -0
  111. package/lib/intervalIndex/intervalIndexUtils.js.map +1 -0
  112. package/lib/intervalIndex/overlappingIntervalsIndex.d.ts +2 -1
  113. package/lib/intervalIndex/overlappingIntervalsIndex.d.ts.map +1 -1
  114. package/lib/intervalIndex/overlappingIntervalsIndex.js +1 -1
  115. package/lib/intervalIndex/overlappingIntervalsIndex.js.map +1 -1
  116. package/lib/intervalIndex/overlappingSequenceIntervalsIndex.js +1 -1
  117. package/lib/intervalIndex/overlappingSequenceIntervalsIndex.js.map +1 -1
  118. package/lib/intervalIndex/sequenceIntervalIndexes.d.ts +1 -1
  119. package/lib/intervalIndex/sequenceIntervalIndexes.d.ts.map +1 -1
  120. package/lib/intervalIndex/sequenceIntervalIndexes.js.map +1 -1
  121. package/lib/intervalIndex/startpointInRangeIndex.d.ts +20 -0
  122. package/lib/intervalIndex/startpointInRangeIndex.d.ts.map +1 -0
  123. package/lib/intervalIndex/startpointInRangeIndex.js +58 -0
  124. package/lib/intervalIndex/startpointInRangeIndex.js.map +1 -0
  125. package/lib/intervalTree.d.ts +1 -47
  126. package/lib/intervalTree.d.ts.map +1 -1
  127. package/lib/intervalTree.js.map +1 -1
  128. package/lib/intervals/index.d.ts +8 -0
  129. package/lib/intervals/index.d.ts.map +1 -0
  130. package/lib/intervals/index.js +8 -0
  131. package/lib/intervals/index.js.map +1 -0
  132. package/lib/intervals/interval.d.ts +88 -0
  133. package/lib/intervals/interval.d.ts.map +1 -0
  134. package/lib/intervals/interval.js +177 -0
  135. package/lib/intervals/interval.js.map +1 -0
  136. package/lib/intervals/intervalUtils.d.ts +194 -0
  137. package/lib/intervals/intervalUtils.d.ts.map +1 -0
  138. package/lib/intervals/intervalUtils.js +74 -0
  139. package/lib/intervals/intervalUtils.js.map +1 -0
  140. package/lib/intervals/sequenceInterval.d.ts +132 -0
  141. package/lib/intervals/sequenceInterval.d.ts.map +1 -0
  142. package/lib/intervals/sequenceInterval.js +307 -0
  143. package/lib/intervals/sequenceInterval.js.map +1 -0
  144. package/lib/packageVersion.d.ts +1 -1
  145. package/lib/packageVersion.js +1 -1
  146. package/lib/packageVersion.js.map +1 -1
  147. package/lib/revertibles.d.ts +1 -1
  148. package/lib/revertibles.d.ts.map +1 -1
  149. package/lib/revertibles.js +21 -14
  150. package/lib/revertibles.js.map +1 -1
  151. package/lib/sequence.d.ts +2 -1
  152. package/lib/sequence.d.ts.map +1 -1
  153. package/lib/sequence.js +5 -2
  154. package/lib/sequence.js.map +1 -1
  155. package/lib/sharedIntervalCollection.d.ts +2 -1
  156. package/lib/sharedIntervalCollection.d.ts.map +1 -1
  157. package/lib/sharedIntervalCollection.js.map +1 -1
  158. package/package.json +15 -15
  159. package/src/index.ts +19 -12
  160. package/src/intervalCollection.ts +75 -1204
  161. package/src/intervalIndex/endpointInRangeIndex.ts +104 -0
  162. package/src/intervalIndex/endpointIndex.ts +78 -0
  163. package/src/intervalIndex/idIntervalIndex.ts +58 -0
  164. package/src/intervalIndex/index.ts +5 -0
  165. package/src/intervalIndex/intervalIndex.ts +31 -0
  166. package/src/intervalIndex/intervalIndexUtils.ts +27 -0
  167. package/src/intervalIndex/overlappingIntervalsIndex.ts +2 -6
  168. package/src/intervalIndex/overlappingSequenceIntervalsIndex.ts +1 -1
  169. package/src/intervalIndex/sequenceIntervalIndexes.ts +1 -1
  170. package/src/intervalIndex/startpointInRangeIndex.ts +109 -0
  171. package/src/intervalTree.ts +1 -54
  172. package/src/intervals/index.ts +25 -0
  173. package/src/intervals/interval.ts +230 -0
  174. package/src/intervals/intervalUtils.ts +249 -0
  175. package/src/intervals/sequenceInterval.ts +494 -0
  176. package/src/packageVersion.ts +1 -1
  177. package/src/revertibles.ts +23 -12
  178. package/src/sequence.ts +6 -3
  179. package/src/sharedIntervalCollection.ts +1 -2
@@ -16,39 +16,12 @@ var __rest = (this && this.__rest) || function (s, e) {
16
16
  /* eslint-disable no-bitwise */
17
17
  import { assert, TypedEventEmitter } from "@fluidframework/common-utils";
18
18
  import { UsageError } from "@fluidframework/container-utils";
19
- import { addProperties, compareReferencePositions, createMap, getSlideToSegoff, MergeTreeDeltaType, minReferencePosition, PropertiesManager, RedBlackTree, ReferenceType, refTypeIncludesFlag, reservedRangeLabelsKey, UnassignedSequenceNumber, maxReferencePosition, createDetachedLocalReferencePosition, DetachedReferencePosition, SlidingPreference, } from "@fluidframework/merge-tree";
19
+ import { addProperties, createMap, getSlideToSegoff, MergeTreeDeltaType, ReferenceType, refTypeIncludesFlag, reservedRangeLabelsKey, UnassignedSequenceNumber, DetachedReferencePosition, } from "@fluidframework/merge-tree";
20
20
  import { LoggingError } from "@fluidframework/telemetry-utils";
21
21
  import { v4 as uuid } from "uuid";
22
- import { createOverlappingIntervalsIndex } from "./intervalIndex";
22
+ import { IntervalOpType, IntervalStickiness, IntervalType, SequenceInterval, createPositionReferenceFromSegoff, endReferenceSlidingPreference, startReferenceSlidingPreference, sequenceIntervalHelpers, createInterval, } from "./intervals";
23
+ import { createEndpointIndex, createIdIntervalIndex, createOverlappingIntervalsIndex, } from "./intervalIndex";
23
24
  const reservedIntervalIdKey = "intervalId";
24
- /**
25
- * Values are used in persisted formats (ops) and revertibles.
26
- * @alpha
27
- */
28
- export const IntervalOpType = {
29
- ADD: "add",
30
- DELETE: "delete",
31
- CHANGE: "change",
32
- PROPERTY_CHANGED: "propertyChanged",
33
- POSITION_REMOVE: "positionRemove",
34
- };
35
- export var IntervalType;
36
- (function (IntervalType) {
37
- IntervalType[IntervalType["Simple"] = 0] = "Simple";
38
- IntervalType[IntervalType["Nest"] = 1] = "Nest";
39
- /**
40
- * SlideOnRemove indicates that the ends of the interval will slide if the segment
41
- * they reference is removed and acked.
42
- * See `packages\dds\merge-tree\docs\REFERENCEPOSITIONS.md` for details
43
- * SlideOnRemove is the default interval behavior and does not need to be specified.
44
- */
45
- IntervalType[IntervalType["SlideOnRemove"] = 2] = "SlideOnRemove";
46
- /**
47
- * A temporary interval, used internally
48
- * @internal
49
- */
50
- IntervalType[IntervalType["Transient"] = 4] = "Transient";
51
- })(IntervalType || (IntervalType = {}));
52
25
  /**
53
26
  * Decompress an interval after loading a summary from JSON. The exact format
54
27
  * of this compression is unspecified and subject to change
@@ -81,665 +54,14 @@ function compressInterval(interval) {
81
54
  }
82
55
  return base;
83
56
  }
84
- function startReferenceSlidingPreference(stickiness) {
85
- // if any start stickiness, prefer sliding backwards
86
- return (stickiness & IntervalStickiness.START) !== 0
87
- ? SlidingPreference.BACKWARD
88
- : SlidingPreference.FORWARD;
89
- }
90
- function endReferenceSlidingPreference(stickiness) {
91
- // if any end stickiness, prefer sliding forwards
92
- return (stickiness & IntervalStickiness.END) !== 0
93
- ? SlidingPreference.FORWARD
94
- : SlidingPreference.BACKWARD;
95
- }
96
- /**
97
- * Determines how an interval should expand when segments are inserted adjacent
98
- * to the range it spans
99
- *
100
- * Note that interval stickiness is currently an experimental feature and must
101
- * be explicitly enabled with the `intervalStickinessEnabled` flag
102
- */
103
- export const IntervalStickiness = {
104
- /**
105
- * Interval does not expand to include adjacent segments
106
- */
107
- NONE: 0b00,
108
- /**
109
- * Interval expands to include segments inserted adjacent to the start
110
- */
111
- START: 0b01,
112
- /**
113
- * Interval expands to include segments inserted adjacent to the end
114
- *
115
- * This is the default stickiness
116
- */
117
- END: 0b10,
118
- /**
119
- * Interval expands to include all segments inserted adjacent to it
120
- */
121
- FULL: 0b11,
122
- };
123
- /**
124
- * Serializable interval whose endpoints are plain-old numbers.
125
- */
126
- export class Interval {
127
- constructor(start, end, props) {
128
- this.start = start;
129
- this.end = end;
130
- this.propertyManager = new PropertiesManager();
131
- this.properties = {};
132
- if (props) {
133
- this.addProperties(props);
134
- }
135
- }
136
- /**
137
- * {@inheritDoc ISerializableInterval.getIntervalId}
138
- */
139
- getIntervalId() {
140
- var _a;
141
- const id = (_a = this.properties) === null || _a === void 0 ? void 0 : _a[reservedIntervalIdKey];
142
- assert(id !== undefined, 0x5e1 /* interval ID should not be undefined */);
143
- return `${id}`;
144
- }
145
- /**
146
- * @returns an array containing any auxiliary property sets added with `addPropertySet`.
147
- */
148
- getAdditionalPropertySets() {
149
- var _a;
150
- return (_a = this.auxProps) !== null && _a !== void 0 ? _a : [];
151
- }
152
- /**
153
- * Adds an auxiliary set of properties to this interval.
154
- * These properties can be recovered using `getAdditionalPropertySets`
155
- * @param props - set of properties to add
156
- * @remarks - This gets called as part of the default conflict resolver for `IIntervalCollection<Interval>`
157
- * (i.e. non-sequence-based interval collections). However, the additional properties don't get serialized.
158
- * This functionality seems half-baked.
159
- */
160
- addPropertySet(props) {
161
- if (this.auxProps === undefined) {
162
- this.auxProps = [];
163
- }
164
- this.auxProps.push(props);
165
- }
166
- /**
167
- * {@inheritDoc ISerializableInterval.serialize}
168
- * @internal
169
- */
170
- serialize() {
171
- const serializedInterval = {
172
- end: this.end,
173
- intervalType: 0,
174
- sequenceNumber: 0,
175
- start: this.start,
176
- };
177
- if (this.properties) {
178
- serializedInterval.properties = this.properties;
179
- }
180
- return serializedInterval;
181
- }
182
- /**
183
- * {@inheritDoc IInterval.clone}
184
- */
185
- clone() {
186
- return new Interval(this.start, this.end, this.properties);
187
- }
188
- /**
189
- * {@inheritDoc IInterval.compare}
190
- */
191
- compare(b) {
192
- const startResult = this.compareStart(b);
193
- if (startResult === 0) {
194
- const endResult = this.compareEnd(b);
195
- if (endResult === 0) {
196
- const thisId = this.getIntervalId();
197
- if (thisId) {
198
- const bId = b.getIntervalId();
199
- if (bId) {
200
- return thisId > bId ? 1 : thisId < bId ? -1 : 0;
201
- }
202
- return 0;
203
- }
204
- return 0;
205
- }
206
- else {
207
- return endResult;
208
- }
209
- }
210
- else {
211
- return startResult;
212
- }
213
- }
214
- /**
215
- * {@inheritDoc IInterval.compareStart}
216
- */
217
- compareStart(b) {
218
- return this.start - b.start;
219
- }
220
- /**
221
- * {@inheritDoc IInterval.compareEnd}
222
- */
223
- compareEnd(b) {
224
- return this.end - b.end;
225
- }
226
- /**
227
- * {@inheritDoc IInterval.overlaps}
228
- */
229
- overlaps(b) {
230
- const result = this.start <= b.end && this.end >= b.start;
231
- return result;
232
- }
233
- /**
234
- * {@inheritDoc IInterval.union}
235
- * @deprecated - This API was never intended to be public and will be marked internal in a future release.
236
- */
237
- union(b) {
238
- return new Interval(Math.min(this.start, b.start), Math.max(this.end, b.end), this.properties);
239
- }
240
- getProperties() {
241
- return this.properties;
242
- }
243
- /**
244
- * {@inheritDoc ISerializableInterval.addProperties}
245
- * @deprecated - This API was never intended to be public and will be marked internal in a future release.
246
- */
247
- addProperties(newProps, collaborating = false, seq, op) {
248
- if (newProps) {
249
- this.initializeProperties();
250
- return this.propertyManager.addProperties(this.properties, newProps, op, seq, collaborating);
251
- }
252
- }
253
- /**
254
- * {@inheritDoc IInterval.modify}
255
- * @deprecated - This API was never intended to be public and will be marked internal in a future release.
256
- */
257
- modify(label, start, end, op) {
258
- const startPos = start !== null && start !== void 0 ? start : this.start;
259
- const endPos = end !== null && end !== void 0 ? end : this.end;
260
- if (this.start === startPos && this.end === endPos) {
261
- // Return undefined to indicate that no change is necessary.
262
- return;
263
- }
264
- const newInterval = new Interval(startPos, endPos);
265
- if (this.properties) {
266
- newInterval.initializeProperties();
267
- this.propertyManager.copyTo(this.properties, newInterval.properties, newInterval.propertyManager);
268
- }
269
- return newInterval;
270
- }
271
- initializeProperties() {
272
- if (!this.propertyManager) {
273
- this.propertyManager = new PropertiesManager();
274
- }
275
- if (!this.properties) {
276
- this.properties = createMap();
277
- }
278
- }
279
- }
280
- /**
281
- * Interval implementation whose ends are associated with positions in a mutatable sequence.
282
- * As such, when content is inserted into the middle of the interval, the interval expands to
283
- * include that content.
284
- *
285
- * @remarks - The endpoint's position should be treated exclusively to get reasonable behavior--i.e.
286
- * an interval referring to "hello" in "hello world" should have a start position of 0 and an end
287
- * position of 5.
288
- *
289
- * To see why, consider what happens if "llo wor" is removed from the string to make "held".
290
- * The interval's startpoint remains on the "h" (it isn't altered), but the interval's endpoint
291
- * slides forward to the next unremoved position, which is the "l" in "held".
292
- * Users would generally expect the interval to now refer to "he" (as it is the subset of content
293
- * remaining after the removal), hence the "l" should be excluded.
294
- * If the interval endpoint was treated inclusively, the interval would now refer to "hel", which
295
- * is undesirable.
296
- *
297
- * Since the end of an interval is treated exclusively but cannot be greater than or equal to the
298
- * length of the associated sequence, application models which leverage interval collections should
299
- * consider inserting a marker at the end of the sequence to represent the end of the content.
300
- */
301
- export class SequenceInterval {
302
- constructor(client,
303
- /**
304
- * Start endpoint of this interval.
305
- * @remarks - This endpoint can be resolved into a character position using the SharedString it's a part of.
306
- */
307
- start,
308
- /**
309
- * End endpoint of this interval.
310
- * @remarks - This endpoint can be resolved into a character position using the SharedString it's a part of.
311
- */
312
- end, intervalType, props, stickiness = IntervalStickiness.END) {
313
- this.client = client;
314
- this.start = start;
315
- this.end = end;
316
- this.intervalType = intervalType;
317
- this.stickiness = stickiness;
318
- this.propertyManager = new PropertiesManager();
319
- this.properties = {};
320
- if (props) {
321
- this.addProperties(props);
322
- }
323
- }
324
- /**
325
- * Subscribes to position change events on this interval if there are no current listeners.
326
- * @internal
327
- */
328
- addPositionChangeListeners(beforePositionChange, afterPositionChange) {
329
- var _a, _b;
330
- var _c, _d;
331
- if (this.callbacks === undefined) {
332
- this.callbacks = {
333
- beforePositionChange,
334
- afterPositionChange,
335
- };
336
- const startCbs = ((_a = (_c = this.start).callbacks) !== null && _a !== void 0 ? _a : (_c.callbacks = {}));
337
- const endCbs = ((_b = (_d = this.end).callbacks) !== null && _b !== void 0 ? _b : (_d.callbacks = {}));
338
- startCbs.beforeSlide = endCbs.beforeSlide = beforePositionChange;
339
- startCbs.afterSlide = endCbs.afterSlide = afterPositionChange;
340
- }
341
- }
342
- /**
343
- * Removes the currently subscribed position change listeners.
344
- * @internal
345
- */
346
- removePositionChangeListeners() {
347
- if (this.callbacks) {
348
- this.callbacks = undefined;
349
- this.start.callbacks = undefined;
350
- this.end.callbacks = undefined;
351
- }
352
- }
353
- /**
354
- * {@inheritDoc ISerializableInterval.serialize}
355
- * @internal
356
- */
357
- serialize() {
358
- const startPosition = this.client.localReferencePositionToPosition(this.start);
359
- const endPosition = this.client.localReferencePositionToPosition(this.end);
360
- const serializedInterval = {
361
- end: endPosition,
362
- intervalType: this.intervalType,
363
- sequenceNumber: this.client.getCurrentSeq(),
364
- start: startPosition,
365
- };
366
- if (this.properties) {
367
- serializedInterval.properties = this.properties;
368
- }
369
- if (this.stickiness !== IntervalStickiness.END) {
370
- serializedInterval.stickiness = this.stickiness;
371
- }
372
- return serializedInterval;
373
- }
374
- /**
375
- * {@inheritDoc IInterval.clone}
376
- */
377
- clone() {
378
- return new SequenceInterval(this.client, this.start, this.end, this.intervalType, this.properties, this.stickiness);
379
- }
380
- /**
381
- * {@inheritDoc IInterval.compare}
382
- */
383
- compare(b) {
384
- const startResult = this.compareStart(b);
385
- if (startResult === 0) {
386
- const endResult = this.compareEnd(b);
387
- if (endResult === 0) {
388
- const thisId = this.getIntervalId();
389
- if (thisId) {
390
- const bId = b.getIntervalId();
391
- if (bId) {
392
- return thisId > bId ? 1 : thisId < bId ? -1 : 0;
393
- }
394
- return 0;
395
- }
396
- return 0;
397
- }
398
- else {
399
- return endResult;
400
- }
401
- }
402
- else {
403
- return startResult;
404
- }
405
- }
406
- /**
407
- * {@inheritDoc IInterval.compareStart}
408
- */
409
- compareStart(b) {
410
- return compareReferencePositions(this.start, b.start);
411
- }
412
- /**
413
- * {@inheritDoc IInterval.compareEnd}
414
- */
415
- compareEnd(b) {
416
- return compareReferencePositions(this.end, b.end);
417
- }
418
- /**
419
- * {@inheritDoc IInterval.overlaps}
420
- */
421
- overlaps(b) {
422
- const result = compareReferencePositions(this.start, b.end) <= 0 &&
423
- compareReferencePositions(this.end, b.start) >= 0;
424
- return result;
425
- }
426
- /**
427
- * {@inheritDoc ISerializableInterval.getIntervalId}
428
- */
429
- getIntervalId() {
430
- var _a;
431
- const id = (_a = this.properties) === null || _a === void 0 ? void 0 : _a[reservedIntervalIdKey];
432
- assert(id !== undefined, 0x5e2 /* interval ID should not be undefined */);
433
- return `${id}`;
434
- }
435
- /**
436
- * {@inheritDoc IInterval.union}
437
- * @deprecated - This API was never intended to be public and will be marked internal in a future release.
438
- */
439
- union(b) {
440
- return new SequenceInterval(this.client, minReferencePosition(this.start, b.start), maxReferencePosition(this.end, b.end), this.intervalType);
441
- }
442
- /**
443
- * {@inheritDoc ISerializableInterval.addProperties}
444
- * @deprecated - This API was never intended to be public and will be marked internal in a future release.
445
- */
446
- addProperties(newProps, collab = false, seq, op) {
447
- this.initializeProperties();
448
- return this.propertyManager.addProperties(this.properties, newProps, op, seq, collab);
449
- }
450
- /**
451
- * @returns whether this interval overlaps two numerical positions.
452
- */
453
- overlapsPos(bstart, bend) {
454
- const startPos = this.client.localReferencePositionToPosition(this.start);
455
- const endPos = this.client.localReferencePositionToPosition(this.end);
456
- return endPos > bstart && startPos < bend;
457
- }
458
- /**
459
- * {@inheritDoc IInterval.modify}
460
- * @deprecated - This API was never intended to be public and will be marked internal in a future release.
461
- */
462
- modify(label, start, end, op, localSeq, stickiness = IntervalStickiness.END) {
463
- const getRefType = (baseType) => {
464
- let refType = baseType;
465
- if (op === undefined) {
466
- refType &= ~ReferenceType.SlideOnRemove;
467
- refType |= ReferenceType.StayOnRemove;
468
- }
469
- return refType;
470
- };
471
- let startRef = this.start;
472
- if (start !== undefined) {
473
- startRef = createPositionReference(this.client, start, getRefType(this.start.refType), op, undefined, localSeq, startReferenceSlidingPreference(stickiness));
474
- if (this.start.properties) {
475
- startRef.addProperties(this.start.properties);
476
- }
477
- }
478
- let endRef = this.end;
479
- if (end !== undefined) {
480
- endRef = createPositionReference(this.client, end, getRefType(this.end.refType), op, undefined, localSeq, endReferenceSlidingPreference(stickiness));
481
- if (this.end.properties) {
482
- endRef.addProperties(this.end.properties);
483
- }
484
- }
485
- const newInterval = new SequenceInterval(this.client, startRef, endRef, this.intervalType);
486
- if (this.properties) {
487
- newInterval.initializeProperties();
488
- this.propertyManager.copyTo(this.properties, newInterval.properties, newInterval.propertyManager);
489
- }
490
- return newInterval;
491
- }
492
- initializeProperties() {
493
- if (!this.propertyManager) {
494
- this.propertyManager = new PropertiesManager();
495
- }
496
- if (!this.properties) {
497
- this.properties = createMap();
498
- }
499
- }
500
- }
501
- export function createPositionReferenceFromSegoff(client, segoff, refType, op, localSeq, fromSnapshot, slidingPreference) {
502
- if (segoff.segment) {
503
- const ref = client.createLocalReferencePosition(segoff.segment, segoff.offset, refType, undefined, slidingPreference);
504
- return ref;
505
- }
506
- // Creating references on detached segments is allowed for:
507
- // - Transient segments
508
- // - References coming from a remote client (location may have been concurrently removed)
509
- // - References being rebased to a new sequence number
510
- // (segment they originally referred to may have been removed with no suitable replacement)
511
- if (!op &&
512
- !localSeq &&
513
- !fromSnapshot &&
514
- !refTypeIncludesFlag(refType, ReferenceType.Transient)) {
515
- throw new UsageError("Non-transient references need segment");
516
- }
517
- return createDetachedLocalReferencePosition(refType);
518
- }
519
- function createPositionReference(client, pos, refType, op, fromSnapshot, localSeq, slidingPreference) {
520
- let segoff;
521
- if (op) {
522
- assert((refType & ReferenceType.SlideOnRemove) !== 0, 0x2f5 /* op create references must be SlideOnRemove */);
523
- segoff = client.getContainingSegment(pos, {
524
- referenceSequenceNumber: op.referenceSequenceNumber,
525
- clientId: op.clientId,
526
- });
527
- segoff = getSlideToSegoff(segoff);
528
- }
529
- else {
530
- assert((refType & ReferenceType.SlideOnRemove) === 0 || !!fromSnapshot, 0x2f6 /* SlideOnRemove references must be op created */);
531
- segoff = client.getContainingSegment(pos, undefined, localSeq);
532
- }
533
- return createPositionReferenceFromSegoff(client, segoff, refType, op, localSeq, fromSnapshot, slidingPreference);
534
- }
535
- export function createSequenceInterval(label, start, end, client, intervalType, op, fromSnapshot, stickiness = IntervalStickiness.END) {
536
- let beginRefType = ReferenceType.RangeBegin;
537
- let endRefType = ReferenceType.RangeEnd;
538
- if (intervalType === IntervalType.Transient) {
539
- beginRefType = ReferenceType.Transient;
540
- endRefType = ReferenceType.Transient;
541
- }
542
- else {
543
- if (intervalType === IntervalType.Nest) {
544
- beginRefType = ReferenceType.NestBegin;
545
- endRefType = ReferenceType.NestEnd;
546
- }
547
- // All non-transient interval references must eventually be SlideOnRemove
548
- // To ensure eventual consistency, they must start as StayOnRemove when
549
- // pending (created locally and creation op is not acked)
550
- if (op || fromSnapshot) {
551
- beginRefType |= ReferenceType.SlideOnRemove;
552
- endRefType |= ReferenceType.SlideOnRemove;
553
- }
554
- else {
555
- beginRefType |= ReferenceType.StayOnRemove;
556
- endRefType |= ReferenceType.StayOnRemove;
557
- }
558
- }
559
- const startLref = createPositionReference(client, start, beginRefType, op, fromSnapshot, undefined, startReferenceSlidingPreference(stickiness));
560
- const endLref = createPositionReference(client, end, endRefType, op, fromSnapshot, undefined, endReferenceSlidingPreference(stickiness));
561
- const rangeProp = {
562
- [reservedRangeLabelsKey]: [label],
563
- };
564
- startLref.addProperties(rangeProp);
565
- endLref.addProperties(rangeProp);
566
- const ival = new SequenceInterval(client, startLref, endLref, intervalType, rangeProp, stickiness);
567
- return ival;
568
- }
569
57
  export function createIntervalIndex() {
570
58
  const helpers = {
571
- compareEnds: compareIntervalEnds,
59
+ compareEnds: (a, b) => a.end - b.end,
572
60
  create: createInterval,
573
61
  };
574
62
  const lc = new LocalIntervalCollection(undefined, "", helpers);
575
63
  return lc;
576
64
  }
577
- class IdIntervalIndex {
578
- constructor() {
579
- this.intervalIdMap = new Map();
580
- }
581
- add(interval) {
582
- const id = interval.getIntervalId();
583
- assert(id !== undefined, 0x2c0 /* "ID must be created before adding interval to collection" */);
584
- // Make the ID immutable.
585
- Object.defineProperty(interval.properties, reservedIntervalIdKey, {
586
- configurable: false,
587
- enumerable: true,
588
- writable: false,
589
- });
590
- this.intervalIdMap.set(id, interval);
591
- }
592
- remove(interval) {
593
- const id = interval.getIntervalId();
594
- assert(id !== undefined, 0x311 /* expected id to exist on interval */);
595
- this.intervalIdMap.delete(id);
596
- }
597
- getIntervalById(id) {
598
- return this.intervalIdMap.get(id);
599
- }
600
- [Symbol.iterator]() {
601
- return this.intervalIdMap.values();
602
- }
603
- }
604
- class EndpointIndex {
605
- constructor(client, helpers) {
606
- this.client = client;
607
- this.helpers = helpers;
608
- // eslint-disable-next-line @typescript-eslint/unbound-method
609
- this.endIntervalTree = new RedBlackTree(helpers.compareEnds);
610
- }
611
- previousInterval(pos) {
612
- const transientInterval = this.helpers.create("transient", pos, pos, this.client, IntervalType.Transient);
613
- const rbNode = this.endIntervalTree.floor(transientInterval);
614
- if (rbNode) {
615
- return rbNode.data;
616
- }
617
- }
618
- nextInterval(pos) {
619
- const transientInterval = this.helpers.create("transient", pos, pos, this.client, IntervalType.Transient);
620
- const rbNode = this.endIntervalTree.ceil(transientInterval);
621
- if (rbNode) {
622
- return rbNode.data;
623
- }
624
- }
625
- add(interval) {
626
- this.endIntervalTree.put(interval, interval);
627
- }
628
- remove(interval) {
629
- this.endIntervalTree.remove(interval);
630
- }
631
- }
632
- /**
633
- * Interface for intervals that have comparison override properties.
634
- */
635
- const forceCompare = Symbol();
636
- /**
637
- * Compares two objects based on their comparison override properties.
638
- * @returns A number indicating the order of the intervals (negative for a is lower than b, 0 for tie, positive for a is greater than b).
639
- */
640
- function compareOverrideables(a, b) {
641
- var _a, _b;
642
- const forceCompareA = (_a = a[forceCompare]) !== null && _a !== void 0 ? _a : 0;
643
- const forceCompareB = (_b = b[forceCompare]) !== null && _b !== void 0 ? _b : 0;
644
- return forceCompareA - forceCompareB;
645
- }
646
- class EndpointInRangeIndex {
647
- constructor(helpers, client) {
648
- this.helpers = helpers;
649
- this.client = client;
650
- this.intervalTree = new RedBlackTree((a, b) => {
651
- const compareEndsResult = helpers.compareEnds(a, b);
652
- if (compareEndsResult !== 0) {
653
- return compareEndsResult;
654
- }
655
- const overrideablesComparison = compareOverrideables(a, b);
656
- if (overrideablesComparison !== 0) {
657
- return overrideablesComparison;
658
- }
659
- const aId = a.getIntervalId();
660
- const bId = b.getIntervalId();
661
- if (aId !== undefined && bId !== undefined) {
662
- return aId.localeCompare(bId);
663
- }
664
- return 0;
665
- });
666
- }
667
- add(interval) {
668
- this.intervalTree.put(interval, interval);
669
- }
670
- remove(interval) {
671
- this.intervalTree.remove(interval);
672
- }
673
- findIntervalsWithEndpointInRange(start, end) {
674
- if (start <= 0 || start > end || this.intervalTree.isEmpty()) {
675
- return [];
676
- }
677
- const results = [];
678
- const action = (node) => {
679
- results.push(node.data);
680
- return true;
681
- };
682
- const transientStartInterval = this.helpers.create("transient", start, start, this.client, IntervalType.Transient);
683
- const transientEndInterval = this.helpers.create("transient", end, end, this.client, IntervalType.Transient);
684
- // Add comparison overrides to the transient intervals
685
- transientStartInterval[forceCompare] = -1;
686
- transientEndInterval[forceCompare] = 1;
687
- this.intervalTree.mapRange(action, results, transientStartInterval, transientEndInterval);
688
- return results;
689
- }
690
- }
691
- class StartpointInRangeIndex {
692
- constructor(helpers, client) {
693
- this.helpers = helpers;
694
- this.client = client;
695
- this.intervalTree = new RedBlackTree((a, b) => {
696
- assert(typeof helpers.compareStarts === "function", 0x6d1 /* compareStarts does not exist in the helpers */);
697
- const compareStartsResult = helpers.compareStarts(a, b);
698
- if (compareStartsResult !== 0) {
699
- return compareStartsResult;
700
- }
701
- const overrideablesComparison = compareOverrideables(a, b);
702
- if (overrideablesComparison !== 0) {
703
- return overrideablesComparison;
704
- }
705
- const aId = a.getIntervalId();
706
- const bId = b.getIntervalId();
707
- if (aId !== undefined && bId !== undefined) {
708
- return aId.localeCompare(bId);
709
- }
710
- return 0;
711
- });
712
- }
713
- add(interval) {
714
- this.intervalTree.put(interval, interval);
715
- }
716
- remove(interval) {
717
- this.intervalTree.remove(interval);
718
- }
719
- findIntervalsWithStartpointInRange(start, end) {
720
- if (start <= 0 || start > end || this.intervalTree.isEmpty()) {
721
- return [];
722
- }
723
- const results = [];
724
- const action = (node) => {
725
- results.push(node.data);
726
- return true;
727
- };
728
- const transientStartInterval = this.helpers.create("transient", start, start, this.client, IntervalType.Transient);
729
- const transientEndInterval = this.helpers.create("transient", end, end, this.client, IntervalType.Transient);
730
- // Add comparison overrides to the transient intervals
731
- transientStartInterval[forceCompare] = -1;
732
- transientEndInterval[forceCompare] = 1;
733
- this.intervalTree.mapRange(action, results, transientStartInterval, transientEndInterval);
734
- return results;
735
- }
736
- }
737
- export function createEndpointInRangeIndex(helpers, client) {
738
- return new EndpointInRangeIndex(helpers, client);
739
- }
740
- export function createStartpointInRangeIndex(helpers, client) {
741
- return new StartpointInRangeIndex(helpers, client);
742
- }
743
65
  export class LocalIntervalCollection {
744
66
  constructor(client, label, helpers,
745
67
  /** Callback invoked each time one of the endpoints of an interval slides. */
@@ -749,8 +71,8 @@ export class LocalIntervalCollection {
749
71
  this.helpers = helpers;
750
72
  this.onPositionChange = onPositionChange;
751
73
  this.overlappingIntervalsIndex = createOverlappingIntervalsIndex(client, helpers);
752
- this.idIntervalIndex = new IdIntervalIndex();
753
- this.endIntervalIndex = new EndpointIndex(client, helpers);
74
+ this.idIntervalIndex = createIdIntervalIndex();
75
+ this.endIntervalIndex = createEndpointIndex(client, helpers);
754
76
  this.indexes = new Set([
755
77
  this.overlappingIntervalsIndex,
756
78
  this.idIntervalIndex,
@@ -904,18 +226,6 @@ export class LocalIntervalCollection {
904
226
  }
905
227
  }
906
228
  LocalIntervalCollection.legacyIdPrefix = "legacy";
907
- export const compareSequenceIntervalEnds = (a, b) => compareReferencePositions(a.end, b.end);
908
- export const compareSequenceIntervalStarts = (a, b) => compareReferencePositions(a.start, b.start);
909
- export const sequenceIntervalHelpers = {
910
- compareEnds: compareSequenceIntervalEnds,
911
- compareStarts: compareSequenceIntervalStarts,
912
- create: createSequenceInterval,
913
- };
914
- export const intervalHelpers = {
915
- compareEnds: (a, b) => a.end - b.end,
916
- compareStarts: (a, b) => a.start - b.start,
917
- create: createInterval,
918
- };
919
229
  class SequenceIntervalCollectionFactory {
920
230
  load(emitter, raw = [], options) {
921
231
  return new IntervalCollection(sequenceIntervalHelpers, true, emitter, raw, options);
@@ -938,18 +248,10 @@ export class SequenceIntervalCollectionValueType {
938
248
  SequenceIntervalCollectionValueType.Name = "sharedStringIntervalCollection";
939
249
  SequenceIntervalCollectionValueType._factory = new SequenceIntervalCollectionFactory();
940
250
  SequenceIntervalCollectionValueType._ops = makeOpsMap();
941
- const compareIntervalEnds = (a, b) => a.end - b.end;
942
- function createInterval(label, start, end, client, intervalType, op, fromSnapshot) {
943
- const rangeProp = {};
944
- if (label && label.length > 0) {
945
- rangeProp[reservedRangeLabelsKey] = [label];
946
- }
947
- return new Interval(start, end, rangeProp);
948
- }
949
251
  class IntervalCollectionFactory {
950
252
  load(emitter, raw = [], options) {
951
253
  const helpers = {
952
- compareEnds: compareIntervalEnds,
254
+ compareEnds: (a, b) => a.end - b.end,
953
255
  create: createInterval,
954
256
  };
955
257
  const collection = new IntervalCollection(helpers, false, emitter, raw, options);
@@ -1667,6 +969,8 @@ export class IntervalCollection extends TypedEventEmitter {
1667
969
  }
1668
970
  /**
1669
971
  * {@inheritdoc IIntervalCollection.CreateForwardIteratorWithStartPosition}
972
+ *
973
+ * @deprecated - The sequence order of collection order will not be supported
1670
974
  */
1671
975
  CreateForwardIteratorWithStartPosition(startPosition) {
1672
976
  const iterator = new IntervalCollectionIterator(this, true, startPosition);
@@ -1674,6 +978,8 @@ export class IntervalCollection extends TypedEventEmitter {
1674
978
  }
1675
979
  /**
1676
980
  * {@inheritdoc IIntervalCollection.CreateBackwardIteratorWithStartPosition}
981
+ *
982
+ * @deprecated - The sequence order of collection order will not be supported
1677
983
  */
1678
984
  CreateBackwardIteratorWithStartPosition(startPosition) {
1679
985
  const iterator = new IntervalCollectionIterator(this, false, startPosition);
@@ -1681,6 +987,8 @@ export class IntervalCollection extends TypedEventEmitter {
1681
987
  }
1682
988
  /**
1683
989
  * {@inheritdoc IIntervalCollection.CreateForwardIteratorWithEndPosition}
990
+ *
991
+ * @deprecated - The sequence order of collection order will not be supported
1684
992
  */
1685
993
  CreateForwardIteratorWithEndPosition(endPosition) {
1686
994
  const iterator = new IntervalCollectionIterator(this, true, undefined, endPosition);
@@ -1688,6 +996,8 @@ export class IntervalCollection extends TypedEventEmitter {
1688
996
  }
1689
997
  /**
1690
998
  * {@inheritdoc IIntervalCollection.CreateBackwardIteratorWithEndPosition}
999
+ *
1000
+ * @deprecated - The sequence order of collection order will not be supported
1691
1001
  */
1692
1002
  CreateBackwardIteratorWithEndPosition(endPosition) {
1693
1003
  const iterator = new IntervalCollectionIterator(this, false, undefined, endPosition);
@@ -1695,6 +1005,8 @@ export class IntervalCollection extends TypedEventEmitter {
1695
1005
  }
1696
1006
  /**
1697
1007
  * {@inheritdoc IIntervalCollection.gatherIterationResults}
1008
+ * @deprecated - This API will be deprecated as its functionality will be moved to the `OverlappingIntervalsIndex`.
1009
+ * We would like the user to attach the index to the collection on their own.
1698
1010
  */
1699
1011
  gatherIterationResults(results, iteratesForward, start, end) {
1700
1012
  if (!this.localCollection) {
@@ -1704,6 +1016,8 @@ export class IntervalCollection extends TypedEventEmitter {
1704
1016
  }
1705
1017
  /**
1706
1018
  * {@inheritdoc IIntervalCollection.findOverlappingIntervals}
1019
+ * @deprecated - This API will be deprecated as its functionality will be moved to the `OverlappingIntervalsIndex`.
1020
+ * We would like the user to attach the index to the collection on their own.
1707
1021
  */
1708
1022
  findOverlappingIntervals(startPosition, endPosition) {
1709
1023
  if (!this.localCollection) {
@@ -1724,6 +1038,9 @@ export class IntervalCollection extends TypedEventEmitter {
1724
1038
  }
1725
1039
  /**
1726
1040
  * {@inheritdoc IIntervalCollection.previousInterval}
1041
+ *
1042
+ * @deprecated - This API will be deprecated as its functionality will be moved to the `EndpointIndex`.
1043
+ * We would like the user to attach the index to the collection on their own.
1727
1044
  */
1728
1045
  previousInterval(pos) {
1729
1046
  if (!this.localCollection) {
@@ -1733,6 +1050,9 @@ export class IntervalCollection extends TypedEventEmitter {
1733
1050
  }
1734
1051
  /**
1735
1052
  * {@inheritdoc IIntervalCollection.nextInterval}
1053
+ *
1054
+ * @deprecated - This API will be deprecated as its functionality will be moved to the `EndpointIndex`.
1055
+ * We would like the user to attach the index to the collection on their own.
1736
1056
  */
1737
1057
  nextInterval(pos) {
1738
1058
  if (!this.localCollection) {