@dabble/patches 0.4.5 → 0.4.6
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/algorithms/client/applyCommittedChanges.d.ts +8 -2
- package/dist/algorithms/client/applyCommittedChanges.js +30 -38
- package/dist/algorithms/client/batching.d.ts +8 -2
- package/dist/algorithms/client/batching.js +38 -37
- package/dist/algorithms/client/breakChange.d.ts +8 -2
- package/dist/algorithms/client/breakChange.js +191 -240
- package/dist/algorithms/client/createStateFromSnapshot.d.ts +8 -2
- package/dist/algorithms/client/createStateFromSnapshot.js +7 -8
- package/dist/algorithms/client/getJSONByteSize.d.ts +3 -1
- package/dist/algorithms/client/getJSONByteSize.js +12 -11
- package/dist/algorithms/client/makeChange.d.ts +8 -2
- package/dist/algorithms/client/makeChange.js +28 -36
- package/dist/algorithms/server/commitChanges.d.ts +9 -3
- package/dist/algorithms/server/commitChanges.js +69 -78
- package/dist/algorithms/server/createVersion.d.ts +9 -3
- package/dist/algorithms/server/createVersion.js +21 -27
- package/dist/algorithms/server/getSnapshotAtRevision.d.ts +9 -3
- package/dist/algorithms/server/getSnapshotAtRevision.js +27 -28
- package/dist/algorithms/server/getStateAtRevision.d.ts +9 -3
- package/dist/algorithms/server/getStateAtRevision.js +13 -17
- package/dist/algorithms/server/handleOfflineSessionsAndBatches.d.ts +9 -3
- package/dist/algorithms/server/handleOfflineSessionsAndBatches.js +60 -77
- package/dist/algorithms/server/transformIncomingChanges.d.ts +8 -2
- package/dist/algorithms/server/transformIncomingChanges.js +27 -39
- package/dist/algorithms/shared/applyChanges.d.ts +8 -2
- package/dist/algorithms/shared/applyChanges.js +11 -16
- package/dist/algorithms/shared/rebaseChanges.d.ts +8 -2
- package/dist/algorithms/shared/rebaseChanges.js +30 -49
- package/dist/chunk-IZ2YBCUP.js +56 -0
- package/dist/client/InMemoryStore.d.ts +9 -3
- package/dist/client/InMemoryStore.js +92 -101
- package/dist/client/IndexedDBStore.d.ts +9 -3
- package/dist/client/IndexedDBStore.js +378 -491
- package/dist/client/Patches.d.ts +18 -13
- package/dist/client/Patches.js +152 -207
- package/dist/client/PatchesDoc.d.ts +14 -8
- package/dist/client/PatchesDoc.js +147 -154
- package/dist/client/PatchesHistoryClient.d.ts +12 -5
- package/dist/client/PatchesHistoryClient.js +110 -117
- package/dist/client/PatchesStore.d.ts +9 -3
- package/dist/client/PatchesStore.js +0 -1
- package/dist/client/index.d.ts +12 -6
- package/dist/client/index.js +5 -5
- package/dist/data/change.d.ts +9 -3
- package/dist/data/change.js +23 -15
- package/dist/data/version.d.ts +9 -3
- package/dist/data/version.js +11 -15
- package/dist/event-signal.d.ts +7 -6
- package/dist/event-signal.js +24 -39
- package/dist/index-CvQws3AB.d.ts +36 -0
- package/dist/index.d.ts +27 -5
- package/dist/index.js +10 -4
- package/dist/json-patch/JSONPatch.d.ts +9 -5
- package/dist/json-patch/JSONPatch.js +175 -183
- package/dist/json-patch/applyPatch.d.ts +5 -2
- package/dist/json-patch/applyPatch.js +27 -35
- package/dist/json-patch/composePatch.d.ts +5 -2
- package/dist/json-patch/composePatch.js +34 -34
- package/dist/json-patch/createJSONPatch.d.ts +7 -2
- package/dist/json-patch/createJSONPatch.js +11 -38
- package/dist/json-patch/index.d.ts +14 -6
- package/dist/json-patch/index.js +20 -9
- package/dist/json-patch/invertPatch.d.ts +5 -2
- package/dist/json-patch/invertPatch.js +31 -30
- package/dist/json-patch/ops/add.d.ts +5 -2
- package/dist/json-patch/ops/add.js +53 -51
- package/dist/json-patch/ops/bitmask.d.ts +8 -5
- package/dist/json-patch/ops/bitmask.js +41 -44
- package/dist/json-patch/ops/copy.d.ts +5 -2
- package/dist/json-patch/ops/copy.js +32 -33
- package/dist/json-patch/ops/increment.d.ts +5 -2
- package/dist/json-patch/ops/increment.js +21 -20
- package/dist/json-patch/ops/index.d.ts +10 -21
- package/dist/json-patch/ops/index.js +34 -24
- package/dist/json-patch/ops/move.d.ts +5 -2
- package/dist/json-patch/ops/move.js +132 -198
- package/dist/json-patch/ops/remove.d.ts +5 -2
- package/dist/json-patch/ops/remove.js +33 -30
- package/dist/json-patch/ops/replace.d.ts +5 -2
- package/dist/json-patch/ops/replace.js +45 -43
- package/dist/json-patch/ops/test.d.ts +5 -2
- package/dist/json-patch/ops/test.js +25 -21
- package/dist/json-patch/ops/text.d.ts +5 -2
- package/dist/json-patch/ops/text.js +54 -54
- package/dist/json-patch/pathProxy.d.ts +9 -3
- package/dist/json-patch/pathProxy.js +27 -48
- package/dist/json-patch/state.d.ts +5 -2
- package/dist/json-patch/state.js +11 -7
- package/dist/json-patch/transformPatch.d.ts +6 -2
- package/dist/json-patch/transformPatch.js +21 -24
- package/dist/json-patch/types.d.ts +9 -7
- package/dist/json-patch/types.js +0 -1
- package/dist/json-patch/utils/deepEqual.d.ts +3 -1
- package/dist/json-patch/utils/deepEqual.js +32 -28
- package/dist/json-patch/utils/exit.d.ts +5 -2
- package/dist/json-patch/utils/exit.js +7 -3
- package/dist/json-patch/utils/get.d.ts +5 -2
- package/dist/json-patch/utils/get.js +8 -4
- package/dist/json-patch/utils/getOpData.d.ts +5 -2
- package/dist/json-patch/utils/getOpData.js +12 -9
- package/dist/json-patch/utils/getType.d.ts +6 -3
- package/dist/json-patch/utils/getType.js +9 -4
- package/dist/json-patch/utils/index.d.ts +15 -14
- package/dist/json-patch/utils/index.js +14 -14
- package/dist/json-patch/utils/log.d.ts +4 -2
- package/dist/json-patch/utils/log.js +8 -3
- package/dist/json-patch/utils/ops.d.ts +8 -5
- package/dist/json-patch/utils/ops.js +83 -100
- package/dist/json-patch/utils/paths.d.ts +12 -9
- package/dist/json-patch/utils/paths.js +54 -51
- package/dist/json-patch/utils/pluck.d.ts +8 -5
- package/dist/json-patch/utils/pluck.js +32 -26
- package/dist/json-patch/utils/shallowCopy.d.ts +3 -1
- package/dist/json-patch/utils/shallowCopy.js +22 -18
- package/dist/json-patch/utils/softWrites.d.ts +6 -3
- package/dist/json-patch/utils/softWrites.js +17 -16
- package/dist/json-patch/utils/toArrayIndex.d.ts +3 -1
- package/dist/json-patch/utils/toArrayIndex.js +14 -10
- package/dist/json-patch/utils/toKeys.d.ts +3 -1
- package/dist/json-patch/utils/toKeys.js +15 -11
- package/dist/json-patch/utils/updateArrayIndexes.d.ts +5 -2
- package/dist/json-patch/utils/updateArrayIndexes.js +33 -37
- package/dist/json-patch/utils/updateArrayPath.d.ts +5 -2
- package/dist/json-patch/utils/updateArrayPath.js +29 -42
- package/dist/net/PatchesClient.d.ts +128 -0
- package/dist/net/PatchesClient.js +161 -0
- package/dist/net/PatchesSync.d.ts +19 -9
- package/dist/net/PatchesSync.js +291 -386
- package/dist/net/error.d.ts +3 -1
- package/dist/net/error.js +9 -6
- package/dist/net/http/FetchTransport.d.ts +21 -0
- package/dist/net/http/FetchTransport.js +34 -0
- package/dist/net/index.d.ts +26 -12
- package/dist/net/index.js +12 -10
- package/dist/net/protocol/JSONRPCClient.d.ts +11 -4
- package/dist/net/protocol/JSONRPCClient.js +95 -103
- package/dist/net/protocol/JSONRPCServer.d.ts +15 -8
- package/dist/net/protocol/JSONRPCServer.js +101 -123
- package/dist/net/protocol/types.d.ts +21 -15
- package/dist/net/protocol/types.js +0 -1
- package/dist/net/protocol/utils.d.ts +12 -0
- package/dist/net/protocol/utils.js +15 -0
- package/dist/net/types.d.ts +4 -2
- package/dist/net/types.js +0 -1
- package/dist/net/webrtc/WebRTCAwareness.d.ts +14 -4
- package/dist/net/webrtc/WebRTCAwareness.js +111 -120
- package/dist/net/webrtc/WebRTCTransport.d.ts +16 -8
- package/dist/net/webrtc/WebRTCTransport.js +149 -157
- package/dist/net/webrtc/index.d.ts +10 -2
- package/dist/net/webrtc/index.js +2 -2
- package/dist/net/websocket/AuthorizationProvider.d.ts +7 -5
- package/dist/net/websocket/AuthorizationProvider.js +12 -17
- package/dist/net/websocket/PatchesWebSocket.d.ts +14 -109
- package/dist/net/websocket/PatchesWebSocket.js +37 -184
- package/dist/net/websocket/RPCServer.d.ts +19 -10
- package/dist/net/websocket/RPCServer.js +190 -192
- package/dist/net/websocket/SignalingService.d.ts +12 -32
- package/dist/net/websocket/SignalingService.js +126 -133
- package/dist/net/websocket/WebSocketServer.d.ts +17 -4
- package/dist/net/websocket/WebSocketServer.js +64 -72
- package/dist/net/websocket/WebSocketTransport.d.ts +13 -5
- package/dist/net/websocket/WebSocketTransport.js +178 -207
- package/dist/net/websocket/onlineState.d.ts +6 -3
- package/dist/net/websocket/onlineState.js +25 -21
- package/dist/server/PatchesBranchManager.d.ts +12 -5
- package/dist/server/PatchesBranchManager.js +132 -142
- package/dist/server/PatchesHistoryManager.d.ts +11 -3
- package/dist/server/PatchesHistoryManager.js +81 -84
- package/dist/server/PatchesServer.d.ts +16 -10
- package/dist/server/PatchesServer.js +131 -137
- package/dist/server/index.d.ts +7 -2
- package/dist/server/index.js +9 -3
- package/dist/server/types.d.ts +9 -3
- package/dist/server/types.js +0 -1
- package/dist/types.d.ts +38 -19
- package/dist/types.js +1 -1
- package/dist/utils/concurrency.d.ts +7 -5
- package/dist/utils/concurrency.js +43 -53
- package/dist/utils/deferred.d.ts +4 -2
- package/dist/utils/deferred.js +25 -21
- package/package.json +5 -7
|
@@ -1,211 +1,145 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
if (Array.isArray(target)) {
|
|
21
|
-
const index = toArrayIndex(target, lastKey);
|
|
22
|
-
if (index < 0 || target.length <= index) {
|
|
23
|
-
return `[op:move] invalid array index: ${path}`;
|
|
24
|
-
}
|
|
25
|
-
value = target[index];
|
|
26
|
-
pluckWithShallowCopy(state, keys, true).splice(index, 1);
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
value = target[lastKey];
|
|
30
|
-
delete pluckWithShallowCopy(state, keys, true)[lastKey];
|
|
31
|
-
}
|
|
32
|
-
return add.apply(state, path, value);
|
|
33
|
-
},
|
|
34
|
-
invert(_state, { path, from }) {
|
|
35
|
-
return { op: 'move', from: path, path: '' + from };
|
|
36
|
-
},
|
|
37
|
-
transform(state, thisOp, otherOps) {
|
|
38
|
-
log('Transforming', otherOps, 'against "move"', thisOp);
|
|
39
|
-
let removed = false;
|
|
40
|
-
const { from, path } = thisOp;
|
|
41
|
-
if (from === path)
|
|
42
|
-
return otherOps;
|
|
43
|
-
const [fromPrefix, fromIndex] = getArrayPrefixAndIndex(state, from);
|
|
44
|
-
const [pathPrefix, pathIndex] = getArrayPrefixAndIndex(state, path);
|
|
45
|
-
const isPathArray = pathPrefix !== undefined;
|
|
46
|
-
const isSameArray = isPathArray && pathPrefix === fromPrefix;
|
|
47
|
-
/*
|
|
48
|
-
A move needs to do a "remove" and an "add" at once with `from` and `path`. If it is being moved from one location in
|
|
49
|
-
an array to another in the same array, this needs to be handled special.
|
|
50
|
-
|
|
51
|
-
1. Ops that were added to where the move lands when not an array should be removed just like with an add/copy
|
|
52
|
-
2. Ops that were added to where the move came from should be translated to the new path
|
|
53
|
-
3. Ops that are in an array with the moved item after need to be adjusted up or down
|
|
54
|
-
3a. But, ops that were translated to the new path shouldn't get adjusted up or down by these adjustments
|
|
55
|
-
*/
|
|
56
|
-
// A move removes the value from one place then adds it to another, update the paths and add a marker to them so
|
|
57
|
-
// they won't be altered by `updateArrayIndexes`, then remove the markers afterwards
|
|
58
|
-
otherOps = mapAndFilterOps(otherOps, otherOp => {
|
|
59
|
-
if (removed) {
|
|
60
|
-
return otherOp;
|
|
61
|
-
}
|
|
62
|
-
const opLike = getTypeLike(state, otherOp);
|
|
63
|
-
if (opLike === 'remove' && from === otherOp.path) {
|
|
64
|
-
// Once an operation removes the moved value, the following ops should be working on the old location and not
|
|
65
|
-
// not the new one. Allow the following operations (which may include add/remove) to affect the old location
|
|
66
|
-
removed = true;
|
|
67
|
-
}
|
|
68
|
-
const original = otherOp;
|
|
69
|
-
otherOp = updateMovePath(state, otherOp, 'path', from, path, original);
|
|
70
|
-
otherOp = updateMovePath(state, otherOp, 'from', from, path, original);
|
|
71
|
-
return otherOp;
|
|
72
|
-
});
|
|
73
|
-
// Remove/adjust items that were affected by this item moving (those that actually moved because of it will not
|
|
74
|
-
// be affected because they have a temporary $ marker prefix that will keep them from doing so)
|
|
75
|
-
if (isSameArray) {
|
|
76
|
-
// need special logic when a move is within one array
|
|
77
|
-
otherOps = updateArrayIndexesForMove(state, fromPrefix, fromIndex, pathIndex, otherOps);
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
// if a move is not within one array, treat it as a remove then add
|
|
81
|
-
if (isArrayPath(from, state)) {
|
|
82
|
-
otherOps = updateArrayIndexes(state, from, otherOps, -1);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
otherOps = updateRemovedOps(state, from, otherOps);
|
|
86
|
-
}
|
|
87
|
-
if (isArrayPath(path, state)) {
|
|
88
|
-
otherOps = updateArrayIndexes(state, path, otherOps, 1);
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
otherOps = updateRemovedOps(state, path, otherOps);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
// Remove the move markers added with `updateMovePath`
|
|
95
|
-
return mapAndFilterOps(otherOps, removeMoveMarkers);
|
|
96
|
-
},
|
|
97
|
-
};
|
|
98
|
-
/**
|
|
99
|
-
* Update paths for a move operation, adding a marker so the path will not be altered by array updates.
|
|
100
|
-
*/
|
|
101
|
-
function updateMovePath(state, op, pathName, from, to, original) {
|
|
102
|
-
const path = op[pathName];
|
|
103
|
-
if (!path)
|
|
104
|
-
return op; // No adjustment needed on a property that doesn't exist
|
|
105
|
-
// If a value is being added or copied to the old location it should not be adjusted
|
|
106
|
-
if (isAdd(state, op, pathName) && op.path === from) {
|
|
107
|
-
return op;
|
|
1
|
+
import "../../chunk-IZ2YBCUP.js";
|
|
2
|
+
import { getOpData } from "../utils/getOpData.js";
|
|
3
|
+
import { getTypeLike } from "../utils/getType.js";
|
|
4
|
+
import { log } from "../utils/log.js";
|
|
5
|
+
import { isAdd, mapAndFilterOps, updateRemovedOps } from "../utils/ops.js";
|
|
6
|
+
import { getArrayPrefixAndIndex, getIndexAndEnd, isArrayPath } from "../utils/paths.js";
|
|
7
|
+
import { getValue, pluckWithShallowCopy } from "../utils/pluck.js";
|
|
8
|
+
import { toArrayIndex } from "../utils/toArrayIndex.js";
|
|
9
|
+
import { updateArrayIndexes } from "../utils/updateArrayIndexes.js";
|
|
10
|
+
import { add } from "./add.js";
|
|
11
|
+
const move = {
|
|
12
|
+
like: "move",
|
|
13
|
+
apply(state, path, from) {
|
|
14
|
+
if (path === from) return;
|
|
15
|
+
let value;
|
|
16
|
+
const [keys, lastKey, target] = getOpData(state, from);
|
|
17
|
+
if (target === null) {
|
|
18
|
+
return `[op:move] path not found: ${from}`;
|
|
108
19
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
20
|
+
if (Array.isArray(target)) {
|
|
21
|
+
const index = toArrayIndex(target, lastKey);
|
|
22
|
+
if (index < 0 || target.length <= index) {
|
|
23
|
+
return `[op:move] invalid array index: ${path}`;
|
|
24
|
+
}
|
|
25
|
+
value = target[index];
|
|
26
|
+
pluckWithShallowCopy(state, keys, true).splice(index, 1);
|
|
27
|
+
} else {
|
|
28
|
+
value = target[lastKey];
|
|
29
|
+
delete pluckWithShallowCopy(state, keys, true)[lastKey];
|
|
30
|
+
}
|
|
31
|
+
return add.apply(state, path, value);
|
|
32
|
+
},
|
|
33
|
+
invert(_state, { path, from }) {
|
|
34
|
+
return { op: "move", from: path, path: "" + from };
|
|
35
|
+
},
|
|
36
|
+
transform(state, thisOp, otherOps) {
|
|
37
|
+
log("Transforming", otherOps, 'against "move"', thisOp);
|
|
38
|
+
let removed = false;
|
|
39
|
+
const { from, path } = thisOp;
|
|
40
|
+
if (from === path) return otherOps;
|
|
41
|
+
const [fromPrefix, fromIndex] = getArrayPrefixAndIndex(state, from);
|
|
42
|
+
const [pathPrefix, pathIndex] = getArrayPrefixAndIndex(state, path);
|
|
43
|
+
const isPathArray = pathPrefix !== void 0;
|
|
44
|
+
const isSameArray = isPathArray && pathPrefix === fromPrefix;
|
|
45
|
+
otherOps = mapAndFilterOps(otherOps, (otherOp) => {
|
|
46
|
+
if (removed) {
|
|
47
|
+
return otherOp;
|
|
48
|
+
}
|
|
49
|
+
const opLike = getTypeLike(state, otherOp);
|
|
50
|
+
if (opLike === "remove" && from === otherOp.path) {
|
|
51
|
+
removed = true;
|
|
52
|
+
}
|
|
53
|
+
const original = otherOp;
|
|
54
|
+
otherOp = updateMovePath(state, otherOp, "path", from, path, original);
|
|
55
|
+
otherOp = updateMovePath(state, otherOp, "from", from, path, original);
|
|
56
|
+
return otherOp;
|
|
57
|
+
});
|
|
58
|
+
if (isSameArray) {
|
|
59
|
+
otherOps = updateArrayIndexesForMove(state, fromPrefix, fromIndex, pathIndex, otherOps);
|
|
60
|
+
} else {
|
|
61
|
+
if (isArrayPath(from, state)) {
|
|
62
|
+
otherOps = updateArrayIndexes(state, from, otherOps, -1);
|
|
63
|
+
} else {
|
|
64
|
+
otherOps = updateRemovedOps(state, from, otherOps);
|
|
65
|
+
}
|
|
66
|
+
if (isArrayPath(path, state)) {
|
|
67
|
+
otherOps = updateArrayIndexes(state, path, otherOps, 1);
|
|
68
|
+
} else {
|
|
69
|
+
otherOps = updateRemovedOps(state, path, otherOps);
|
|
70
|
+
}
|
|
118
71
|
}
|
|
72
|
+
return mapAndFilterOps(otherOps, removeMoveMarkers);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
function updateMovePath(state, op, pathName, from, to, original) {
|
|
76
|
+
const path = op[pathName];
|
|
77
|
+
if (!path) return op;
|
|
78
|
+
if (isAdd(state, op, pathName) && op.path === from) {
|
|
119
79
|
return op;
|
|
80
|
+
}
|
|
81
|
+
if (path === from || path.indexOf(from + "/") === 0) {
|
|
82
|
+
if (op === original) op = Object.assign({}, op);
|
|
83
|
+
log("Moving", op, "from", from, "to", to);
|
|
84
|
+
op[pathName] = "$" + path.replace(from, to);
|
|
85
|
+
}
|
|
86
|
+
return op;
|
|
120
87
|
}
|
|
121
|
-
/**
|
|
122
|
-
* Update array indexes to account for values being added or removed from an array. If the path is not an array index
|
|
123
|
-
* or if nothing is changed then the original array is returned.
|
|
124
|
-
*/
|
|
125
88
|
function updateArrayIndexesForMove(state, prefix, fromIndex, pathIndex, otherOps) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
otherOp.from = fromUpdate.from;
|
|
138
|
-
}
|
|
139
|
-
return otherOp;
|
|
140
|
-
});
|
|
89
|
+
log(`Shifting array indexes for a move between ${prefix}/${fromIndex} and ${prefix}/${pathIndex}`);
|
|
90
|
+
return mapAndFilterOps(otherOps, (otherOp) => {
|
|
91
|
+
const fromUpdate = updateArrayPathForMove(state, otherOp, "from", prefix, fromIndex, pathIndex);
|
|
92
|
+
const pathUpdate = updateArrayPathForMove(state, otherOp, "path", prefix, fromIndex, pathIndex);
|
|
93
|
+
if (!fromUpdate || !pathUpdate) return null;
|
|
94
|
+
if (fromUpdate !== otherOp || pathUpdate !== otherOp) {
|
|
95
|
+
otherOp = { ...otherOp, path: pathUpdate.path };
|
|
96
|
+
if (fromUpdate.from) otherOp.from = fromUpdate.from;
|
|
97
|
+
}
|
|
98
|
+
return otherOp;
|
|
99
|
+
});
|
|
141
100
|
}
|
|
142
|
-
/**
|
|
143
|
-
* Get the adjusted path if it is higher, or undefined if not.
|
|
144
|
-
*/
|
|
145
101
|
function updateArrayPathForMove(state, otherOp, pathName, prefix, from, to) {
|
|
146
|
-
|
|
147
|
-
|
|
102
|
+
const path = otherOp[pathName];
|
|
103
|
+
if (!path || !path.startsWith(prefix)) return otherOp;
|
|
104
|
+
const min = Math.min(from, to);
|
|
105
|
+
const max = Math.max(from, to);
|
|
106
|
+
const [otherIndex, end] = getIndexAndEnd(state, path, prefix.length);
|
|
107
|
+
if (otherIndex === void 0) return otherOp;
|
|
108
|
+
const isFinalProp = end === path.length;
|
|
109
|
+
const opLike = getTypeLike(state, otherOp);
|
|
110
|
+
if (otherIndex < min || otherIndex > max) {
|
|
111
|
+
return otherOp;
|
|
112
|
+
}
|
|
113
|
+
if (isFinalProp && isAdd(state, otherOp, pathName)) {
|
|
114
|
+
if (otherIndex === min) {
|
|
115
|
+
if (min === from) {
|
|
148
116
|
return otherOp;
|
|
149
|
-
|
|
150
|
-
const max = Math.max(from, to);
|
|
151
|
-
const [otherIndex, end] = getIndexAndEnd(state, path, prefix.length);
|
|
152
|
-
if (otherIndex === undefined)
|
|
153
|
-
return otherOp; // if a prop on an array is being set, for e.g.
|
|
154
|
-
const isFinalProp = end === path.length;
|
|
155
|
-
const opLike = getTypeLike(state, otherOp);
|
|
156
|
-
// If this index is not within the movement boundary, don't touch it
|
|
157
|
-
if (otherIndex < min || otherIndex > max) {
|
|
117
|
+
} else {
|
|
158
118
|
return otherOp;
|
|
119
|
+
}
|
|
120
|
+
} else if (otherIndex === max) {
|
|
121
|
+
if (max === from) {
|
|
122
|
+
const fromIndex = getIndexAndEnd(state, otherOp.from, prefix.length)[0];
|
|
123
|
+
if (opLike === "move" && pathName === "path" && to <= fromIndex && fromIndex < from) return otherOp;
|
|
124
|
+
} else {
|
|
125
|
+
return otherOp;
|
|
126
|
+
}
|
|
159
127
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
use the remove logic with an add
|
|
165
|
-
|
|
166
|
-
if the move is from high to low (min is an add, max is a remove) then
|
|
167
|
-
use the add logic at the bottom
|
|
168
|
-
*/
|
|
169
|
-
if (otherIndex === min) {
|
|
170
|
-
if (min === from) {
|
|
171
|
-
// treat like a remove
|
|
172
|
-
return otherOp;
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
// treat like an add
|
|
176
|
-
return otherOp;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
else if (otherIndex === max) {
|
|
180
|
-
if (max === from) {
|
|
181
|
-
// treat like a remove
|
|
182
|
-
const fromIndex = getIndexAndEnd(state, otherOp.from, prefix.length)[0];
|
|
183
|
-
if (opLike === 'move' && pathName === 'path' && to <= fromIndex && fromIndex < from)
|
|
184
|
-
return otherOp;
|
|
185
|
-
// continue
|
|
186
|
-
}
|
|
187
|
-
else {
|
|
188
|
-
// treat like an add
|
|
189
|
-
return otherOp;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
const modifier = from === min ? -1 : 1;
|
|
194
|
-
const newPath = prefix + (otherIndex + modifier) + path.slice(end);
|
|
195
|
-
return getValue(state, otherOp, pathName, newPath);
|
|
128
|
+
}
|
|
129
|
+
const modifier = from === min ? -1 : 1;
|
|
130
|
+
const newPath = prefix + (otherIndex + modifier) + path.slice(end);
|
|
131
|
+
return getValue(state, otherOp, pathName, newPath);
|
|
196
132
|
}
|
|
197
|
-
/**
|
|
198
|
-
* Remove any move markers placed during updateMovePath. This occurs in-place since these objects have already been
|
|
199
|
-
* cloned.
|
|
200
|
-
*/
|
|
201
133
|
function removeMoveMarkers(op) {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
return op;
|
|
134
|
+
if (op.path[0] === "$") {
|
|
135
|
+
op.path = op.path.slice(1);
|
|
136
|
+
}
|
|
137
|
+
if (op.from && op.from[0] === "$") {
|
|
138
|
+
op.from = op.from.slice(1);
|
|
139
|
+
}
|
|
140
|
+
if (op.from === op.path) return null;
|
|
141
|
+
return op;
|
|
211
142
|
}
|
|
143
|
+
export {
|
|
144
|
+
move
|
|
145
|
+
};
|
|
@@ -1,31 +1,34 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
1
|
+
import "../../chunk-IZ2YBCUP.js";
|
|
2
|
+
import { getOpData } from "../utils/getOpData.js";
|
|
3
|
+
import { log } from "../utils/log.js";
|
|
4
|
+
import { transformRemove } from "../utils/ops.js";
|
|
5
|
+
import { pluckWithShallowCopy } from "../utils/pluck.js";
|
|
6
|
+
import { toArrayIndex } from "../utils/toArrayIndex.js";
|
|
7
|
+
const remove = {
|
|
8
|
+
like: "remove",
|
|
9
|
+
apply(state, path) {
|
|
10
|
+
const [keys, lastKey, target] = getOpData(state, path);
|
|
11
|
+
if (target === null) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (Array.isArray(target)) {
|
|
15
|
+
const index = toArrayIndex(target, lastKey);
|
|
16
|
+
if (index < 0 || target.length <= index) {
|
|
17
|
+
return "[op:remove] invalid array index: " + path;
|
|
18
|
+
}
|
|
19
|
+
pluckWithShallowCopy(state, keys).splice(index, 1);
|
|
20
|
+
} else {
|
|
21
|
+
delete pluckWithShallowCopy(state, keys)[lastKey];
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
invert(_state, { path }, value) {
|
|
25
|
+
return { op: "add", path, value };
|
|
26
|
+
},
|
|
27
|
+
transform(state, thisOp, otherOps) {
|
|
28
|
+
log("Transforming", otherOps, 'against "remove"', thisOp);
|
|
29
|
+
return transformRemove(state, thisOp.path, otherOps, true);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
export {
|
|
33
|
+
remove
|
|
31
34
|
};
|
|
@@ -1,44 +1,46 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
1
|
+
import "../../chunk-IZ2YBCUP.js";
|
|
2
|
+
import { deepEqual } from "../utils/deepEqual.js";
|
|
3
|
+
import { getOpData } from "../utils/getOpData.js";
|
|
4
|
+
import { log } from "../utils/log.js";
|
|
5
|
+
import { updateRemovedOps } from "../utils/ops.js";
|
|
6
|
+
import { pluckWithShallowCopy } from "../utils/pluck.js";
|
|
7
|
+
import { toArrayIndex } from "../utils/toArrayIndex.js";
|
|
8
|
+
const replace = {
|
|
9
|
+
like: "replace",
|
|
10
|
+
apply(state, path, value) {
|
|
11
|
+
if (typeof value === "undefined") {
|
|
12
|
+
return "[op:replace] require value, but got undefined";
|
|
13
|
+
}
|
|
14
|
+
const [keys, lastKey, target] = getOpData(state, path, true);
|
|
15
|
+
if (target === null) {
|
|
16
|
+
return `[op:replace] path not found: ${path}`;
|
|
17
|
+
}
|
|
18
|
+
if (Array.isArray(target)) {
|
|
19
|
+
const index = toArrayIndex(target, lastKey);
|
|
20
|
+
if (index < 0 || target.length <= index) {
|
|
21
|
+
return `[op:replace] invalid array index: ${path}`;
|
|
22
|
+
}
|
|
23
|
+
if (!deepEqual(target[index], value)) {
|
|
24
|
+
pluckWithShallowCopy(state, keys, true).splice(index, 1, value);
|
|
25
|
+
}
|
|
26
|
+
} else {
|
|
27
|
+
if (!deepEqual(target[lastKey], value)) {
|
|
28
|
+
pluckWithShallowCopy(state, keys, true)[lastKey] = value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
invert(_state, { path }, value, changedObj) {
|
|
33
|
+
if (path.endsWith("/-")) path = path.replace("-", changedObj.length);
|
|
34
|
+
return value === void 0 ? { op: "remove", path } : { op: "replace", path, value };
|
|
35
|
+
},
|
|
36
|
+
transform(state, thisOp, otherOps) {
|
|
37
|
+
log("Transforming ", otherOps, ' against "replace"', thisOp);
|
|
38
|
+
return updateRemovedOps(state, thisOp.path, otherOps);
|
|
39
|
+
},
|
|
40
|
+
compose(_state, _value1, value2) {
|
|
41
|
+
return value2;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
export {
|
|
45
|
+
replace
|
|
44
46
|
};
|
|
@@ -1,22 +1,26 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
import "../../chunk-IZ2YBCUP.js";
|
|
2
|
+
import { deepEqual } from "../utils/deepEqual.js";
|
|
3
|
+
import { getOpData } from "../utils/getOpData.js";
|
|
4
|
+
const test = {
|
|
5
|
+
like: "test",
|
|
6
|
+
apply(state, path, expected) {
|
|
7
|
+
const [, lastKey, target] = getOpData(state, path);
|
|
8
|
+
if (target === null) {
|
|
9
|
+
return `[op:test] path not found: ${path}`;
|
|
10
|
+
}
|
|
11
|
+
if (!deepEqual(target[lastKey], expected)) {
|
|
12
|
+
const a = JSON.stringify(target[lastKey]);
|
|
13
|
+
const b = JSON.stringify(expected);
|
|
14
|
+
return `[op:test] not matched: ${a} ${b}`;
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
invert() {
|
|
18
|
+
return void 0;
|
|
19
|
+
},
|
|
20
|
+
transform(_state, _other, ops) {
|
|
21
|
+
return ops;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
export {
|
|
25
|
+
test
|
|
22
26
|
};
|