@fluidframework/sequence 1.4.0-115997 → 2.0.0-dev-rc.1.0.0.224419
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.
- package/.eslintrc.js +9 -11
- package/.mocharc.js +12 -0
- package/CHANGELOG.md +449 -0
- package/README.md +364 -183
- package/api-extractor-lint.json +4 -0
- package/api-extractor.json +2 -2
- package/api-report/sequence.api.md +741 -0
- package/dist/{defaultMap.js → defaultMap.cjs} +29 -22
- package/dist/defaultMap.cjs.map +1 -0
- package/dist/defaultMap.d.ts +7 -6
- package/dist/defaultMap.d.ts.map +1 -1
- package/dist/defaultMapInterfaces.cjs +7 -0
- package/dist/defaultMapInterfaces.cjs.map +1 -0
- package/dist/defaultMapInterfaces.d.ts +44 -12
- package/dist/defaultMapInterfaces.d.ts.map +1 -1
- package/dist/index.cjs +60 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +14 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/intervalCollection.cjs +1159 -0
- package/dist/intervalCollection.cjs.map +1 -0
- package/dist/intervalCollection.d.ts +461 -162
- package/dist/intervalCollection.d.ts.map +1 -1
- package/dist/intervalIndex/endpointInRangeIndex.cjs +66 -0
- package/dist/intervalIndex/endpointInRangeIndex.cjs.map +1 -0
- package/dist/intervalIndex/endpointInRangeIndex.d.ts +34 -0
- package/dist/intervalIndex/endpointInRangeIndex.d.ts.map +1 -0
- package/dist/intervalIndex/endpointIndex.cjs +47 -0
- package/dist/intervalIndex/endpointIndex.cjs.map +1 -0
- package/dist/intervalIndex/endpointIndex.d.ts +38 -0
- package/dist/intervalIndex/endpointIndex.d.ts.map +1 -0
- package/dist/intervalIndex/idIntervalIndex.cjs +44 -0
- package/dist/intervalIndex/idIntervalIndex.cjs.map +1 -0
- package/dist/intervalIndex/idIntervalIndex.d.ts +18 -0
- package/dist/intervalIndex/idIntervalIndex.d.ts.map +1 -0
- package/dist/intervalIndex/index.cjs +24 -0
- package/dist/intervalIndex/index.cjs.map +1 -0
- package/dist/intervalIndex/index.d.ts +13 -0
- package/dist/intervalIndex/index.d.ts.map +1 -0
- package/dist/{defaultMapInterfaces.js → intervalIndex/intervalIndex.cjs} +1 -1
- package/dist/intervalIndex/intervalIndex.cjs.map +1 -0
- package/dist/intervalIndex/intervalIndex.d.ts +30 -0
- package/dist/intervalIndex/intervalIndex.d.ts.map +1 -0
- package/dist/intervalIndex/intervalIndexUtils.cjs +22 -0
- package/dist/intervalIndex/intervalIndexUtils.cjs.map +1 -0
- package/dist/intervalIndex/intervalIndexUtils.d.ts +17 -0
- package/dist/intervalIndex/intervalIndexUtils.d.ts.map +1 -0
- package/dist/intervalIndex/overlappingIntervalsIndex.cjs +116 -0
- package/dist/intervalIndex/overlappingIntervalsIndex.cjs.map +1 -0
- package/dist/intervalIndex/overlappingIntervalsIndex.d.ts +44 -0
- package/dist/intervalIndex/overlappingIntervalsIndex.d.ts.map +1 -0
- package/dist/intervalIndex/overlappingSequenceIntervalsIndex.cjs +41 -0
- package/dist/intervalIndex/overlappingSequenceIntervalsIndex.cjs.map +1 -0
- package/dist/intervalIndex/overlappingSequenceIntervalsIndex.d.ts +11 -0
- package/dist/intervalIndex/overlappingSequenceIntervalsIndex.d.ts.map +1 -0
- package/dist/intervalIndex/sequenceIntervalIndexes.cjs +7 -0
- package/dist/intervalIndex/sequenceIntervalIndexes.cjs.map +1 -0
- package/dist/intervalIndex/sequenceIntervalIndexes.d.ts +35 -0
- package/dist/intervalIndex/sequenceIntervalIndexes.d.ts.map +1 -0
- package/dist/intervalIndex/startpointInRangeIndex.cjs +66 -0
- package/dist/intervalIndex/startpointInRangeIndex.cjs.map +1 -0
- package/dist/intervalIndex/startpointInRangeIndex.d.ts +34 -0
- package/dist/intervalIndex/startpointInRangeIndex.d.ts.map +1 -0
- package/dist/intervalTree.cjs +80 -0
- package/dist/intervalTree.cjs.map +1 -0
- package/dist/intervalTree.d.ts +24 -0
- package/dist/intervalTree.d.ts.map +1 -0
- package/dist/intervals/index.cjs +23 -0
- package/dist/intervals/index.cjs.map +1 -0
- package/dist/intervals/index.d.ts +8 -0
- package/dist/intervals/index.d.ts.map +1 -0
- package/dist/intervals/interval.cjs +181 -0
- package/dist/intervals/interval.cjs.map +1 -0
- package/dist/intervals/interval.d.ts +84 -0
- package/dist/intervals/interval.d.ts.map +1 -0
- package/dist/intervals/intervalUtils.cjs +83 -0
- package/dist/intervals/intervalUtils.cjs.map +1 -0
- package/dist/intervals/intervalUtils.d.ts +230 -0
- package/dist/intervals/intervalUtils.d.ts.map +1 -0
- package/dist/intervals/sequenceInterval.cjs +378 -0
- package/dist/intervals/sequenceInterval.cjs.map +1 -0
- package/dist/intervals/sequenceInterval.d.ts +137 -0
- package/dist/intervals/sequenceInterval.d.ts.map +1 -0
- package/dist/{localValues.js → localValues.cjs} +1 -1
- package/dist/localValues.cjs.map +1 -0
- package/dist/localValues.d.ts +2 -1
- package/dist/localValues.d.ts.map +1 -1
- package/dist/{packageVersion.js → packageVersion.cjs} +2 -2
- package/dist/packageVersion.cjs.map +1 -0
- package/dist/packageVersion.d.ts +1 -1
- package/dist/packageVersion.d.ts.map +1 -1
- package/dist/revertibles.cjs +425 -0
- package/dist/revertibles.cjs.map +1 -0
- package/dist/revertibles.d.ts +86 -0
- package/dist/revertibles.d.ts.map +1 -0
- package/dist/sequence-alpha.d.ts +1315 -0
- package/dist/sequence-beta.d.ts +244 -0
- package/dist/sequence-public.d.ts +244 -0
- package/dist/sequence-untrimmed.d.ts +1803 -0
- package/dist/{sequence.js → sequence.cjs} +226 -156
- package/dist/sequence.cjs.map +1 -0
- package/dist/sequence.d.ts +125 -48
- package/dist/sequence.d.ts.map +1 -1
- package/dist/{sequenceDeltaEvent.js → sequenceDeltaEvent.cjs} +18 -8
- package/dist/sequenceDeltaEvent.cjs.map +1 -0
- package/dist/sequenceDeltaEvent.d.ts +24 -7
- package/dist/sequenceDeltaEvent.d.ts.map +1 -1
- package/dist/sequenceFactory.cjs +55 -0
- package/dist/sequenceFactory.cjs.map +1 -0
- package/dist/sequenceFactory.d.ts +3 -89
- package/dist/sequenceFactory.d.ts.map +1 -1
- package/dist/{sharedIntervalCollection.js → sharedIntervalCollection.cjs} +17 -22
- package/dist/sharedIntervalCollection.cjs.map +1 -0
- package/dist/sharedIntervalCollection.d.ts +12 -12
- package/dist/sharedIntervalCollection.d.ts.map +1 -1
- package/dist/{sharedSequence.js → sharedSequence.cjs} +29 -22
- package/dist/sharedSequence.cjs.map +1 -0
- package/dist/sharedSequence.d.ts +14 -2
- package/dist/sharedSequence.d.ts.map +1 -1
- package/dist/sharedString.cjs +286 -0
- package/dist/sharedString.cjs.map +1 -0
- package/dist/sharedString.d.ts +58 -22
- package/dist/sharedString.d.ts.map +1 -1
- package/dist/tsdoc-metadata.json +11 -0
- package/lib/{defaultMap.d.ts → defaultMap.d.mts} +7 -6
- package/lib/defaultMap.d.mts.map +1 -0
- package/lib/{defaultMap.js → defaultMap.mjs} +28 -21
- package/lib/defaultMap.mjs.map +1 -0
- package/lib/{defaultMapInterfaces.d.ts → defaultMapInterfaces.d.mts} +44 -12
- package/lib/defaultMapInterfaces.d.mts.map +1 -0
- package/lib/defaultMapInterfaces.mjs +6 -0
- package/lib/defaultMapInterfaces.mjs.map +1 -0
- package/lib/index.d.mts +17 -0
- package/lib/index.d.mts.map +1 -0
- package/lib/index.mjs +16 -0
- package/lib/index.mjs.map +1 -0
- package/lib/intervalCollection.d.mts +569 -0
- package/lib/intervalCollection.d.mts.map +1 -0
- package/lib/intervalCollection.mjs +1144 -0
- package/lib/intervalCollection.mjs.map +1 -0
- package/lib/intervalIndex/endpointInRangeIndex.d.mts +34 -0
- package/lib/intervalIndex/endpointInRangeIndex.d.mts.map +1 -0
- package/lib/intervalIndex/endpointInRangeIndex.mjs +61 -0
- package/lib/intervalIndex/endpointInRangeIndex.mjs.map +1 -0
- package/lib/intervalIndex/endpointIndex.d.mts +38 -0
- package/lib/intervalIndex/endpointIndex.d.mts.map +1 -0
- package/lib/intervalIndex/endpointIndex.mjs +42 -0
- package/lib/intervalIndex/endpointIndex.mjs.map +1 -0
- package/lib/intervalIndex/idIntervalIndex.d.mts +18 -0
- package/lib/intervalIndex/idIntervalIndex.d.mts.map +1 -0
- package/lib/intervalIndex/idIntervalIndex.mjs +40 -0
- package/lib/intervalIndex/idIntervalIndex.mjs.map +1 -0
- package/lib/intervalIndex/index.d.mts +13 -0
- package/lib/intervalIndex/index.d.mts.map +1 -0
- package/lib/intervalIndex/index.mjs +11 -0
- package/lib/intervalIndex/index.mjs.map +1 -0
- package/lib/intervalIndex/intervalIndex.d.mts +30 -0
- package/lib/intervalIndex/intervalIndex.d.mts.map +1 -0
- package/lib/{defaultMapInterfaces.js → intervalIndex/intervalIndex.mjs} +1 -1
- package/lib/intervalIndex/intervalIndex.mjs.map +1 -0
- package/lib/intervalIndex/intervalIndexUtils.d.mts +17 -0
- package/lib/intervalIndex/intervalIndexUtils.d.mts.map +1 -0
- package/lib/intervalIndex/intervalIndexUtils.mjs +18 -0
- package/lib/intervalIndex/intervalIndexUtils.mjs.map +1 -0
- package/lib/intervalIndex/overlappingIntervalsIndex.d.mts +44 -0
- package/lib/intervalIndex/overlappingIntervalsIndex.d.mts.map +1 -0
- package/lib/intervalIndex/overlappingIntervalsIndex.mjs +111 -0
- package/lib/intervalIndex/overlappingIntervalsIndex.mjs.map +1 -0
- package/lib/intervalIndex/overlappingSequenceIntervalsIndex.d.mts +11 -0
- package/lib/intervalIndex/overlappingSequenceIntervalsIndex.d.mts.map +1 -0
- package/lib/intervalIndex/overlappingSequenceIntervalsIndex.mjs +37 -0
- package/lib/intervalIndex/overlappingSequenceIntervalsIndex.mjs.map +1 -0
- package/lib/intervalIndex/sequenceIntervalIndexes.d.mts +35 -0
- package/lib/intervalIndex/sequenceIntervalIndexes.d.mts.map +1 -0
- package/lib/intervalIndex/sequenceIntervalIndexes.mjs +6 -0
- package/lib/intervalIndex/sequenceIntervalIndexes.mjs.map +1 -0
- package/lib/intervalIndex/startpointInRangeIndex.d.mts +34 -0
- package/lib/intervalIndex/startpointInRangeIndex.d.mts.map +1 -0
- package/lib/intervalIndex/startpointInRangeIndex.mjs +61 -0
- package/lib/intervalIndex/startpointInRangeIndex.mjs.map +1 -0
- package/lib/intervalTree.d.mts +24 -0
- package/lib/intervalTree.d.mts.map +1 -0
- package/lib/intervalTree.mjs +76 -0
- package/lib/intervalTree.mjs.map +1 -0
- package/lib/intervals/index.d.mts +8 -0
- package/lib/intervals/index.d.mts.map +1 -0
- package/lib/intervals/index.mjs +8 -0
- package/lib/intervals/index.mjs.map +1 -0
- package/lib/intervals/interval.d.mts +84 -0
- package/lib/intervals/interval.d.mts.map +1 -0
- package/lib/intervals/interval.mjs +176 -0
- package/lib/intervals/interval.mjs.map +1 -0
- package/lib/intervals/intervalUtils.d.mts +230 -0
- package/lib/intervals/intervalUtils.d.mts.map +1 -0
- package/lib/intervals/intervalUtils.mjs +77 -0
- package/lib/intervals/intervalUtils.mjs.map +1 -0
- package/lib/intervals/sequenceInterval.d.mts +137 -0
- package/lib/intervals/sequenceInterval.d.mts.map +1 -0
- package/lib/intervals/sequenceInterval.mjs +370 -0
- package/lib/intervals/sequenceInterval.mjs.map +1 -0
- package/lib/{localValues.d.ts → localValues.d.mts} +3 -2
- package/lib/localValues.d.mts.map +1 -0
- package/lib/{localValues.js → localValues.mjs} +2 -2
- package/lib/localValues.mjs.map +1 -0
- package/lib/{packageVersion.d.ts → packageVersion.d.mts} +1 -1
- package/lib/{packageVersion.d.ts.map → packageVersion.d.mts.map} +1 -1
- package/lib/{packageVersion.js → packageVersion.mjs} +2 -2
- package/lib/packageVersion.mjs.map +1 -0
- package/lib/revertibles.d.mts +86 -0
- package/lib/revertibles.d.mts.map +1 -0
- package/lib/revertibles.mjs +416 -0
- package/lib/revertibles.mjs.map +1 -0
- package/lib/sequence-alpha.d.mts +1315 -0
- package/lib/sequence-beta.d.mts +244 -0
- package/lib/sequence-public.d.mts +244 -0
- package/lib/sequence-untrimmed.d.mts +1803 -0
- package/lib/{sequence.d.ts → sequence.d.mts} +127 -50
- package/lib/sequence.d.mts.map +1 -0
- package/lib/{sequence.js → sequence.mjs} +225 -152
- package/lib/sequence.mjs.map +1 -0
- package/lib/{sequenceDeltaEvent.d.ts → sequenceDeltaEvent.d.mts} +24 -7
- package/lib/sequenceDeltaEvent.d.mts.map +1 -0
- package/lib/{sequenceDeltaEvent.js → sequenceDeltaEvent.mjs} +20 -8
- package/lib/sequenceDeltaEvent.mjs.map +1 -0
- package/lib/sequenceFactory.d.mts +22 -0
- package/lib/sequenceFactory.d.mts.map +1 -0
- package/lib/sequenceFactory.mjs +51 -0
- package/lib/sequenceFactory.mjs.map +1 -0
- package/lib/{sharedIntervalCollection.d.ts → sharedIntervalCollection.d.mts} +12 -12
- package/lib/sharedIntervalCollection.d.mts.map +1 -0
- package/lib/{sharedIntervalCollection.js → sharedIntervalCollection.mjs} +16 -21
- package/lib/sharedIntervalCollection.mjs.map +1 -0
- package/lib/{sharedSequence.d.ts → sharedSequence.d.mts} +15 -3
- package/lib/sharedSequence.d.mts.map +1 -0
- package/lib/{sharedSequence.js → sharedSequence.mjs} +30 -23
- package/lib/sharedSequence.mjs.map +1 -0
- package/lib/{sharedString.d.ts → sharedString.d.mts} +60 -24
- package/lib/sharedString.d.mts.map +1 -0
- package/lib/sharedString.mjs +281 -0
- package/lib/sharedString.mjs.map +1 -0
- package/package.json +146 -75
- package/prettier.config.cjs +8 -0
- package/sequence.test-files.tar +0 -0
- package/src/defaultMap.ts +417 -403
- package/src/defaultMapInterfaces.ts +157 -117
- package/src/index.ts +86 -26
- package/src/intervalCollection.ts +2043 -1563
- package/src/intervalIndex/endpointInRangeIndex.ts +116 -0
- package/src/intervalIndex/endpointIndex.ts +91 -0
- package/src/intervalIndex/idIntervalIndex.ts +64 -0
- package/src/intervalIndex/index.ts +25 -0
- package/src/intervalIndex/intervalIndex.ts +32 -0
- package/src/intervalIndex/intervalIndexUtils.ts +27 -0
- package/src/intervalIndex/overlappingIntervalsIndex.ts +187 -0
- package/src/intervalIndex/overlappingSequenceIntervalsIndex.ts +80 -0
- package/src/intervalIndex/sequenceIntervalIndexes.ts +34 -0
- package/src/intervalIndex/startpointInRangeIndex.ts +114 -0
- package/src/intervalTree.ts +98 -0
- package/src/intervals/index.ts +25 -0
- package/src/intervals/interval.ts +238 -0
- package/src/intervals/intervalUtils.ts +288 -0
- package/src/intervals/sequenceInterval.ts +616 -0
- package/src/localValues.ts +68 -73
- package/src/packageVersion.ts +1 -1
- package/src/revertibles.ts +693 -0
- package/src/sequence.ts +845 -690
- package/src/sequenceDeltaEvent.ts +164 -131
- package/src/sequenceFactory.ts +58 -214
- package/src/sharedIntervalCollection.ts +161 -152
- package/src/sharedSequence.ts +181 -167
- package/src/sharedString.ts +390 -234
- package/tsc-multi.test.json +10 -0
- package/tsconfig.json +11 -13
- package/.editorconfig +0 -7
- package/.vscode/launch.json +0 -15
- package/dist/defaultMap.js.map +0 -1
- package/dist/defaultMapInterfaces.js.map +0 -1
- package/dist/index.js +0 -44
- package/dist/index.js.map +0 -1
- package/dist/intervalCollection.js +0 -1250
- package/dist/intervalCollection.js.map +0 -1
- package/dist/localValues.js.map +0 -1
- package/dist/packageVersion.js.map +0 -1
- package/dist/sequence.js.map +0 -1
- package/dist/sequenceDeltaEvent.js.map +0 -1
- package/dist/sequenceFactory.js +0 -192
- package/dist/sequenceFactory.js.map +0 -1
- package/dist/sharedIntervalCollection.js.map +0 -1
- package/dist/sharedNumberSequence.d.ts +0 -50
- package/dist/sharedNumberSequence.d.ts.map +0 -1
- package/dist/sharedNumberSequence.js +0 -61
- package/dist/sharedNumberSequence.js.map +0 -1
- package/dist/sharedObjectSequence.d.ts +0 -50
- package/dist/sharedObjectSequence.d.ts.map +0 -1
- package/dist/sharedObjectSequence.js +0 -61
- package/dist/sharedObjectSequence.js.map +0 -1
- package/dist/sharedSequence.js.map +0 -1
- package/dist/sharedString.js +0 -187
- package/dist/sharedString.js.map +0 -1
- package/dist/sparsematrix.d.ts +0 -139
- package/dist/sparsematrix.d.ts.map +0 -1
- package/dist/sparsematrix.js +0 -332
- package/dist/sparsematrix.js.map +0 -1
- package/lib/defaultMap.d.ts.map +0 -1
- package/lib/defaultMap.js.map +0 -1
- package/lib/defaultMapInterfaces.d.ts.map +0 -1
- package/lib/defaultMapInterfaces.js.map +0 -1
- package/lib/index.d.ts +0 -27
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -26
- package/lib/index.js.map +0 -1
- package/lib/intervalCollection.d.ts +0 -270
- package/lib/intervalCollection.d.ts.map +0 -1
- package/lib/intervalCollection.js +0 -1238
- package/lib/intervalCollection.js.map +0 -1
- package/lib/localValues.d.ts.map +0 -1
- package/lib/localValues.js.map +0 -1
- package/lib/packageVersion.js.map +0 -1
- package/lib/sequence.d.ts.map +0 -1
- package/lib/sequence.js.map +0 -1
- package/lib/sequenceDeltaEvent.d.ts.map +0 -1
- package/lib/sequenceDeltaEvent.js.map +0 -1
- package/lib/sequenceFactory.d.ts +0 -108
- package/lib/sequenceFactory.d.ts.map +0 -1
- package/lib/sequenceFactory.js +0 -186
- package/lib/sequenceFactory.js.map +0 -1
- package/lib/sharedIntervalCollection.d.ts.map +0 -1
- package/lib/sharedIntervalCollection.js.map +0 -1
- package/lib/sharedNumberSequence.d.ts +0 -50
- package/lib/sharedNumberSequence.d.ts.map +0 -1
- package/lib/sharedNumberSequence.js +0 -57
- package/lib/sharedNumberSequence.js.map +0 -1
- package/lib/sharedObjectSequence.d.ts +0 -50
- package/lib/sharedObjectSequence.d.ts.map +0 -1
- package/lib/sharedObjectSequence.js +0 -57
- package/lib/sharedObjectSequence.js.map +0 -1
- package/lib/sharedSequence.d.ts.map +0 -1
- package/lib/sharedSequence.js.map +0 -1
- package/lib/sharedString.d.ts.map +0 -1
- package/lib/sharedString.js +0 -183
- package/lib/sharedString.js.map +0 -1
- package/lib/sparsematrix.d.ts +0 -139
- package/lib/sparsematrix.d.ts.map +0 -1
- package/lib/sparsematrix.js +0 -323
- package/lib/sparsematrix.js.map +0 -1
- package/src/sharedNumberSequence.ts +0 -62
- package/src/sharedObjectSequence.ts +0 -62
- package/src/sparsematrix.ts +0 -421
- package/tsconfig.esnext.json +0 -7
|
@@ -0,0 +1,1144 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { TypedEventEmitter } from "@fluid-internal/client-utils";
|
|
6
|
+
import { assert } from "@fluidframework/core-utils";
|
|
7
|
+
import { addProperties, createMap, getSlideToSegoff, MergeTreeDeltaType, ReferenceType, refTypeIncludesFlag, reservedRangeLabelsKey, UnassignedSequenceNumber, DetachedReferencePosition, UniversalSequenceNumber, SlidingPreference, } from "@fluidframework/merge-tree";
|
|
8
|
+
import { LoggingError, UsageError } from "@fluidframework/telemetry-utils";
|
|
9
|
+
import { v4 as uuid } from "uuid";
|
|
10
|
+
import { IntervalOpType, IntervalStickiness, IntervalType, SequenceInterval, createPositionReferenceFromSegoff, endReferenceSlidingPreference, startReferenceSlidingPreference, sequenceIntervalHelpers, createInterval, } from "./intervals/index.mjs";
|
|
11
|
+
import { EndpointIndex, OverlappingIntervalsIndex, createIdIntervalIndex, } from "./intervalIndex/index.mjs";
|
|
12
|
+
/**
|
|
13
|
+
* Defines a side relative to a character in a sequence.
|
|
14
|
+
*
|
|
15
|
+
* @remarks See {@link SequencePlace} for additional context on usage.
|
|
16
|
+
* @alpha
|
|
17
|
+
*/
|
|
18
|
+
export var Side;
|
|
19
|
+
(function (Side) {
|
|
20
|
+
Side[Side["Before"] = 0] = "Before";
|
|
21
|
+
Side[Side["After"] = 1] = "After";
|
|
22
|
+
})(Side || (Side = {}));
|
|
23
|
+
const reservedIntervalIdKey = "intervalId";
|
|
24
|
+
export function sidesFromStickiness(stickiness) {
|
|
25
|
+
const startSide = (stickiness & IntervalStickiness.START) !== 0 ? Side.After : Side.Before;
|
|
26
|
+
const endSide = (stickiness & IntervalStickiness.END) !== 0 ? Side.Before : Side.After;
|
|
27
|
+
return { startSide, endSide };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Decompress an interval after loading a summary from JSON. The exact format
|
|
31
|
+
* of this compression is unspecified and subject to change
|
|
32
|
+
*/
|
|
33
|
+
function decompressInterval(interval, label) {
|
|
34
|
+
const stickiness = interval[5] ?? IntervalStickiness.END;
|
|
35
|
+
const { startSide, endSide } = sidesFromStickiness(stickiness);
|
|
36
|
+
return {
|
|
37
|
+
start: interval[0],
|
|
38
|
+
end: interval[1],
|
|
39
|
+
sequenceNumber: interval[2],
|
|
40
|
+
intervalType: interval[3],
|
|
41
|
+
properties: { ...interval[4], [reservedRangeLabelsKey]: [label] },
|
|
42
|
+
stickiness,
|
|
43
|
+
startSide,
|
|
44
|
+
endSide,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Compress an interval prior to serialization as JSON. The exact format of this
|
|
49
|
+
* compression is unspecified and subject to change
|
|
50
|
+
*/
|
|
51
|
+
function compressInterval(interval) {
|
|
52
|
+
const { start, end, sequenceNumber, intervalType, properties } = interval;
|
|
53
|
+
let base = [
|
|
54
|
+
start,
|
|
55
|
+
end,
|
|
56
|
+
sequenceNumber,
|
|
57
|
+
intervalType,
|
|
58
|
+
// remove the `referenceRangeLabels` property as it is already stored
|
|
59
|
+
// in the `label` field of the summary
|
|
60
|
+
{ ...properties, [reservedRangeLabelsKey]: undefined },
|
|
61
|
+
];
|
|
62
|
+
if (interval.stickiness !== undefined && interval.stickiness !== IntervalStickiness.END) {
|
|
63
|
+
// reassignment to make it easier for typescript to reason about types
|
|
64
|
+
base = [...base, interval.stickiness];
|
|
65
|
+
}
|
|
66
|
+
return base;
|
|
67
|
+
}
|
|
68
|
+
export function endpointPosAndSide(start, end) {
|
|
69
|
+
const startIsPlainEndpoint = typeof start === "number" || start === "start" || start === "end";
|
|
70
|
+
const endIsPlainEndpoint = typeof end === "number" || end === "start" || end === "end";
|
|
71
|
+
const startSide = startIsPlainEndpoint ? Side.Before : start?.side;
|
|
72
|
+
const endSide = endIsPlainEndpoint ? Side.Before : end?.side;
|
|
73
|
+
const startPos = startIsPlainEndpoint ? start : start?.pos;
|
|
74
|
+
const endPos = endIsPlainEndpoint ? end : end?.pos;
|
|
75
|
+
return {
|
|
76
|
+
startSide,
|
|
77
|
+
endSide,
|
|
78
|
+
startPos,
|
|
79
|
+
endPos,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function toSequencePlace(pos, side) {
|
|
83
|
+
return typeof pos === "number" ? { pos, side } : pos;
|
|
84
|
+
}
|
|
85
|
+
function toOptionalSequencePlace(pos, side = Side.Before) {
|
|
86
|
+
return typeof pos === "number" ? { pos, side } : pos;
|
|
87
|
+
}
|
|
88
|
+
export function computeStickinessFromSide(startPos = -1, startSide = Side.Before, endPos = -1, endSide = Side.Before) {
|
|
89
|
+
let stickiness = IntervalStickiness.NONE;
|
|
90
|
+
if (startSide === Side.After || startPos === "start") {
|
|
91
|
+
stickiness |= IntervalStickiness.START;
|
|
92
|
+
}
|
|
93
|
+
if (endSide === Side.Before || endPos === "end") {
|
|
94
|
+
stickiness |= IntervalStickiness.END;
|
|
95
|
+
}
|
|
96
|
+
return stickiness;
|
|
97
|
+
}
|
|
98
|
+
export function createIntervalIndex() {
|
|
99
|
+
const helpers = {
|
|
100
|
+
create: createInterval,
|
|
101
|
+
};
|
|
102
|
+
const lc = new LocalIntervalCollection(undefined, "", helpers, {});
|
|
103
|
+
return lc;
|
|
104
|
+
}
|
|
105
|
+
export class LocalIntervalCollection {
|
|
106
|
+
constructor(client, label, helpers, options,
|
|
107
|
+
/** Callback invoked each time one of the endpoints of an interval slides. */
|
|
108
|
+
onPositionChange) {
|
|
109
|
+
this.client = client;
|
|
110
|
+
this.label = label;
|
|
111
|
+
this.helpers = helpers;
|
|
112
|
+
this.options = options;
|
|
113
|
+
this.onPositionChange = onPositionChange;
|
|
114
|
+
this.overlappingIntervalsIndex = new OverlappingIntervalsIndex(client, helpers);
|
|
115
|
+
this.idIntervalIndex = createIdIntervalIndex();
|
|
116
|
+
this.endIntervalIndex = new EndpointIndex(client, helpers);
|
|
117
|
+
this.indexes = new Set([
|
|
118
|
+
this.overlappingIntervalsIndex,
|
|
119
|
+
this.idIntervalIndex,
|
|
120
|
+
this.endIntervalIndex,
|
|
121
|
+
]);
|
|
122
|
+
}
|
|
123
|
+
createLegacyId(start, end) {
|
|
124
|
+
// Create a non-unique ID based on start and end to be used on intervals that come from legacy clients
|
|
125
|
+
// without ID's.
|
|
126
|
+
return `${LocalIntervalCollection.legacyIdPrefix}${start}-${end}`;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Validates that a serialized interval has the ID property. Creates an ID
|
|
130
|
+
* if one does not already exist
|
|
131
|
+
*
|
|
132
|
+
* @param serializedInterval - The interval to be checked
|
|
133
|
+
* @returns The interval's existing or newly created id
|
|
134
|
+
*/
|
|
135
|
+
ensureSerializedId(serializedInterval) {
|
|
136
|
+
let id = serializedInterval.properties?.[reservedIntervalIdKey];
|
|
137
|
+
if (id === undefined) {
|
|
138
|
+
// Back-compat: 0.39 and earlier did not have IDs on intervals. If an interval from such a client
|
|
139
|
+
// comes over the wire, create a non-unique one based on start/end.
|
|
140
|
+
// This will allow all clients to refer to this interval consistently.
|
|
141
|
+
id = this.createLegacyId(serializedInterval.start, serializedInterval.end);
|
|
142
|
+
const newProps = {
|
|
143
|
+
[reservedIntervalIdKey]: id,
|
|
144
|
+
};
|
|
145
|
+
serializedInterval.properties = addProperties(serializedInterval.properties, newProps);
|
|
146
|
+
}
|
|
147
|
+
// Make the ID immutable for safety's sake.
|
|
148
|
+
Object.defineProperty(serializedInterval.properties, reservedIntervalIdKey, {
|
|
149
|
+
configurable: false,
|
|
150
|
+
enumerable: true,
|
|
151
|
+
writable: false,
|
|
152
|
+
});
|
|
153
|
+
return id;
|
|
154
|
+
}
|
|
155
|
+
removeIntervalFromIndexes(interval) {
|
|
156
|
+
for (const index of this.indexes) {
|
|
157
|
+
index.remove(interval);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
appendIndex(index) {
|
|
161
|
+
this.indexes.add(index);
|
|
162
|
+
}
|
|
163
|
+
removeIndex(index) {
|
|
164
|
+
return this.indexes.delete(index);
|
|
165
|
+
}
|
|
166
|
+
removeExistingInterval(interval) {
|
|
167
|
+
this.removeIntervalFromIndexes(interval);
|
|
168
|
+
this.removeIntervalListeners(interval);
|
|
169
|
+
}
|
|
170
|
+
createInterval(start, end, intervalType, op) {
|
|
171
|
+
return this.helpers.create(this.label, start, end, this.client, intervalType, op, undefined, this.options.mergeTreeReferencesCanSlideToEndpoint);
|
|
172
|
+
}
|
|
173
|
+
addInterval(start, end, intervalType, props, op) {
|
|
174
|
+
var _a;
|
|
175
|
+
const interval = this.createInterval(start, end, intervalType, op);
|
|
176
|
+
if (interval) {
|
|
177
|
+
if (!interval.properties) {
|
|
178
|
+
interval.properties = createMap();
|
|
179
|
+
}
|
|
180
|
+
if (props) {
|
|
181
|
+
// This check is intended to prevent scenarios where a random interval is created and then
|
|
182
|
+
// inserted into a collection. The aim is to ensure that the collection is created first
|
|
183
|
+
// then the user can create/add intervals based on the collection
|
|
184
|
+
if (props[reservedRangeLabelsKey] !== undefined &&
|
|
185
|
+
props[reservedRangeLabelsKey][0] !== this.label) {
|
|
186
|
+
throw new LoggingError("Adding an interval that belongs to another interval collection is not permitted");
|
|
187
|
+
}
|
|
188
|
+
interval.addProperties(props);
|
|
189
|
+
}
|
|
190
|
+
(_a = interval.properties)[reservedIntervalIdKey] ?? (_a[reservedIntervalIdKey] = uuid());
|
|
191
|
+
this.add(interval);
|
|
192
|
+
}
|
|
193
|
+
return interval;
|
|
194
|
+
}
|
|
195
|
+
linkEndpointsToInterval(interval) {
|
|
196
|
+
if (interval instanceof SequenceInterval) {
|
|
197
|
+
interval.start.addProperties({ interval });
|
|
198
|
+
interval.end.addProperties({ interval });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
addIntervalToIndexes(interval) {
|
|
202
|
+
for (const index of this.indexes) {
|
|
203
|
+
index.add(interval);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
add(interval) {
|
|
207
|
+
this.linkEndpointsToInterval(interval);
|
|
208
|
+
this.addIntervalToIndexes(interval);
|
|
209
|
+
this.addIntervalListeners(interval);
|
|
210
|
+
}
|
|
211
|
+
changeInterval(interval, start, end, op, localSeq) {
|
|
212
|
+
const newInterval = interval.modify(this.label, start, end, op, localSeq, this.options.mergeTreeReferencesCanSlideToEndpoint);
|
|
213
|
+
if (newInterval) {
|
|
214
|
+
this.removeExistingInterval(interval);
|
|
215
|
+
this.add(newInterval);
|
|
216
|
+
}
|
|
217
|
+
return newInterval;
|
|
218
|
+
}
|
|
219
|
+
serialize() {
|
|
220
|
+
return {
|
|
221
|
+
label: this.label,
|
|
222
|
+
intervals: Array.from(this.idIntervalIndex, (interval) => compressInterval(interval.serialize())),
|
|
223
|
+
version: 2,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
addIntervalListeners(interval) {
|
|
227
|
+
const cloneRef = (ref) => {
|
|
228
|
+
const segment = ref.getSegment();
|
|
229
|
+
if (segment === undefined) {
|
|
230
|
+
// Cloning is unnecessary: refs which have slid off the string entirely
|
|
231
|
+
// never get slid back on. Creation code for refs doesn't accept undefined segment
|
|
232
|
+
// either, so this must be special-cased.
|
|
233
|
+
return ref;
|
|
234
|
+
}
|
|
235
|
+
return this.client.createLocalReferencePosition(segment, ref.getOffset(), ReferenceType.Transient, ref.properties, ref.slidingPreference, ref.canSlideToEndpoint);
|
|
236
|
+
};
|
|
237
|
+
if (interval instanceof SequenceInterval) {
|
|
238
|
+
let previousInterval;
|
|
239
|
+
let pendingChanges = 0;
|
|
240
|
+
interval.addPositionChangeListeners(() => {
|
|
241
|
+
pendingChanges++;
|
|
242
|
+
// Note: both start and end can change and invoke beforeSlide on each endpoint before afterSlide.
|
|
243
|
+
if (!previousInterval) {
|
|
244
|
+
previousInterval = interval.clone();
|
|
245
|
+
previousInterval.start = cloneRef(previousInterval.start);
|
|
246
|
+
previousInterval.end = cloneRef(previousInterval.end);
|
|
247
|
+
this.removeIntervalFromIndexes(interval);
|
|
248
|
+
}
|
|
249
|
+
}, () => {
|
|
250
|
+
assert(previousInterval !== undefined, 0x3fa /* Invalid interleaving of before/after slide */);
|
|
251
|
+
pendingChanges--;
|
|
252
|
+
if (pendingChanges === 0) {
|
|
253
|
+
this.addIntervalToIndexes(interval);
|
|
254
|
+
this.onPositionChange?.(interval, previousInterval);
|
|
255
|
+
previousInterval = undefined;
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
removeIntervalListeners(interval) {
|
|
261
|
+
if (interval instanceof SequenceInterval) {
|
|
262
|
+
interval.removePositionChangeListeners();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
LocalIntervalCollection.legacyIdPrefix = "legacy";
|
|
267
|
+
class SequenceIntervalCollectionFactory {
|
|
268
|
+
load(emitter, raw = [], options) {
|
|
269
|
+
return new IntervalCollection(sequenceIntervalHelpers, true, emitter, raw, options);
|
|
270
|
+
}
|
|
271
|
+
store(value) {
|
|
272
|
+
return value.serializeInternal();
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
export class SequenceIntervalCollectionValueType {
|
|
276
|
+
get name() {
|
|
277
|
+
return SequenceIntervalCollectionValueType.Name;
|
|
278
|
+
}
|
|
279
|
+
get factory() {
|
|
280
|
+
return SequenceIntervalCollectionValueType._factory;
|
|
281
|
+
}
|
|
282
|
+
get ops() {
|
|
283
|
+
return SequenceIntervalCollectionValueType._ops;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
SequenceIntervalCollectionValueType.Name = "sharedStringIntervalCollection";
|
|
287
|
+
SequenceIntervalCollectionValueType._factory = new SequenceIntervalCollectionFactory();
|
|
288
|
+
SequenceIntervalCollectionValueType._ops = makeOpsMap();
|
|
289
|
+
class IntervalCollectionFactory {
|
|
290
|
+
load(emitter, raw = [], options) {
|
|
291
|
+
const helpers = {
|
|
292
|
+
create: createInterval,
|
|
293
|
+
};
|
|
294
|
+
const collection = new IntervalCollection(helpers, false, emitter, raw, options);
|
|
295
|
+
collection.attachGraph(undefined, "");
|
|
296
|
+
return collection;
|
|
297
|
+
}
|
|
298
|
+
store(value) {
|
|
299
|
+
return value.serializeInternal();
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
export class IntervalCollectionValueType {
|
|
303
|
+
get name() {
|
|
304
|
+
return IntervalCollectionValueType.Name;
|
|
305
|
+
}
|
|
306
|
+
get factory() {
|
|
307
|
+
return IntervalCollectionValueType._factory;
|
|
308
|
+
}
|
|
309
|
+
get ops() {
|
|
310
|
+
return IntervalCollectionValueType._ops;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
IntervalCollectionValueType.Name = "sharedIntervalCollection";
|
|
314
|
+
IntervalCollectionValueType._factory = new IntervalCollectionFactory();
|
|
315
|
+
IntervalCollectionValueType._ops = makeOpsMap();
|
|
316
|
+
export function makeOpsMap() {
|
|
317
|
+
const rebase = (collection, op, localOpMetadata) => {
|
|
318
|
+
const { localSeq } = localOpMetadata;
|
|
319
|
+
const rebasedValue = collection.rebaseLocalInterval(op.opName, op.value, localSeq);
|
|
320
|
+
if (rebasedValue === undefined) {
|
|
321
|
+
return undefined;
|
|
322
|
+
}
|
|
323
|
+
const rebasedOp = { ...op, value: rebasedValue };
|
|
324
|
+
return { rebasedOp, rebasedLocalOpMetadata: localOpMetadata };
|
|
325
|
+
};
|
|
326
|
+
return new Map([
|
|
327
|
+
[
|
|
328
|
+
IntervalOpType.ADD,
|
|
329
|
+
{
|
|
330
|
+
process: (collection, params, local, op, localOpMetadata) => {
|
|
331
|
+
// if params is undefined, the interval was deleted during
|
|
332
|
+
// rebasing
|
|
333
|
+
if (!params) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
assert(op !== undefined, 0x3fb /* op should exist here */);
|
|
337
|
+
collection.ackAdd(params, local, op, localOpMetadata);
|
|
338
|
+
},
|
|
339
|
+
rebase,
|
|
340
|
+
},
|
|
341
|
+
],
|
|
342
|
+
[
|
|
343
|
+
IntervalOpType.DELETE,
|
|
344
|
+
{
|
|
345
|
+
process: (collection, params, local, op) => {
|
|
346
|
+
assert(op !== undefined, 0x3fc /* op should exist here */);
|
|
347
|
+
collection.ackDelete(params, local, op);
|
|
348
|
+
},
|
|
349
|
+
rebase: (collection, op, localOpMetadata) => {
|
|
350
|
+
// Deletion of intervals is based on id, so requires no rebasing.
|
|
351
|
+
return { rebasedOp: op, rebasedLocalOpMetadata: localOpMetadata };
|
|
352
|
+
},
|
|
353
|
+
},
|
|
354
|
+
],
|
|
355
|
+
[
|
|
356
|
+
IntervalOpType.CHANGE,
|
|
357
|
+
{
|
|
358
|
+
process: (collection, params, local, op, localOpMetadata) => {
|
|
359
|
+
// if params is undefined, the interval was deleted during
|
|
360
|
+
// rebasing
|
|
361
|
+
if (!params) {
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
assert(op !== undefined, 0x3fd /* op should exist here */);
|
|
365
|
+
collection.ackChange(params, local, op, localOpMetadata);
|
|
366
|
+
},
|
|
367
|
+
rebase,
|
|
368
|
+
},
|
|
369
|
+
],
|
|
370
|
+
]);
|
|
371
|
+
}
|
|
372
|
+
class IntervalCollectionIterator {
|
|
373
|
+
constructor(collection, iteratesForward = true, start, end) {
|
|
374
|
+
this.results = [];
|
|
375
|
+
this.index = 0;
|
|
376
|
+
collection.gatherIterationResults(this.results, iteratesForward, start, end);
|
|
377
|
+
}
|
|
378
|
+
next() {
|
|
379
|
+
if (this.index < this.results.length) {
|
|
380
|
+
return {
|
|
381
|
+
value: this.results[this.index++],
|
|
382
|
+
done: false,
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
return {
|
|
386
|
+
value: undefined,
|
|
387
|
+
done: true,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
// solely for type checking in the implementation of add - will be removed once
|
|
392
|
+
// deprecated signatures are removed
|
|
393
|
+
const isSequencePlace = (place) => {
|
|
394
|
+
return typeof place === "number" || typeof place === "string" || place.pos !== undefined;
|
|
395
|
+
};
|
|
396
|
+
/**
|
|
397
|
+
* {@inheritdoc IIntervalCollection}
|
|
398
|
+
*/
|
|
399
|
+
export class IntervalCollection extends TypedEventEmitter {
|
|
400
|
+
get attached() {
|
|
401
|
+
return !!this.localCollection;
|
|
402
|
+
}
|
|
403
|
+
/** @internal */
|
|
404
|
+
constructor(helpers, requiresClient, emitter, serializedIntervals, options = {}) {
|
|
405
|
+
super();
|
|
406
|
+
this.helpers = helpers;
|
|
407
|
+
this.requiresClient = requiresClient;
|
|
408
|
+
this.emitter = emitter;
|
|
409
|
+
this.options = options;
|
|
410
|
+
this.localSeqToSerializedInterval = new Map();
|
|
411
|
+
this.localSeqToRebasedInterval = new Map();
|
|
412
|
+
this.pendingChangesStart = new Map();
|
|
413
|
+
this.pendingChangesEnd = new Map();
|
|
414
|
+
this.savedSerializedIntervals = Array.isArray(serializedIntervals)
|
|
415
|
+
? serializedIntervals
|
|
416
|
+
: serializedIntervals.intervals.map((i) => decompressInterval(i, serializedIntervals.label));
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* {@inheritdoc IIntervalCollection.attachIndex}
|
|
420
|
+
*/
|
|
421
|
+
attachIndex(index) {
|
|
422
|
+
if (!this.attached) {
|
|
423
|
+
throw new LoggingError("The local interval collection must exist");
|
|
424
|
+
}
|
|
425
|
+
for (const interval of this) {
|
|
426
|
+
index.add(interval);
|
|
427
|
+
}
|
|
428
|
+
this.localCollection?.appendIndex(index);
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* {@inheritdoc IIntervalCollection.detachIndex}
|
|
432
|
+
*/
|
|
433
|
+
detachIndex(index) {
|
|
434
|
+
if (!this.attached) {
|
|
435
|
+
throw new LoggingError("The local interval collection must exist");
|
|
436
|
+
}
|
|
437
|
+
// Avoid removing intervals if the index does not exist
|
|
438
|
+
if (!this.localCollection?.removeIndex(index)) {
|
|
439
|
+
return false;
|
|
440
|
+
}
|
|
441
|
+
for (const interval of this) {
|
|
442
|
+
index.remove(interval);
|
|
443
|
+
}
|
|
444
|
+
return true;
|
|
445
|
+
}
|
|
446
|
+
rebasePositionWithSegmentSlide(pos, seqNumberFrom, localSeq) {
|
|
447
|
+
if (!this.client) {
|
|
448
|
+
throw new LoggingError("mergeTree client must exist");
|
|
449
|
+
}
|
|
450
|
+
if (pos === "start" || pos === "end") {
|
|
451
|
+
return pos;
|
|
452
|
+
}
|
|
453
|
+
const { clientId } = this.client.getCollabWindow();
|
|
454
|
+
const { segment, offset } = this.client.getContainingSegment(pos, {
|
|
455
|
+
referenceSequenceNumber: seqNumberFrom,
|
|
456
|
+
clientId: this.client.getLongClientId(clientId),
|
|
457
|
+
}, localSeq);
|
|
458
|
+
// if segment is undefined, it slid off the string
|
|
459
|
+
assert(segment !== undefined, 0x54e /* No segment found */);
|
|
460
|
+
const segoff = getSlideToSegoff({ segment, offset }, undefined, this.options.mergeTreeReferencesCanSlideToEndpoint) ?? segment;
|
|
461
|
+
// case happens when rebasing op, but concurrently entire string has been deleted
|
|
462
|
+
if (segoff.segment === undefined || segoff.offset === undefined) {
|
|
463
|
+
return DetachedReferencePosition;
|
|
464
|
+
}
|
|
465
|
+
assert(offset !== undefined && 0 <= offset && offset < segment.cachedLength, 0x54f /* Invalid offset */);
|
|
466
|
+
return this.client.findReconnectionPosition(segoff.segment, localSeq) + segoff.offset;
|
|
467
|
+
}
|
|
468
|
+
computeRebasedPositions(localSeq) {
|
|
469
|
+
assert(this.client !== undefined, 0x550 /* Client should be defined when computing rebased position */);
|
|
470
|
+
const original = this.localSeqToSerializedInterval.get(localSeq);
|
|
471
|
+
assert(original !== undefined, 0x551 /* Failed to store pending serialized interval info for this localSeq. */);
|
|
472
|
+
const rebased = { ...original };
|
|
473
|
+
const { start, end, sequenceNumber } = original;
|
|
474
|
+
if (start !== undefined) {
|
|
475
|
+
rebased.start = this.rebasePositionWithSegmentSlide(start, sequenceNumber, localSeq);
|
|
476
|
+
}
|
|
477
|
+
if (end !== undefined) {
|
|
478
|
+
rebased.end = this.rebasePositionWithSegmentSlide(end, sequenceNumber, localSeq);
|
|
479
|
+
}
|
|
480
|
+
return rebased;
|
|
481
|
+
}
|
|
482
|
+
/** @internal */
|
|
483
|
+
attachGraph(client, label) {
|
|
484
|
+
if (this.attached) {
|
|
485
|
+
throw new LoggingError("Only supports one Sequence attach");
|
|
486
|
+
}
|
|
487
|
+
if (client === undefined && this.requiresClient) {
|
|
488
|
+
throw new LoggingError("Client required for this collection");
|
|
489
|
+
}
|
|
490
|
+
// Instantiate the local interval collection based on the saved intervals
|
|
491
|
+
this.client = client;
|
|
492
|
+
if (client) {
|
|
493
|
+
client.on("normalize", () => {
|
|
494
|
+
for (const localSeq of this.localSeqToSerializedInterval.keys()) {
|
|
495
|
+
this.localSeqToRebasedInterval.set(localSeq, this.computeRebasedPositions(localSeq));
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
this.localCollection = new LocalIntervalCollection(client, label, this.helpers, this.options, (interval, previousInterval) => this.emitChange(interval, previousInterval, true, true));
|
|
500
|
+
if (this.savedSerializedIntervals) {
|
|
501
|
+
for (const serializedInterval of this.savedSerializedIntervals) {
|
|
502
|
+
this.localCollection.ensureSerializedId(serializedInterval);
|
|
503
|
+
const { start: startPos, end: endPos, intervalType, properties, startSide, endSide, } = serializedInterval;
|
|
504
|
+
const start = typeof startPos === "number" && startSide !== undefined
|
|
505
|
+
? { pos: startPos, side: startSide }
|
|
506
|
+
: startPos;
|
|
507
|
+
const end = typeof endPos === "number" && endSide !== undefined
|
|
508
|
+
? { pos: endPos, side: endSide }
|
|
509
|
+
: endPos;
|
|
510
|
+
const interval = this.helpers.create(label, start, end, client, intervalType, undefined, true, this.options.mergeTreeReferencesCanSlideToEndpoint);
|
|
511
|
+
if (properties) {
|
|
512
|
+
interval.addProperties(properties);
|
|
513
|
+
}
|
|
514
|
+
this.localCollection.add(interval);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
this.savedSerializedIntervals = undefined;
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Gets the next local sequence number, modifying this client's collab window in doing so.
|
|
521
|
+
*/
|
|
522
|
+
getNextLocalSeq() {
|
|
523
|
+
if (this.client) {
|
|
524
|
+
return ++this.client.getCollabWindow().localSeq;
|
|
525
|
+
}
|
|
526
|
+
return 0;
|
|
527
|
+
}
|
|
528
|
+
emitChange(interval, previousInterval, local, slide, op) {
|
|
529
|
+
// Temporarily make references transient so that positional queries work (non-transient refs
|
|
530
|
+
// on resolve to DetachedPosition on any segments that don't contain them). The original refType
|
|
531
|
+
// is restored as single-endpoint changes re-use previous references.
|
|
532
|
+
let startRefType;
|
|
533
|
+
let endRefType;
|
|
534
|
+
if (previousInterval instanceof SequenceInterval) {
|
|
535
|
+
startRefType = previousInterval.start.refType;
|
|
536
|
+
endRefType = previousInterval.end.refType;
|
|
537
|
+
previousInterval.start.refType = ReferenceType.Transient;
|
|
538
|
+
previousInterval.end.refType = ReferenceType.Transient;
|
|
539
|
+
this.emit("changeInterval", interval, previousInterval, local, op, slide);
|
|
540
|
+
previousInterval.start.refType = startRefType;
|
|
541
|
+
previousInterval.end.refType = endRefType;
|
|
542
|
+
}
|
|
543
|
+
else {
|
|
544
|
+
this.emit("changeInterval", interval, previousInterval, local, op, slide);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* {@inheritdoc IIntervalCollection.getIntervalById}
|
|
549
|
+
*/
|
|
550
|
+
getIntervalById(id) {
|
|
551
|
+
if (!this.localCollection) {
|
|
552
|
+
throw new LoggingError("attach must be called before accessing intervals");
|
|
553
|
+
}
|
|
554
|
+
return this.localCollection.idIntervalIndex.getIntervalById(id);
|
|
555
|
+
}
|
|
556
|
+
assertStickinessEnabled(start, end) {
|
|
557
|
+
if (!(typeof start === "number" && typeof end === "number") &&
|
|
558
|
+
!this.options.intervalStickinessEnabled) {
|
|
559
|
+
throw new UsageError("attempted to set interval stickiness without enabling `intervalStickinessEnabled` feature flag");
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
add(start, end, intervalType, props) {
|
|
563
|
+
let intStart;
|
|
564
|
+
let intEnd;
|
|
565
|
+
let type;
|
|
566
|
+
let properties;
|
|
567
|
+
if (isSequencePlace(start)) {
|
|
568
|
+
intStart = start;
|
|
569
|
+
assert(end !== undefined, 0x7c0 /* end must be defined */);
|
|
570
|
+
intEnd = end;
|
|
571
|
+
assert(intervalType !== undefined, 0x7c1 /* intervalType must be defined */);
|
|
572
|
+
type = intervalType;
|
|
573
|
+
properties = props;
|
|
574
|
+
}
|
|
575
|
+
else {
|
|
576
|
+
intStart = start.start;
|
|
577
|
+
intEnd = start.end;
|
|
578
|
+
type = IntervalType.SlideOnRemove;
|
|
579
|
+
properties = start.props;
|
|
580
|
+
}
|
|
581
|
+
if (!this.localCollection) {
|
|
582
|
+
throw new LoggingError("attach must be called prior to adding intervals");
|
|
583
|
+
}
|
|
584
|
+
if (type & IntervalType.Transient) {
|
|
585
|
+
throw new LoggingError("Can not add transient intervals");
|
|
586
|
+
}
|
|
587
|
+
const { startSide, endSide, startPos, endPos } = endpointPosAndSide(intStart, intEnd);
|
|
588
|
+
assert(startPos !== undefined &&
|
|
589
|
+
endPos !== undefined &&
|
|
590
|
+
startSide !== undefined &&
|
|
591
|
+
endSide !== undefined, 0x793 /* start and end cannot be undefined because they were not passed in as undefined */);
|
|
592
|
+
const stickiness = computeStickinessFromSide(startPos, startSide, endPos, endSide);
|
|
593
|
+
this.assertStickinessEnabled(intStart, intEnd);
|
|
594
|
+
const interval = this.localCollection.addInterval(toSequencePlace(startPos, startSide), toSequencePlace(endPos, endSide), type, properties);
|
|
595
|
+
if (interval) {
|
|
596
|
+
if (!this.isCollaborating && interval instanceof SequenceInterval) {
|
|
597
|
+
setSlideOnRemove(interval.start);
|
|
598
|
+
setSlideOnRemove(interval.end);
|
|
599
|
+
}
|
|
600
|
+
const serializedInterval = {
|
|
601
|
+
start: startPos,
|
|
602
|
+
end: endPos,
|
|
603
|
+
intervalType: type,
|
|
604
|
+
properties: interval.properties,
|
|
605
|
+
sequenceNumber: this.client?.getCurrentSeq() ?? 0,
|
|
606
|
+
stickiness,
|
|
607
|
+
startSide,
|
|
608
|
+
endSide,
|
|
609
|
+
};
|
|
610
|
+
const localSeq = this.getNextLocalSeq();
|
|
611
|
+
this.localSeqToSerializedInterval.set(localSeq, serializedInterval);
|
|
612
|
+
// Local ops get submitted to the server. Remote ops have the deserializer run.
|
|
613
|
+
this.emitter.emit("add", undefined, serializedInterval, { localSeq });
|
|
614
|
+
}
|
|
615
|
+
this.emit("addInterval", interval, true, undefined);
|
|
616
|
+
return interval;
|
|
617
|
+
}
|
|
618
|
+
deleteExistingInterval(interval, local, op) {
|
|
619
|
+
if (!this.localCollection) {
|
|
620
|
+
throw new LoggingError("Attach must be called before accessing intervals");
|
|
621
|
+
}
|
|
622
|
+
// The given interval is known to exist in the collection.
|
|
623
|
+
this.localCollection.removeExistingInterval(interval);
|
|
624
|
+
if (interval) {
|
|
625
|
+
// Local ops get submitted to the server. Remote ops have the deserializer run.
|
|
626
|
+
if (local) {
|
|
627
|
+
this.emitter.emit("delete", undefined, interval.serialize(), {
|
|
628
|
+
localSeq: this.getNextLocalSeq(),
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
else {
|
|
632
|
+
if (this.onDeserialize) {
|
|
633
|
+
this.onDeserialize(interval);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
this.emit("deleteInterval", interval, local, op);
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* {@inheritdoc IIntervalCollection.removeIntervalById}
|
|
641
|
+
*/
|
|
642
|
+
removeIntervalById(id) {
|
|
643
|
+
if (!this.localCollection) {
|
|
644
|
+
throw new LoggingError("Attach must be called before accessing intervals");
|
|
645
|
+
}
|
|
646
|
+
const interval = this.localCollection.idIntervalIndex.getIntervalById(id);
|
|
647
|
+
if (interval) {
|
|
648
|
+
this.deleteExistingInterval(interval, true, undefined);
|
|
649
|
+
}
|
|
650
|
+
return interval;
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* {@inheritdoc IIntervalCollection.changeProperties}
|
|
654
|
+
* @deprecated - call change with the id and an object containing the new props values
|
|
655
|
+
*/
|
|
656
|
+
changeProperties(id, props) {
|
|
657
|
+
this.change(id, { props });
|
|
658
|
+
}
|
|
659
|
+
change(arg1, arg2, arg3) {
|
|
660
|
+
const id = arg1;
|
|
661
|
+
let start;
|
|
662
|
+
let end;
|
|
663
|
+
let props;
|
|
664
|
+
if (isSequencePlace(arg2)) {
|
|
665
|
+
start = arg2;
|
|
666
|
+
end = arg3;
|
|
667
|
+
}
|
|
668
|
+
else {
|
|
669
|
+
start = arg2.start;
|
|
670
|
+
end = arg2.end;
|
|
671
|
+
props = arg2.props;
|
|
672
|
+
}
|
|
673
|
+
if (!this.localCollection) {
|
|
674
|
+
throw new LoggingError("Attach must be called before accessing intervals");
|
|
675
|
+
}
|
|
676
|
+
// Force id to be a string.
|
|
677
|
+
if (typeof id !== "string") {
|
|
678
|
+
throw new UsageError("Change API requires an ID that is a string");
|
|
679
|
+
}
|
|
680
|
+
// Ensure that both start and end are defined or both are undefined.
|
|
681
|
+
if ((start === undefined) !== (end === undefined)) {
|
|
682
|
+
throw new UsageError("Change API requires both start and end to be defined or undefined");
|
|
683
|
+
}
|
|
684
|
+
// prevent the overwriting of an interval label, it should remain unchanged
|
|
685
|
+
// once it has been inserted into the collection.
|
|
686
|
+
if (props?.[reservedRangeLabelsKey] !== undefined) {
|
|
687
|
+
throw new UsageError("The label property should not be modified once inserted to the collection");
|
|
688
|
+
}
|
|
689
|
+
const interval = this.getIntervalById(id);
|
|
690
|
+
if (interval) {
|
|
691
|
+
let deltaProps;
|
|
692
|
+
let newInterval;
|
|
693
|
+
if (props !== undefined) {
|
|
694
|
+
deltaProps = interval.addProperties(props, true, this.isCollaborating ? UnassignedSequenceNumber : UniversalSequenceNumber);
|
|
695
|
+
}
|
|
696
|
+
if (start !== undefined && end !== undefined) {
|
|
697
|
+
newInterval = this.localCollection.changeInterval(interval, start, end);
|
|
698
|
+
if (!this.isCollaborating && newInterval instanceof SequenceInterval) {
|
|
699
|
+
setSlideOnRemove(newInterval.start);
|
|
700
|
+
setSlideOnRemove(newInterval.end);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
const serializedInterval = interval.serialize();
|
|
704
|
+
const { startPos, startSide, endPos, endSide } = endpointPosAndSide(start, end);
|
|
705
|
+
const stickiness = computeStickinessFromSide(startPos, startSide, endPos, endSide);
|
|
706
|
+
serializedInterval.start = startPos;
|
|
707
|
+
serializedInterval.end = endPos;
|
|
708
|
+
serializedInterval.startSide = startSide;
|
|
709
|
+
serializedInterval.endSide = endSide;
|
|
710
|
+
serializedInterval.stickiness = stickiness;
|
|
711
|
+
// Emit a property bag containing the ID and the other (if any) properties changed
|
|
712
|
+
serializedInterval.properties = {
|
|
713
|
+
[reservedIntervalIdKey]: interval.getIntervalId(),
|
|
714
|
+
...props,
|
|
715
|
+
};
|
|
716
|
+
const localSeq = this.getNextLocalSeq();
|
|
717
|
+
this.localSeqToSerializedInterval.set(localSeq, serializedInterval);
|
|
718
|
+
this.emitter.emit("change", undefined, serializedInterval, { localSeq });
|
|
719
|
+
if (deltaProps !== undefined) {
|
|
720
|
+
this.emit("propertyChanged", interval, deltaProps, true, undefined);
|
|
721
|
+
}
|
|
722
|
+
if (newInterval) {
|
|
723
|
+
this.addPendingChange(id, serializedInterval);
|
|
724
|
+
this.emitChange(newInterval, interval, true, false);
|
|
725
|
+
}
|
|
726
|
+
return newInterval;
|
|
727
|
+
}
|
|
728
|
+
// No interval to change
|
|
729
|
+
return undefined;
|
|
730
|
+
}
|
|
731
|
+
get isCollaborating() {
|
|
732
|
+
return this.client?.getCollabWindow().collaborating ?? false;
|
|
733
|
+
}
|
|
734
|
+
addPendingChange(id, serializedInterval) {
|
|
735
|
+
if (!this.isCollaborating) {
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
if (serializedInterval.start !== undefined) {
|
|
739
|
+
this.addPendingChangeHelper(id, this.pendingChangesStart, serializedInterval);
|
|
740
|
+
}
|
|
741
|
+
if (serializedInterval.end !== undefined) {
|
|
742
|
+
this.addPendingChangeHelper(id, this.pendingChangesEnd, serializedInterval);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
addPendingChangeHelper(id, pendingChanges, serializedInterval) {
|
|
746
|
+
let entries = pendingChanges.get(id);
|
|
747
|
+
if (!entries) {
|
|
748
|
+
entries = [];
|
|
749
|
+
pendingChanges.set(id, entries);
|
|
750
|
+
}
|
|
751
|
+
entries.push(serializedInterval);
|
|
752
|
+
}
|
|
753
|
+
removePendingChange(serializedInterval) {
|
|
754
|
+
// Change ops always have an ID.
|
|
755
|
+
const id = serializedInterval.properties?.[reservedIntervalIdKey];
|
|
756
|
+
if (serializedInterval.start !== undefined) {
|
|
757
|
+
this.removePendingChangeHelper(id, this.pendingChangesStart, serializedInterval);
|
|
758
|
+
}
|
|
759
|
+
if (serializedInterval.end !== undefined) {
|
|
760
|
+
this.removePendingChangeHelper(id, this.pendingChangesEnd, serializedInterval);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
removePendingChangeHelper(id, pendingChanges, serializedInterval) {
|
|
764
|
+
const entries = pendingChanges.get(id);
|
|
765
|
+
if (entries) {
|
|
766
|
+
const pendingChange = entries.shift();
|
|
767
|
+
if (entries.length === 0) {
|
|
768
|
+
pendingChanges.delete(id);
|
|
769
|
+
}
|
|
770
|
+
if (pendingChange?.start !== serializedInterval.start ||
|
|
771
|
+
pendingChange?.end !== serializedInterval.end) {
|
|
772
|
+
throw new LoggingError("Mismatch in pending changes");
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
hasPendingChangeStart(id) {
|
|
777
|
+
const entries = this.pendingChangesStart.get(id);
|
|
778
|
+
return entries && entries.length !== 0;
|
|
779
|
+
}
|
|
780
|
+
hasPendingChangeEnd(id) {
|
|
781
|
+
const entries = this.pendingChangesEnd.get(id);
|
|
782
|
+
return entries && entries.length !== 0;
|
|
783
|
+
}
|
|
784
|
+
/** @internal */
|
|
785
|
+
ackChange(serializedInterval, local, op, localOpMetadata) {
|
|
786
|
+
if (!this.localCollection) {
|
|
787
|
+
throw new LoggingError("Attach must be called before accessing intervals");
|
|
788
|
+
}
|
|
789
|
+
if (local) {
|
|
790
|
+
assert(localOpMetadata !== undefined, 0x552 /* op metadata should be defined for local op */);
|
|
791
|
+
this.localSeqToSerializedInterval.delete(localOpMetadata?.localSeq);
|
|
792
|
+
// This is an ack from the server. Remove the pending change.
|
|
793
|
+
this.removePendingChange(serializedInterval);
|
|
794
|
+
}
|
|
795
|
+
// Note that the ID is in the property bag only to allow us to find the interval.
|
|
796
|
+
// This API cannot change the ID, and writing to the ID property will result in an exception. So we
|
|
797
|
+
// strip it out of the properties here.
|
|
798
|
+
const { [reservedIntervalIdKey]: id, ...newProps } = serializedInterval.properties ?? {};
|
|
799
|
+
assert(id !== undefined, 0x3fe /* id must exist on the interval */);
|
|
800
|
+
const interval = this.getIntervalById(id);
|
|
801
|
+
if (!interval) {
|
|
802
|
+
// The interval has been removed locally; no-op.
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
if (local) {
|
|
806
|
+
// Let the propertyManager prune its pending change-properties set.
|
|
807
|
+
interval.propertyManager?.ackPendingProperties({
|
|
808
|
+
type: MergeTreeDeltaType.ANNOTATE,
|
|
809
|
+
props: serializedInterval.properties ?? {},
|
|
810
|
+
});
|
|
811
|
+
this.ackInterval(interval, op);
|
|
812
|
+
}
|
|
813
|
+
else {
|
|
814
|
+
// If there are pending changes with this ID, don't apply the remote start/end change, as the local ack
|
|
815
|
+
// should be the winning change.
|
|
816
|
+
let start;
|
|
817
|
+
let end;
|
|
818
|
+
// Track pending start/end independently of one another.
|
|
819
|
+
if (!this.hasPendingChangeStart(id)) {
|
|
820
|
+
start = serializedInterval.start;
|
|
821
|
+
}
|
|
822
|
+
if (!this.hasPendingChangeEnd(id)) {
|
|
823
|
+
end = serializedInterval.end;
|
|
824
|
+
}
|
|
825
|
+
let newInterval = interval;
|
|
826
|
+
if (start !== undefined || end !== undefined) {
|
|
827
|
+
// If changeInterval gives us a new interval, work with that one. Otherwise keep working with
|
|
828
|
+
// the one we originally found in the tree.
|
|
829
|
+
newInterval =
|
|
830
|
+
this.localCollection.changeInterval(interval, toOptionalSequencePlace(start, serializedInterval.startSide), toOptionalSequencePlace(end, serializedInterval.endSide), op) ?? interval;
|
|
831
|
+
}
|
|
832
|
+
const deltaProps = newInterval.addProperties(newProps, true, op.sequenceNumber);
|
|
833
|
+
if (this.onDeserialize) {
|
|
834
|
+
this.onDeserialize(newInterval);
|
|
835
|
+
}
|
|
836
|
+
if (newInterval !== interval) {
|
|
837
|
+
this.emitChange(newInterval, interval, local, false, op);
|
|
838
|
+
}
|
|
839
|
+
const changedProperties = Object.keys(newProps).length > 0;
|
|
840
|
+
if (changedProperties) {
|
|
841
|
+
this.emit("propertyChanged", interval, deltaProps, local, op);
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* {@inheritdoc IIntervalCollection.attachDeserializer}
|
|
847
|
+
*/
|
|
848
|
+
attachDeserializer(onDeserialize) {
|
|
849
|
+
// If no deserializer is specified can skip all processing work
|
|
850
|
+
if (!onDeserialize) {
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
// Start by storing the callbacks so that any subsequent modifications make use of them
|
|
854
|
+
this.onDeserialize = onDeserialize;
|
|
855
|
+
// Trigger the async prepare work across all values in the collection
|
|
856
|
+
if (this.attached) {
|
|
857
|
+
this.map(onDeserialize);
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Returns new interval after rebasing. If undefined, the interval was
|
|
862
|
+
* deleted as a result of rebasing. This can occur if the interval applies
|
|
863
|
+
* to a range that no longer exists, and the interval was unable to slide.
|
|
864
|
+
*
|
|
865
|
+
* @internal
|
|
866
|
+
*/
|
|
867
|
+
rebaseLocalInterval(opName, serializedInterval, localSeq) {
|
|
868
|
+
if (!this.client) {
|
|
869
|
+
// If there's no associated mergeTree client, the originally submitted op is still correct.
|
|
870
|
+
return serializedInterval;
|
|
871
|
+
}
|
|
872
|
+
if (!this.attached) {
|
|
873
|
+
throw new LoggingError("attachSequence must be called");
|
|
874
|
+
}
|
|
875
|
+
const { intervalType, properties, stickiness, startSide, endSide } = serializedInterval;
|
|
876
|
+
const { start: startRebased, end: endRebased } = this.localSeqToRebasedInterval.get(localSeq) ?? this.computeRebasedPositions(localSeq);
|
|
877
|
+
const intervalId = properties?.[reservedIntervalIdKey];
|
|
878
|
+
const localInterval = this.localCollection?.idIntervalIndex.getIntervalById(intervalId);
|
|
879
|
+
const rebased = {
|
|
880
|
+
start: startRebased,
|
|
881
|
+
end: endRebased,
|
|
882
|
+
intervalType,
|
|
883
|
+
sequenceNumber: this.client?.getCurrentSeq() ?? 0,
|
|
884
|
+
properties,
|
|
885
|
+
stickiness,
|
|
886
|
+
startSide,
|
|
887
|
+
endSide,
|
|
888
|
+
};
|
|
889
|
+
if (opName === "change" &&
|
|
890
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- ?? is not logically equivalent when .hasPendingChangeStart returns false.
|
|
891
|
+
(this.hasPendingChangeStart(intervalId) || this.hasPendingChangeEnd(intervalId))) {
|
|
892
|
+
this.removePendingChange(serializedInterval);
|
|
893
|
+
this.addPendingChange(intervalId, rebased);
|
|
894
|
+
}
|
|
895
|
+
// if the interval slid off the string, rebase the op to be a noop and delete the interval.
|
|
896
|
+
if (startRebased === DetachedReferencePosition ||
|
|
897
|
+
endRebased === DetachedReferencePosition) {
|
|
898
|
+
if (localInterval) {
|
|
899
|
+
this.localCollection?.removeExistingInterval(localInterval);
|
|
900
|
+
}
|
|
901
|
+
return undefined;
|
|
902
|
+
}
|
|
903
|
+
if (localInterval !== undefined) {
|
|
904
|
+
// we know we must be using `SequenceInterval` because `this.client` exists
|
|
905
|
+
assert(localInterval instanceof SequenceInterval, 0x3a0 /* localInterval must be `SequenceInterval` when used with client */);
|
|
906
|
+
// The rebased op may place this interval's endpoints on different segments. Calling `changeInterval` here
|
|
907
|
+
// updates the local client's state to be consistent with the emitted op.
|
|
908
|
+
this.localCollection?.changeInterval(localInterval, toOptionalSequencePlace(startRebased, startSide), toOptionalSequencePlace(endRebased, endSide), undefined, localSeq);
|
|
909
|
+
}
|
|
910
|
+
return rebased;
|
|
911
|
+
}
|
|
912
|
+
getSlideToSegment(lref) {
|
|
913
|
+
if (!this.client) {
|
|
914
|
+
throw new LoggingError("client does not exist");
|
|
915
|
+
}
|
|
916
|
+
const segoff = { segment: lref.getSegment(), offset: lref.getOffset() };
|
|
917
|
+
if (segoff.segment?.localRefs?.has(lref) !== true) {
|
|
918
|
+
return undefined;
|
|
919
|
+
}
|
|
920
|
+
const newSegoff = getSlideToSegoff(segoff, undefined, this.options.mergeTreeReferencesCanSlideToEndpoint);
|
|
921
|
+
const value = segoff.segment === newSegoff.segment && segoff.offset === newSegoff.offset
|
|
922
|
+
? undefined
|
|
923
|
+
: newSegoff;
|
|
924
|
+
return value;
|
|
925
|
+
}
|
|
926
|
+
ackInterval(interval, op) {
|
|
927
|
+
// Only SequenceIntervals need potential sliding
|
|
928
|
+
if (!(interval instanceof SequenceInterval)) {
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
if (!refTypeIncludesFlag(interval.start, ReferenceType.StayOnRemove) &&
|
|
932
|
+
!refTypeIncludesFlag(interval.end, ReferenceType.StayOnRemove)) {
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
const newStart = this.getSlideToSegment(interval.start);
|
|
936
|
+
const newEnd = this.getSlideToSegment(interval.end);
|
|
937
|
+
const id = interval.properties[reservedIntervalIdKey];
|
|
938
|
+
const hasPendingStartChange = this.hasPendingChangeStart(id);
|
|
939
|
+
const hasPendingEndChange = this.hasPendingChangeEnd(id);
|
|
940
|
+
if (!hasPendingStartChange) {
|
|
941
|
+
setSlideOnRemove(interval.start);
|
|
942
|
+
}
|
|
943
|
+
if (!hasPendingEndChange) {
|
|
944
|
+
setSlideOnRemove(interval.end);
|
|
945
|
+
}
|
|
946
|
+
const needsStartUpdate = newStart !== undefined && !hasPendingStartChange;
|
|
947
|
+
const needsEndUpdate = newEnd !== undefined && !hasPendingEndChange;
|
|
948
|
+
if (needsStartUpdate || needsEndUpdate) {
|
|
949
|
+
if (!this.localCollection) {
|
|
950
|
+
throw new LoggingError("Attach must be called before accessing intervals");
|
|
951
|
+
}
|
|
952
|
+
// `interval`'s endpoints will get modified in-place, so clone it prior to doing so for event emission.
|
|
953
|
+
const oldInterval = interval.clone();
|
|
954
|
+
// In this case, where we change the start or end of an interval,
|
|
955
|
+
// it is necessary to remove and re-add the interval listeners.
|
|
956
|
+
// This ensures that the correct listeners are added to the LocalReferencePosition.
|
|
957
|
+
this.localCollection.removeExistingInterval(interval);
|
|
958
|
+
if (!this.client) {
|
|
959
|
+
throw new LoggingError("client does not exist");
|
|
960
|
+
}
|
|
961
|
+
if (needsStartUpdate) {
|
|
962
|
+
const props = interval.start.properties;
|
|
963
|
+
interval.start = createPositionReferenceFromSegoff(this.client, newStart, interval.start.refType, op, undefined, undefined, startReferenceSlidingPreference(interval.stickiness), startReferenceSlidingPreference(interval.stickiness) ===
|
|
964
|
+
SlidingPreference.BACKWARD);
|
|
965
|
+
if (props) {
|
|
966
|
+
interval.start.addProperties(props);
|
|
967
|
+
}
|
|
968
|
+
const oldSeg = oldInterval.start.getSegment();
|
|
969
|
+
// remove and rebuild start interval as transient for event
|
|
970
|
+
this.client.removeLocalReferencePosition(oldInterval.start);
|
|
971
|
+
oldInterval.start.refType = ReferenceType.Transient;
|
|
972
|
+
oldSeg?.localRefs?.addLocalRef(oldInterval.start, oldInterval.start.getOffset());
|
|
973
|
+
}
|
|
974
|
+
if (needsEndUpdate) {
|
|
975
|
+
const props = interval.end.properties;
|
|
976
|
+
interval.end = createPositionReferenceFromSegoff(this.client, newEnd, interval.end.refType, op, undefined, undefined, endReferenceSlidingPreference(interval.stickiness), endReferenceSlidingPreference(interval.stickiness) ===
|
|
977
|
+
SlidingPreference.FORWARD);
|
|
978
|
+
if (props) {
|
|
979
|
+
interval.end.addProperties(props);
|
|
980
|
+
}
|
|
981
|
+
// remove and rebuild end interval as transient for event
|
|
982
|
+
const oldSeg = oldInterval.end.getSegment();
|
|
983
|
+
this.client.removeLocalReferencePosition(oldInterval.end);
|
|
984
|
+
oldInterval.end.refType = ReferenceType.Transient;
|
|
985
|
+
oldSeg?.localRefs?.addLocalRef(oldInterval.end, oldInterval.end.getOffset());
|
|
986
|
+
}
|
|
987
|
+
this.localCollection.add(interval);
|
|
988
|
+
this.emitChange(interval, oldInterval, true, true, op);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
/** @internal */
|
|
992
|
+
ackAdd(serializedInterval, local, op, localOpMetadata) {
|
|
993
|
+
if (local) {
|
|
994
|
+
assert(localOpMetadata !== undefined, 0x553 /* op metadata should be defined for local op */);
|
|
995
|
+
this.localSeqToSerializedInterval.delete(localOpMetadata.localSeq);
|
|
996
|
+
const id = serializedInterval.properties?.[reservedIntervalIdKey];
|
|
997
|
+
const localInterval = this.getIntervalById(id);
|
|
998
|
+
if (localInterval) {
|
|
999
|
+
this.ackInterval(localInterval, op);
|
|
1000
|
+
}
|
|
1001
|
+
return;
|
|
1002
|
+
}
|
|
1003
|
+
if (!this.localCollection) {
|
|
1004
|
+
throw new LoggingError("attachSequence must be called");
|
|
1005
|
+
}
|
|
1006
|
+
this.localCollection.ensureSerializedId(serializedInterval);
|
|
1007
|
+
const interval = this.localCollection.addInterval(toSequencePlace(serializedInterval.start, serializedInterval.startSide ?? Side.Before), toSequencePlace(serializedInterval.end, serializedInterval.endSide ?? Side.Before), serializedInterval.intervalType, serializedInterval.properties, op);
|
|
1008
|
+
if (interval) {
|
|
1009
|
+
if (this.onDeserialize) {
|
|
1010
|
+
this.onDeserialize(interval);
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
this.emit("addInterval", interval, local, op);
|
|
1014
|
+
return interval;
|
|
1015
|
+
}
|
|
1016
|
+
/** @internal */
|
|
1017
|
+
ackDelete(serializedInterval, local, op) {
|
|
1018
|
+
if (local) {
|
|
1019
|
+
// Local ops were applied when the message was created and there's no "pending delete"
|
|
1020
|
+
// state to book keep: remote operation application takes into account possibility of
|
|
1021
|
+
// locally deleted interval whenever a lookup happens.
|
|
1022
|
+
return;
|
|
1023
|
+
}
|
|
1024
|
+
if (!this.localCollection) {
|
|
1025
|
+
throw new LoggingError("attach must be called prior to deleting intervals");
|
|
1026
|
+
}
|
|
1027
|
+
const id = this.localCollection.ensureSerializedId(serializedInterval);
|
|
1028
|
+
const interval = this.localCollection.idIntervalIndex.getIntervalById(id);
|
|
1029
|
+
if (interval) {
|
|
1030
|
+
this.deleteExistingInterval(interval, local, op);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
/**
|
|
1034
|
+
* @internal
|
|
1035
|
+
*/
|
|
1036
|
+
serializeInternal() {
|
|
1037
|
+
if (!this.localCollection) {
|
|
1038
|
+
throw new LoggingError("attachSequence must be called");
|
|
1039
|
+
}
|
|
1040
|
+
return this.localCollection.serialize();
|
|
1041
|
+
}
|
|
1042
|
+
/**
|
|
1043
|
+
* @returns an iterator over all intervals in this collection.
|
|
1044
|
+
*/
|
|
1045
|
+
[Symbol.iterator]() {
|
|
1046
|
+
const iterator = new IntervalCollectionIterator(this);
|
|
1047
|
+
return iterator;
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* {@inheritdoc IIntervalCollection.CreateForwardIteratorWithStartPosition}
|
|
1051
|
+
*/
|
|
1052
|
+
CreateForwardIteratorWithStartPosition(startPosition) {
|
|
1053
|
+
const iterator = new IntervalCollectionIterator(this, true, startPosition);
|
|
1054
|
+
return iterator;
|
|
1055
|
+
}
|
|
1056
|
+
/**
|
|
1057
|
+
* {@inheritdoc IIntervalCollection.CreateBackwardIteratorWithStartPosition}
|
|
1058
|
+
*/
|
|
1059
|
+
CreateBackwardIteratorWithStartPosition(startPosition) {
|
|
1060
|
+
const iterator = new IntervalCollectionIterator(this, false, startPosition);
|
|
1061
|
+
return iterator;
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* {@inheritdoc IIntervalCollection.CreateForwardIteratorWithEndPosition}
|
|
1065
|
+
*/
|
|
1066
|
+
CreateForwardIteratorWithEndPosition(endPosition) {
|
|
1067
|
+
const iterator = new IntervalCollectionIterator(this, true, undefined, endPosition);
|
|
1068
|
+
return iterator;
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* {@inheritdoc IIntervalCollection.CreateBackwardIteratorWithEndPosition}
|
|
1072
|
+
*/
|
|
1073
|
+
CreateBackwardIteratorWithEndPosition(endPosition) {
|
|
1074
|
+
const iterator = new IntervalCollectionIterator(this, false, undefined, endPosition);
|
|
1075
|
+
return iterator;
|
|
1076
|
+
}
|
|
1077
|
+
/**
|
|
1078
|
+
* {@inheritdoc IIntervalCollection.gatherIterationResults}
|
|
1079
|
+
*/
|
|
1080
|
+
gatherIterationResults(results, iteratesForward, start, end) {
|
|
1081
|
+
if (!this.localCollection) {
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
this.localCollection.overlappingIntervalsIndex.gatherIterationResults(results, iteratesForward, start, end);
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* {@inheritdoc IIntervalCollection.findOverlappingIntervals}
|
|
1088
|
+
*/
|
|
1089
|
+
findOverlappingIntervals(startPosition, endPosition) {
|
|
1090
|
+
if (!this.localCollection) {
|
|
1091
|
+
throw new LoggingError("attachSequence must be called");
|
|
1092
|
+
}
|
|
1093
|
+
return this.localCollection.overlappingIntervalsIndex.findOverlappingIntervals(startPosition, endPosition);
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* {@inheritdoc IIntervalCollection.map}
|
|
1097
|
+
*/
|
|
1098
|
+
map(fn) {
|
|
1099
|
+
if (!this.localCollection) {
|
|
1100
|
+
throw new LoggingError("attachSequence must be called");
|
|
1101
|
+
}
|
|
1102
|
+
for (const interval of this.localCollection.idIntervalIndex) {
|
|
1103
|
+
fn(interval);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
/**
|
|
1107
|
+
* {@inheritdoc IIntervalCollection.previousInterval}
|
|
1108
|
+
*/
|
|
1109
|
+
previousInterval(pos) {
|
|
1110
|
+
if (!this.localCollection) {
|
|
1111
|
+
throw new LoggingError("attachSequence must be called");
|
|
1112
|
+
}
|
|
1113
|
+
return this.localCollection.endIntervalIndex.previousInterval(pos);
|
|
1114
|
+
}
|
|
1115
|
+
/**
|
|
1116
|
+
* {@inheritdoc IIntervalCollection.nextInterval}
|
|
1117
|
+
*/
|
|
1118
|
+
nextInterval(pos) {
|
|
1119
|
+
if (!this.localCollection) {
|
|
1120
|
+
throw new LoggingError("attachSequence must be called");
|
|
1121
|
+
}
|
|
1122
|
+
return this.localCollection.endIntervalIndex.nextInterval(pos);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
function setSlideOnRemove(lref) {
|
|
1126
|
+
let refType = lref.refType;
|
|
1127
|
+
refType = refType & ~ReferenceType.StayOnRemove;
|
|
1128
|
+
refType = refType | ReferenceType.SlideOnRemove;
|
|
1129
|
+
lref.refType = refType;
|
|
1130
|
+
}
|
|
1131
|
+
/**
|
|
1132
|
+
* Returns an object that can be used to find the interval a given LocalReferencePosition belongs to.
|
|
1133
|
+
* @returns undefined if the reference position is not the endpoint of any interval (e.g. it was created
|
|
1134
|
+
* on the merge tree directly by app code), otherwise an {@link IntervalLocator} for the interval this
|
|
1135
|
+
* endpoint is a part of.
|
|
1136
|
+
* @internal
|
|
1137
|
+
*/
|
|
1138
|
+
export function intervalLocatorFromEndpoint(potentialEndpoint) {
|
|
1139
|
+
const { interval, [reservedRangeLabelsKey]: collectionNameArray } = potentialEndpoint.properties ?? {};
|
|
1140
|
+
return interval && collectionNameArray?.length === 1
|
|
1141
|
+
? { label: collectionNameArray[0], interval }
|
|
1142
|
+
: undefined;
|
|
1143
|
+
}
|
|
1144
|
+
//# sourceMappingURL=intervalCollection.mjs.map
|