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
|
@@ -1,82 +1,88 @@
|
|
|
1
|
-
import { getTypeInfo } from "../getTypeInfo"
|
|
2
|
-
import { resolveStandardType, resolveTypeChecker } from "../resolveTypeChecker"
|
|
3
|
-
import type { AnyStandardType, AnyType } from "../schemas"
|
|
4
|
-
import { lateTypeChecker, TypeChecker, TypeInfo, TypeInfoGen } from "../TypeChecker"
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Wrap a given type with tag information.
|
|
8
|
-
* This allows you to associate metadata with the type of a prop that
|
|
9
|
-
* you can use at runtime.
|
|
10
|
-
*
|
|
11
|
-
* Example:
|
|
12
|
-
* ```ts
|
|
13
|
-
* const widthType = types.tag(types.number, { displayName: "Width in Inches", required: true }, "dimension")
|
|
14
|
-
* const heightType = types.tag(types.number, { displayName: "Height in Inches", required: true }, "dimension")
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* These can then be accessed at runtime through inspection APIs, e.g.
|
|
18
|
-
* ```
|
|
19
|
-
* @model('MyModel')
|
|
20
|
-
* class MyModel extends Model({
|
|
21
|
-
* width: tProp(widthType, 10),
|
|
22
|
-
* height: tProp(heightType, 10)
|
|
23
|
-
* }) {}
|
|
24
|
-
*
|
|
25
|
-
* const m = new MyModel({})
|
|
26
|
-
* const type = types.model<typeof Model>(m.constructor)
|
|
27
|
-
* const modelTypeInfo = getTypeInfo(type) as ModelTypeInfo
|
|
28
|
-
* const propTypeInfo = modelTypeInfo.props.width.typeInfo as TagTypeInfo
|
|
29
|
-
* const displayName = propTypeInfo.displayName
|
|
30
|
-
* ```
|
|
31
|
-
* @template T Base type.
|
|
32
|
-
* @param baseType Base type.
|
|
33
|
-
* @template A Tag object.
|
|
34
|
-
* @param tag Abitrary object that can be queried at runtime.
|
|
35
|
-
* @returns
|
|
36
|
-
*/
|
|
37
|
-
export function typesTag<T extends AnyType, A>(baseType: T, tag: A, typeName?: string): T {
|
|
38
|
-
const typeInfoGen: TypeInfoGen = (t) =>
|
|
39
|
-
new TagTypeInfo(t, resolveStandardType(baseType), tag, typeName)
|
|
40
|
-
|
|
41
|
-
return lateTypeChecker(() => {
|
|
42
|
-
const baseChecker = resolveTypeChecker(baseType)
|
|
43
|
-
|
|
44
|
-
const getTypeName = (...recursiveTypeCheckers: TypeChecker[]) => {
|
|
45
|
-
const baseTypeName = baseChecker.getTypeName(...recursiveTypeCheckers, baseChecker)
|
|
46
|
-
const taggedName = typeName || "tagged"
|
|
47
|
-
return `${taggedName}<${baseTypeName}>`
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const thisTc: TypeChecker = new TypeChecker(
|
|
51
|
-
baseChecker.baseType,
|
|
52
|
-
(data, path, typeCheckedValue) => baseChecker.check(data, path, typeCheckedValue),
|
|
53
|
-
getTypeName,
|
|
54
|
-
typeInfoGen,
|
|
55
|
-
(sn) => baseChecker.snapshotType(sn),
|
|
56
|
-
(sn) => baseChecker.fromSnapshotProcessor(sn),
|
|
57
|
-
(sn) => baseChecker.toSnapshotProcessor(sn)
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
return thisTc
|
|
61
|
-
}, typeInfoGen) as any
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* `types.tag` type info.
|
|
66
|
-
*/
|
|
67
|
-
export class TagTypeInfo<A> extends TypeInfo {
|
|
68
|
-
readonly kind = "tag"
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
1
|
+
import { getTypeInfo } from "../getTypeInfo"
|
|
2
|
+
import { resolveStandardType, resolveTypeChecker } from "../resolveTypeChecker"
|
|
3
|
+
import type { AnyStandardType, AnyType } from "../schemas"
|
|
4
|
+
import { lateTypeChecker, TypeChecker, TypeInfo, type TypeInfoGen } from "../TypeChecker"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Wrap a given type with tag information.
|
|
8
|
+
* This allows you to associate metadata with the type of a prop that
|
|
9
|
+
* you can use at runtime.
|
|
10
|
+
*
|
|
11
|
+
* Example:
|
|
12
|
+
* ```ts
|
|
13
|
+
* const widthType = types.tag(types.number, { displayName: "Width in Inches", required: true }, "dimension")
|
|
14
|
+
* const heightType = types.tag(types.number, { displayName: "Height in Inches", required: true }, "dimension")
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* These can then be accessed at runtime through inspection APIs, e.g.
|
|
18
|
+
* ```
|
|
19
|
+
* @model('MyModel')
|
|
20
|
+
* class MyModel extends Model({
|
|
21
|
+
* width: tProp(widthType, 10),
|
|
22
|
+
* height: tProp(heightType, 10)
|
|
23
|
+
* }) {}
|
|
24
|
+
*
|
|
25
|
+
* const m = new MyModel({})
|
|
26
|
+
* const type = types.model<typeof Model>(m.constructor)
|
|
27
|
+
* const modelTypeInfo = getTypeInfo(type) as ModelTypeInfo
|
|
28
|
+
* const propTypeInfo = modelTypeInfo.props.width.typeInfo as TagTypeInfo
|
|
29
|
+
* const displayName = propTypeInfo.displayName
|
|
30
|
+
* ```
|
|
31
|
+
* @template T Base type.
|
|
32
|
+
* @param baseType Base type.
|
|
33
|
+
* @template A Tag object.
|
|
34
|
+
* @param tag Abitrary object that can be queried at runtime.
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
export function typesTag<T extends AnyType, A>(baseType: T, tag: A, typeName?: string): T {
|
|
38
|
+
const typeInfoGen: TypeInfoGen = (t) =>
|
|
39
|
+
new TagTypeInfo(t, resolveStandardType(baseType), tag, typeName)
|
|
40
|
+
|
|
41
|
+
return lateTypeChecker(() => {
|
|
42
|
+
const baseChecker = resolveTypeChecker(baseType)
|
|
43
|
+
|
|
44
|
+
const getTypeName = (...recursiveTypeCheckers: TypeChecker[]) => {
|
|
45
|
+
const baseTypeName = baseChecker.getTypeName(...recursiveTypeCheckers, baseChecker)
|
|
46
|
+
const taggedName = typeName || "tagged"
|
|
47
|
+
return `${taggedName}<${baseTypeName}>`
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const thisTc: TypeChecker = new TypeChecker(
|
|
51
|
+
baseChecker.baseType,
|
|
52
|
+
(data, path, typeCheckedValue) => baseChecker.check(data, path, typeCheckedValue),
|
|
53
|
+
getTypeName,
|
|
54
|
+
typeInfoGen,
|
|
55
|
+
(sn) => baseChecker.snapshotType(sn),
|
|
56
|
+
(sn) => baseChecker.fromSnapshotProcessor(sn),
|
|
57
|
+
(sn) => baseChecker.toSnapshotProcessor(sn)
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
return thisTc
|
|
61
|
+
}, typeInfoGen) as any
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* `types.tag` type info.
|
|
66
|
+
*/
|
|
67
|
+
export class TagTypeInfo<A> extends TypeInfo {
|
|
68
|
+
readonly kind = "tag"
|
|
69
|
+
readonly baseType: AnyStandardType
|
|
70
|
+
readonly tag: A
|
|
71
|
+
readonly typeName: string | undefined
|
|
72
|
+
|
|
73
|
+
get baseTypeInfo(): TypeInfo {
|
|
74
|
+
return getTypeInfo(this.baseType)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
constructor(
|
|
78
|
+
thisType: AnyStandardType,
|
|
79
|
+
baseType: AnyStandardType,
|
|
80
|
+
tag: A,
|
|
81
|
+
typeName: string | undefined
|
|
82
|
+
) {
|
|
83
|
+
super(thisType)
|
|
84
|
+
this.baseType = baseType
|
|
85
|
+
this.tag = tag
|
|
86
|
+
this.typeName = typeName
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import { identityFn } from "../../utils"
|
|
2
|
-
import type { IdentityType } from "../schemas"
|
|
3
|
-
import { TypeChecker, TypeCheckerBaseType, TypeInfo } from "../TypeChecker"
|
|
4
|
-
|
|
5
|
-
const unchecked: IdentityType<any> = new TypeChecker(
|
|
6
|
-
TypeCheckerBaseType.Any,
|
|
7
|
-
null,
|
|
8
|
-
() => "any",
|
|
9
|
-
(t) => new UncheckedTypeInfo(t),
|
|
10
|
-
|
|
11
|
-
() => unchecked as any,
|
|
12
|
-
|
|
13
|
-
identityFn,
|
|
14
|
-
identityFn
|
|
15
|
-
) as any
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* A type that represents a given value that won't be type checked.
|
|
19
|
-
* This is basically a way to bail out of the runtime type checking system.
|
|
20
|
-
*
|
|
21
|
-
* Example:
|
|
22
|
-
* ```ts
|
|
23
|
-
* const uncheckedSomeModel = types.unchecked<SomeModel>()
|
|
24
|
-
* const anyType = types.unchecked<any>()
|
|
25
|
-
* const customUncheckedType = types.unchecked<(A & B) | C>()
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @template T Type of the value, or unkown if not given.
|
|
29
|
-
* @returns
|
|
30
|
-
*/
|
|
31
|
-
export function typesUnchecked<T = never>(): IdentityType<T> {
|
|
32
|
-
return unchecked
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* `types.unchecked` type info.
|
|
37
|
-
*/
|
|
38
|
-
export class UncheckedTypeInfo extends TypeInfo {
|
|
39
|
-
readonly kind = "unchecked"
|
|
40
|
-
}
|
|
1
|
+
import { identityFn } from "../../utils"
|
|
2
|
+
import type { IdentityType } from "../schemas"
|
|
3
|
+
import { TypeChecker, TypeCheckerBaseType, TypeInfo } from "../TypeChecker"
|
|
4
|
+
|
|
5
|
+
const unchecked: IdentityType<any> = new TypeChecker(
|
|
6
|
+
TypeCheckerBaseType.Any,
|
|
7
|
+
null,
|
|
8
|
+
() => "any",
|
|
9
|
+
(t) => new UncheckedTypeInfo(t),
|
|
10
|
+
|
|
11
|
+
() => unchecked as any,
|
|
12
|
+
|
|
13
|
+
identityFn,
|
|
14
|
+
identityFn
|
|
15
|
+
) as any
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A type that represents a given value that won't be type checked.
|
|
19
|
+
* This is basically a way to bail out of the runtime type checking system.
|
|
20
|
+
*
|
|
21
|
+
* Example:
|
|
22
|
+
* ```ts
|
|
23
|
+
* const uncheckedSomeModel = types.unchecked<SomeModel>()
|
|
24
|
+
* const anyType = types.unchecked<any>()
|
|
25
|
+
* const customUncheckedType = types.unchecked<(A & B) | C>()
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @template T Type of the value, or unkown if not given.
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
export function typesUnchecked<T = never>(): IdentityType<T> {
|
|
32
|
+
return unchecked
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* `types.unchecked` type info.
|
|
37
|
+
*/
|
|
38
|
+
export class UncheckedTypeInfo extends TypeInfo {
|
|
39
|
+
readonly kind = "unchecked"
|
|
40
|
+
}
|
package/src/utils/AnyFunction.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Any function type.
|
|
3
|
-
*/
|
|
4
|
-
export type AnyFunction = (...args: any[]) => any
|
|
1
|
+
/**
|
|
2
|
+
* Any function type.
|
|
3
|
+
*/
|
|
4
|
+
export type AnyFunction = (...args: any[]) => any
|
package/src/utils/ModelPool.ts
CHANGED
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import type { AnyModel } from "../model/BaseModel"
|
|
2
|
-
import { getModelIdPropertyName } from "../model/getModelMetadata"
|
|
3
|
-
import { modelIdKey, modelTypeKey } from "../model/metadata"
|
|
4
|
-
import { getSnapshotModelType, isModel } from "../model/utils"
|
|
5
|
-
import { ModelClass } from "../modelShared/BaseModelShared"
|
|
6
|
-
import { getModelInfoForName } from "../modelShared/modelInfo"
|
|
7
|
-
import { dataObjectParent } from "../parent/core"
|
|
8
|
-
import {
|
|
9
|
-
getDeepObjectChildren,
|
|
10
|
-
registerDeepObjectChildrenExtension,
|
|
11
|
-
} from "../parent/coreObjectChildren"
|
|
12
|
-
|
|
13
|
-
function byModelTypeAndIdKey(modelType: string, modelId: string) {
|
|
14
|
-
return JSON.stringify([modelType, modelId])
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class ModelPool {
|
|
18
|
-
private pool: ReadonlyMap<string, AnyModel>
|
|
19
|
-
|
|
20
|
-
constructor(root: object) {
|
|
21
|
-
// make sure we don't use the sub-data $ object
|
|
22
|
-
root = dataObjectParent.get(root) ?? root
|
|
23
|
-
|
|
24
|
-
this.pool = getDeepChildrenModels(getDeepObjectChildren(root))
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
findModelByTypeAndId(modelType: string, modelId: string | undefined): AnyModel | undefined {
|
|
28
|
-
return modelId ? this.pool.get(byModelTypeAndIdKey(modelType, modelId)) : undefined
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
findModelForSnapshot(sn: any): AnyModel | undefined {
|
|
32
|
-
const modelType = getSnapshotModelType(sn)
|
|
33
|
-
if (modelType === undefined) {
|
|
34
|
-
return undefined
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const modelInfo = getModelInfoForName(modelType)!
|
|
38
|
-
const modelIdPropertyName = getModelIdPropertyName(modelInfo.class as ModelClass<AnyModel>)
|
|
39
|
-
|
|
40
|
-
return modelIdPropertyName
|
|
41
|
-
? this.findModelByTypeAndId(modelType, (sn as any)[modelIdPropertyName])
|
|
42
|
-
: undefined
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const getDeepChildrenModels = registerDeepObjectChildrenExtension<Map<string, AnyModel>>({
|
|
47
|
-
initData() {
|
|
48
|
-
return new Map()
|
|
49
|
-
},
|
|
50
|
-
|
|
51
|
-
addNode(node, data) {
|
|
52
|
-
if (isModel(node)) {
|
|
53
|
-
const id = node[modelIdKey]
|
|
54
|
-
if (id) {
|
|
55
|
-
data.set(byModelTypeAndIdKey(node[modelTypeKey], id), node)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
})
|
|
1
|
+
import type { AnyModel } from "../model/BaseModel"
|
|
2
|
+
import { getModelIdPropertyName } from "../model/getModelMetadata"
|
|
3
|
+
import { modelIdKey, modelTypeKey } from "../model/metadata"
|
|
4
|
+
import { getSnapshotModelType, isModel } from "../model/utils"
|
|
5
|
+
import type { ModelClass } from "../modelShared/BaseModelShared"
|
|
6
|
+
import { getModelInfoForName } from "../modelShared/modelInfo"
|
|
7
|
+
import { dataObjectParent } from "../parent/core"
|
|
8
|
+
import {
|
|
9
|
+
getDeepObjectChildren,
|
|
10
|
+
registerDeepObjectChildrenExtension,
|
|
11
|
+
} from "../parent/coreObjectChildren"
|
|
12
|
+
|
|
13
|
+
function byModelTypeAndIdKey(modelType: string, modelId: string) {
|
|
14
|
+
return JSON.stringify([modelType, modelId])
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class ModelPool {
|
|
18
|
+
private pool: ReadonlyMap<string, AnyModel>
|
|
19
|
+
|
|
20
|
+
constructor(root: object) {
|
|
21
|
+
// make sure we don't use the sub-data $ object
|
|
22
|
+
root = dataObjectParent.get(root) ?? root
|
|
23
|
+
|
|
24
|
+
this.pool = getDeepChildrenModels(getDeepObjectChildren(root))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
findModelByTypeAndId(modelType: string, modelId: string | undefined): AnyModel | undefined {
|
|
28
|
+
return modelId ? this.pool.get(byModelTypeAndIdKey(modelType, modelId)) : undefined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
findModelForSnapshot(sn: any): AnyModel | undefined {
|
|
32
|
+
const modelType = getSnapshotModelType(sn)
|
|
33
|
+
if (modelType === undefined) {
|
|
34
|
+
return undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const modelInfo = getModelInfoForName(modelType)!
|
|
38
|
+
const modelIdPropertyName = getModelIdPropertyName(modelInfo.class as ModelClass<AnyModel>)
|
|
39
|
+
|
|
40
|
+
return modelIdPropertyName
|
|
41
|
+
? this.findModelByTypeAndId(modelType, (sn as any)[modelIdPropertyName])
|
|
42
|
+
: undefined
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const getDeepChildrenModels = registerDeepObjectChildrenExtension<Map<string, AnyModel>>({
|
|
47
|
+
initData() {
|
|
48
|
+
return new Map()
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
addNode(node, data) {
|
|
52
|
+
if (isModel(node)) {
|
|
53
|
+
const id = node[modelIdKey]
|
|
54
|
+
if (id) {
|
|
55
|
+
data.set(byModelTypeAndIdKey(node[modelTypeKey], id), node)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
})
|
package/src/utils/chainFns.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
export function chainFns<F extends Function>(...fns: (F | undefined)[]): F | undefined {
|
|
2
|
-
const definedFns = fns.filter((fn) => !!fn)
|
|
3
|
-
if (definedFns.length <= 0) {
|
|
4
|
-
return undefined
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
const chainedFn = (v: any, ...args: any[]) => {
|
|
8
|
-
let ret = v
|
|
9
|
-
|
|
10
|
-
for (let i = 0; i < definedFns.length; i++) {
|
|
11
|
-
ret = definedFns[i](ret, ...args)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return ret
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return chainedFn as unknown as F
|
|
18
|
-
}
|
|
1
|
+
export function chainFns<F extends Function>(...fns: (F | undefined)[]): F | undefined {
|
|
2
|
+
const definedFns = fns.filter((fn) => !!fn)
|
|
3
|
+
if (definedFns.length <= 0) {
|
|
4
|
+
return undefined
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const chainedFn = (v: any, ...args: any[]) => {
|
|
8
|
+
let ret = v
|
|
9
|
+
|
|
10
|
+
for (let i = 0; i < definedFns.length; i++) {
|
|
11
|
+
ret = definedFns[i](ret, ...args)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return ret
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return chainedFn as unknown as F
|
|
18
|
+
}
|