@fluid-experimental/tree 1.3.0 → 1.3.2-112998
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/dist/HistoryEditFactory.d.ts +5 -3
- package/dist/HistoryEditFactory.d.ts.map +1 -1
- package/dist/HistoryEditFactory.js +18 -3
- package/dist/HistoryEditFactory.js.map +1 -1
- package/dist/SharedTree.js +1 -1
- package/dist/SharedTree.js.map +1 -1
- package/lib/HistoryEditFactory.d.ts +5 -3
- package/lib/HistoryEditFactory.d.ts.map +1 -1
- package/lib/HistoryEditFactory.js +18 -3
- package/lib/HistoryEditFactory.js.map +1 -1
- package/lib/SharedTree.js +1 -1
- package/lib/SharedTree.js.map +1 -1
- package/lib/test/HistoryEditFactory.tests.js +48 -9
- package/lib/test/HistoryEditFactory.tests.js.map +1 -1
- package/package.json +18 -18
- package/src/HistoryEditFactory.ts +24 -3
- package/src/SharedTree.ts +1 -1
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
|
+
import { ITelemetryLogger } from '@fluidframework/common-definitions';
|
|
5
6
|
import { ChangeInternal } from './persisted-types';
|
|
6
7
|
import { RevisionView } from './RevisionView';
|
|
7
8
|
/**
|
|
@@ -9,12 +10,13 @@ import { RevisionView } from './RevisionView';
|
|
|
9
10
|
* @param changes - the changes for which to produce an inverse.
|
|
10
11
|
* @param before - a view of the tree state before `changes` are/were applied - used as a basis for generating the inverse.
|
|
11
12
|
* @returns if the changes could be reverted, a sequence of changes _r_ that will produce `before` if applied to a view _A_, where _A_ is the result of
|
|
12
|
-
* applying `changes` to `before`.
|
|
13
|
-
* not
|
|
13
|
+
* applying `changes` to `before`. Note that the size of the array of reverted changes may not be the same as the input array, and may even be empty in cases where
|
|
14
|
+
* the view did not change. Applying _r_ to views other than _A_ is legal but may cause the changes to fail to apply or may not be a true semantic inverse.
|
|
15
|
+
* If the changes could not be reverted given the state of `before`, returns undefined.
|
|
14
16
|
*
|
|
15
17
|
* TODO: what should this do if `changes` fails to apply to `before`?
|
|
16
18
|
* TODO:#68574: Pass a view that corresponds to the appropriate Fluid reference sequence number rather than the view just before
|
|
17
19
|
* @internal
|
|
18
20
|
*/
|
|
19
|
-
export declare function revert(changes: readonly ChangeInternal[], before: RevisionView): ChangeInternal[] | undefined;
|
|
21
|
+
export declare function revert(changes: readonly ChangeInternal[], before: RevisionView, logger?: ITelemetryLogger): ChangeInternal[] | undefined;
|
|
20
22
|
//# sourceMappingURL=HistoryEditFactory.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HistoryEditFactory.d.ts","sourceRoot":"","sources":["../src/HistoryEditFactory.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"HistoryEditFactory.d.ts","sourceRoot":"","sources":["../src/HistoryEditFactory.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AAItE,OAAO,EACN,cAAc,EAQd,MAAM,mBAAmB,CAAC;AAI3B,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAI9C;;;;;;;;;;;;GAYG;AACH,wBAAgB,MAAM,CACrB,OAAO,EAAE,SAAS,cAAc,EAAE,EAClC,MAAM,EAAE,YAAY,EACpB,MAAM,CAAC,EAAE,gBAAgB,GACvB,cAAc,EAAE,GAAG,SAAS,CA8G9B"}
|
|
@@ -17,14 +17,15 @@ const SerializationUtilities_1 = require("./SerializationUtilities");
|
|
|
17
17
|
* @param changes - the changes for which to produce an inverse.
|
|
18
18
|
* @param before - a view of the tree state before `changes` are/were applied - used as a basis for generating the inverse.
|
|
19
19
|
* @returns if the changes could be reverted, a sequence of changes _r_ that will produce `before` if applied to a view _A_, where _A_ is the result of
|
|
20
|
-
* applying `changes` to `before`.
|
|
21
|
-
* not
|
|
20
|
+
* applying `changes` to `before`. Note that the size of the array of reverted changes may not be the same as the input array, and may even be empty in cases where
|
|
21
|
+
* the view did not change. Applying _r_ to views other than _A_ is legal but may cause the changes to fail to apply or may not be a true semantic inverse.
|
|
22
|
+
* If the changes could not be reverted given the state of `before`, returns undefined.
|
|
22
23
|
*
|
|
23
24
|
* TODO: what should this do if `changes` fails to apply to `before`?
|
|
24
25
|
* TODO:#68574: Pass a view that corresponds to the appropriate Fluid reference sequence number rather than the view just before
|
|
25
26
|
* @internal
|
|
26
27
|
*/
|
|
27
|
-
function revert(changes, before) {
|
|
28
|
+
function revert(changes, before, logger) {
|
|
28
29
|
const result = [];
|
|
29
30
|
const builtNodes = new Map();
|
|
30
31
|
const detachedNodes = new Map();
|
|
@@ -57,10 +58,20 @@ function revert(changes, before) {
|
|
|
57
58
|
const nodesBuilt = builtNodes.get(source);
|
|
58
59
|
const nodesDetached = detachedNodes.get(source);
|
|
59
60
|
if (nodesBuilt !== undefined) {
|
|
61
|
+
if (nodesBuilt.length === 0) {
|
|
62
|
+
builtNodes.delete(source);
|
|
63
|
+
logger === null || logger === void 0 ? void 0 : logger.sendTelemetryEvent({ eventName: 'reverting insertion of empty traits' });
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
60
66
|
result.unshift(createInvertedInsert(change, nodesBuilt));
|
|
61
67
|
builtNodes.delete(source);
|
|
62
68
|
}
|
|
63
69
|
else if (nodesDetached !== undefined) {
|
|
70
|
+
if (nodesDetached.length === 0) {
|
|
71
|
+
detachedNodes.delete(source);
|
|
72
|
+
logger === null || logger === void 0 ? void 0 : logger.sendTelemetryEvent({ eventName: 'reverting insertion of empty traits' });
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
64
75
|
result.unshift(createInvertedInsert(change, nodesDetached, true));
|
|
65
76
|
detachedNodes.delete(source);
|
|
66
77
|
}
|
|
@@ -79,6 +90,10 @@ function revert(changes, before) {
|
|
|
79
90
|
return undefined;
|
|
80
91
|
}
|
|
81
92
|
const { invertedDetach, detachedNodeIds } = invert;
|
|
93
|
+
if (detachedNodeIds.length === 0) {
|
|
94
|
+
logger === null || logger === void 0 ? void 0 : logger.sendTelemetryEvent({ eventName: 'reverting detachment of empty traits' });
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
82
97
|
if (destination !== undefined) {
|
|
83
98
|
if (builtNodes.has(destination) || detachedNodes.has(destination)) {
|
|
84
99
|
// Malformed: destination was already used by a prior build or detach
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HistoryEditFactory.js","sourceRoot":"","sources":["../src/HistoryEditFactory.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,+CAAiF;AACjF,qCAAwC;AACxC,2DAA2D;AAC3D,uDAS2B;AAC3B,+DAA4D;AAC5D,mDAAiF;AAIjF,qEAAqE;AAErE;;;;;;;;;;;GAWG;AACH,SAAgB,MAAM,CAAC,OAAkC,EAAE,MAAoB;IAC9E,MAAM,MAAM,GAAqB,EAAE,CAAC;IAEpC,MAAM,UAAU,GAAG,IAAI,GAAG,EAAgC,CAAC;IAC3D,MAAM,aAAa,GAAG,IAAI,GAAG,EAAgC,CAAC;IAE9D,mEAAmE;IACnE,MAAM,MAAM,GAAG,yCAAmB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACnD,gDAAgD;IAChD,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;QAC7B,qCAAqC;QACrC,QAAQ,MAAM,CAAC,IAAI,EAAE;YACpB,KAAK,oCAAkB,CAAC,KAAK,CAAC,CAAC;gBAC9B,mEAAmE;gBACnE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;gBACvC,IAAA,eAAM,EAAC,CAAC,UAAU,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,6DAA6D,CAAC,CAAC;gBACpG,IAAA,eAAM,EAAC,CAAC,aAAa,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,8DAA8D,CAAC,CAAC;g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AO;QACN,cAAc,EAAE;YACf,gCAAc,CAAC,KAAK,CACnB,eAAe,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,IAAA,kDAAyB,EAAC,gBAAgB,EAAE,EAAE,CAAC,CAAC,EAC5E,kBAAkB,CAClB;YACD,gCAAc,CAAC,MAAM,CAAC,kBAAkB,EAAE,iBAAiB,CAAC;SAC5D;QACD,eAAe;KACf,CAAC;AACH,CAAC;AAED;;GAEG;AACH,SAAS,sBAAsB,CAAC,QAA0B,EAAE,gBAA0B;IACrF,MAAM,EAAE,YAAY,EAAE,GAAG,QAAQ,CAAC;IAClC,MAAM,IAAI,GAAG,gBAAgB,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC;IAC3D,IAAI,IAAI,KAAK,SAAS,EAAE;QACvB,oFAAoF;QACpF,OAAO,SAAS,CAAC;KACjB;IAED,+EAA+E;IAC/E,IAAI,IAAI,CAAC,OAAO,KAAK,IAAI,EAAE;QAC1B,OAAO,CAAC,gCAAc,CAAC,UAAU,CAAC,YAAY,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;KAC/D;IACD,OAAO,CAAC,gCAAc,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC,CAAC;AACpD,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { DetachedSequenceId, isDetachedSequenceId, NodeId } from './Identifiers';\nimport { assert, fail } from './Common';\nimport { rangeFromStableRange } from './TreeViewUtilities';\nimport {\n\tChangeInternal,\n\tChangeTypeInternal,\n\tDetachInternal,\n\tSetValueInternal,\n\tInsertInternal,\n\tBuildNodeInternal,\n\tSide,\n\tStableRangeInternal,\n} from './persisted-types';\nimport { TransactionInternal } from './TransactionInternal';\nimport { RangeValidationResultKind, validateStableRange } from './EditUtilities';\nimport { StablePlace } from './ChangeTypes';\nimport { RevisionView } from './RevisionView';\nimport { TreeView } from './TreeView';\nimport { getChangeNodeFromViewNode } from './SerializationUtilities';\n\n/**\n * Given a sequence of changes, produces an inverse sequence of changes, i.e. the minimal changes required to revert the given changes\n * @param changes - the changes for which to produce an inverse.\n * @param before - a view of the tree state before `changes` are/were applied - used as a basis for generating the inverse.\n * @returns if the changes could be reverted, a sequence of changes _r_ that will produce `before` if applied to a view _A_, where _A_ is the result of\n * applying `changes` to `before`. Applying _r_ to views other than _A_ is legal but may cause the changes to fail to apply or may\n * not be a true semantic inverse. If the changes could not be reverted given the state of `before`, returns undefined.\n *\n * TODO: what should this do if `changes` fails to apply to `before`?\n * TODO:#68574: Pass a view that corresponds to the appropriate Fluid reference sequence number rather than the view just before\n * @internal\n */\nexport function revert(changes: readonly ChangeInternal[], before: RevisionView): ChangeInternal[] | undefined {\n\tconst result: ChangeInternal[] = [];\n\n\tconst builtNodes = new Map<DetachedSequenceId, NodeId[]>();\n\tconst detachedNodes = new Map<DetachedSequenceId, NodeId[]>();\n\n\t// Open edit on revision to update it as changes are walked through\n\tconst editor = TransactionInternal.factory(before);\n\t// Apply `edit`, generating an inverse as we go.\n\tfor (const change of changes) {\n\t\t// Generate an inverse of each change\n\t\tswitch (change.type) {\n\t\t\tcase ChangeTypeInternal.Build: {\n\t\t\t\t// Save nodes added to the detached state for use in future changes\n\t\t\t\tconst { destination, source } = change;\n\t\t\t\tassert(!builtNodes.has(destination), `Cannot revert Build: destination is already used by a Build`);\n\t\t\t\tassert(!detachedNodes.has(destination), `Cannot revert Build: destination is already used by a Detach`);\n\t\t\t\tbuiltNodes.set(\n\t\t\t\t\tdestination,\n\t\t\t\t\tsource.reduce((ids: NodeId[], curr: BuildNodeInternal) => {\n\t\t\t\t\t\tif (isDetachedSequenceId(curr)) {\n\t\t\t\t\t\t\tconst nodesForDetachedSequence =\n\t\t\t\t\t\t\t\tbuiltNodes.get(curr) ?? fail('detached sequence must have associated built nodes');\n\n\t\t\t\t\t\t\tids.push(...nodesForDetachedSequence);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tids.push(curr.identifier);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn ids;\n\t\t\t\t\t}, [])\n\t\t\t\t);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase ChangeTypeInternal.Insert: {\n\t\t\t\tconst { source } = change;\n\t\t\t\tconst nodesBuilt = builtNodes.get(source);\n\t\t\t\tconst nodesDetached = detachedNodes.get(source);\n\n\t\t\t\tif (nodesBuilt !== undefined) {\n\t\t\t\t\tresult.unshift(createInvertedInsert(change, nodesBuilt));\n\t\t\t\t\tbuiltNodes.delete(source);\n\t\t\t\t} else if (nodesDetached !== undefined) {\n\t\t\t\t\tresult.unshift(createInvertedInsert(change, nodesDetached, true));\n\t\t\t\t\tdetachedNodes.delete(source);\n\t\t\t\t} else {\n\t\t\t\t\t// Cannot revert an insert whose source is no longer available for inserting (i.e. not just built, and not detached)\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase ChangeTypeInternal.Detach: {\n\t\t\t\tconst { destination } = change;\n\t\t\t\tconst invert = createInvertedDetach(change, editor.view);\n\t\t\t\tif (invert === undefined) {\n\t\t\t\t\t// Cannot revert a detach whose source does not exist in the tree\n\t\t\t\t\t// TODO:68574: May not be possible once associated todo in `createInvertedDetach` is addressed\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\t\t\t\tconst { invertedDetach, detachedNodeIds } = invert;\n\n\t\t\t\tif (destination !== undefined) {\n\t\t\t\t\tif (builtNodes.has(destination) || detachedNodes.has(destination)) {\n\t\t\t\t\t\t// Malformed: destination was already used by a prior build or detach\n\t\t\t\t\t\treturn undefined;\n\t\t\t\t\t}\n\t\t\t\t\tdetachedNodes.set(destination, detachedNodeIds);\n\t\t\t\t}\n\n\t\t\t\tresult.unshift(...invertedDetach);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase ChangeTypeInternal.SetValue: {\n\t\t\t\tconst invert = createInvertedSetValue(change, editor.view);\n\t\t\t\tif (invert === undefined) {\n\t\t\t\t\t// Cannot revert a set for a node that does not exist in the tree\n\t\t\t\t\t// TODO:68574: May not be possible once associated todo in `createInvertedSetValue` is addressed\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\t\t\t\tresult.unshift(...invert);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase ChangeTypeInternal.Constraint:\n\t\t\t\t// TODO:#46759: Support Constraint in reverts\n\t\t\t\tfail('Revert currently does not support Constraints');\n\t\t\tdefault:\n\t\t\t\tfail('Revert does not support the change type.');\n\t\t}\n\n\t\t// Update the revision\n\t\teditor.applyChange(change);\n\t}\n\n\teditor.close();\n\treturn result;\n}\n\n/**\n * The inverse of an Insert is a Detach that starts before the leftmost node inserted and ends after the rightmost.\n */\nfunction createInvertedInsert(\n\tinsert: InsertInternal,\n\tnodesInserted: readonly NodeId[],\n\tsaveDetached = false\n): ChangeInternal {\n\tconst leftmostNode = nodesInserted[0];\n\tconst rightmostNode = nodesInserted[nodesInserted.length - 1];\n\n\tconst source: StableRangeInternal = {\n\t\tstart: {\n\t\t\treferenceSibling: leftmostNode,\n\t\t\tside: Side.Before,\n\t\t},\n\t\tend: {\n\t\t\treferenceSibling: rightmostNode,\n\t\t\tside: Side.After,\n\t\t},\n\t};\n\n\treturn ChangeInternal.detach(source, saveDetached ? insert.source : undefined);\n}\n\n/**\n * If a detach does not include a destination, its inverse is a build and insert. Otherwise, it is just an insert.\n * Information on the nodes that were detached is obtained by going to the revision before the detach.\n *\n * The anchor for the resulting Insert is chosen in the following order:\n * 1. If detach.source.start.side is After: detach.source.start\n *\n * ex: For nodes A B [C..F] G H where [C..F] represents the detached nodes,\n * if detach.source.start is \"After B\", the anchor for the resulting Insert will also be \"After B\".\n *\n * For nodes [A..F] G H where [A..F] represents the detached nodes,\n * if detach.source.start is \"After start of trait\", the anchor for the resulting Insert will also be \"After start of trait\".\n *\n * 2. Else if detach.source.end.side is Before: detach.source.end\n *\n * ex: For nodes A B [C..F] G H where [C..F] represents the detached nodes,\n * if detach.source.start is \"Before C\" and detach.source.end is \"Before G\",\n * the anchor for the resulting Insert will be \"Before G\".\n *\n * 3. Else: After the node directly to the left of the originally detached nodes\n *\n * ex: For nodes A B [C..F] G H where [C..F] represents the detached nodes,\n * if detach.source.start is \"Before C\" and detach.source.end is \"After F\",\n * the anchor for the resulting Insert will be \"After B\".\n *\n * When choosing the anchor, the existing anchors on detach.source are preferred when they have a valid sibling. Otherwise, the valid\n * anchor to the left of the originally detached nodes is chosen.\n */\nfunction createInvertedDetach(\n\tdetach: DetachInternal,\n\tviewBeforeChange: TreeView\n): { invertedDetach: ChangeInternal[]; detachedNodeIds: NodeId[] } | undefined {\n\tconst validatedSource = validateStableRange(viewBeforeChange, detach.source);\n\tif (validatedSource.result !== RangeValidationResultKind.Valid) {\n\t\t// TODO:#68574: having the reference view would potentially allow us to revert some detaches that currently conflict\n\t\treturn undefined;\n\t}\n\n\tconst { start, end } = rangeFromStableRange(viewBeforeChange, validatedSource);\n\tconst { trait: referenceTrait } = start;\n\tconst nodes = viewBeforeChange.getTrait(referenceTrait);\n\n\tconst startIndex = viewBeforeChange.findIndexWithinTrait(start);\n\tconst endIndex = viewBeforeChange.findIndexWithinTrait(end);\n\tconst detachedNodeIds: NodeId[] = nodes.slice(startIndex, endIndex);\n\n\tconst leftOfDetached = nodes.slice(0, startIndex);\n\n\tlet insertDestination: StablePlace;\n\n\tif (start.side === Side.After) {\n\t\tinsertDestination =\n\t\t\tstart.sibling === undefined\n\t\t\t\t? { side: Side.After, referenceTrait }\n\t\t\t\t: { side: Side.After, referenceSibling: start.sibling };\n\t} else if (end.side === Side.Before) {\n\t\tinsertDestination =\n\t\t\tend.sibling === undefined\n\t\t\t\t? { side: Side.Before, referenceTrait }\n\t\t\t\t: { side: Side.Before, referenceSibling: end.sibling };\n\t} else {\n\t\tconst referenceSibling = leftOfDetached.pop();\n\t\tinsertDestination = {\n\t\t\tside: Side.After,\n\t\t\treferenceSibling,\n\t\t\treferenceTrait: referenceSibling === undefined ? referenceTrait : undefined,\n\t\t};\n\t}\n\n\tif (detach.destination !== undefined) {\n\t\treturn {\n\t\t\tinvertedDetach: [ChangeInternal.insert(detach.destination, insertDestination)],\n\t\t\tdetachedNodeIds,\n\t\t};\n\t}\n\n\tconst detachedSequenceId = 0 as DetachedSequenceId;\n\treturn {\n\t\tinvertedDetach: [\n\t\t\tChangeInternal.build(\n\t\t\t\tdetachedNodeIds.map((id) => getChangeNodeFromViewNode(viewBeforeChange, id)),\n\t\t\t\tdetachedSequenceId\n\t\t\t),\n\t\t\tChangeInternal.insert(detachedSequenceId, insertDestination),\n\t\t],\n\t\tdetachedNodeIds,\n\t};\n}\n\n/**\n * The inverse of a SetValue is a SetValue that sets the value to what it was prior to the change.\n */\nfunction createInvertedSetValue(setValue: SetValueInternal, viewBeforeChange: TreeView): ChangeInternal[] | undefined {\n\tconst { nodeToModify } = setValue;\n\tconst node = viewBeforeChange.tryGetViewNode(nodeToModify);\n\tif (node === undefined) {\n\t\t// TODO:68574: With a reference view, may be able to better resolve conflicting sets\n\t\treturn undefined;\n\t}\n\n\t// Rationale: 'undefined' is reserved for future use (see 'SetValue' interface)\n\tif (node.payload !== null) {\n\t\treturn [ChangeInternal.setPayload(nodeToModify, node.payload)];\n\t}\n\treturn [ChangeInternal.clearPayload(nodeToModify)];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"HistoryEditFactory.js","sourceRoot":"","sources":["../src/HistoryEditFactory.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAGH,+CAAiF;AACjF,qCAAwC;AACxC,2DAA2D;AAC3D,uDAS2B;AAC3B,+DAA4D;AAC5D,mDAAiF;AAIjF,qEAAqE;AAErE;;;;;;;;;;;;GAYG;AACH,SAAgB,MAAM,CACrB,OAAkC,EAClC,MAAoB,EACpB,MAAyB;IAEzB,MAAM,MAAM,GAAqB,EAAE,CAAC;IAEpC,MAAM,UAAU,GAAG,IAAI,GAAG,EAAgC,CAAC;IAC3D,MAAM,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,CAAC,EAAE,EAAE,EAAE,CAAC,IAAA,kDAAyB,EAAC,gBAAgB,EAAE,EAAE,CAAC,CAAC,EAC5E,kBAAkB,CAClB;YACD,gCAAc,CAAC,MAAM,CAAC,kBAAkB,EAAE,iBAAiB,CAAC;SAC5D;QACD,eAAe;KACf,CAAC;AACH,CAAC;AAED;;GAEG;AACH,SAAS,sBAAsB,CAAC,QAA0B,EAAE,gBAA0B;IACrF,MAAM,EAAE,YAAY,EAAE,GAAG,QAAQ,CAAC;IAClC,MAAM,IAAI,GAAG,gBAAgB,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC;IAC3D,IAAI,IAAI,KAAK,SAAS,EAAE;QACvB,oFAAoF;QACpF,OAAO,SAAS,CAAC;KACjB;IAED,+EAA+E;IAC/E,IAAI,IAAI,CAAC,OAAO,KAAK,IAAI,EAAE;QAC1B,OAAO,CAAC,gCAAc,CAAC,UAAU,CAAC,YAAY,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;KAC/D;IACD,OAAO,CAAC,gCAAc,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC,CAAC;AACpD,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { ITelemetryLogger } from '@fluidframework/common-definitions';\nimport { DetachedSequenceId, isDetachedSequenceId, NodeId } from './Identifiers';\nimport { assert, fail } from './Common';\nimport { rangeFromStableRange } from './TreeViewUtilities';\nimport {\n\tChangeInternal,\n\tChangeTypeInternal,\n\tDetachInternal,\n\tSetValueInternal,\n\tInsertInternal,\n\tBuildNodeInternal,\n\tSide,\n\tStableRangeInternal,\n} from './persisted-types';\nimport { TransactionInternal } from './TransactionInternal';\nimport { RangeValidationResultKind, validateStableRange } from './EditUtilities';\nimport { StablePlace } from './ChangeTypes';\nimport { RevisionView } from './RevisionView';\nimport { TreeView } from './TreeView';\nimport { getChangeNodeFromViewNode } from './SerializationUtilities';\n\n/**\n * Given a sequence of changes, produces an inverse sequence of changes, i.e. the minimal changes required to revert the given changes\n * @param changes - the changes for which to produce an inverse.\n * @param before - a view of the tree state before `changes` are/were applied - used as a basis for generating the inverse.\n * @returns if the changes could be reverted, a sequence of changes _r_ that will produce `before` if applied to a view _A_, where _A_ is the result of\n * applying `changes` to `before`. Note that the size of the array of reverted changes may not be the same as the input array, and may even be empty in cases where\n * the view did not change. Applying _r_ to views other than _A_ is legal but may cause the changes to fail to apply or may not be a true semantic inverse.\n * If the changes could not be reverted given the state of `before`, returns undefined.\n *\n * TODO: what should this do if `changes` fails to apply to `before`?\n * TODO:#68574: Pass a view that corresponds to the appropriate Fluid reference sequence number rather than the view just before\n * @internal\n */\nexport function revert(\n\tchanges: readonly ChangeInternal[],\n\tbefore: RevisionView,\n\tlogger?: ITelemetryLogger\n): ChangeInternal[] | undefined {\n\tconst result: ChangeInternal[] = [];\n\n\tconst builtNodes = new Map<DetachedSequenceId, NodeId[]>();\n\tconst detachedNodes = new Map<DetachedSequenceId, NodeId[]>();\n\n\t// Open edit on revision to update it as changes are walked through\n\tconst editor = TransactionInternal.factory(before);\n\t// Apply `edit`, generating an inverse as we go.\n\tfor (const change of changes) {\n\t\t// Generate an inverse of each change\n\t\tswitch (change.type) {\n\t\t\tcase ChangeTypeInternal.Build: {\n\t\t\t\t// Save nodes added to the detached state for use in future changes\n\t\t\t\tconst { destination, source } = change;\n\t\t\t\tassert(!builtNodes.has(destination), `Cannot revert Build: destination is already used by a Build`);\n\t\t\t\tassert(!detachedNodes.has(destination), `Cannot revert Build: destination is already used by a Detach`);\n\t\t\t\tbuiltNodes.set(\n\t\t\t\t\tdestination,\n\t\t\t\t\tsource.reduce((ids: NodeId[], curr: BuildNodeInternal) => {\n\t\t\t\t\t\tif (isDetachedSequenceId(curr)) {\n\t\t\t\t\t\t\tconst nodesForDetachedSequence =\n\t\t\t\t\t\t\t\tbuiltNodes.get(curr) ?? fail('detached sequence must have associated built nodes');\n\n\t\t\t\t\t\t\tids.push(...nodesForDetachedSequence);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tids.push(curr.identifier);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn ids;\n\t\t\t\t\t}, [])\n\t\t\t\t);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase ChangeTypeInternal.Insert: {\n\t\t\t\tconst { source } = change;\n\t\t\t\tconst nodesBuilt = builtNodes.get(source);\n\t\t\t\tconst nodesDetached = detachedNodes.get(source);\n\n\t\t\t\tif (nodesBuilt !== undefined) {\n\t\t\t\t\tif (nodesBuilt.length === 0) {\n\t\t\t\t\t\tbuiltNodes.delete(source);\n\t\t\t\t\t\tlogger?.sendTelemetryEvent({ eventName: 'reverting insertion of empty traits' });\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tresult.unshift(createInvertedInsert(change, nodesBuilt));\n\t\t\t\t\tbuiltNodes.delete(source);\n\t\t\t\t} else if (nodesDetached !== undefined) {\n\t\t\t\t\tif (nodesDetached.length === 0) {\n\t\t\t\t\t\tdetachedNodes.delete(source);\n\t\t\t\t\t\tlogger?.sendTelemetryEvent({ eventName: 'reverting insertion of empty traits' });\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tresult.unshift(createInvertedInsert(change, nodesDetached, true));\n\t\t\t\t\tdetachedNodes.delete(source);\n\t\t\t\t} else {\n\t\t\t\t\t// Cannot revert an insert whose source is no longer available for inserting (i.e. not just built, and not detached)\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase ChangeTypeInternal.Detach: {\n\t\t\t\tconst { destination } = change;\n\t\t\t\tconst invert = createInvertedDetach(change, editor.view);\n\t\t\t\tif (invert === undefined) {\n\t\t\t\t\t// Cannot revert a detach whose source does not exist in the tree\n\t\t\t\t\t// TODO:68574: May not be possible once associated todo in `createInvertedDetach` is addressed\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\t\t\t\tconst { invertedDetach, detachedNodeIds } = invert;\n\n\t\t\t\tif (detachedNodeIds.length === 0) {\n\t\t\t\t\tlogger?.sendTelemetryEvent({ eventName: 'reverting detachment of empty traits' });\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tif (destination !== undefined) {\n\t\t\t\t\tif (builtNodes.has(destination) || detachedNodes.has(destination)) {\n\t\t\t\t\t\t// Malformed: destination was already used by a prior build or detach\n\t\t\t\t\t\treturn undefined;\n\t\t\t\t\t}\n\t\t\t\t\tdetachedNodes.set(destination, detachedNodeIds);\n\t\t\t\t}\n\n\t\t\t\tresult.unshift(...invertedDetach);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase ChangeTypeInternal.SetValue: {\n\t\t\t\tconst invert = createInvertedSetValue(change, editor.view);\n\t\t\t\tif (invert === undefined) {\n\t\t\t\t\t// Cannot revert a set for a node that does not exist in the tree\n\t\t\t\t\t// TODO:68574: May not be possible once associated todo in `createInvertedSetValue` is addressed\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\t\t\t\tresult.unshift(...invert);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase ChangeTypeInternal.Constraint:\n\t\t\t\t// TODO:#46759: Support Constraint in reverts\n\t\t\t\tfail('Revert currently does not support Constraints');\n\t\t\tdefault:\n\t\t\t\tfail('Revert does not support the change type.');\n\t\t}\n\n\t\t// Update the revision\n\t\teditor.applyChange(change);\n\t}\n\n\teditor.close();\n\treturn result;\n}\n\n/**\n * The inverse of an Insert is a Detach that starts before the leftmost node inserted and ends after the rightmost.\n */\nfunction createInvertedInsert(\n\tinsert: InsertInternal,\n\tnodesInserted: readonly NodeId[],\n\tsaveDetached = false\n): ChangeInternal {\n\tconst leftmostNode = nodesInserted[0];\n\tconst rightmostNode = nodesInserted[nodesInserted.length - 1];\n\n\tconst source: StableRangeInternal = {\n\t\tstart: {\n\t\t\treferenceSibling: leftmostNode,\n\t\t\tside: Side.Before,\n\t\t},\n\t\tend: {\n\t\t\treferenceSibling: rightmostNode,\n\t\t\tside: Side.After,\n\t\t},\n\t};\n\n\treturn ChangeInternal.detach(source, saveDetached ? insert.source : undefined);\n}\n\n/**\n * If a detach does not include a destination, its inverse is a build and insert. Otherwise, it is just an insert.\n * Information on the nodes that were detached is obtained by going to the revision before the detach.\n *\n * The anchor for the resulting Insert is chosen in the following order:\n * 1. If detach.source.start.side is After: detach.source.start\n *\n * ex: For nodes A B [C..F] G H where [C..F] represents the detached nodes,\n * if detach.source.start is \"After B\", the anchor for the resulting Insert will also be \"After B\".\n *\n * For nodes [A..F] G H where [A..F] represents the detached nodes,\n * if detach.source.start is \"After start of trait\", the anchor for the resulting Insert will also be \"After start of trait\".\n *\n * 2. Else if detach.source.end.side is Before: detach.source.end\n *\n * ex: For nodes A B [C..F] G H where [C..F] represents the detached nodes,\n * if detach.source.start is \"Before C\" and detach.source.end is \"Before G\",\n * the anchor for the resulting Insert will be \"Before G\".\n *\n * 3. Else: After the node directly to the left of the originally detached nodes\n *\n * ex: For nodes A B [C..F] G H where [C..F] represents the detached nodes,\n * if detach.source.start is \"Before C\" and detach.source.end is \"After F\",\n * the anchor for the resulting Insert will be \"After B\".\n *\n * When choosing the anchor, the existing anchors on detach.source are preferred when they have a valid sibling. Otherwise, the valid\n * anchor to the left of the originally detached nodes is chosen.\n */\nfunction createInvertedDetach(\n\tdetach: DetachInternal,\n\tviewBeforeChange: TreeView\n): { invertedDetach: ChangeInternal[]; detachedNodeIds: NodeId[] } | undefined {\n\tconst validatedSource = validateStableRange(viewBeforeChange, detach.source);\n\tif (validatedSource.result !== RangeValidationResultKind.Valid) {\n\t\t// TODO:#68574: having the reference view would potentially allow us to revert some detaches that currently conflict\n\t\treturn undefined;\n\t}\n\n\tconst { start, end } = rangeFromStableRange(viewBeforeChange, validatedSource);\n\tconst { trait: referenceTrait } = start;\n\tconst nodes = viewBeforeChange.getTrait(referenceTrait);\n\n\tconst startIndex = viewBeforeChange.findIndexWithinTrait(start);\n\tconst endIndex = viewBeforeChange.findIndexWithinTrait(end);\n\tconst detachedNodeIds: NodeId[] = nodes.slice(startIndex, endIndex);\n\n\tconst leftOfDetached = nodes.slice(0, startIndex);\n\n\tlet insertDestination: StablePlace;\n\n\tif (start.side === Side.After) {\n\t\tinsertDestination =\n\t\t\tstart.sibling === undefined\n\t\t\t\t? { side: Side.After, referenceTrait }\n\t\t\t\t: { side: Side.After, referenceSibling: start.sibling };\n\t} else if (end.side === Side.Before) {\n\t\tinsertDestination =\n\t\t\tend.sibling === undefined\n\t\t\t\t? { side: Side.Before, referenceTrait }\n\t\t\t\t: { side: Side.Before, referenceSibling: end.sibling };\n\t} else {\n\t\tconst referenceSibling = leftOfDetached.pop();\n\t\tinsertDestination = {\n\t\t\tside: Side.After,\n\t\t\treferenceSibling,\n\t\t\treferenceTrait: referenceSibling === undefined ? referenceTrait : undefined,\n\t\t};\n\t}\n\n\tif (detach.destination !== undefined) {\n\t\treturn {\n\t\t\tinvertedDetach: [ChangeInternal.insert(detach.destination, insertDestination)],\n\t\t\tdetachedNodeIds,\n\t\t};\n\t}\n\n\tconst detachedSequenceId = 0 as DetachedSequenceId;\n\treturn {\n\t\tinvertedDetach: [\n\t\t\tChangeInternal.build(\n\t\t\t\tdetachedNodeIds.map((id) => getChangeNodeFromViewNode(viewBeforeChange, id)),\n\t\t\t\tdetachedSequenceId\n\t\t\t),\n\t\t\tChangeInternal.insert(detachedSequenceId, insertDestination),\n\t\t],\n\t\tdetachedNodeIds,\n\t};\n}\n\n/**\n * The inverse of a SetValue is a SetValue that sets the value to what it was prior to the change.\n */\nfunction createInvertedSetValue(setValue: SetValueInternal, viewBeforeChange: TreeView): ChangeInternal[] | undefined {\n\tconst { nodeToModify } = setValue;\n\tconst node = viewBeforeChange.tryGetViewNode(nodeToModify);\n\tif (node === undefined) {\n\t\t// TODO:68574: With a reference view, may be able to better resolve conflicting sets\n\t\treturn undefined;\n\t}\n\n\t// Rationale: 'undefined' is reserved for future use (see 'SetValue' interface)\n\tif (node.payload !== null) {\n\t\treturn [ChangeInternal.setPayload(nodeToModify, node.payload)];\n\t}\n\treturn [ChangeInternal.clearPayload(nodeToModify)];\n}\n"]}
|
package/dist/SharedTree.js
CHANGED
|
@@ -999,7 +999,7 @@ class SharedTree extends shared_object_base_1.SharedObject {
|
|
|
999
999
|
* @internal
|
|
1000
1000
|
*/
|
|
1001
1001
|
revertChanges(changes, before) {
|
|
1002
|
-
return (0, HistoryEditFactory_1.revert)(changes, before);
|
|
1002
|
+
return (0, HistoryEditFactory_1.revert)(changes, before, this.logger);
|
|
1003
1003
|
}
|
|
1004
1004
|
/**
|
|
1005
1005
|
* Submits an edit by the local client to the runtime.
|