mobx-keystone 1.24.1 → 1.25.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 +14 -0
- package/README.md +5 -1
- package/dist/mobx-keystone.esm.js +141 -131
- package/dist/mobx-keystone.esm.mjs +141 -131
- package/dist/mobx-keystone.umd.js +141 -131
- package/package.json +7 -7
- package/src/actionMiddlewares/undoMiddleware.ts +11 -14
- package/src/model/BaseModel.ts +11 -5
- package/src/model/newModel.ts +11 -4
- package/src/parent/findParent.ts +14 -9
- package/src/parent/onChildAttachedTo.ts +1 -12
- package/src/parent/path.ts +6 -4
- package/src/parent/setParent.ts +26 -14
- package/src/patch/applyPatches.ts +10 -30
- package/src/snapshot/applySnapshot.ts +9 -0
- package/src/snapshot/fromSnapshot.ts +3 -1
- package/src/snapshot/modelInitialData.ts +14 -19
- package/src/snapshot/reconcileModelSnapshot.ts +11 -3
- package/src/snapshot/reconcileSnapshot.ts +13 -4
- package/src/tweaker/tweak.ts +6 -7
- package/src/tweaker/tweakArray.ts +3 -17
- package/src/tweaker/tweakFrozen.ts +0 -1
- package/src/tweaker/tweakModel.ts +0 -1
- package/src/tweaker/tweakPlainObject.ts +2 -6
- package/src/tweaker/typeChecking.ts +9 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mobx-keystone",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.25.0",
|
|
4
4
|
"description": "A MobX powered state management solution based on data trees with first class support for TypeScript, snapshots, patches and much more",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mobx",
|
|
@@ -73,11 +73,11 @@
|
|
|
73
73
|
"@babel/plugin-transform-class-static-block": "^8.0.1",
|
|
74
74
|
"@babel/plugin-transform-typescript": "^8.0.1",
|
|
75
75
|
"@babel/preset-env": "^8.0.2",
|
|
76
|
-
"@swc/core": "^1.16.
|
|
76
|
+
"@swc/core": "^1.16.2",
|
|
77
77
|
"@types/babel__core": "^7.20.5",
|
|
78
|
-
"@types/node": "^26.
|
|
78
|
+
"@types/node": "^26.4.1",
|
|
79
79
|
"@typescript/typescript6": "^6.0.2",
|
|
80
|
-
"@vitest/coverage-v8": "^
|
|
80
|
+
"@vitest/coverage-v8": "^5.0.0",
|
|
81
81
|
"mobx-v4": "npm:mobx@^4.15.7",
|
|
82
82
|
"mobx-v5": "npm:mobx@^5.15.7",
|
|
83
83
|
"mobx-v6": "npm:mobx@^6.0.0",
|
|
@@ -87,9 +87,9 @@
|
|
|
87
87
|
"ts-node": "^10.9.2",
|
|
88
88
|
"typedoc": "^0.28.20",
|
|
89
89
|
"typescript": "^7.0.2",
|
|
90
|
-
"vite": "^8.2.
|
|
91
|
-
"vite-plugin-dts": "^5.0
|
|
92
|
-
"vitest": "^
|
|
90
|
+
"vite": "^8.2.2",
|
|
91
|
+
"vite-plugin-dts": "^5.1.0",
|
|
92
|
+
"vitest": "^5.0.0"
|
|
93
93
|
},
|
|
94
94
|
"dependencies": {
|
|
95
95
|
"fast-deep-equal": "^3.1.3",
|
|
@@ -99,21 +99,16 @@ export interface UndoEventGroup {
|
|
|
99
99
|
|
|
100
100
|
function toSingleEvents(
|
|
101
101
|
event: UndoEventWithoutAttachedState,
|
|
102
|
-
|
|
103
|
-
):
|
|
102
|
+
array: UndoSingleEvent[] = []
|
|
103
|
+
): UndoSingleEvent[] {
|
|
104
104
|
if (event.type === UndoEventType.Single) {
|
|
105
|
-
|
|
105
|
+
array.push(event)
|
|
106
106
|
} else {
|
|
107
|
-
const array: UndoSingleEvent[] = []
|
|
108
107
|
for (const e of event.events) {
|
|
109
|
-
|
|
110
|
-
array.unshift(...toSingleEvents(e, true))
|
|
111
|
-
} else {
|
|
112
|
-
array.push(...toSingleEvents(e, false))
|
|
113
|
-
}
|
|
108
|
+
toSingleEvents(e, array)
|
|
114
109
|
}
|
|
115
|
-
return array
|
|
116
110
|
}
|
|
111
|
+
return array
|
|
117
112
|
}
|
|
118
113
|
|
|
119
114
|
/**
|
|
@@ -386,9 +381,11 @@ export class UndoManager {
|
|
|
386
381
|
const event = this.undoQueue[this.undoQueue.length - 1]
|
|
387
382
|
|
|
388
383
|
withoutUndo(() => {
|
|
389
|
-
toSingleEvents(event
|
|
390
|
-
|
|
391
|
-
|
|
384
|
+
toSingleEvents(event)
|
|
385
|
+
.reverse()
|
|
386
|
+
.forEach((e) => {
|
|
387
|
+
applyPatches(this.subtreeRoot, e.inversePatches, true)
|
|
388
|
+
})
|
|
392
389
|
|
|
393
390
|
// restore the attached state before the operation was made
|
|
394
391
|
if (event.attachedState?.beforeEvent) {
|
|
@@ -411,7 +408,7 @@ export class UndoManager {
|
|
|
411
408
|
const event = this.redoQueue[this.redoQueue.length - 1]
|
|
412
409
|
|
|
413
410
|
withoutUndo(() => {
|
|
414
|
-
toSingleEvents(event
|
|
411
|
+
toSingleEvents(event).forEach((e) => {
|
|
415
412
|
applyPatches(this.subtreeRoot, e.patches)
|
|
416
413
|
})
|
|
417
414
|
|
package/src/model/BaseModel.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
ModelPropsToUntransformedData,
|
|
13
13
|
} from "../modelShared/prop"
|
|
14
14
|
import { getSnapshot } from "../snapshot/getSnapshot"
|
|
15
|
+
import { setModelInitialDataSnapshot } from "../snapshot/modelInitialData"
|
|
15
16
|
import type { SnapshotInOfModel, SnapshotOutOfModel } from "../snapshot/SnapshotOf"
|
|
16
17
|
import { typesModel } from "../types/objectBased/typesModel"
|
|
17
18
|
import type { TypeCheckError } from "../types/TypeCheckError"
|
|
@@ -146,11 +147,16 @@ export abstract class BaseModel<
|
|
|
146
147
|
// plain new
|
|
147
148
|
assertIsObject(initialData, "initialData")
|
|
148
149
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
150
|
+
// Empty input is filled by the constructor below. Track its primitive
|
|
151
|
+
// defaults directly instead of rereading observable fields for the snapshot.
|
|
152
|
+
const useInitialDataSnapshot = Reflect.ownKeys(initialData).length === 0
|
|
153
|
+
const observableInitialData = observable.object(initialData as any, undefined, {
|
|
154
|
+
deep: false,
|
|
155
|
+
})
|
|
156
|
+
if (useInitialDataSnapshot) {
|
|
157
|
+
setModelInitialDataSnapshot(observableInitialData, {})
|
|
158
|
+
}
|
|
159
|
+
internalNewModel(this, observableInitialData, modelClass!, useInitialDataSnapshot)
|
|
154
160
|
}
|
|
155
161
|
}
|
|
156
162
|
|
package/src/model/newModel.ts
CHANGED
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
hasGlobalPatchListeners,
|
|
20
20
|
} from "../patch/emitPatch"
|
|
21
21
|
import type { Patch } from "../patch/Patch"
|
|
22
|
-
import {
|
|
22
|
+
import { updateModelInitialDataSnapshot } from "../snapshot/modelInitialData"
|
|
23
23
|
import { tweakModel } from "../tweaker/tweakModel"
|
|
24
24
|
import { tweakPlainObject } from "../tweaker/tweakPlainObject"
|
|
25
25
|
import { failure, inDevMode, makePropReadonly } from "../utils"
|
|
@@ -38,7 +38,8 @@ export const internalNewModel = action(
|
|
|
38
38
|
<M extends AnyModel>(
|
|
39
39
|
origModelObj: M,
|
|
40
40
|
initialData: ModelCreationData<M>,
|
|
41
|
-
modelClass: ModelClass<AnyModel
|
|
41
|
+
modelClass: ModelClass<AnyModel>,
|
|
42
|
+
useInitialDataSnapshot: boolean
|
|
42
43
|
): void => {
|
|
43
44
|
if (inDevMode) {
|
|
44
45
|
assertIsModelClass(modelClass, "modelClass")
|
|
@@ -56,6 +57,9 @@ export const internalNewModel = action(
|
|
|
56
57
|
id = (modelIdPropData._defaultFn as () => string)()
|
|
57
58
|
}
|
|
58
59
|
setIfDifferent(initialData, modelIdPropertyName, id)
|
|
60
|
+
if (useInitialDataSnapshot) {
|
|
61
|
+
updateModelInitialDataSnapshot(initialData, modelIdPropertyName, id)
|
|
62
|
+
}
|
|
59
63
|
}
|
|
60
64
|
|
|
61
65
|
const modelObj = origModelObj as O.Writable<M>
|
|
@@ -99,6 +103,9 @@ export const internalNewModel = action(
|
|
|
99
103
|
if (changed) {
|
|
100
104
|
// setIfDifferent not required
|
|
101
105
|
set(initialData, k, newValue)
|
|
106
|
+
if (useInitialDataSnapshot) {
|
|
107
|
+
updateModelInitialDataSnapshot(initialData, k, newValue)
|
|
108
|
+
}
|
|
102
109
|
}
|
|
103
110
|
}
|
|
104
111
|
|
|
@@ -166,7 +173,7 @@ export const internalFromSnapshotModel = action(
|
|
|
166
173
|
const valueChanged = setIfDifferentWithReturn(initialData, modelIdPropertyName, id)
|
|
167
174
|
|
|
168
175
|
if (valueChanged) {
|
|
169
|
-
|
|
176
|
+
updateModelInitialDataSnapshot(initialData, modelIdPropertyName, id)
|
|
170
177
|
}
|
|
171
178
|
|
|
172
179
|
if (valueChanged && shouldEmitPatches) {
|
|
@@ -210,7 +217,7 @@ export const internalFromSnapshotModel = action(
|
|
|
210
217
|
if (changed) {
|
|
211
218
|
// setIfDifferent not required
|
|
212
219
|
set(initialData, k, newValue)
|
|
213
|
-
|
|
220
|
+
updateModelInitialDataSnapshot(initialData, k, newValue)
|
|
214
221
|
|
|
215
222
|
if (newValue !== initialValue) {
|
|
216
223
|
if (shouldEmitPatches) {
|
package/src/parent/findParent.ts
CHANGED
|
@@ -19,8 +19,7 @@ export function findParent<T extends object = any>(
|
|
|
19
19
|
predicate: (parentNode: object) => boolean,
|
|
20
20
|
maxDepth = 0
|
|
21
21
|
): T | undefined {
|
|
22
|
-
|
|
23
|
-
return foundParentPath ? foundParentPath.parent : undefined
|
|
22
|
+
return findParentInternal<T>(child, predicate, maxDepth)
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
/**
|
|
@@ -56,22 +55,28 @@ export function findParentPath<T extends object = any>(
|
|
|
56
55
|
predicate: (parentNode: object) => boolean,
|
|
57
56
|
maxDepth = 0
|
|
58
57
|
): FoundParentPath<T> | undefined {
|
|
59
|
-
assertTweakedObject(child, "child")
|
|
60
|
-
|
|
61
58
|
const path: WritablePath = []
|
|
59
|
+
const parent = findParentInternal<T>(child, predicate, maxDepth, path)
|
|
60
|
+
return parent ? { parent, path: path.reverse() } : undefined
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function findParentInternal<T extends object>(
|
|
64
|
+
child: object,
|
|
65
|
+
predicate: (parentNode: object) => boolean,
|
|
66
|
+
maxDepth: number,
|
|
67
|
+
path?: WritablePath
|
|
68
|
+
): T | undefined {
|
|
69
|
+
assertTweakedObject(child, "child")
|
|
62
70
|
|
|
63
71
|
let current: any = child
|
|
64
72
|
let depth = 0
|
|
65
73
|
|
|
66
74
|
let parentPath: ParentPath<any> | undefined
|
|
67
75
|
while ((parentPath = fastGetParentPath(current, true))) {
|
|
68
|
-
path
|
|
76
|
+
path?.push(parentPath.path)
|
|
69
77
|
current = parentPath.parent
|
|
70
78
|
if (predicate(current)) {
|
|
71
|
-
return
|
|
72
|
-
parent: current,
|
|
73
|
-
path,
|
|
74
|
-
}
|
|
79
|
+
return current
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
depth++
|
|
@@ -59,18 +59,7 @@ export function onChildAttachedTo(
|
|
|
59
59
|
const t = target()
|
|
60
60
|
assertTweakedObject(t, "target()")
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const set = new Set<object>()
|
|
65
|
-
|
|
66
|
-
const iter = children.values()
|
|
67
|
-
let cur = iter.next()
|
|
68
|
-
while (!cur.done) {
|
|
69
|
-
set.add(cur.value)
|
|
70
|
-
cur = iter.next()
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return set
|
|
62
|
+
return new Set(getChildrenObjects(t, getChildrenObjectOpts))
|
|
74
63
|
}
|
|
75
64
|
|
|
76
65
|
const currentChildren = opts.fireForCurrentChildren ? new Set<object>() : getCurrentChildren()
|
package/src/parent/path.ts
CHANGED
|
@@ -175,10 +175,12 @@ export function fastGetRootPath<T extends object = any>(
|
|
|
175
175
|
let parentPath: ParentPath<any> | undefined
|
|
176
176
|
while ((parentPath = fastGetParentPath(root, useAtom))) {
|
|
177
177
|
root = parentPath.parent
|
|
178
|
-
path.
|
|
179
|
-
pathObjects.
|
|
178
|
+
path.push(parentPath.path)
|
|
179
|
+
pathObjects.push(parentPath.parent)
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
+
path.reverse()
|
|
183
|
+
pathObjects.reverse()
|
|
182
184
|
return { root, path, pathObjects } as RootPath<any>
|
|
183
185
|
}
|
|
184
186
|
|
|
@@ -376,11 +378,11 @@ export function fastGetParentToChildPath(
|
|
|
376
378
|
let current = toChild
|
|
377
379
|
let parentPath: ParentPath<any> | undefined
|
|
378
380
|
while ((parentPath = fastGetParentPath(current, useAtom))) {
|
|
379
|
-
path.
|
|
381
|
+
path.push(parentPath.path)
|
|
380
382
|
|
|
381
383
|
current = parentPath.parent
|
|
382
384
|
if (current === fromParent) {
|
|
383
|
-
return path
|
|
385
|
+
return path.reverse()
|
|
384
386
|
}
|
|
385
387
|
}
|
|
386
388
|
return undefined
|
package/src/parent/setParent.ts
CHANGED
|
@@ -19,6 +19,31 @@ import {
|
|
|
19
19
|
import { addObjectChild, removeObjectChild } from "./coreObjectChildren"
|
|
20
20
|
import { fastGetParentPath, fastGetRoot, type ParentPath } from "./path"
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Updates the unchanged tail of an array before a splice is applied. These
|
|
24
|
+
* children keep their parent, so neither attachment nor child-index work is
|
|
25
|
+
* needed. One action batches path notifications for the entire tail.
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
export const reindexArrayChildren = action(
|
|
29
|
+
"reindexArrayChildren",
|
|
30
|
+
(array: readonly unknown[], oldStart: number, newStart: number): void => {
|
|
31
|
+
for (let i = oldStart, j = newStart; i < array.length; i++, j++) {
|
|
32
|
+
const value = array[i]
|
|
33
|
+
if (isPrimitive(value)) {
|
|
34
|
+
continue
|
|
35
|
+
}
|
|
36
|
+
const metadata = treeNodeMetadata.get(value as object)
|
|
37
|
+
if (inDevMode && metadata?.parentPath?.parent !== array) {
|
|
38
|
+
throw failure("assertion failed: reindexed child must already belong to the array")
|
|
39
|
+
}
|
|
40
|
+
// Replace the path object: callers may have retained the previous path.
|
|
41
|
+
metadata!.parentPath = { parent: array, path: j }
|
|
42
|
+
reportParentPathChanged(value as object)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
)
|
|
46
|
+
|
|
22
47
|
/**
|
|
23
48
|
* @internal
|
|
24
49
|
*/
|
|
@@ -27,7 +52,6 @@ export const setParent = action(
|
|
|
27
52
|
(
|
|
28
53
|
value: any,
|
|
29
54
|
parentPath: ParentPath<any> | undefined,
|
|
30
|
-
indexChangeAllowed: boolean,
|
|
31
55
|
isDataObject: boolean,
|
|
32
56
|
cloneIfApplicable: boolean
|
|
33
57
|
): any => {
|
|
@@ -36,11 +60,6 @@ export const setParent = action(
|
|
|
36
60
|
}
|
|
37
61
|
|
|
38
62
|
if (inDevMode) {
|
|
39
|
-
if (indexChangeAllowed && cloneIfApplicable) {
|
|
40
|
-
throw failure(
|
|
41
|
-
"assertion failed: 'indexChangeAllowed' and 'cloneIfApplicable' cannot be set at the same time"
|
|
42
|
-
)
|
|
43
|
-
}
|
|
44
63
|
if (typeof value === "function" || typeof value === "symbol") {
|
|
45
64
|
throw failure(`assertion failed: value cannot be a function or a symbol`)
|
|
46
65
|
}
|
|
@@ -92,14 +111,7 @@ export const setParent = action(
|
|
|
92
111
|
}
|
|
93
112
|
|
|
94
113
|
if (oldParentPath && parentPath) {
|
|
95
|
-
|
|
96
|
-
// just changing the index
|
|
97
|
-
treeNodeMetadata.get(value)!.parentPath = parentPath
|
|
98
|
-
reportParentPathChanged(value)
|
|
99
|
-
return value
|
|
100
|
-
} else {
|
|
101
|
-
throw failure("an object cannot be assigned a new parent when it already has one")
|
|
102
|
-
}
|
|
114
|
+
throw failure("an object cannot be assigned a new parent when it already has one")
|
|
103
115
|
}
|
|
104
116
|
|
|
105
117
|
const postUntweaker = parentPath ? undefined : tryUntweak(value)
|
|
@@ -3,7 +3,6 @@ import { BuiltInAction } from "../action/builtInActions"
|
|
|
3
3
|
import { ActionContextActionType } from "../action/context"
|
|
4
4
|
import { wrapInAction } from "../action/wrapInAction"
|
|
5
5
|
import { modelToDataNode } from "../parent/core"
|
|
6
|
-
import type { PathElement } from "../parent/pathTypes"
|
|
7
6
|
import type { Patch } from "../patch/Patch"
|
|
8
7
|
import { reconcileSnapshot } from "../snapshot/reconcileSnapshot"
|
|
9
8
|
import { assertTweakedObject } from "../tweaker/core"
|
|
@@ -84,7 +83,16 @@ const wrappedInternalApplyPatches = lazy(() =>
|
|
|
84
83
|
)
|
|
85
84
|
|
|
86
85
|
function applySinglePatch(obj: object, patch: Patch, modelPool: ModelPool): void {
|
|
87
|
-
const
|
|
86
|
+
const path = patch.path
|
|
87
|
+
if (inDevMode && !isArray(path)) {
|
|
88
|
+
throw failure(`invalid path: ${path}`)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let target: any = modelToDataNode(obj)
|
|
92
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
93
|
+
target = modelToDataNode(target[path[i]])
|
|
94
|
+
}
|
|
95
|
+
const prop = path.length > 0 ? path[path.length - 1] : undefined
|
|
88
96
|
|
|
89
97
|
if (isArray(target)) {
|
|
90
98
|
switch (patch.op) {
|
|
@@ -157,31 +165,3 @@ function applySinglePatchWithPath(obj: object, patch: Patch, modelPool: ModelPoo
|
|
|
157
165
|
applySinglePatch(obj, patch, modelPool)
|
|
158
166
|
})
|
|
159
167
|
}
|
|
160
|
-
|
|
161
|
-
function pathArrayToObjectAndProp(
|
|
162
|
-
obj: object,
|
|
163
|
-
path: Patch["path"]
|
|
164
|
-
): { target: any; prop?: PathElement } {
|
|
165
|
-
if (inDevMode) {
|
|
166
|
-
if (!isArray(path)) {
|
|
167
|
-
throw failure(`invalid path: ${path}`)
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
let target: any = modelToDataNode(obj)
|
|
172
|
-
|
|
173
|
-
if (path.length === 0) {
|
|
174
|
-
return {
|
|
175
|
-
target,
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
for (let i = 0; i <= path.length - 2; i++) {
|
|
180
|
-
target = modelToDataNode(target[path[i]])
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return {
|
|
184
|
-
target,
|
|
185
|
-
prop: path[path.length - 1],
|
|
186
|
-
}
|
|
187
|
-
}
|
|
@@ -17,6 +17,7 @@ import { assertTweakedObject } from "../tweaker/core"
|
|
|
17
17
|
import { assertIsObject, inDevMode, isArray, isMap, isPlainObject, isSet, lazy } from "../utils"
|
|
18
18
|
import { runWithErrorDiagnosticsContext } from "../utils/errorDiagnostics"
|
|
19
19
|
import { ModelPool } from "../utils/ModelPool"
|
|
20
|
+
import { flushInternalSnapshot, getInternalSnapshot } from "./internal"
|
|
20
21
|
import { reconcileSnapshot } from "./reconcileSnapshot"
|
|
21
22
|
import type { SnapshotInOf, SnapshotOutOf } from "./SnapshotOf"
|
|
22
23
|
import { SnapshotProcessingError } from "./SnapshotProcessingError"
|
|
@@ -52,6 +53,14 @@ export function internalApplySnapshot<T extends object>(
|
|
|
52
53
|
const obj = this
|
|
53
54
|
|
|
54
55
|
const reconcile = () => {
|
|
56
|
+
// Avoid building a model pool (and its deep children traversal) when the
|
|
57
|
+
// node already holds this exact snapshot. Child updates may still await
|
|
58
|
+
// propagation within an action, so flush before trusting the reference.
|
|
59
|
+
flushInternalSnapshot(obj, false)
|
|
60
|
+
if (getInternalSnapshot(obj)?.transformed === sn) {
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
|
|
55
64
|
const modelPool = new ModelPool(obj)
|
|
56
65
|
const ret = reconcileSnapshot(obj, sn, modelPool, undefined)
|
|
57
66
|
|
|
@@ -211,7 +211,9 @@ function snapshotToInitialData(
|
|
|
211
211
|
}
|
|
212
212
|
}
|
|
213
213
|
const observableInitialData = observable.object(initialData, undefined, observableOptions)
|
|
214
|
-
|
|
214
|
+
if (allValuesPrimitive) {
|
|
215
|
+
setModelInitialDataSnapshot(observableInitialData, initialData)
|
|
216
|
+
}
|
|
215
217
|
return observableInitialData
|
|
216
218
|
}
|
|
217
219
|
|
|
@@ -1,40 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
readonly snapshot: Record<string, unknown>
|
|
3
|
-
readonly allValuesPrimitive: boolean
|
|
4
|
-
reusable: boolean
|
|
5
|
-
}
|
|
1
|
+
import { isPrimitive, setProtoProp } from "../utils"
|
|
6
2
|
|
|
7
|
-
|
|
3
|
+
// Only primitive-only initial data can double as a snapshot without tweaking.
|
|
4
|
+
const modelInitialDataSnapshots = new WeakMap<object, Record<string, unknown>>()
|
|
8
5
|
|
|
9
6
|
/**
|
|
10
7
|
* @internal
|
|
11
8
|
*/
|
|
12
|
-
export function setModelInitialDataSnapshot(
|
|
13
|
-
data
|
|
14
|
-
snapshot: Record<string, unknown>,
|
|
15
|
-
allValuesPrimitive: boolean
|
|
16
|
-
): void {
|
|
17
|
-
modelInitialDataSnapshots.set(data, {
|
|
18
|
-
snapshot,
|
|
19
|
-
allValuesPrimitive,
|
|
20
|
-
reusable: true,
|
|
21
|
-
})
|
|
9
|
+
export function setModelInitialDataSnapshot(data: object, snapshot: Record<string, unknown>): void {
|
|
10
|
+
modelInitialDataSnapshots.set(data, snapshot)
|
|
22
11
|
}
|
|
23
12
|
|
|
24
13
|
/**
|
|
25
14
|
* @internal
|
|
26
15
|
*/
|
|
27
|
-
export function
|
|
16
|
+
export function updateModelInitialDataSnapshot(data: object, key: string, value: unknown): void {
|
|
28
17
|
const initialDataSnapshot = modelInitialDataSnapshots.get(data)
|
|
29
18
|
if (initialDataSnapshot) {
|
|
30
|
-
|
|
19
|
+
if (!isPrimitive(value)) {
|
|
20
|
+
modelInitialDataSnapshots.delete(data)
|
|
21
|
+
} else if (key === "__proto__") {
|
|
22
|
+
setProtoProp(initialDataSnapshot, value)
|
|
23
|
+
} else {
|
|
24
|
+
initialDataSnapshot[key] = value
|
|
25
|
+
}
|
|
31
26
|
}
|
|
32
27
|
}
|
|
33
28
|
|
|
34
29
|
/**
|
|
35
30
|
* @internal
|
|
36
31
|
*/
|
|
37
|
-
export function takeModelInitialDataSnapshot(data: object):
|
|
32
|
+
export function takeModelInitialDataSnapshot(data: object): Record<string, unknown> | undefined {
|
|
38
33
|
const initialDataSnapshot = modelInitialDataSnapshots.get(data)
|
|
39
34
|
modelInitialDataSnapshots.delete(data)
|
|
40
35
|
return initialDataSnapshot
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { remove } from "mobx"
|
|
1
|
+
import { remove, set } from "mobx"
|
|
2
2
|
import type { AnyModel } from "../model/BaseModel"
|
|
3
3
|
import { getModelIdPropertyName } from "../model/getModelMetadata"
|
|
4
4
|
import { isReservedModelKey, modelIdKey, modelTypeKey } from "../model/metadata"
|
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
runTypeCheckingAfterChange,
|
|
18
18
|
} from "../tweaker/typeChecking"
|
|
19
19
|
import { withoutTypeChecking } from "../tweaker/withoutTypeChecking"
|
|
20
|
-
import { isArray } from "../utils"
|
|
20
|
+
import { isArray, isPrimitive } from "../utils"
|
|
21
21
|
import { withErrorModelTrailEntry, withErrorPathSegment } from "../utils/errorDiagnostics"
|
|
22
22
|
import type { ModelPool } from "../utils/ModelPool"
|
|
23
23
|
import { setIfDifferent } from "../utils/setIfDifferent"
|
|
@@ -51,7 +51,7 @@ function reconcileModelSnapshot(
|
|
|
51
51
|
// the canonical output snapshot. A successful canonical no-op therefore
|
|
52
52
|
// cannot produce a diagnostic, so keep this hottest path outside the
|
|
53
53
|
// model-trail bookkeeping and defer id metadata until needed. The
|
|
54
|
-
// positional value was already flushed by the
|
|
54
|
+
// positional value was already flushed by the internal snapshot read in
|
|
55
55
|
// reconcileSnapshot, so its transformed snapshot is current.
|
|
56
56
|
if (isCanonicalSnapshotNoOp(value, type, sn)) {
|
|
57
57
|
return value
|
|
@@ -157,6 +157,14 @@ function reconcileModelSnapshot(
|
|
|
157
157
|
const v = processedSn[k]
|
|
158
158
|
|
|
159
159
|
const oldValue = data[k]
|
|
160
|
+
// Non-null primitives need neither reconciliation nor detachment.
|
|
161
|
+
// Nullish values must still pass through default handling below.
|
|
162
|
+
if (v != null && isPrimitive(v)) {
|
|
163
|
+
if (v !== oldValue) {
|
|
164
|
+
set(data, k, v)
|
|
165
|
+
}
|
|
166
|
+
continue
|
|
167
|
+
}
|
|
160
168
|
let newValue = withErrorPathSegment(k, () =>
|
|
161
169
|
reconcileSnapshot(oldValue, v, modelPool, modelObj)
|
|
162
170
|
)
|
|
@@ -5,6 +5,7 @@ import { fastGetParentPathIncludingDataObjects } from "../parent"
|
|
|
5
5
|
import { isMap, isPrimitive, isSet } from "../utils"
|
|
6
6
|
import type { ModelPool } from "../utils/ModelPool"
|
|
7
7
|
import { getSnapshot } from "./getSnapshot"
|
|
8
|
+
import { flushInternalSnapshot, getInternalSnapshot } from "./internal"
|
|
8
9
|
import { registerDefaultReconcilers } from "./registerDefaultReconcilers"
|
|
9
10
|
import { SnapshotProcessingError } from "./SnapshotProcessingError"
|
|
10
11
|
|
|
@@ -28,10 +29,18 @@ export function reconcileSnapshot(value: any, sn: any, modelPool: ModelPool, par
|
|
|
28
29
|
return sn
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
// Unlike getSnapshot, this comparison never exposes the snapshot, so it needs
|
|
33
|
+
// neither freezing nor observation registration.
|
|
34
|
+
const currentSnapshot = getInternalSnapshot(value)
|
|
35
|
+
if (currentSnapshot) {
|
|
36
|
+
flushInternalSnapshot(value, false)
|
|
37
|
+
if (currentSnapshot.transformed === sn) {
|
|
38
|
+
return value
|
|
39
|
+
}
|
|
40
|
+
} else if (!isPrimitive(value)) {
|
|
41
|
+
// A non-primitive without an internal snapshot is not a valid reconciliation
|
|
42
|
+
// target (e.g. a non-node object); let getSnapshot raise the proper error.
|
|
43
|
+
getSnapshot(value)
|
|
35
44
|
}
|
|
36
45
|
|
|
37
46
|
registerDefaultReconcilers()
|
package/src/tweaker/tweak.ts
CHANGED
|
@@ -86,16 +86,11 @@ export function registerTweaker<T>(priority: number, tweaker: Tweaker<T>): void
|
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
function internalTweak<T>(value: T, parentPath: ParentPath<any> | undefined): T {
|
|
89
|
-
if (isPrimitive(value)) {
|
|
90
|
-
return value
|
|
91
|
-
}
|
|
92
|
-
|
|
93
89
|
// already tweaked
|
|
94
90
|
if (isTweakedObject(value, true)) {
|
|
95
91
|
value = setParent(
|
|
96
92
|
value,
|
|
97
93
|
parentPath,
|
|
98
|
-
false, // indexChangeAllowed
|
|
99
94
|
false, // isDataObject
|
|
100
95
|
true // cloneIfApplicable
|
|
101
96
|
)
|
|
@@ -135,10 +130,15 @@ function internalTweak<T>(value: T, parentPath: ParentPath<any> | undefined): T
|
|
|
135
130
|
)
|
|
136
131
|
}
|
|
137
132
|
|
|
133
|
+
const tweakNonPrimitive = action("tweak", internalTweak)
|
|
134
|
+
|
|
138
135
|
/**
|
|
139
136
|
* @internal
|
|
140
137
|
*/
|
|
141
|
-
export
|
|
138
|
+
export function tweak<T>(value: T, parentPath: ParentPath<any> | undefined): T {
|
|
139
|
+
// Primitives need no tree bookkeeping or MobX action boundary.
|
|
140
|
+
return isPrimitive(value) ? value : tweakNonPrimitive(value, parentPath)
|
|
141
|
+
}
|
|
142
142
|
|
|
143
143
|
/**
|
|
144
144
|
* @internal
|
|
@@ -169,7 +169,6 @@ export function tryUntweak(value: any): (() => void) | undefined {
|
|
|
169
169
|
setParent(
|
|
170
170
|
children[i], // value
|
|
171
171
|
undefined, // parentPath
|
|
172
|
-
false, // indexChangeAllowed
|
|
173
172
|
false, // isDataObject
|
|
174
173
|
// no need to clone if unsetting the parent
|
|
175
174
|
false // cloneIfApplicable
|
|
@@ -12,7 +12,7 @@ import { assertCanWrite } from "../action/protection"
|
|
|
12
12
|
import { emitArraySpliceDeepChange, emitArrayUpdateDeepChange } from "../deepChange/onDeepChange"
|
|
13
13
|
import { getGlobalConfig } from "../globalConfig"
|
|
14
14
|
import type { ParentPath } from "../parent/path"
|
|
15
|
-
import { setParent } from "../parent/setParent"
|
|
15
|
+
import { reindexArrayChildren, setParent } from "../parent/setParent"
|
|
16
16
|
import { hasPatchListenersFor, InternalPatchRecorder } from "../patch/emitPatch"
|
|
17
17
|
import type { Patch } from "../patch/Patch"
|
|
18
18
|
import {
|
|
@@ -62,7 +62,6 @@ export function tweakArray<T extends any[]>(
|
|
|
62
62
|
setParent(
|
|
63
63
|
tweakedArr, // value
|
|
64
64
|
parentPath,
|
|
65
|
-
false, // indexChangeAllowed
|
|
66
65
|
false, // isDataObject
|
|
67
66
|
// arrays shouldn't be cloned anyway
|
|
68
67
|
false // cloneIfApplicable
|
|
@@ -97,7 +96,6 @@ export function tweakArray<T extends any[]>(
|
|
|
97
96
|
setParent(
|
|
98
97
|
tweakedValue, // value
|
|
99
98
|
path, // parentPath
|
|
100
|
-
false, // indexChangeAllowed
|
|
101
99
|
false, // isDataObject
|
|
102
100
|
// the value is already a new value (the result of a fromSnapshot)
|
|
103
101
|
false // cloneIfApplicable
|
|
@@ -482,20 +480,8 @@ function interceptArrayMutationSplice(change: IArrayWillSplice) {
|
|
|
482
480
|
const oldNextIndex = change.index + change.removedCount
|
|
483
481
|
const newNextIndex = change.index + change.added.length
|
|
484
482
|
|
|
485
|
-
if (oldNextIndex !== newNextIndex) {
|
|
486
|
-
|
|
487
|
-
setParent(
|
|
488
|
-
change.object[i], // value
|
|
489
|
-
{
|
|
490
|
-
parent: change.object,
|
|
491
|
-
path: j,
|
|
492
|
-
}, // parentPath
|
|
493
|
-
true, // indexChangeAllowed
|
|
494
|
-
false, // isDataObject
|
|
495
|
-
// just re-indexing
|
|
496
|
-
false // cloneIfApplicable
|
|
497
|
-
)
|
|
498
|
-
}
|
|
483
|
+
if (oldNextIndex !== newNextIndex && oldNextIndex < change.object.length) {
|
|
484
|
+
reindexArrayChildren(change.object, oldNextIndex, newNextIndex)
|
|
499
485
|
}
|
|
500
486
|
}
|
|
501
487
|
|