mobx-keystone 1.20.0 → 1.22.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 +864 -853
- package/README.md +61 -61
- package/dist/mobx-keystone.esm.js +11336 -11148
- package/dist/mobx-keystone.esm.mjs +11336 -11148
- package/dist/mobx-keystone.umd.js +12229 -11738
- package/dist/types/actionMiddlewares/undoMiddleware.d.ts +1 -1
- package/dist/types/modelShared/prop.d.ts +5 -0
- package/dist/types/redux/connectReduxDevTools.d.ts +17 -4
- package/dist/types/ref/Ref.d.ts +1 -1
- package/dist/types/treeUtils/sandbox.d.ts +1 -1
- 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 +14 -12
- 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 +39 -7
- 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 +27 -33
- package/src/snapshot/fromModelSnapshot.ts +70 -70
- package/src/snapshot/fromPlainObjectSnapshot.ts +47 -42
- package/src/snapshot/fromSnapshot.ts +198 -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 +530 -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 +183 -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
package/src/transforms/asMap.ts
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import type { ModelPropTransform } from "../modelShared/prop"
|
|
2
|
-
import { asMap } from "../wrappers/asMap"
|
|
3
|
-
|
|
4
|
-
const _objectToMapTransform: ModelPropTransform<Record<string, unknown>, Map<string, unknown>> = {
|
|
5
|
-
transform({ originalValue: obj, cachedTransformedValue: cachedMap }) {
|
|
6
|
-
return cachedMap ?? (asMap(obj) as unknown as Map<string, unknown>)
|
|
7
|
-
},
|
|
8
|
-
|
|
9
|
-
untransform({ transformedValue: map }) {
|
|
10
|
-
// do not cache map <-> obj relationship
|
|
11
|
-
|
|
12
|
-
const obj: Record<string, unknown> = {}
|
|
13
|
-
map.forEach((v, k) => {
|
|
14
|
-
obj[k] = v
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
return obj
|
|
18
|
-
},
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @deprecated Prefer the codec equivalent: `tProp(types.mapFromObject(valueType))`.
|
|
23
|
-
* Use `tProp(types.skipCheck(types.mapFromObject(valueType)))` if you don't need runtime validation.
|
|
24
|
-
*/
|
|
25
|
-
export const objectToMapTransform = <T>() =>
|
|
26
|
-
_objectToMapTransform as ModelPropTransform<Record<string, T>, Map<string, T>>
|
|
27
|
-
|
|
28
|
-
const _arrayToMapTransform: ModelPropTransform<Array<[unknown, unknown]>, Map<unknown, unknown>> = {
|
|
29
|
-
transform: ({ originalValue: arr, cachedTransformedValue: cachedMap }) => {
|
|
30
|
-
return cachedMap ?? (asMap(arr) as unknown as Map<string, unknown>)
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
untransform({ transformedValue: map }) {
|
|
34
|
-
// do not cache map <-> arr relationship
|
|
35
|
-
|
|
36
|
-
const arr: Array<[unknown, unknown]> = []
|
|
37
|
-
map.forEach((v, k) => {
|
|
38
|
-
arr.push([k, v])
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
return arr
|
|
42
|
-
},
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* @deprecated Prefer the codec equivalent: `tProp(types.mapFromArray(keyType, valueType))`.
|
|
47
|
-
* Use `tProp(types.skipCheck(types.mapFromArray(keyType, valueType)))` if you don't need runtime validation.
|
|
48
|
-
*/
|
|
49
|
-
export const arrayToMapTransform = <K, V>() =>
|
|
50
|
-
_arrayToMapTransform as ModelPropTransform<Array<[K, V]>, Map<K, V>>
|
|
1
|
+
import type { ModelPropTransform } from "../modelShared/prop"
|
|
2
|
+
import { asMap } from "../wrappers/asMap"
|
|
3
|
+
|
|
4
|
+
const _objectToMapTransform: ModelPropTransform<Record<string, unknown>, Map<string, unknown>> = {
|
|
5
|
+
transform({ originalValue: obj, cachedTransformedValue: cachedMap }) {
|
|
6
|
+
return cachedMap ?? (asMap(obj) as unknown as Map<string, unknown>)
|
|
7
|
+
},
|
|
8
|
+
|
|
9
|
+
untransform({ transformedValue: map }) {
|
|
10
|
+
// do not cache map <-> obj relationship
|
|
11
|
+
|
|
12
|
+
const obj: Record<string, unknown> = {}
|
|
13
|
+
map.forEach((v, k) => {
|
|
14
|
+
obj[k] = v
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
return obj
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @deprecated Prefer the codec equivalent: `tProp(types.mapFromObject(valueType))`.
|
|
23
|
+
* Use `tProp(types.skipCheck(types.mapFromObject(valueType)))` if you don't need runtime validation.
|
|
24
|
+
*/
|
|
25
|
+
export const objectToMapTransform = <T>() =>
|
|
26
|
+
_objectToMapTransform as ModelPropTransform<Record<string, T>, Map<string, T>>
|
|
27
|
+
|
|
28
|
+
const _arrayToMapTransform: ModelPropTransform<Array<[unknown, unknown]>, Map<unknown, unknown>> = {
|
|
29
|
+
transform: ({ originalValue: arr, cachedTransformedValue: cachedMap }) => {
|
|
30
|
+
return cachedMap ?? (asMap(arr) as unknown as Map<string, unknown>)
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
untransform({ transformedValue: map }) {
|
|
34
|
+
// do not cache map <-> arr relationship
|
|
35
|
+
|
|
36
|
+
const arr: Array<[unknown, unknown]> = []
|
|
37
|
+
map.forEach((v, k) => {
|
|
38
|
+
arr.push([k, v])
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
return arr
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @deprecated Prefer the codec equivalent: `tProp(types.mapFromArray(keyType, valueType))`.
|
|
47
|
+
* Use `tProp(types.skipCheck(types.mapFromArray(keyType, valueType)))` if you don't need runtime validation.
|
|
48
|
+
*/
|
|
49
|
+
export const arrayToMapTransform = <K, V>() =>
|
|
50
|
+
_arrayToMapTransform as ModelPropTransform<Array<[K, V]>, Map<K, V>>
|
package/src/transforms/asSet.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { ObservableSet } from "mobx"
|
|
2
|
-
import type { ModelPropTransform } from "../modelShared/prop"
|
|
3
|
-
import { asSet } from "../wrappers/asSet"
|
|
4
|
-
|
|
5
|
-
const _arrayToSetTransform: ModelPropTransform<Array<unknown>, ObservableSet<unknown>> = {
|
|
6
|
-
transform({ originalValue: arr, cachedTransformedValue: cachedSet }) {
|
|
7
|
-
return cachedSet ?? asSet(arr)
|
|
8
|
-
},
|
|
9
|
-
|
|
10
|
-
untransform({ transformedValue: set }) {
|
|
11
|
-
// do not cache set <-> arr relationship
|
|
12
|
-
|
|
13
|
-
return Array.from(set.values())
|
|
14
|
-
},
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @deprecated Prefer the codec equivalent: `tProp(types.setFromArray(valueType))`.
|
|
19
|
-
* Use `tProp(types.skipCheck(types.setFromArray(valueType)))` if you don't need runtime validation.
|
|
20
|
-
*/
|
|
21
|
-
export const arrayToSetTransform = <T>() =>
|
|
22
|
-
_arrayToSetTransform as ModelPropTransform<Array<T>, Set<T> | ObservableSet<T>>
|
|
1
|
+
import type { ObservableSet } from "mobx"
|
|
2
|
+
import type { ModelPropTransform } from "../modelShared/prop"
|
|
3
|
+
import { asSet } from "../wrappers/asSet"
|
|
4
|
+
|
|
5
|
+
const _arrayToSetTransform: ModelPropTransform<Array<unknown>, ObservableSet<unknown>> = {
|
|
6
|
+
transform({ originalValue: arr, cachedTransformedValue: cachedSet }) {
|
|
7
|
+
return cachedSet ?? asSet(arr)
|
|
8
|
+
},
|
|
9
|
+
|
|
10
|
+
untransform({ transformedValue: set }) {
|
|
11
|
+
// do not cache set <-> arr relationship
|
|
12
|
+
|
|
13
|
+
return Array.from(set.values())
|
|
14
|
+
},
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated Prefer the codec equivalent: `tProp(types.setFromArray(valueType))`.
|
|
19
|
+
* Use `tProp(types.skipCheck(types.setFromArray(valueType)))` if you don't need runtime validation.
|
|
20
|
+
*/
|
|
21
|
+
export const arrayToSetTransform = <T>() =>
|
|
22
|
+
_arrayToSetTransform as ModelPropTransform<Array<T>, Set<T> | ObservableSet<T>>
|
package/src/transforms/bigint.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import type { ModelPropTransform } from "../modelShared/prop"
|
|
2
|
-
|
|
3
|
-
const _stringToBigIntTransform: ModelPropTransform<string, bigint> = {
|
|
4
|
-
transform({ originalValue, cachedTransformedValue }) {
|
|
5
|
-
return cachedTransformedValue ?? BigInt(originalValue)
|
|
6
|
-
},
|
|
7
|
-
|
|
8
|
-
untransform({ transformedValue, cacheTransformedValue }) {
|
|
9
|
-
if (typeof transformedValue === "bigint") {
|
|
10
|
-
cacheTransformedValue()
|
|
11
|
-
}
|
|
12
|
-
return transformedValue.toString()
|
|
13
|
-
},
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @deprecated Prefer the codec equivalent: `tProp(types.bigint)`.
|
|
18
|
-
* Use `tProp(types.skipCheck(types.bigint))` if you don't need runtime validation.
|
|
19
|
-
*/
|
|
20
|
-
export const stringToBigIntTransform = () => _stringToBigIntTransform
|
|
1
|
+
import type { ModelPropTransform } from "../modelShared/prop"
|
|
2
|
+
|
|
3
|
+
const _stringToBigIntTransform: ModelPropTransform<string, bigint> = {
|
|
4
|
+
transform({ originalValue, cachedTransformedValue }) {
|
|
5
|
+
return cachedTransformedValue ?? BigInt(originalValue)
|
|
6
|
+
},
|
|
7
|
+
|
|
8
|
+
untransform({ transformedValue, cacheTransformedValue }) {
|
|
9
|
+
if (typeof transformedValue === "bigint") {
|
|
10
|
+
cacheTransformedValue()
|
|
11
|
+
}
|
|
12
|
+
return transformedValue.toString()
|
|
13
|
+
},
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated Prefer the codec equivalent: `tProp(types.bigint)`.
|
|
18
|
+
* Use `tProp(types.skipCheck(types.bigint))` if you don't need runtime validation.
|
|
19
|
+
*/
|
|
20
|
+
export const stringToBigIntTransform = () => _stringToBigIntTransform
|
package/src/transforms/date.ts
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import type { ModelPropTransform } from "../modelShared/prop"
|
|
2
|
-
import { ImmutableDate } from "./ImmutableDate"
|
|
3
|
-
|
|
4
|
-
const _timestampToDateTransform: ModelPropTransform<number, Date> = {
|
|
5
|
-
transform({ originalValue, cachedTransformedValue }) {
|
|
6
|
-
return cachedTransformedValue ?? new ImmutableDate(originalValue)
|
|
7
|
-
},
|
|
8
|
-
|
|
9
|
-
untransform({ transformedValue, cacheTransformedValue }) {
|
|
10
|
-
if (transformedValue instanceof ImmutableDate) {
|
|
11
|
-
cacheTransformedValue()
|
|
12
|
-
}
|
|
13
|
-
return +transformedValue
|
|
14
|
-
},
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @deprecated Prefer the codec equivalent: `tProp(types.dateAsTimestamp)`.
|
|
19
|
-
* Use `tProp(types.skipCheck(types.dateAsTimestamp))` if you don't need runtime validation.
|
|
20
|
-
*/
|
|
21
|
-
export const timestampToDateTransform = () => _timestampToDateTransform
|
|
22
|
-
|
|
23
|
-
const _isoStringToDateTransform: ModelPropTransform<string, Date> = {
|
|
24
|
-
transform({ originalValue, cachedTransformedValue }) {
|
|
25
|
-
return cachedTransformedValue ?? new ImmutableDate(originalValue)
|
|
26
|
-
},
|
|
27
|
-
|
|
28
|
-
untransform({ transformedValue, cacheTransformedValue }) {
|
|
29
|
-
if (transformedValue instanceof ImmutableDate) {
|
|
30
|
-
cacheTransformedValue()
|
|
31
|
-
}
|
|
32
|
-
return transformedValue.toISOString()
|
|
33
|
-
},
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @deprecated Prefer the codec equivalent: `tProp(types.dateAsIsoString)`.
|
|
38
|
-
* Use `tProp(types.skipCheck(types.dateAsIsoString))` if you don't need runtime validation.
|
|
39
|
-
*/
|
|
40
|
-
export const isoStringToDateTransform = () => _isoStringToDateTransform
|
|
1
|
+
import type { ModelPropTransform } from "../modelShared/prop"
|
|
2
|
+
import { ImmutableDate } from "./ImmutableDate"
|
|
3
|
+
|
|
4
|
+
const _timestampToDateTransform: ModelPropTransform<number, Date> = {
|
|
5
|
+
transform({ originalValue, cachedTransformedValue }) {
|
|
6
|
+
return cachedTransformedValue ?? new ImmutableDate(originalValue)
|
|
7
|
+
},
|
|
8
|
+
|
|
9
|
+
untransform({ transformedValue, cacheTransformedValue }) {
|
|
10
|
+
if (transformedValue instanceof ImmutableDate) {
|
|
11
|
+
cacheTransformedValue()
|
|
12
|
+
}
|
|
13
|
+
return +transformedValue
|
|
14
|
+
},
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated Prefer the codec equivalent: `tProp(types.dateAsTimestamp)`.
|
|
19
|
+
* Use `tProp(types.skipCheck(types.dateAsTimestamp))` if you don't need runtime validation.
|
|
20
|
+
*/
|
|
21
|
+
export const timestampToDateTransform = () => _timestampToDateTransform
|
|
22
|
+
|
|
23
|
+
const _isoStringToDateTransform: ModelPropTransform<string, Date> = {
|
|
24
|
+
transform({ originalValue, cachedTransformedValue }) {
|
|
25
|
+
return cachedTransformedValue ?? new ImmutableDate(originalValue)
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
untransform({ transformedValue, cacheTransformedValue }) {
|
|
29
|
+
if (transformedValue instanceof ImmutableDate) {
|
|
30
|
+
cacheTransformedValue()
|
|
31
|
+
}
|
|
32
|
+
return transformedValue.toISOString()
|
|
33
|
+
},
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @deprecated Prefer the codec equivalent: `tProp(types.dateAsIsoString)`.
|
|
38
|
+
* Use `tProp(types.skipCheck(types.dateAsIsoString))` if you don't need runtime validation.
|
|
39
|
+
*/
|
|
40
|
+
export const isoStringToDateTransform = () => _isoStringToDateTransform
|
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
import fastDeepEqual from "fast-deep-equal/es6"
|
|
2
|
-
import { isObservable, toJS } from "mobx"
|
|
3
|
-
import { getSnapshot } from "../snapshot"
|
|
4
|
-
import { isTreeNode } from "../tweaker"
|
|
5
|
-
import { getMobxVersion } from "../utils"
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Deeply compares two values.
|
|
9
|
-
*
|
|
10
|
-
* Supported values are:
|
|
11
|
-
* - Primitives
|
|
12
|
-
* - Boxed observables
|
|
13
|
-
* - Objects, observable objects
|
|
14
|
-
* - Arrays, observable arrays
|
|
15
|
-
* - Typed arrays
|
|
16
|
-
* - Maps, observable maps
|
|
17
|
-
* - Sets, observable sets
|
|
18
|
-
* - Tree nodes (optimized by using snapshot comparison internally)
|
|
19
|
-
*
|
|
20
|
-
* Note that in the case of models the result will be false if their model IDs are different.
|
|
21
|
-
*
|
|
22
|
-
* @param a First value to compare.
|
|
23
|
-
* @param b Second value to compare.
|
|
24
|
-
* @returns `true` if they are the equivalent, `false` otherwise.
|
|
25
|
-
*/
|
|
26
|
-
export function deepEquals(a: any, b: any): boolean {
|
|
27
|
-
// quick check for reference
|
|
28
|
-
if (a === b) {
|
|
29
|
-
return true
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// use snapshots to compare if possible
|
|
33
|
-
// since snapshots use structural sharing it is more likely
|
|
34
|
-
// to speed up comparisons
|
|
35
|
-
if (isTreeNode(a)) {
|
|
36
|
-
a = getSnapshot(a)
|
|
37
|
-
} else if (isObservable(a)) {
|
|
38
|
-
a = toJS(a, toJSOptions)
|
|
39
|
-
}
|
|
40
|
-
if (isTreeNode(b)) {
|
|
41
|
-
b = getSnapshot(b)
|
|
42
|
-
} else if (isObservable(b)) {
|
|
43
|
-
b = toJS(b, toJSOptions)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return fastDeepEqual(a, b)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const toJSOptions =
|
|
50
|
-
getMobxVersion() >= 6
|
|
51
|
-
? undefined
|
|
52
|
-
: {
|
|
53
|
-
exportMapsAsObjects: false,
|
|
54
|
-
recurseEverything: false,
|
|
55
|
-
}
|
|
1
|
+
import fastDeepEqual from "fast-deep-equal/es6"
|
|
2
|
+
import { isObservable, toJS } from "mobx"
|
|
3
|
+
import { getSnapshot } from "../snapshot"
|
|
4
|
+
import { isTreeNode } from "../tweaker"
|
|
5
|
+
import { getMobxVersion } from "../utils"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Deeply compares two values.
|
|
9
|
+
*
|
|
10
|
+
* Supported values are:
|
|
11
|
+
* - Primitives
|
|
12
|
+
* - Boxed observables
|
|
13
|
+
* - Objects, observable objects
|
|
14
|
+
* - Arrays, observable arrays
|
|
15
|
+
* - Typed arrays
|
|
16
|
+
* - Maps, observable maps
|
|
17
|
+
* - Sets, observable sets
|
|
18
|
+
* - Tree nodes (optimized by using snapshot comparison internally)
|
|
19
|
+
*
|
|
20
|
+
* Note that in the case of models the result will be false if their model IDs are different.
|
|
21
|
+
*
|
|
22
|
+
* @param a First value to compare.
|
|
23
|
+
* @param b Second value to compare.
|
|
24
|
+
* @returns `true` if they are the equivalent, `false` otherwise.
|
|
25
|
+
*/
|
|
26
|
+
export function deepEquals(a: any, b: any): boolean {
|
|
27
|
+
// quick check for reference
|
|
28
|
+
if (a === b) {
|
|
29
|
+
return true
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// use snapshots to compare if possible
|
|
33
|
+
// since snapshots use structural sharing it is more likely
|
|
34
|
+
// to speed up comparisons
|
|
35
|
+
if (isTreeNode(a)) {
|
|
36
|
+
a = getSnapshot(a)
|
|
37
|
+
} else if (isObservable(a)) {
|
|
38
|
+
a = toJS(a, toJSOptions)
|
|
39
|
+
}
|
|
40
|
+
if (isTreeNode(b)) {
|
|
41
|
+
b = getSnapshot(b)
|
|
42
|
+
} else if (isObservable(b)) {
|
|
43
|
+
b = toJS(b, toJSOptions)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return fastDeepEqual(a, b)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const toJSOptions =
|
|
50
|
+
getMobxVersion() >= 6
|
|
51
|
+
? undefined
|
|
52
|
+
: {
|
|
53
|
+
exportMapsAsObjects: false,
|
|
54
|
+
recurseEverything: false,
|
|
55
|
+
}
|
package/src/treeUtils/sandbox.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { reaction, runInAction } from "mobx"
|
|
2
2
|
import {
|
|
3
|
+
type ReadonlyMiddlewareReturn,
|
|
3
4
|
readonlyMiddleware,
|
|
4
|
-
ReadonlyMiddlewareReturn,
|
|
5
5
|
} from "../actionMiddlewares/readonlyMiddleware"
|
|
6
6
|
import { createContext } from "../context"
|
|
7
7
|
import { getParentToChildPath, resolvePath } from "../parent/path"
|
|
8
8
|
import { applyPatches } from "../patch/applyPatches"
|
|
9
9
|
import { onPatches } from "../patch/emitPatch"
|
|
10
10
|
import type { Patch } from "../patch/Patch"
|
|
11
|
-
import { PatchRecorder, patchRecorder } from "../patch/patchRecorder"
|
|
11
|
+
import { type PatchRecorder, patchRecorder } from "../patch/patchRecorder"
|
|
12
12
|
import {
|
|
13
13
|
fastIsRootStoreNoAtom,
|
|
14
14
|
isRootStore,
|
|
@@ -83,13 +83,16 @@ export class SandboxManager {
|
|
|
83
83
|
*/
|
|
84
84
|
private isCommitting = false
|
|
85
85
|
|
|
86
|
+
private readonly subtreeRoot: object
|
|
87
|
+
|
|
86
88
|
/**
|
|
87
89
|
* Creates an instance of `SandboxManager`.
|
|
88
90
|
* Do not use directly, use `sandbox` instead.
|
|
89
91
|
*
|
|
90
92
|
* @param subtreeRoot Subtree root target object.
|
|
91
93
|
*/
|
|
92
|
-
constructor(
|
|
94
|
+
constructor(subtreeRoot: object) {
|
|
95
|
+
this.subtreeRoot = subtreeRoot
|
|
93
96
|
assertTweakedObject(subtreeRoot, "subtreeRoot")
|
|
94
97
|
|
|
95
98
|
// we temporarily set the default value of the context manager so that
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @internal
|
|
3
|
-
*/
|
|
4
|
-
export enum TweakerPriority {
|
|
5
|
-
Model = 0,
|
|
6
|
-
Array = 1,
|
|
7
|
-
PlainObject = 2,
|
|
8
|
-
Frozen = 3,
|
|
9
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
export enum TweakerPriority {
|
|
5
|
+
Model = 0,
|
|
6
|
+
Array = 1,
|
|
7
|
+
PlainObject = 2,
|
|
8
|
+
Frozen = 3,
|
|
9
|
+
}
|
package/src/tweaker/tweak.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { action } from "mobx"
|
|
|
2
2
|
import { isDataModel } from "../dataModel/utils"
|
|
3
3
|
import { isModelAutoTypeCheckingEnabled } from "../globalConfig/globalConfig"
|
|
4
4
|
import { getObjectChildren } from "../parent/coreObjectChildren"
|
|
5
|
-
import { fastGetParent, ParentPath } from "../parent/path"
|
|
5
|
+
import { fastGetParent, type ParentPath } from "../parent/path"
|
|
6
6
|
import { setParent } from "../parent/setParent"
|
|
7
7
|
import { unsetInternalSnapshot } from "../snapshot/internal"
|
|
8
8
|
import type { AnyStandardType, TypeToData } from "../types/schemas"
|
|
@@ -38,11 +38,12 @@ export function toTreeNode<TType extends AnyStandardType, V extends TypeToData<T
|
|
|
38
38
|
export function toTreeNode<T extends object>(value: T): T
|
|
39
39
|
|
|
40
40
|
// base
|
|
41
|
-
export function toTreeNode(arg1: any, arg2
|
|
41
|
+
export function toTreeNode(...args: [arg1: any] | [arg1: any, arg2: any]): any {
|
|
42
|
+
const [arg1, arg2] = args
|
|
42
43
|
let value: object
|
|
43
44
|
let type: AnyStandardType | undefined
|
|
44
45
|
let hasType: boolean
|
|
45
|
-
if (
|
|
46
|
+
if (args.length === 1) {
|
|
46
47
|
hasType = false
|
|
47
48
|
value = arg1
|
|
48
49
|
} else {
|