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,221 +1,220 @@
|
|
|
1
|
-
import type { O } from "ts-toolbelt"
|
|
2
|
-
import type { AnyModel } from "../../model/BaseModel"
|
|
3
|
-
import { getModelMetadata } from "../../model/getModelMetadata"
|
|
4
|
-
import { modelTypeKey } from "../../model/metadata"
|
|
5
|
-
import { isModelClass } from "../../model/utils"
|
|
6
|
-
import type { ModelClass } from "../../modelShared/BaseModelShared"
|
|
7
|
-
import { modelInfoByClass } from "../../modelShared/modelInfo"
|
|
8
|
-
import { getInternalModelClassPropsInfo } from "../../modelShared/modelPropsInfo"
|
|
9
|
-
import { noDefaultValue } from "../../modelShared/prop"
|
|
10
|
-
import { isObject, lazy } from "../../utils"
|
|
11
|
-
import { getTypeInfo } from "../getTypeInfo"
|
|
12
|
-
import { registerStandardTypeResolver, resolveTypeChecker } from "../resolveTypeChecker"
|
|
13
|
-
import type { AnyStandardType, ModelType } from "../schemas"
|
|
14
|
-
import { TypeCheckError } from "../TypeCheckError"
|
|
15
|
-
import {
|
|
16
|
-
lateTypeChecker,
|
|
17
|
-
TypeChecker,
|
|
18
|
-
TypeCheckerBaseType,
|
|
19
|
-
TypeInfo,
|
|
20
|
-
TypeInfoGen,
|
|
21
|
-
} from "../TypeChecker"
|
|
22
|
-
|
|
23
|
-
const cachedModelTypeChecker = new WeakMap<ModelClass<AnyModel>, TypeChecker>()
|
|
24
|
-
const modelStandardTypes = new WeakSet<object>()
|
|
25
|
-
|
|
26
|
-
type _Class<T> = abstract new (...args: any[]) => T
|
|
27
|
-
type _ClassOrObject<M, K> = K extends M ? object : _Class<K> | (() => _Class<K>)
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* A type that represents a model. The type referenced in the model decorator will be used for type checking.
|
|
31
|
-
*
|
|
32
|
-
* Example:
|
|
33
|
-
* ```ts
|
|
34
|
-
* const someModelType = types.model(SomeModel)
|
|
35
|
-
* // or for recursive models
|
|
36
|
-
* const someModelType = types.model<SomeModel>(() => SomeModel)
|
|
37
|
-
* ```
|
|
38
|
-
*
|
|
39
|
-
* @template M Model type.
|
|
40
|
-
* @param modelClass Model class.
|
|
41
|
-
* @returns
|
|
42
|
-
*/
|
|
43
|
-
export function typesModel<M = never, K = M>(modelClass: _ClassOrObject<M, K>): ModelType<K> {
|
|
44
|
-
// if we type it any stronger then recursive defs and so on stop working
|
|
45
|
-
|
|
46
|
-
if (!isModelClass(modelClass) && typeof modelClass === "function") {
|
|
47
|
-
// resolve later
|
|
48
|
-
const modelClassFn = modelClass as () => ModelClass<AnyModel>
|
|
49
|
-
const typeInfoGen: TypeInfoGen = (t) => new ModelTypeInfo(t, modelClassFn())
|
|
50
|
-
const tc = lateTypeChecker(() => typesModel(modelClassFn()) as any, typeInfoGen) as any
|
|
51
|
-
modelStandardTypes.add(tc as object)
|
|
52
|
-
return tc
|
|
53
|
-
} else {
|
|
54
|
-
const modelClazz: ModelClass<AnyModel> = modelClass as any
|
|
55
|
-
|
|
56
|
-
const cachedTypeChecker = cachedModelTypeChecker.get(modelClazz)
|
|
57
|
-
if (cachedTypeChecker) {
|
|
58
|
-
return cachedTypeChecker as any
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const typeInfoGen: TypeInfoGen = (t) => new ModelTypeInfo(t, modelClazz)
|
|
62
|
-
|
|
63
|
-
const tc = lateTypeChecker(() => {
|
|
64
|
-
const modelInfo = modelInfoByClass.get(modelClazz)!
|
|
65
|
-
const typeName = `Model(${modelInfo.name})`
|
|
66
|
-
|
|
67
|
-
const dataTypeChecker = getModelMetadata(modelClazz).dataType
|
|
68
|
-
const resolvedDataTypeChecker = dataTypeChecker
|
|
69
|
-
? resolveTypeChecker(dataTypeChecker)
|
|
70
|
-
: undefined
|
|
71
|
-
|
|
72
|
-
const thisTc: TypeChecker = new TypeChecker(
|
|
73
|
-
TypeCheckerBaseType.Object,
|
|
74
|
-
|
|
75
|
-
(value, path, typeCheckedValue) => {
|
|
76
|
-
if (!(value instanceof modelClazz)) {
|
|
77
|
-
return new TypeCheckError({
|
|
78
|
-
path,
|
|
79
|
-
expectedTypeName: typeName,
|
|
80
|
-
actualValue: value,
|
|
81
|
-
typeCheckedValue,
|
|
82
|
-
})
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (resolvedDataTypeChecker) {
|
|
86
|
-
return resolvedDataTypeChecker.check(value.$, path, typeCheckedValue)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return null
|
|
90
|
-
},
|
|
91
|
-
() => typeName,
|
|
92
|
-
typeInfoGen,
|
|
93
|
-
|
|
94
|
-
(value) => {
|
|
95
|
-
if (!isObject(value)) {
|
|
96
|
-
return null
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (value[modelTypeKey] !== undefined) {
|
|
100
|
-
// fast check
|
|
101
|
-
return value[modelTypeKey] === modelInfo.name ? thisTc : null
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (resolvedDataTypeChecker) {
|
|
105
|
-
return resolvedDataTypeChecker.snapshotType(value) ? thisTc : null
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// not enough info to be able to tell
|
|
109
|
-
return null
|
|
110
|
-
},
|
|
111
|
-
|
|
112
|
-
// No withErrorPathSegment wrapping needed here — model snapshot processors
|
|
113
|
-
// delegate to the data type checker, which handles path segments at the property level.
|
|
114
|
-
(sn) => {
|
|
115
|
-
if (sn[modelTypeKey]) {
|
|
116
|
-
return sn
|
|
117
|
-
} else {
|
|
118
|
-
return {
|
|
119
|
-
...sn,
|
|
120
|
-
[modelTypeKey]: modelInfo.name,
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
},
|
|
124
|
-
|
|
125
|
-
(sn) => sn
|
|
126
|
-
)
|
|
127
|
-
|
|
128
|
-
return thisTc
|
|
129
|
-
}, typeInfoGen) as any
|
|
130
|
-
|
|
131
|
-
cachedModelTypeChecker.set(modelClazz, tc)
|
|
132
|
-
modelStandardTypes.add(tc as object)
|
|
133
|
-
|
|
134
|
-
return tc
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* `types.model` type info for a model props.
|
|
140
|
-
*/
|
|
141
|
-
export interface ModelTypeInfoProps {
|
|
142
|
-
readonly [propName: string]: Readonly<{
|
|
143
|
-
type: AnyStandardType | undefined
|
|
144
|
-
typeInfo: TypeInfo | undefined
|
|
145
|
-
hasDefault: boolean
|
|
146
|
-
default: any
|
|
147
|
-
}>
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* `types.model` type info.
|
|
152
|
-
*/
|
|
153
|
-
export class ModelTypeInfo extends TypeInfo {
|
|
154
|
-
readonly kind = "model"
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
let
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
1
|
+
import type { O } from "ts-toolbelt"
|
|
2
|
+
import type { AnyModel } from "../../model/BaseModel"
|
|
3
|
+
import { getModelMetadata } from "../../model/getModelMetadata"
|
|
4
|
+
import { modelTypeKey } from "../../model/metadata"
|
|
5
|
+
import { isModelClass } from "../../model/utils"
|
|
6
|
+
import type { ModelClass } from "../../modelShared/BaseModelShared"
|
|
7
|
+
import { modelInfoByClass } from "../../modelShared/modelInfo"
|
|
8
|
+
import { getInternalModelClassPropsInfo } from "../../modelShared/modelPropsInfo"
|
|
9
|
+
import { noDefaultValue } from "../../modelShared/prop"
|
|
10
|
+
import { isObject, lazy } from "../../utils"
|
|
11
|
+
import { getTypeInfo } from "../getTypeInfo"
|
|
12
|
+
import { registerStandardTypeResolver, resolveTypeChecker } from "../resolveTypeChecker"
|
|
13
|
+
import type { AnyStandardType, ModelType } from "../schemas"
|
|
14
|
+
import { TypeCheckError } from "../TypeCheckError"
|
|
15
|
+
import {
|
|
16
|
+
lateTypeChecker,
|
|
17
|
+
TypeChecker,
|
|
18
|
+
TypeCheckerBaseType,
|
|
19
|
+
TypeInfo,
|
|
20
|
+
type TypeInfoGen,
|
|
21
|
+
} from "../TypeChecker"
|
|
22
|
+
|
|
23
|
+
const cachedModelTypeChecker = new WeakMap<ModelClass<AnyModel>, TypeChecker>()
|
|
24
|
+
const modelStandardTypes = new WeakSet<object>()
|
|
25
|
+
|
|
26
|
+
type _Class<T> = abstract new (...args: any[]) => T
|
|
27
|
+
type _ClassOrObject<M, K> = K extends M ? object : _Class<K> | (() => _Class<K>)
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A type that represents a model. The type referenced in the model decorator will be used for type checking.
|
|
31
|
+
*
|
|
32
|
+
* Example:
|
|
33
|
+
* ```ts
|
|
34
|
+
* const someModelType = types.model(SomeModel)
|
|
35
|
+
* // or for recursive models
|
|
36
|
+
* const someModelType = types.model<SomeModel>(() => SomeModel)
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @template M Model type.
|
|
40
|
+
* @param modelClass Model class.
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
export function typesModel<M = never, K = M>(modelClass: _ClassOrObject<M, K>): ModelType<K> {
|
|
44
|
+
// if we type it any stronger then recursive defs and so on stop working
|
|
45
|
+
|
|
46
|
+
if (!isModelClass(modelClass) && typeof modelClass === "function") {
|
|
47
|
+
// resolve later
|
|
48
|
+
const modelClassFn = modelClass as () => ModelClass<AnyModel>
|
|
49
|
+
const typeInfoGen: TypeInfoGen = (t) => new ModelTypeInfo(t, modelClassFn())
|
|
50
|
+
const tc = lateTypeChecker(() => typesModel(modelClassFn()) as any, typeInfoGen) as any
|
|
51
|
+
modelStandardTypes.add(tc as object)
|
|
52
|
+
return tc
|
|
53
|
+
} else {
|
|
54
|
+
const modelClazz: ModelClass<AnyModel> = modelClass as any
|
|
55
|
+
|
|
56
|
+
const cachedTypeChecker = cachedModelTypeChecker.get(modelClazz)
|
|
57
|
+
if (cachedTypeChecker) {
|
|
58
|
+
return cachedTypeChecker as any
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const typeInfoGen: TypeInfoGen = (t) => new ModelTypeInfo(t, modelClazz)
|
|
62
|
+
|
|
63
|
+
const tc = lateTypeChecker(() => {
|
|
64
|
+
const modelInfo = modelInfoByClass.get(modelClazz)!
|
|
65
|
+
const typeName = `Model(${modelInfo.name})`
|
|
66
|
+
|
|
67
|
+
const dataTypeChecker = getModelMetadata(modelClazz).dataType
|
|
68
|
+
const resolvedDataTypeChecker = dataTypeChecker
|
|
69
|
+
? resolveTypeChecker(dataTypeChecker)
|
|
70
|
+
: undefined
|
|
71
|
+
|
|
72
|
+
const thisTc: TypeChecker = new TypeChecker(
|
|
73
|
+
TypeCheckerBaseType.Object,
|
|
74
|
+
|
|
75
|
+
(value, path, typeCheckedValue) => {
|
|
76
|
+
if (!(value instanceof modelClazz)) {
|
|
77
|
+
return new TypeCheckError({
|
|
78
|
+
path,
|
|
79
|
+
expectedTypeName: typeName,
|
|
80
|
+
actualValue: value,
|
|
81
|
+
typeCheckedValue,
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (resolvedDataTypeChecker) {
|
|
86
|
+
return resolvedDataTypeChecker.check(value.$, path, typeCheckedValue)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return null
|
|
90
|
+
},
|
|
91
|
+
() => typeName,
|
|
92
|
+
typeInfoGen,
|
|
93
|
+
|
|
94
|
+
(value) => {
|
|
95
|
+
if (!isObject(value)) {
|
|
96
|
+
return null
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (value[modelTypeKey] !== undefined) {
|
|
100
|
+
// fast check
|
|
101
|
+
return value[modelTypeKey] === modelInfo.name ? thisTc : null
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (resolvedDataTypeChecker) {
|
|
105
|
+
return resolvedDataTypeChecker.snapshotType(value) ? thisTc : null
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// not enough info to be able to tell
|
|
109
|
+
return null
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
// No withErrorPathSegment wrapping needed here — model snapshot processors
|
|
113
|
+
// delegate to the data type checker, which handles path segments at the property level.
|
|
114
|
+
(sn) => {
|
|
115
|
+
if (sn[modelTypeKey]) {
|
|
116
|
+
return sn
|
|
117
|
+
} else {
|
|
118
|
+
return {
|
|
119
|
+
...sn,
|
|
120
|
+
[modelTypeKey]: modelInfo.name,
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
(sn) => sn
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
return thisTc
|
|
129
|
+
}, typeInfoGen) as any
|
|
130
|
+
|
|
131
|
+
cachedModelTypeChecker.set(modelClazz, tc)
|
|
132
|
+
modelStandardTypes.add(tc as object)
|
|
133
|
+
|
|
134
|
+
return tc
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* `types.model` type info for a model props.
|
|
140
|
+
*/
|
|
141
|
+
export interface ModelTypeInfoProps {
|
|
142
|
+
readonly [propName: string]: Readonly<{
|
|
143
|
+
type: AnyStandardType | undefined
|
|
144
|
+
typeInfo: TypeInfo | undefined
|
|
145
|
+
hasDefault: boolean
|
|
146
|
+
default: any
|
|
147
|
+
}>
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* `types.model` type info.
|
|
152
|
+
*/
|
|
153
|
+
export class ModelTypeInfo extends TypeInfo {
|
|
154
|
+
readonly kind = "model"
|
|
155
|
+
readonly modelClass: ModelClass<AnyModel>
|
|
156
|
+
|
|
157
|
+
private _props = lazy(() => {
|
|
158
|
+
const objSchema = getInternalModelClassPropsInfo(this.modelClass)
|
|
159
|
+
|
|
160
|
+
const propTypes: O.Writable<ModelTypeInfoProps> = {}
|
|
161
|
+
Object.keys(objSchema).forEach((propName) => {
|
|
162
|
+
const propData = objSchema[propName]
|
|
163
|
+
|
|
164
|
+
const type = propData._typeChecker
|
|
165
|
+
|
|
166
|
+
let typeInfo: TypeInfo | undefined
|
|
167
|
+
if (type) {
|
|
168
|
+
typeInfo = getTypeInfo(type)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let hasDefault = false
|
|
172
|
+
let defaultValue: any
|
|
173
|
+
if (propData._defaultFn !== noDefaultValue) {
|
|
174
|
+
defaultValue = propData._defaultFn
|
|
175
|
+
hasDefault = true
|
|
176
|
+
} else if (propData._defaultValue !== noDefaultValue) {
|
|
177
|
+
defaultValue = propData._defaultValue
|
|
178
|
+
hasDefault = true
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
propTypes[propName] = {
|
|
182
|
+
type,
|
|
183
|
+
typeInfo,
|
|
184
|
+
hasDefault,
|
|
185
|
+
default: defaultValue,
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
return propTypes
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
get props(): ModelTypeInfoProps {
|
|
192
|
+
return this._props()
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
get modelType(): string {
|
|
196
|
+
const modelInfo = modelInfoByClass.get(this.modelClass)!
|
|
197
|
+
return modelInfo.name
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
constructor(thisType: AnyStandardType, modelClass: ModelClass<AnyModel>) {
|
|
201
|
+
super(thisType)
|
|
202
|
+
this.modelClass = modelClass
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @internal
|
|
208
|
+
*/
|
|
209
|
+
export function registerModelStandardTypeResolver() {
|
|
210
|
+
registerStandardTypeResolver((v) => (isModelClass(v) ? typesModel(v) : undefined))
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* @internal
|
|
215
|
+
*/
|
|
216
|
+
export function isModelStandardType(value: unknown): boolean {
|
|
217
|
+
return typeof value === "object" || typeof value === "function"
|
|
218
|
+
? modelStandardTypes.has(value as object)
|
|
219
|
+
: false
|
|
220
|
+
}
|