mobx-keystone 1.20.0 → 1.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +859 -853
- package/README.md +61 -61
- package/dist/mobx-keystone.esm.js +11287 -11148
- package/dist/mobx-keystone.esm.mjs +11287 -11148
- package/dist/mobx-keystone.umd.js +12180 -11738
- package/dist/types/actionMiddlewares/undoMiddleware.d.ts +1 -1
- package/dist/types/modelShared/prop.d.ts +5 -0
- package/dist/types/ref/Ref.d.ts +1 -1
- package/dist/types/treeUtils/sandbox.d.ts +1 -1
- package/dist/types/types/TypeCheckError.d.ts +2 -12
- package/dist/types/types/TypeChecker.d.ts +1 -1
- package/dist/types/types/arrayBased/typesArray.d.ts +1 -1
- package/dist/types/types/arrayBased/typesTuple.d.ts +1 -1
- package/dist/types/types/objectBased/typesArraySet.d.ts +1 -1
- package/dist/types/types/objectBased/typesDataModelData.d.ts +1 -1
- package/dist/types/types/objectBased/typesModel.d.ts +1 -1
- package/dist/types/types/objectBased/typesObject.d.ts +3 -3
- package/dist/types/types/objectBased/typesObjectMap.d.ts +1 -1
- package/dist/types/types/objectBased/typesRecord.d.ts +1 -1
- package/dist/types/types/primitiveBased/typesPrimitive.d.ts +1 -1
- package/dist/types/types/types.d.ts +3 -3
- package/dist/types/types/utility/typesCodec.d.ts +1 -6
- package/dist/types/types/utility/typesCodecCore.d.ts +1 -1
- package/dist/types/types/utility/typesOr.d.ts +1 -1
- package/dist/types/types/utility/typesRefinement.d.ts +1 -1
- package/dist/types/types/utility/typesSkipCheck.d.ts +1 -1
- package/dist/types/types/utility/typesTag.d.ts +1 -1
- package/dist/types/wrappers/ObjectMap.d.ts +2 -0
- package/dist/types/wrappers/asMap.d.ts +10 -6
- package/package.json +9 -7
- package/src/action/applyAction.ts +125 -114
- package/src/action/applyMethodCall.ts +37 -37
- package/src/action/isModelAction.ts +16 -16
- package/src/action/middleware.ts +137 -137
- package/src/action/modelFlow.ts +240 -240
- package/src/action/modelFlowPromiseGenerator.ts +127 -125
- package/src/action/pendingActions.ts +42 -42
- package/src/action/wrapInAction.ts +156 -156
- package/src/actionMiddlewares/actionSerialization/actionSerialization.ts +1 -1
- package/src/actionMiddlewares/actionSerialization/applySerializedAction.ts +172 -172
- package/src/actionMiddlewares/actionSerialization/arraySerializer.ts +20 -20
- package/src/actionMiddlewares/actionSerialization/dateSerializer.ts +17 -17
- package/src/actionMiddlewares/actionSerialization/mapSerializer.ts +42 -42
- package/src/actionMiddlewares/actionSerialization/objectPathSerializer.ts +50 -50
- package/src/actionMiddlewares/actionSerialization/objectSnapshotSerializer.ts +21 -21
- package/src/actionMiddlewares/actionSerialization/plainObjectSerializer.ts +24 -32
- package/src/actionMiddlewares/actionSerialization/primitiveSerializer.ts +51 -51
- package/src/actionMiddlewares/actionSerialization/setSerializer.ts +37 -37
- package/src/actionMiddlewares/actionTrackingMiddleware.ts +3 -3
- package/src/actionMiddlewares/onActionMiddleware.ts +2 -2
- package/src/actionMiddlewares/readonlyMiddleware.ts +2 -2
- package/src/actionMiddlewares/transactionMiddleware.ts +4 -4
- package/src/actionMiddlewares/undoMiddleware.ts +804 -796
- package/src/actionMiddlewares/utils.ts +2 -2
- package/src/computedTree/computedTree.ts +143 -143
- package/src/context/context.ts +1 -1
- package/src/dataModel/BaseDataModel.ts +194 -192
- package/src/dataModel/actions.ts +1 -1
- package/src/dataModel/newDataModel.ts +57 -57
- package/src/deepChange/index.ts +1 -1
- package/src/deepChange/onDeepChange.ts +244 -244
- package/src/frozen/Frozen.ts +146 -146
- package/src/globalConfig/globalConfig.ts +107 -107
- package/src/index.ts +24 -24
- package/src/model/BaseModel.ts +275 -273
- package/src/model/ModelConstructorOptions.ts +15 -15
- package/src/model/getModelMetadata.ts +51 -51
- package/src/model/index.ts +6 -6
- package/src/model/mixins.ts +288 -288
- package/src/model/newModel.ts +301 -301
- package/src/model/utils.ts +249 -249
- package/src/modelShared/modelDecorator.ts +260 -253
- package/src/modelShared/modelInfo.ts +50 -50
- package/src/modelShared/modelSymbols.ts +3 -3
- package/src/modelShared/newModel.ts +25 -25
- package/src/modelShared/prop.ts +713 -684
- package/src/modelShared/sharedInternalModel.ts +427 -417
- package/src/modelShared/utils.ts +24 -24
- package/src/parent/core.ts +65 -65
- package/src/parent/coreObjectChildren.ts +180 -180
- package/src/parent/detach.ts +47 -47
- package/src/parent/findParent.ts +1 -1
- package/src/parent/getChildrenObjects.ts +25 -25
- package/src/parent/path.ts +386 -381
- package/src/parent/setParent.ts +149 -149
- package/src/parent/walkTree.ts +1 -1
- package/src/patch/applyPatches.ts +187 -187
- package/src/patch/jsonPatch.ts +108 -108
- package/src/patch/patchRecorder.ts +6 -1
- package/src/redux/connectReduxDevTools.ts +2 -2
- package/src/redux/redux.ts +6 -2
- package/src/ref/Ref.ts +1 -1
- package/src/ref/core.ts +339 -345
- package/src/ref/customRef.ts +3 -3
- package/src/ref/rootRef.ts +100 -100
- package/src/rootStore/attachDetach.ts +89 -89
- package/src/rootStore/rootStore.ts +1 -1
- package/src/snapshot/SnapshotOf.ts +104 -104
- package/src/snapshot/SnapshotProcessingError.ts +45 -45
- package/src/snapshot/applySnapshot.ts +177 -181
- package/src/snapshot/cloneTreeValue.ts +30 -30
- package/src/snapshot/fromArraySnapshot.ts +33 -33
- package/src/snapshot/fromModelSnapshot.ts +70 -70
- package/src/snapshot/fromPlainObjectSnapshot.ts +42 -42
- package/src/snapshot/fromSnapshot.ts +199 -198
- package/src/snapshot/getSnapshot.ts +80 -79
- package/src/snapshot/index.ts +8 -8
- package/src/snapshot/internal.ts +230 -230
- package/src/snapshot/reconcileArraySnapshot.ts +70 -70
- package/src/snapshot/reconcileModelSnapshot.ts +155 -150
- package/src/snapshot/reconcilePlainObjectSnapshot.ts +72 -72
- package/src/snapshot/reconcileSnapshot.ts +88 -88
- package/src/standardActions/actions.ts +64 -64
- package/src/standardActions/objectActions.ts +60 -60
- package/src/transforms/ImmutableDate.ts +93 -93
- package/src/transforms/asMap.ts +50 -50
- package/src/transforms/asSet.ts +22 -22
- package/src/transforms/bigint.ts +20 -20
- package/src/transforms/date.ts +40 -40
- package/src/treeUtils/deepEquals.ts +55 -55
- package/src/treeUtils/sandbox.ts +6 -3
- package/src/tweaker/TweakerPriority.ts +9 -9
- package/src/tweaker/tweak.ts +4 -3
- package/src/tweaker/tweakArray.ts +475 -475
- package/src/tweaker/tweakFrozen.ts +42 -42
- package/src/tweaker/tweakModel.ts +1 -1
- package/src/tweaker/tweakPlainObject.ts +355 -355
- package/src/tweaker/typeChecking.ts +101 -101
- package/src/types/SnapshotTypeMismatchError.ts +46 -46
- package/src/types/TypeCheckError.ts +165 -184
- package/src/types/TypeChecker.ts +202 -185
- package/src/types/arrayBased/typesArray.ts +136 -137
- package/src/types/arrayBased/typesTuple.ts +138 -139
- package/src/types/createPerEntryCachedCheck.ts +166 -160
- package/src/types/index.ts +6 -6
- package/src/types/objectBased/typesArraySet.ts +133 -134
- package/src/types/objectBased/typesDataModelData.ts +203 -204
- package/src/types/objectBased/typesModel.ts +220 -221
- package/src/types/objectBased/typesObject.ts +308 -310
- package/src/types/objectBased/typesObjectMap.ts +142 -143
- package/src/types/objectBased/typesRecord.ts +156 -157
- package/src/types/objectBased/typesRef.ts +89 -89
- package/src/types/primitiveBased/typesEnum.ts +63 -63
- package/src/types/primitiveBased/typesPrimitive.ts +219 -220
- package/src/types/registerDefaultStandardTypeResolvers.ts +21 -21
- package/src/types/resolveTypeChecker.ts +78 -78
- package/src/types/schemas.ts +208 -208
- package/src/types/tProp.ts +277 -268
- package/src/types/typeCheck.ts +21 -21
- package/src/types/typeCheckErrorUtils.ts +23 -23
- package/src/types/types.ts +142 -142
- package/src/types/utility/typeToStoredData.ts +37 -37
- package/src/types/utility/typesBigInt.ts +15 -15
- package/src/types/utility/typesCodec.ts +41 -38
- package/src/types/utility/typesCodecCore.ts +178 -178
- package/src/types/utility/typesCodecSupport.ts +806 -806
- package/src/types/utility/typesDate.ts +27 -27
- package/src/types/utility/typesMap.ts +253 -222
- package/src/types/utility/typesOr.ts +263 -259
- package/src/types/utility/typesRefinement.ts +131 -125
- package/src/types/utility/typesSet.ts +110 -110
- package/src/types/utility/typesSkipCheck.ts +69 -70
- package/src/types/utility/typesTag.ts +88 -82
- package/src/types/utility/typesUnchecked.ts +40 -40
- package/src/utils/AnyFunction.ts +4 -4
- package/src/utils/ModelPool.ts +59 -59
- package/src/utils/chainFns.ts +18 -18
- package/src/utils/decorators.ts +274 -274
- package/src/utils/errorDiagnostics.ts +204 -204
- package/src/utils/index.ts +443 -372
- package/src/wrappers/ArraySet.ts +156 -156
- package/src/wrappers/ObjectMap.ts +155 -136
- package/src/wrappers/asMap.ts +65 -30
- package/src/wrappers/asSet.ts +3 -3
|
@@ -1,475 +1,475 @@
|
|
|
1
|
-
import {
|
|
2
|
-
IArrayDidChange,
|
|
3
|
-
IArrayWillChange,
|
|
4
|
-
IArrayWillSplice,
|
|
5
|
-
IObservableArray,
|
|
6
|
-
intercept,
|
|
7
|
-
isObservableArray,
|
|
8
|
-
observable,
|
|
9
|
-
observe,
|
|
10
|
-
} from "mobx"
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import type {
|
|
16
|
-
import { setParent } from "../parent/setParent"
|
|
17
|
-
import { InternalPatchRecorder } from "../patch/emitPatch"
|
|
18
|
-
import type { Patch } from "../patch/Patch"
|
|
19
|
-
import {
|
|
20
|
-
freezeInternalSnapshot,
|
|
21
|
-
getInternalSnapshot,
|
|
22
|
-
setNewInternalSnapshot,
|
|
23
|
-
updateInternalSnapshot,
|
|
24
|
-
} from "../snapshot/internal"
|
|
25
|
-
import { failure, inDevMode, isArray, isPrimitive } from "../utils"
|
|
26
|
-
import { setIfDifferent } from "../utils/setIfDifferent"
|
|
27
|
-
import { runningWithoutSnapshotOrPatches, tweakedObjects } from "./core"
|
|
28
|
-
import { TweakerPriority } from "./TweakerPriority"
|
|
29
|
-
import { registerTweaker, tweak } from "./tweak"
|
|
30
|
-
import { runTypeCheckingAfterChange } from "./typeChecking"
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @internal
|
|
34
|
-
*/
|
|
35
|
-
export function tweakArray<T extends any[]>(
|
|
36
|
-
value: T,
|
|
37
|
-
parentPath: ParentPath<any> | undefined,
|
|
38
|
-
doNotTweakChildren: boolean
|
|
39
|
-
): T {
|
|
40
|
-
const originalArr: ReadonlyArray<any> = value
|
|
41
|
-
const arrLn = originalArr.length
|
|
42
|
-
const tweakedArr = isObservableArray(originalArr)
|
|
43
|
-
? originalArr
|
|
44
|
-
: observable.array(undefined, observableOptions)
|
|
45
|
-
if (tweakedArr !== originalArr) {
|
|
46
|
-
tweakedArr.length = originalArr.length
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
let interceptDisposer: () => void
|
|
50
|
-
let observeDisposer: () => void
|
|
51
|
-
|
|
52
|
-
const untweak = () => {
|
|
53
|
-
interceptDisposer()
|
|
54
|
-
observeDisposer()
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
tweakedObjects.set(tweakedArr, untweak)
|
|
58
|
-
setParent(
|
|
59
|
-
tweakedArr, // value
|
|
60
|
-
parentPath,
|
|
61
|
-
false, // indexChangeAllowed
|
|
62
|
-
false, // isDataObject
|
|
63
|
-
// arrays shouldn't be cloned anyway
|
|
64
|
-
false // cloneIfApplicable
|
|
65
|
-
)
|
|
66
|
-
|
|
67
|
-
const untransformedSn: any[] = []
|
|
68
|
-
untransformedSn.length = arrLn
|
|
69
|
-
|
|
70
|
-
// substitute initial values by proxied values
|
|
71
|
-
for (let i = 0; i < arrLn; i++) {
|
|
72
|
-
const v = originalArr[i]
|
|
73
|
-
|
|
74
|
-
if (isPrimitive(v)) {
|
|
75
|
-
if (!doNotTweakChildren) {
|
|
76
|
-
setIfDifferent(tweakedArr, i, v)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
untransformedSn[i] = v
|
|
80
|
-
} else {
|
|
81
|
-
const path = { parent: tweakedArr, path: i }
|
|
82
|
-
|
|
83
|
-
let tweakedValue: any
|
|
84
|
-
if (doNotTweakChildren) {
|
|
85
|
-
tweakedValue = v
|
|
86
|
-
setParent(
|
|
87
|
-
tweakedValue, // value
|
|
88
|
-
path, // parentPath
|
|
89
|
-
false, // indexChangeAllowed
|
|
90
|
-
false, // isDataObject
|
|
91
|
-
// the value is already a new value (the result of a fromSnapshot)
|
|
92
|
-
false // cloneIfApplicable
|
|
93
|
-
)
|
|
94
|
-
} else {
|
|
95
|
-
tweakedValue = tweak(v, path)
|
|
96
|
-
setIfDifferent(tweakedArr, i, tweakedValue)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const valueSn = getInternalSnapshot(tweakedValue)!
|
|
100
|
-
untransformedSn[i] = valueSn.transformed
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
setNewInternalSnapshot(tweakedArr, untransformedSn, undefined)
|
|
105
|
-
|
|
106
|
-
interceptDisposer = intercept(tweakedArr, interceptArrayMutation.bind(undefined, tweakedArr))
|
|
107
|
-
observeDisposer = observe(tweakedArr, arrayDidChange)
|
|
108
|
-
|
|
109
|
-
return tweakedArr as any
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function mutateSet(k: number, v: unknown, sn: unknown[]) {
|
|
113
|
-
sn[k] = v
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function mutateSplice(index: number, removedCount: number, addedItems: any[], sn: any[]) {
|
|
117
|
-
sn.splice(index, removedCount, ...addedItems)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const patchRecorder = new InternalPatchRecorder()
|
|
121
|
-
|
|
122
|
-
const emptyPath: Path = Object.freeze([])
|
|
123
|
-
|
|
124
|
-
function arrayDidChange(change: IArrayDidChange) {
|
|
125
|
-
const arr = change.object
|
|
126
|
-
const oldSnapshot = getInternalSnapshot(arr as Array<unknown>)!.untransformed
|
|
127
|
-
|
|
128
|
-
patchRecorder.reset()
|
|
129
|
-
|
|
130
|
-
let mutate: ((sn: any[]) => void) | undefined
|
|
131
|
-
|
|
132
|
-
switch (change.type) {
|
|
133
|
-
case "splice":
|
|
134
|
-
mutate = arrayDidChangeSplice(change, oldSnapshot)
|
|
135
|
-
// Emit deep change for splice
|
|
136
|
-
emitDeepChange(arr, {
|
|
137
|
-
type: DeepChangeType.ArraySplice,
|
|
138
|
-
target: arr,
|
|
139
|
-
path: emptyPath,
|
|
140
|
-
index: change.index as number,
|
|
141
|
-
addedValues: change.added,
|
|
142
|
-
removedValues: change.removed,
|
|
143
|
-
isInit: getIsInInitPhase(),
|
|
144
|
-
})
|
|
145
|
-
break
|
|
146
|
-
|
|
147
|
-
case "update":
|
|
148
|
-
mutate = arrayDidChangeUpdate(change, oldSnapshot)
|
|
149
|
-
// Emit deep change for update
|
|
150
|
-
emitDeepChange(arr, {
|
|
151
|
-
type: DeepChangeType.ArrayUpdate,
|
|
152
|
-
target: arr,
|
|
153
|
-
path: emptyPath,
|
|
154
|
-
index: change.index as number,
|
|
155
|
-
newValue: change.newValue,
|
|
156
|
-
oldValue: change.oldValue,
|
|
157
|
-
isInit: getIsInInitPhase(),
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
break
|
|
161
|
-
|
|
162
|
-
default:
|
|
163
|
-
break
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
runTypeCheckingAfterChange(arr, patchRecorder)
|
|
167
|
-
|
|
168
|
-
if (!runningWithoutSnapshotOrPatches && mutate) {
|
|
169
|
-
updateInternalSnapshot(arr, mutate)
|
|
170
|
-
patchRecorder.emit(arr)
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const undefinedInsideArrayErrorMsg =
|
|
175
|
-
"undefined is not supported inside arrays since it is not serializable in JSON, consider using null instead"
|
|
176
|
-
|
|
177
|
-
function arrayDidChangeUpdate(change: any /*IArrayDidChange*/, oldSnapshot: any) {
|
|
178
|
-
const k = change.index
|
|
179
|
-
const val = change.newValue
|
|
180
|
-
const oldVal = oldSnapshot[k]
|
|
181
|
-
let newVal: any
|
|
182
|
-
if (isPrimitive(val)) {
|
|
183
|
-
newVal = val
|
|
184
|
-
} else {
|
|
185
|
-
const valueSn = getInternalSnapshot(val)!
|
|
186
|
-
newVal = valueSn.transformed
|
|
187
|
-
}
|
|
188
|
-
const mutate = mutateSet.bind(undefined, k, newVal)
|
|
189
|
-
|
|
190
|
-
const path = [k]
|
|
191
|
-
|
|
192
|
-
patchRecorder.record(
|
|
193
|
-
[
|
|
194
|
-
{
|
|
195
|
-
op: "replace",
|
|
196
|
-
path,
|
|
197
|
-
value: freezeInternalSnapshot(newVal),
|
|
198
|
-
},
|
|
199
|
-
],
|
|
200
|
-
[
|
|
201
|
-
{
|
|
202
|
-
op: "replace",
|
|
203
|
-
path,
|
|
204
|
-
value: freezeInternalSnapshot(oldVal),
|
|
205
|
-
},
|
|
206
|
-
]
|
|
207
|
-
)
|
|
208
|
-
return mutate
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
function arrayDidChangeSplice(change: any /*IArrayDidChange*/, oldSnapshot: any) {
|
|
212
|
-
const index = change.index as number
|
|
213
|
-
const addedCount = change.addedCount as number
|
|
214
|
-
const removedCount = change.removedCount as number
|
|
215
|
-
|
|
216
|
-
const addedItems: any[] = []
|
|
217
|
-
addedItems.length = addedCount
|
|
218
|
-
for (let i = 0; i < addedCount; i++) {
|
|
219
|
-
const v = change.added[i]
|
|
220
|
-
if (isPrimitive(v)) {
|
|
221
|
-
addedItems[i] = v
|
|
222
|
-
} else {
|
|
223
|
-
addedItems[i] = getInternalSnapshot(v)!.transformed
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const oldLen = oldSnapshot.length
|
|
228
|
-
const mutate = mutateSplice.bind(undefined, index, removedCount, addedItems)
|
|
229
|
-
|
|
230
|
-
const patches: Patch[] = []
|
|
231
|
-
const invPatches: Patch[] = []
|
|
232
|
-
|
|
233
|
-
// optimization: if we add as many as we remove then remove/readd instead
|
|
234
|
-
|
|
235
|
-
// we cannot replace since we might end up in a situation where the same node
|
|
236
|
-
// might attempt to be temporarily twice inside the same tree (e.g. sorting)
|
|
237
|
-
|
|
238
|
-
// it would be faster to keep holes rather than remove/readd, but if we do that then
|
|
239
|
-
// validation might fail
|
|
240
|
-
|
|
241
|
-
if (addedCount === removedCount) {
|
|
242
|
-
const readdPatches: Patch[] = []
|
|
243
|
-
const readdInvPatches: Patch[] = []
|
|
244
|
-
let removed = 0
|
|
245
|
-
|
|
246
|
-
for (let i = 0; i < addedCount; i++) {
|
|
247
|
-
const realIndex = index + i
|
|
248
|
-
|
|
249
|
-
const newVal = getValueAfterSplice(oldSnapshot, realIndex, index, removedCount, addedItems)
|
|
250
|
-
const oldVal = oldSnapshot[realIndex]
|
|
251
|
-
|
|
252
|
-
if (newVal !== oldVal) {
|
|
253
|
-
const removePath = [realIndex - removed]
|
|
254
|
-
patches.push({
|
|
255
|
-
op: "remove",
|
|
256
|
-
path: removePath,
|
|
257
|
-
})
|
|
258
|
-
invPatches.push({
|
|
259
|
-
op: "remove",
|
|
260
|
-
path: removePath,
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
removed++
|
|
264
|
-
|
|
265
|
-
const readdPath = [realIndex]
|
|
266
|
-
readdPatches.push({
|
|
267
|
-
op: "add",
|
|
268
|
-
path: readdPath,
|
|
269
|
-
value: freezeInternalSnapshot(newVal),
|
|
270
|
-
})
|
|
271
|
-
|
|
272
|
-
readdInvPatches.push({
|
|
273
|
-
op: "add",
|
|
274
|
-
path: readdPath,
|
|
275
|
-
value: freezeInternalSnapshot(oldVal),
|
|
276
|
-
})
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
patches.push(...readdPatches)
|
|
281
|
-
invPatches.push(...readdInvPatches)
|
|
282
|
-
// we need to reverse since inverse patches are applied in reverse
|
|
283
|
-
invPatches.reverse()
|
|
284
|
-
} else {
|
|
285
|
-
const interimLen = oldLen - removedCount
|
|
286
|
-
|
|
287
|
-
// first remove items
|
|
288
|
-
if (removedCount > 0) {
|
|
289
|
-
// optimization, when removing from the end set the length instead
|
|
290
|
-
const removeUsingSetLength = index >= interimLen
|
|
291
|
-
if (removeUsingSetLength) {
|
|
292
|
-
patches.push({
|
|
293
|
-
op: "replace",
|
|
294
|
-
path: ["length"],
|
|
295
|
-
value: interimLen,
|
|
296
|
-
})
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
for (let i = removedCount - 1; i >= 0; i--) {
|
|
300
|
-
const realIndex = index + i
|
|
301
|
-
const path = [realIndex]
|
|
302
|
-
|
|
303
|
-
if (!removeUsingSetLength) {
|
|
304
|
-
// remove ...2, 1, 0
|
|
305
|
-
patches.push({
|
|
306
|
-
op: "remove",
|
|
307
|
-
path,
|
|
308
|
-
})
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// add 0, 1, 2... since inverse patches are applied in reverse
|
|
312
|
-
invPatches.push({
|
|
313
|
-
op: "add",
|
|
314
|
-
path,
|
|
315
|
-
value: freezeInternalSnapshot(oldSnapshot[realIndex]),
|
|
316
|
-
})
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
// then add items
|
|
321
|
-
if (addedCount > 0) {
|
|
322
|
-
// optimization, for inverse patches, when adding from the end set the length to restore instead
|
|
323
|
-
const restoreUsingSetLength = index >= interimLen
|
|
324
|
-
if (restoreUsingSetLength) {
|
|
325
|
-
invPatches.push({
|
|
326
|
-
op: "replace",
|
|
327
|
-
path: ["length"],
|
|
328
|
-
value: interimLen,
|
|
329
|
-
})
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
for (let i = 0; i < addedCount; i++) {
|
|
333
|
-
const realIndex = index + i
|
|
334
|
-
const path = [realIndex]
|
|
335
|
-
|
|
336
|
-
// add 0, 1, 2...
|
|
337
|
-
patches.push({
|
|
338
|
-
op: "add",
|
|
339
|
-
path,
|
|
340
|
-
value: freezeInternalSnapshot(
|
|
341
|
-
getValueAfterSplice(oldSnapshot, realIndex, index, removedCount, addedItems)
|
|
342
|
-
),
|
|
343
|
-
})
|
|
344
|
-
|
|
345
|
-
// remove ...2, 1, 0 since inverse patches are applied in reverse
|
|
346
|
-
if (!restoreUsingSetLength) {
|
|
347
|
-
invPatches.push({
|
|
348
|
-
op: "remove",
|
|
349
|
-
path,
|
|
350
|
-
})
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
patchRecorder.record(patches, invPatches)
|
|
357
|
-
return mutate
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
// TODO: remove array parameter and just use change.object once mobx update event is fixed
|
|
361
|
-
function interceptArrayMutation(
|
|
362
|
-
array: IObservableArray,
|
|
363
|
-
change: IArrayWillChange | IArrayWillSplice
|
|
364
|
-
) {
|
|
365
|
-
assertCanWrite()
|
|
366
|
-
|
|
367
|
-
switch (change.type) {
|
|
368
|
-
case "splice":
|
|
369
|
-
interceptArrayMutationSplice(change)
|
|
370
|
-
break
|
|
371
|
-
|
|
372
|
-
case "update":
|
|
373
|
-
interceptArrayMutationUpdate(change, array)
|
|
374
|
-
break
|
|
375
|
-
|
|
376
|
-
default:
|
|
377
|
-
break
|
|
378
|
-
}
|
|
379
|
-
return change
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
function interceptArrayMutationUpdate(change: IArrayWillChange, array: IObservableArray) {
|
|
383
|
-
if (
|
|
384
|
-
inDevMode &&
|
|
385
|
-
!getGlobalConfig().allowUndefinedArrayElements &&
|
|
386
|
-
change.newValue === undefined
|
|
387
|
-
) {
|
|
388
|
-
throw failure(undefinedInsideArrayErrorMsg)
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
// TODO: should be change.object, but mobx is bugged and doesn't send the proxy
|
|
392
|
-
const oldVal = array[change.index]
|
|
393
|
-
tweak(oldVal, undefined) // set old prop obj parent to undefined
|
|
394
|
-
|
|
395
|
-
change.newValue = tweak(change.newValue, { parent: array, path: change.index })
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
function interceptArrayMutationSplice(change: IArrayWillSplice) {
|
|
399
|
-
if (inDevMode && !getGlobalConfig().allowUndefinedArrayElements) {
|
|
400
|
-
const len = change.added.length
|
|
401
|
-
for (let i = 0; i < len; i++) {
|
|
402
|
-
const v = change.added[i]
|
|
403
|
-
if (v === undefined) {
|
|
404
|
-
throw failure(undefinedInsideArrayErrorMsg)
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
for (let i = 0; i < change.removedCount; i++) {
|
|
410
|
-
const removedValue = change.object[change.index + i]
|
|
411
|
-
tweak(removedValue, undefined)
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
for (let i = 0; i < change.added.length; i++) {
|
|
415
|
-
change.added[i] = tweak(change.added[i], {
|
|
416
|
-
parent: change.object,
|
|
417
|
-
path: change.index + i,
|
|
418
|
-
})
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
// we might also need to update the parent of the next indexes
|
|
422
|
-
const oldNextIndex = change.index + change.removedCount
|
|
423
|
-
const newNextIndex = change.index + change.added.length
|
|
424
|
-
|
|
425
|
-
if (oldNextIndex !== newNextIndex) {
|
|
426
|
-
for (let i = oldNextIndex, j = newNextIndex; i < change.object.length; i++, j++) {
|
|
427
|
-
setParent(
|
|
428
|
-
change.object[i], // value
|
|
429
|
-
{
|
|
430
|
-
parent: change.object,
|
|
431
|
-
path: j,
|
|
432
|
-
}, // parentPath
|
|
433
|
-
true, // indexChangeAllowed
|
|
434
|
-
false, // isDataObject
|
|
435
|
-
// just re-indexing
|
|
436
|
-
false // cloneIfApplicable
|
|
437
|
-
)
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* @internal
|
|
444
|
-
*/
|
|
445
|
-
export function registerArrayTweaker() {
|
|
446
|
-
registerTweaker(TweakerPriority.Array, (value, parentPath) => {
|
|
447
|
-
if (isArray(value)) {
|
|
448
|
-
return tweakArray(value, parentPath, false)
|
|
449
|
-
}
|
|
450
|
-
return undefined
|
|
451
|
-
})
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
const observableOptions = {
|
|
455
|
-
deep: false,
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
function getValueAfterSplice<T>(
|
|
459
|
-
array: readonly T[],
|
|
460
|
-
i: number,
|
|
461
|
-
index: number,
|
|
462
|
-
remove: number,
|
|
463
|
-
addedItems: readonly T[]
|
|
464
|
-
) {
|
|
465
|
-
const base = i - index
|
|
466
|
-
if (base < 0) {
|
|
467
|
-
return array[i]
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
if (base < addedItems.length) {
|
|
471
|
-
return addedItems[base]
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
return array[i - addedItems.length + remove]
|
|
475
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
type IArrayDidChange,
|
|
3
|
+
type IArrayWillChange,
|
|
4
|
+
type IArrayWillSplice,
|
|
5
|
+
type IObservableArray,
|
|
6
|
+
intercept,
|
|
7
|
+
isObservableArray,
|
|
8
|
+
observable,
|
|
9
|
+
observe,
|
|
10
|
+
} from "mobx"
|
|
11
|
+
import { assertCanWrite } from "../action/protection"
|
|
12
|
+
import { DeepChangeType, emitDeepChange, getIsInInitPhase } from "../deepChange/onDeepChange"
|
|
13
|
+
import { getGlobalConfig } from "../globalConfig"
|
|
14
|
+
import type { ParentPath } from "../parent/path"
|
|
15
|
+
import type { Path } from "../parent/pathTypes"
|
|
16
|
+
import { setParent } from "../parent/setParent"
|
|
17
|
+
import { InternalPatchRecorder } from "../patch/emitPatch"
|
|
18
|
+
import type { Patch } from "../patch/Patch"
|
|
19
|
+
import {
|
|
20
|
+
freezeInternalSnapshot,
|
|
21
|
+
getInternalSnapshot,
|
|
22
|
+
setNewInternalSnapshot,
|
|
23
|
+
updateInternalSnapshot,
|
|
24
|
+
} from "../snapshot/internal"
|
|
25
|
+
import { failure, inDevMode, isArray, isPrimitive } from "../utils"
|
|
26
|
+
import { setIfDifferent } from "../utils/setIfDifferent"
|
|
27
|
+
import { runningWithoutSnapshotOrPatches, tweakedObjects } from "./core"
|
|
28
|
+
import { TweakerPriority } from "./TweakerPriority"
|
|
29
|
+
import { registerTweaker, tweak } from "./tweak"
|
|
30
|
+
import { runTypeCheckingAfterChange } from "./typeChecking"
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
export function tweakArray<T extends any[]>(
|
|
36
|
+
value: T,
|
|
37
|
+
parentPath: ParentPath<any> | undefined,
|
|
38
|
+
doNotTweakChildren: boolean
|
|
39
|
+
): T {
|
|
40
|
+
const originalArr: ReadonlyArray<any> = value
|
|
41
|
+
const arrLn = originalArr.length
|
|
42
|
+
const tweakedArr = isObservableArray(originalArr)
|
|
43
|
+
? originalArr
|
|
44
|
+
: observable.array(undefined, observableOptions)
|
|
45
|
+
if (tweakedArr !== originalArr) {
|
|
46
|
+
tweakedArr.length = originalArr.length
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let interceptDisposer: () => void
|
|
50
|
+
let observeDisposer: () => void
|
|
51
|
+
|
|
52
|
+
const untweak = () => {
|
|
53
|
+
interceptDisposer()
|
|
54
|
+
observeDisposer()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
tweakedObjects.set(tweakedArr, untweak)
|
|
58
|
+
setParent(
|
|
59
|
+
tweakedArr, // value
|
|
60
|
+
parentPath,
|
|
61
|
+
false, // indexChangeAllowed
|
|
62
|
+
false, // isDataObject
|
|
63
|
+
// arrays shouldn't be cloned anyway
|
|
64
|
+
false // cloneIfApplicable
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
const untransformedSn: any[] = []
|
|
68
|
+
untransformedSn.length = arrLn
|
|
69
|
+
|
|
70
|
+
// substitute initial values by proxied values
|
|
71
|
+
for (let i = 0; i < arrLn; i++) {
|
|
72
|
+
const v = originalArr[i]
|
|
73
|
+
|
|
74
|
+
if (isPrimitive(v)) {
|
|
75
|
+
if (!doNotTweakChildren) {
|
|
76
|
+
setIfDifferent(tweakedArr, i, v)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
untransformedSn[i] = v
|
|
80
|
+
} else {
|
|
81
|
+
const path = { parent: tweakedArr, path: i }
|
|
82
|
+
|
|
83
|
+
let tweakedValue: any
|
|
84
|
+
if (doNotTweakChildren) {
|
|
85
|
+
tweakedValue = v
|
|
86
|
+
setParent(
|
|
87
|
+
tweakedValue, // value
|
|
88
|
+
path, // parentPath
|
|
89
|
+
false, // indexChangeAllowed
|
|
90
|
+
false, // isDataObject
|
|
91
|
+
// the value is already a new value (the result of a fromSnapshot)
|
|
92
|
+
false // cloneIfApplicable
|
|
93
|
+
)
|
|
94
|
+
} else {
|
|
95
|
+
tweakedValue = tweak(v, path)
|
|
96
|
+
setIfDifferent(tweakedArr, i, tweakedValue)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const valueSn = getInternalSnapshot(tweakedValue)!
|
|
100
|
+
untransformedSn[i] = valueSn.transformed
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
setNewInternalSnapshot(tweakedArr, untransformedSn, undefined)
|
|
105
|
+
|
|
106
|
+
interceptDisposer = intercept(tweakedArr, interceptArrayMutation.bind(undefined, tweakedArr))
|
|
107
|
+
observeDisposer = observe(tweakedArr, arrayDidChange)
|
|
108
|
+
|
|
109
|
+
return tweakedArr as any
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function mutateSet(k: number, v: unknown, sn: unknown[]) {
|
|
113
|
+
sn[k] = v
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function mutateSplice(index: number, removedCount: number, addedItems: any[], sn: any[]) {
|
|
117
|
+
sn.splice(index, removedCount, ...addedItems)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const patchRecorder = new InternalPatchRecorder()
|
|
121
|
+
|
|
122
|
+
const emptyPath: Path = Object.freeze([])
|
|
123
|
+
|
|
124
|
+
function arrayDidChange(change: IArrayDidChange) {
|
|
125
|
+
const arr = change.object
|
|
126
|
+
const oldSnapshot = getInternalSnapshot(arr as Array<unknown>)!.untransformed
|
|
127
|
+
|
|
128
|
+
patchRecorder.reset()
|
|
129
|
+
|
|
130
|
+
let mutate: ((sn: any[]) => void) | undefined
|
|
131
|
+
|
|
132
|
+
switch (change.type) {
|
|
133
|
+
case "splice":
|
|
134
|
+
mutate = arrayDidChangeSplice(change, oldSnapshot)
|
|
135
|
+
// Emit deep change for splice
|
|
136
|
+
emitDeepChange(arr, {
|
|
137
|
+
type: DeepChangeType.ArraySplice,
|
|
138
|
+
target: arr,
|
|
139
|
+
path: emptyPath,
|
|
140
|
+
index: change.index as number,
|
|
141
|
+
addedValues: change.added,
|
|
142
|
+
removedValues: change.removed,
|
|
143
|
+
isInit: getIsInInitPhase(),
|
|
144
|
+
})
|
|
145
|
+
break
|
|
146
|
+
|
|
147
|
+
case "update":
|
|
148
|
+
mutate = arrayDidChangeUpdate(change, oldSnapshot)
|
|
149
|
+
// Emit deep change for update
|
|
150
|
+
emitDeepChange(arr, {
|
|
151
|
+
type: DeepChangeType.ArrayUpdate,
|
|
152
|
+
target: arr,
|
|
153
|
+
path: emptyPath,
|
|
154
|
+
index: change.index as number,
|
|
155
|
+
newValue: change.newValue,
|
|
156
|
+
oldValue: change.oldValue,
|
|
157
|
+
isInit: getIsInInitPhase(),
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
break
|
|
161
|
+
|
|
162
|
+
default:
|
|
163
|
+
break
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
runTypeCheckingAfterChange(arr, patchRecorder)
|
|
167
|
+
|
|
168
|
+
if (!runningWithoutSnapshotOrPatches && mutate) {
|
|
169
|
+
updateInternalSnapshot(arr, mutate)
|
|
170
|
+
patchRecorder.emit(arr)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const undefinedInsideArrayErrorMsg =
|
|
175
|
+
"undefined is not supported inside arrays since it is not serializable in JSON, consider using null instead"
|
|
176
|
+
|
|
177
|
+
function arrayDidChangeUpdate(change: any /*IArrayDidChange*/, oldSnapshot: any) {
|
|
178
|
+
const k = change.index
|
|
179
|
+
const val = change.newValue
|
|
180
|
+
const oldVal = oldSnapshot[k]
|
|
181
|
+
let newVal: any
|
|
182
|
+
if (isPrimitive(val)) {
|
|
183
|
+
newVal = val
|
|
184
|
+
} else {
|
|
185
|
+
const valueSn = getInternalSnapshot(val)!
|
|
186
|
+
newVal = valueSn.transformed
|
|
187
|
+
}
|
|
188
|
+
const mutate = mutateSet.bind(undefined, k, newVal)
|
|
189
|
+
|
|
190
|
+
const path = [k]
|
|
191
|
+
|
|
192
|
+
patchRecorder.record(
|
|
193
|
+
[
|
|
194
|
+
{
|
|
195
|
+
op: "replace",
|
|
196
|
+
path,
|
|
197
|
+
value: freezeInternalSnapshot(newVal),
|
|
198
|
+
},
|
|
199
|
+
],
|
|
200
|
+
[
|
|
201
|
+
{
|
|
202
|
+
op: "replace",
|
|
203
|
+
path,
|
|
204
|
+
value: freezeInternalSnapshot(oldVal),
|
|
205
|
+
},
|
|
206
|
+
]
|
|
207
|
+
)
|
|
208
|
+
return mutate
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function arrayDidChangeSplice(change: any /*IArrayDidChange*/, oldSnapshot: any) {
|
|
212
|
+
const index = change.index as number
|
|
213
|
+
const addedCount = change.addedCount as number
|
|
214
|
+
const removedCount = change.removedCount as number
|
|
215
|
+
|
|
216
|
+
const addedItems: any[] = []
|
|
217
|
+
addedItems.length = addedCount
|
|
218
|
+
for (let i = 0; i < addedCount; i++) {
|
|
219
|
+
const v = change.added[i]
|
|
220
|
+
if (isPrimitive(v)) {
|
|
221
|
+
addedItems[i] = v
|
|
222
|
+
} else {
|
|
223
|
+
addedItems[i] = getInternalSnapshot(v)!.transformed
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const oldLen = oldSnapshot.length
|
|
228
|
+
const mutate = mutateSplice.bind(undefined, index, removedCount, addedItems)
|
|
229
|
+
|
|
230
|
+
const patches: Patch[] = []
|
|
231
|
+
const invPatches: Patch[] = []
|
|
232
|
+
|
|
233
|
+
// optimization: if we add as many as we remove then remove/readd instead
|
|
234
|
+
|
|
235
|
+
// we cannot replace since we might end up in a situation where the same node
|
|
236
|
+
// might attempt to be temporarily twice inside the same tree (e.g. sorting)
|
|
237
|
+
|
|
238
|
+
// it would be faster to keep holes rather than remove/readd, but if we do that then
|
|
239
|
+
// validation might fail
|
|
240
|
+
|
|
241
|
+
if (addedCount === removedCount) {
|
|
242
|
+
const readdPatches: Patch[] = []
|
|
243
|
+
const readdInvPatches: Patch[] = []
|
|
244
|
+
let removed = 0
|
|
245
|
+
|
|
246
|
+
for (let i = 0; i < addedCount; i++) {
|
|
247
|
+
const realIndex = index + i
|
|
248
|
+
|
|
249
|
+
const newVal = getValueAfterSplice(oldSnapshot, realIndex, index, removedCount, addedItems)
|
|
250
|
+
const oldVal = oldSnapshot[realIndex]
|
|
251
|
+
|
|
252
|
+
if (newVal !== oldVal) {
|
|
253
|
+
const removePath = [realIndex - removed]
|
|
254
|
+
patches.push({
|
|
255
|
+
op: "remove",
|
|
256
|
+
path: removePath,
|
|
257
|
+
})
|
|
258
|
+
invPatches.push({
|
|
259
|
+
op: "remove",
|
|
260
|
+
path: removePath,
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
removed++
|
|
264
|
+
|
|
265
|
+
const readdPath = [realIndex]
|
|
266
|
+
readdPatches.push({
|
|
267
|
+
op: "add",
|
|
268
|
+
path: readdPath,
|
|
269
|
+
value: freezeInternalSnapshot(newVal),
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
readdInvPatches.push({
|
|
273
|
+
op: "add",
|
|
274
|
+
path: readdPath,
|
|
275
|
+
value: freezeInternalSnapshot(oldVal),
|
|
276
|
+
})
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
patches.push(...readdPatches)
|
|
281
|
+
invPatches.push(...readdInvPatches)
|
|
282
|
+
// we need to reverse since inverse patches are applied in reverse
|
|
283
|
+
invPatches.reverse()
|
|
284
|
+
} else {
|
|
285
|
+
const interimLen = oldLen - removedCount
|
|
286
|
+
|
|
287
|
+
// first remove items
|
|
288
|
+
if (removedCount > 0) {
|
|
289
|
+
// optimization, when removing from the end set the length instead
|
|
290
|
+
const removeUsingSetLength = index >= interimLen
|
|
291
|
+
if (removeUsingSetLength) {
|
|
292
|
+
patches.push({
|
|
293
|
+
op: "replace",
|
|
294
|
+
path: ["length"],
|
|
295
|
+
value: interimLen,
|
|
296
|
+
})
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
for (let i = removedCount - 1; i >= 0; i--) {
|
|
300
|
+
const realIndex = index + i
|
|
301
|
+
const path = [realIndex]
|
|
302
|
+
|
|
303
|
+
if (!removeUsingSetLength) {
|
|
304
|
+
// remove ...2, 1, 0
|
|
305
|
+
patches.push({
|
|
306
|
+
op: "remove",
|
|
307
|
+
path,
|
|
308
|
+
})
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// add 0, 1, 2... since inverse patches are applied in reverse
|
|
312
|
+
invPatches.push({
|
|
313
|
+
op: "add",
|
|
314
|
+
path,
|
|
315
|
+
value: freezeInternalSnapshot(oldSnapshot[realIndex]),
|
|
316
|
+
})
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// then add items
|
|
321
|
+
if (addedCount > 0) {
|
|
322
|
+
// optimization, for inverse patches, when adding from the end set the length to restore instead
|
|
323
|
+
const restoreUsingSetLength = index >= interimLen
|
|
324
|
+
if (restoreUsingSetLength) {
|
|
325
|
+
invPatches.push({
|
|
326
|
+
op: "replace",
|
|
327
|
+
path: ["length"],
|
|
328
|
+
value: interimLen,
|
|
329
|
+
})
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
for (let i = 0; i < addedCount; i++) {
|
|
333
|
+
const realIndex = index + i
|
|
334
|
+
const path = [realIndex]
|
|
335
|
+
|
|
336
|
+
// add 0, 1, 2...
|
|
337
|
+
patches.push({
|
|
338
|
+
op: "add",
|
|
339
|
+
path,
|
|
340
|
+
value: freezeInternalSnapshot(
|
|
341
|
+
getValueAfterSplice(oldSnapshot, realIndex, index, removedCount, addedItems)
|
|
342
|
+
),
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
// remove ...2, 1, 0 since inverse patches are applied in reverse
|
|
346
|
+
if (!restoreUsingSetLength) {
|
|
347
|
+
invPatches.push({
|
|
348
|
+
op: "remove",
|
|
349
|
+
path,
|
|
350
|
+
})
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
patchRecorder.record(patches, invPatches)
|
|
357
|
+
return mutate
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// TODO: remove array parameter and just use change.object once mobx update event is fixed
|
|
361
|
+
function interceptArrayMutation(
|
|
362
|
+
array: IObservableArray,
|
|
363
|
+
change: IArrayWillChange | IArrayWillSplice
|
|
364
|
+
) {
|
|
365
|
+
assertCanWrite()
|
|
366
|
+
|
|
367
|
+
switch (change.type) {
|
|
368
|
+
case "splice":
|
|
369
|
+
interceptArrayMutationSplice(change)
|
|
370
|
+
break
|
|
371
|
+
|
|
372
|
+
case "update":
|
|
373
|
+
interceptArrayMutationUpdate(change, array)
|
|
374
|
+
break
|
|
375
|
+
|
|
376
|
+
default:
|
|
377
|
+
break
|
|
378
|
+
}
|
|
379
|
+
return change
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function interceptArrayMutationUpdate(change: IArrayWillChange, array: IObservableArray) {
|
|
383
|
+
if (
|
|
384
|
+
inDevMode &&
|
|
385
|
+
!getGlobalConfig().allowUndefinedArrayElements &&
|
|
386
|
+
change.newValue === undefined
|
|
387
|
+
) {
|
|
388
|
+
throw failure(undefinedInsideArrayErrorMsg)
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// TODO: should be change.object, but mobx is bugged and doesn't send the proxy
|
|
392
|
+
const oldVal = array[change.index]
|
|
393
|
+
tweak(oldVal, undefined) // set old prop obj parent to undefined
|
|
394
|
+
|
|
395
|
+
change.newValue = tweak(change.newValue, { parent: array, path: change.index })
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function interceptArrayMutationSplice(change: IArrayWillSplice) {
|
|
399
|
+
if (inDevMode && !getGlobalConfig().allowUndefinedArrayElements) {
|
|
400
|
+
const len = change.added.length
|
|
401
|
+
for (let i = 0; i < len; i++) {
|
|
402
|
+
const v = change.added[i]
|
|
403
|
+
if (v === undefined) {
|
|
404
|
+
throw failure(undefinedInsideArrayErrorMsg)
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
for (let i = 0; i < change.removedCount; i++) {
|
|
410
|
+
const removedValue = change.object[change.index + i]
|
|
411
|
+
tweak(removedValue, undefined)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
for (let i = 0; i < change.added.length; i++) {
|
|
415
|
+
change.added[i] = tweak(change.added[i], {
|
|
416
|
+
parent: change.object,
|
|
417
|
+
path: change.index + i,
|
|
418
|
+
})
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// we might also need to update the parent of the next indexes
|
|
422
|
+
const oldNextIndex = change.index + change.removedCount
|
|
423
|
+
const newNextIndex = change.index + change.added.length
|
|
424
|
+
|
|
425
|
+
if (oldNextIndex !== newNextIndex) {
|
|
426
|
+
for (let i = oldNextIndex, j = newNextIndex; i < change.object.length; i++, j++) {
|
|
427
|
+
setParent(
|
|
428
|
+
change.object[i], // value
|
|
429
|
+
{
|
|
430
|
+
parent: change.object,
|
|
431
|
+
path: j,
|
|
432
|
+
}, // parentPath
|
|
433
|
+
true, // indexChangeAllowed
|
|
434
|
+
false, // isDataObject
|
|
435
|
+
// just re-indexing
|
|
436
|
+
false // cloneIfApplicable
|
|
437
|
+
)
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* @internal
|
|
444
|
+
*/
|
|
445
|
+
export function registerArrayTweaker() {
|
|
446
|
+
registerTweaker(TweakerPriority.Array, (value, parentPath) => {
|
|
447
|
+
if (isArray(value)) {
|
|
448
|
+
return tweakArray(value, parentPath, false)
|
|
449
|
+
}
|
|
450
|
+
return undefined
|
|
451
|
+
})
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const observableOptions = {
|
|
455
|
+
deep: false,
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function getValueAfterSplice<T>(
|
|
459
|
+
array: readonly T[],
|
|
460
|
+
i: number,
|
|
461
|
+
index: number,
|
|
462
|
+
remove: number,
|
|
463
|
+
addedItems: readonly T[]
|
|
464
|
+
) {
|
|
465
|
+
const base = i - index
|
|
466
|
+
if (base < 0) {
|
|
467
|
+
return array[i]
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
if (base < addedItems.length) {
|
|
471
|
+
return addedItems[base]
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return array[i - addedItems.length + remove]
|
|
475
|
+
}
|