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,417 +1,427 @@
|
|
|
1
|
-
import { applySet } from "../action/applySet"
|
|
2
|
-
import { getCurrentActionContext } from "../action/context"
|
|
3
|
-
import { modelAction } from "../action/modelAction"
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import type {
|
|
10
|
-
import type {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import type {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
//
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
modelProps
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const
|
|
196
|
-
const
|
|
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
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
if (
|
|
353
|
-
const fn =
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
)
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
)
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
}
|
|
1
|
+
import { applySet } from "../action/applySet"
|
|
2
|
+
import { getCurrentActionContext } from "../action/context"
|
|
3
|
+
import { modelAction } from "../action/modelAction"
|
|
4
|
+
import {
|
|
5
|
+
type AnyDataModel,
|
|
6
|
+
BaseDataModel,
|
|
7
|
+
baseDataModelPropNames,
|
|
8
|
+
} from "../dataModel/BaseDataModel"
|
|
9
|
+
import type { DataModelConstructorOptions } from "../dataModel/DataModelConstructorOptions"
|
|
10
|
+
import type { DataModelMetadata } from "../dataModel/getDataModelMetadata"
|
|
11
|
+
import { getGlobalConfig } from "../globalConfig/globalConfig"
|
|
12
|
+
import { type AnyModel, BaseModel, baseModelPropNames } from "../model/BaseModel"
|
|
13
|
+
import type { ModelMetadata } from "../model/getModelMetadata"
|
|
14
|
+
import type { ModelConstructorOptions } from "../model/ModelConstructorOptions"
|
|
15
|
+
import { modelTypeKey } from "../model/metadata"
|
|
16
|
+
import { typesObject } from "../types/objectBased/typesObject"
|
|
17
|
+
import { typesString } from "../types/primitiveBased/typesPrimitive"
|
|
18
|
+
import type { AnyType } from "../types/schemas"
|
|
19
|
+
import { tProp } from "../types/tProp"
|
|
20
|
+
import { resolveStoredType } from "../types/utility/typesCodec"
|
|
21
|
+
import { typesUnchecked } from "../types/utility/typesUnchecked"
|
|
22
|
+
import { addHiddenProp, assertIsObject, failure, propNameToSetterName } from "../utils"
|
|
23
|
+
import { chainFns } from "../utils/chainFns"
|
|
24
|
+
import { withErrorPathSegment } from "../utils/errorDiagnostics"
|
|
25
|
+
import { type ModelClass, modelInitializedSymbol } from "./BaseModelShared"
|
|
26
|
+
import { type ModelClassInitializer, modelInitializersSymbol } from "./modelClassInitializer"
|
|
27
|
+
import { getInternalModelClassPropsInfo, setInternalModelClassPropsInfo } from "./modelPropsInfo"
|
|
28
|
+
import { modelMetadataSymbol, modelUnwrappedClassSymbol } from "./modelSymbols"
|
|
29
|
+
import {
|
|
30
|
+
type AnyModelProp,
|
|
31
|
+
getModelPropStoredDefaultValue,
|
|
32
|
+
type ModelProps,
|
|
33
|
+
noDefaultValue,
|
|
34
|
+
prop,
|
|
35
|
+
} from "./prop"
|
|
36
|
+
import { assertIsClassOrDataModelClass } from "./utils"
|
|
37
|
+
|
|
38
|
+
function createGetModelInstanceDataField<M extends AnyModel | AnyDataModel>(
|
|
39
|
+
modelProp: AnyModelProp,
|
|
40
|
+
modelPropName: string
|
|
41
|
+
): (this: M) => unknown {
|
|
42
|
+
const transformFn = modelProp._transform?.transform
|
|
43
|
+
|
|
44
|
+
if (!transformFn) {
|
|
45
|
+
// no need to use get since these vars always get on the initial $
|
|
46
|
+
return function (this) {
|
|
47
|
+
return this.$[modelPropName]
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const transformValue = (model: M, value: unknown) =>
|
|
52
|
+
transformFn(value, model, modelPropName, (newValue) => {
|
|
53
|
+
// use apply set instead to wrap it in an action
|
|
54
|
+
// set the $ object to set the original value directly
|
|
55
|
+
applySet(model.$, modelPropName, newValue)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
return function (this) {
|
|
59
|
+
// no need to use get since these vars always get on the initial $
|
|
60
|
+
const value = this.$[modelPropName]
|
|
61
|
+
return transformValue(this, value)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
type SetModelInstanceDataFieldFn = <M extends AnyModel | AnyDataModel>(
|
|
66
|
+
modelProp: AnyModelProp,
|
|
67
|
+
modelPropName: string,
|
|
68
|
+
model: M,
|
|
69
|
+
value: unknown
|
|
70
|
+
) => boolean | void
|
|
71
|
+
|
|
72
|
+
const setModelInstanceDataField: SetModelInstanceDataFieldFn = (
|
|
73
|
+
modelProp,
|
|
74
|
+
modelPropName,
|
|
75
|
+
model,
|
|
76
|
+
value
|
|
77
|
+
): void => {
|
|
78
|
+
if (modelProp._setter === "assign" && !getCurrentActionContext()) {
|
|
79
|
+
// use apply set instead to wrap it in an action
|
|
80
|
+
applySet(model, modelPropName as any, value)
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (modelProp._setterValueTransform) {
|
|
85
|
+
value = modelProp._setterValueTransform(value)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let untransformedValue = modelProp._transform
|
|
89
|
+
? withErrorPathSegment(modelPropName, () =>
|
|
90
|
+
modelProp._transform!.untransform(value, model, modelPropName)
|
|
91
|
+
)
|
|
92
|
+
: value
|
|
93
|
+
|
|
94
|
+
// apply default value if applicable
|
|
95
|
+
if (untransformedValue == null) {
|
|
96
|
+
const defaultValue = getModelPropStoredDefaultValue(modelProp, model, modelPropName)
|
|
97
|
+
if (defaultValue !== noDefaultValue) {
|
|
98
|
+
untransformedValue = defaultValue
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// no need to use set since these vars always get on the initial $
|
|
103
|
+
model.$[modelPropName] = untransformedValue
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const setModelInstanceDataFieldWithPrecheck: SetModelInstanceDataFieldFn = (
|
|
107
|
+
modelProp,
|
|
108
|
+
modelPropName,
|
|
109
|
+
model,
|
|
110
|
+
value
|
|
111
|
+
): boolean => {
|
|
112
|
+
// hack to only permit setting these values once fully constructed
|
|
113
|
+
// this is to ignore abstract properties being set by babel
|
|
114
|
+
// see https://github.com/xaviergonz/mobx-keystone/issues/18
|
|
115
|
+
if (!(model as any)[modelInitializedSymbol]) {
|
|
116
|
+
return false
|
|
117
|
+
}
|
|
118
|
+
setModelInstanceDataField(modelProp, modelPropName, model, value)
|
|
119
|
+
return true
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const defaultModelIdGenerator = () => getGlobalConfig().modelIdGenerator()
|
|
123
|
+
const tPropForDefaultId = tProp(typesString, defaultModelIdGenerator)
|
|
124
|
+
tPropForDefaultId._isId = true
|
|
125
|
+
const propForDefaultId = prop(defaultModelIdGenerator)
|
|
126
|
+
propForDefaultId._isId = true
|
|
127
|
+
|
|
128
|
+
type FromSnapshotProcessorFn = (sn: any) => any
|
|
129
|
+
type ToSnapshotProcessorFn = (sn: any, instance: any) => any
|
|
130
|
+
|
|
131
|
+
export function sharedInternalModel<
|
|
132
|
+
TProps extends ModelProps,
|
|
133
|
+
TBaseModel extends AnyModel | AnyDataModel,
|
|
134
|
+
>({
|
|
135
|
+
modelProps,
|
|
136
|
+
baseModel,
|
|
137
|
+
type,
|
|
138
|
+
valueType,
|
|
139
|
+
fromSnapshotProcessor,
|
|
140
|
+
toSnapshotProcessor,
|
|
141
|
+
}: {
|
|
142
|
+
modelProps: TProps
|
|
143
|
+
baseModel: ModelClass<TBaseModel> | undefined
|
|
144
|
+
type: "class" | "data"
|
|
145
|
+
valueType: boolean
|
|
146
|
+
fromSnapshotProcessor: FromSnapshotProcessorFn | undefined
|
|
147
|
+
toSnapshotProcessor: ToSnapshotProcessorFn | undefined
|
|
148
|
+
}): any {
|
|
149
|
+
assertIsObject(modelProps, "modelProps")
|
|
150
|
+
|
|
151
|
+
// make sure we avoid prototype pollution
|
|
152
|
+
modelProps = Object.assign(Object.create(null), modelProps)
|
|
153
|
+
|
|
154
|
+
if (baseModel) {
|
|
155
|
+
assertIsClassOrDataModelClass(baseModel, "baseModel")
|
|
156
|
+
|
|
157
|
+
// if the baseModel is wrapped with the model decorator get the original one
|
|
158
|
+
const unwrappedClass = (baseModel as any)[modelUnwrappedClassSymbol]
|
|
159
|
+
if (unwrappedClass) {
|
|
160
|
+
baseModel = unwrappedClass
|
|
161
|
+
assertIsClassOrDataModelClass(baseModel, "baseModel")
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const composedModelProps: ModelProps = modelProps
|
|
166
|
+
let baseModelProps: ModelProps | undefined
|
|
167
|
+
if (baseModel) {
|
|
168
|
+
baseModelProps = getInternalModelClassPropsInfo(baseModel)
|
|
169
|
+
for (const oldModelPropKey of Object.keys(baseModelProps)) {
|
|
170
|
+
if (!modelProps[oldModelPropKey]) {
|
|
171
|
+
composedModelProps[oldModelPropKey] = baseModelProps[oldModelPropKey]
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// look for id keys
|
|
177
|
+
const idKeys = Object.keys(composedModelProps).filter((k) => {
|
|
178
|
+
const p = composedModelProps[k]
|
|
179
|
+
return p._isId
|
|
180
|
+
})
|
|
181
|
+
if (type === "class") {
|
|
182
|
+
if (idKeys.length > 1) {
|
|
183
|
+
throw failure(`expected at most one idProp but got many: ${JSON.stringify(idKeys)}`)
|
|
184
|
+
}
|
|
185
|
+
} else if (idKeys.length > 0) {
|
|
186
|
+
throw failure(`expected no idProp but got some: ${JSON.stringify(idKeys)}`)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const needsTypeChecker = Object.values(composedModelProps).some((mp) => !!mp._typeChecker)
|
|
190
|
+
|
|
191
|
+
// transform id keys (only one really)
|
|
192
|
+
let idKey: string | undefined
|
|
193
|
+
if (idKeys.length > 0) {
|
|
194
|
+
idKey = idKeys[0]
|
|
195
|
+
const idProp = composedModelProps[idKey]
|
|
196
|
+
const idPropGenerator = idProp._idGenerator
|
|
197
|
+
const baseIdProp = baseModelProps?.[idKey]
|
|
198
|
+
const baseIdPropGenerator = baseIdProp?._idGenerator
|
|
199
|
+
const isOverridingBaseIdProp = !!baseIdProp?._isId && Object.hasOwn(modelProps, idKey)
|
|
200
|
+
if (isOverridingBaseIdProp && baseIdPropGenerator !== idPropGenerator) {
|
|
201
|
+
throw failure(
|
|
202
|
+
`expected same idProp.withGenerator function when overriding a base idProp, but got different references`
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
const resolvedIdGenerator = idPropGenerator ?? baseIdPropGenerator
|
|
206
|
+
const effectiveModelIdGenerator = resolvedIdGenerator ?? defaultModelIdGenerator
|
|
207
|
+
let baseProp: AnyModelProp
|
|
208
|
+
if (effectiveModelIdGenerator === defaultModelIdGenerator) {
|
|
209
|
+
baseProp = needsTypeChecker ? tPropForDefaultId : propForDefaultId
|
|
210
|
+
} else {
|
|
211
|
+
baseProp = needsTypeChecker
|
|
212
|
+
? tProp(typesString, effectiveModelIdGenerator)
|
|
213
|
+
: prop(effectiveModelIdGenerator)
|
|
214
|
+
baseProp._isId = true
|
|
215
|
+
}
|
|
216
|
+
if (idProp._setterValueTransform) {
|
|
217
|
+
baseProp = baseProp.withSetter(idProp._setterValueTransform)
|
|
218
|
+
} else {
|
|
219
|
+
switch (idProp._setter) {
|
|
220
|
+
case true:
|
|
221
|
+
baseProp = baseProp.withSetter()
|
|
222
|
+
break
|
|
223
|
+
case "assign":
|
|
224
|
+
baseProp = baseProp.withSetter("assign")
|
|
225
|
+
break
|
|
226
|
+
default:
|
|
227
|
+
break
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
baseProp._idGenerator = resolvedIdGenerator
|
|
231
|
+
composedModelProps[idKey] = baseProp
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// create type checker if needed
|
|
235
|
+
let dataTypeChecker: AnyType | undefined
|
|
236
|
+
if (needsTypeChecker) {
|
|
237
|
+
const typeCheckerObj: {
|
|
238
|
+
[k: string]: any
|
|
239
|
+
} = {}
|
|
240
|
+
for (const [k, mp] of Object.entries(composedModelProps)) {
|
|
241
|
+
typeCheckerObj[k] = mp._typeChecker ? resolveStoredType(mp._typeChecker) : typesUnchecked()
|
|
242
|
+
}
|
|
243
|
+
dataTypeChecker = typesObject(() => typeCheckerObj)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const base: any = baseModel ?? (type === "class" ? BaseModel : BaseDataModel)
|
|
247
|
+
const basePropNames = type === "class" ? baseModelPropNames : baseDataModelPropNames
|
|
248
|
+
|
|
249
|
+
let propsToDeleteFromBase: string[] | undefined
|
|
250
|
+
|
|
251
|
+
// we use this weird hack rather than just class CustomBaseModel extends base {}
|
|
252
|
+
// in order to work around problems with ES5 classes extending ES6 classes
|
|
253
|
+
// see https://github.com/xaviergonz/mobx-keystone/issues/15
|
|
254
|
+
function ThisModel(
|
|
255
|
+
this: any,
|
|
256
|
+
initialData: any,
|
|
257
|
+
constructorOptions?: ModelConstructorOptions | DataModelConstructorOptions
|
|
258
|
+
) {
|
|
259
|
+
const modelClass = constructorOptions?.modelClass ?? this.constructor
|
|
260
|
+
const baseModel = new base(initialData, {
|
|
261
|
+
...constructorOptions,
|
|
262
|
+
modelClass,
|
|
263
|
+
} as ModelConstructorOptions & DataModelConstructorOptions)
|
|
264
|
+
|
|
265
|
+
// add prop, it is faster than having to go to the root of the prototype to not find it
|
|
266
|
+
addHiddenProp(baseModel, modelInitializedSymbol, false, true)
|
|
267
|
+
|
|
268
|
+
// make sure abstract classes do not override prototype props
|
|
269
|
+
if (!propsToDeleteFromBase) {
|
|
270
|
+
propsToDeleteFromBase = Object.keys(modelProps).filter(
|
|
271
|
+
(p) => !basePropNames.has(p as any) && Object.hasOwn(baseModel, p)
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
propsToDeleteFromBase.forEach((prop) => {
|
|
276
|
+
delete baseModel[prop]
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
return baseModel
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// copy static props from base
|
|
283
|
+
Object.assign(ThisModel, base)
|
|
284
|
+
delete (ThisModel as any)[modelUnwrappedClassSymbol]
|
|
285
|
+
|
|
286
|
+
const initializers: ModelClassInitializer[] = base[modelInitializersSymbol]
|
|
287
|
+
if (initializers) {
|
|
288
|
+
ThisModel[modelInitializersSymbol] = initializers.slice()
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
setInternalModelClassPropsInfo(ThisModel as any, composedModelProps)
|
|
292
|
+
|
|
293
|
+
if (type === "class") {
|
|
294
|
+
const metadata: ModelMetadata = {
|
|
295
|
+
dataType: dataTypeChecker,
|
|
296
|
+
modelIdProperty: idKey,
|
|
297
|
+
valueType,
|
|
298
|
+
}
|
|
299
|
+
ThisModel[modelMetadataSymbol] = metadata
|
|
300
|
+
} else {
|
|
301
|
+
const metadata: DataModelMetadata = {
|
|
302
|
+
dataType: dataTypeChecker,
|
|
303
|
+
}
|
|
304
|
+
ThisModel[modelMetadataSymbol] = metadata
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
ThisModel.prototype = Object.create(base.prototype)
|
|
308
|
+
ThisModel.prototype.constructor = ThisModel
|
|
309
|
+
|
|
310
|
+
let setFn: SetModelInstanceDataFieldFn = (modelProp, modelPropName, model, value) => {
|
|
311
|
+
if (setModelInstanceDataFieldWithPrecheck(modelProp, modelPropName, model, value)) {
|
|
312
|
+
setFn = setModelInstanceDataField
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
for (const [propName, propData] of Object.entries(modelProps)) {
|
|
317
|
+
if (!(basePropNames as Set<string>).has(propName)) {
|
|
318
|
+
const get = createGetModelInstanceDataField(propData, propName)
|
|
319
|
+
|
|
320
|
+
Object.defineProperty(ThisModel.prototype, propName, {
|
|
321
|
+
get,
|
|
322
|
+
set(value: unknown) {
|
|
323
|
+
setFn(propData, propName, this, value)
|
|
324
|
+
},
|
|
325
|
+
enumerable: true,
|
|
326
|
+
configurable: false,
|
|
327
|
+
})
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (propData._setter === true) {
|
|
331
|
+
const setterName = propNameToSetterName(propName)
|
|
332
|
+
|
|
333
|
+
if (!(basePropNames as Set<string>).has(setterName)) {
|
|
334
|
+
const newPropDescriptor: any = modelAction(ThisModel.prototype, setterName, {
|
|
335
|
+
value: function (this: any, value: any) {
|
|
336
|
+
this[propName] = value
|
|
337
|
+
},
|
|
338
|
+
writable: true,
|
|
339
|
+
enumerable: false,
|
|
340
|
+
configurable: false,
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
Object.defineProperty(ThisModel.prototype, setterName, newPropDescriptor)
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const modelPropsFromSnapshotProcessor = getModelPropsFromSnapshotProcessor(composedModelProps)
|
|
349
|
+
|
|
350
|
+
const modelPropsToSnapshotProcessor = getModelPropsToSnapshotProcessor(composedModelProps)
|
|
351
|
+
|
|
352
|
+
if (fromSnapshotProcessor) {
|
|
353
|
+
const fn = fromSnapshotProcessor
|
|
354
|
+
fromSnapshotProcessor = (sn) => {
|
|
355
|
+
return {
|
|
356
|
+
...fn(sn),
|
|
357
|
+
[modelTypeKey]: sn[modelTypeKey],
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (toSnapshotProcessor) {
|
|
363
|
+
const fn = toSnapshotProcessor
|
|
364
|
+
toSnapshotProcessor = (sn, modelInstance) => {
|
|
365
|
+
if (modelInstance && !(modelInstance as any)[modelInitializedSymbol]) {
|
|
366
|
+
return sn
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return {
|
|
370
|
+
...fn(sn, modelInstance),
|
|
371
|
+
[modelTypeKey]: sn[modelTypeKey],
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
ThisModel.fromSnapshotProcessor = chainFns(fromSnapshotProcessor, modelPropsFromSnapshotProcessor)
|
|
377
|
+
ThisModel.toSnapshotProcessor = chainFns(modelPropsToSnapshotProcessor, toSnapshotProcessor)
|
|
378
|
+
|
|
379
|
+
return ThisModel
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function getModelPropsFromSnapshotProcessor(
|
|
383
|
+
composedModelProps: ModelProps
|
|
384
|
+
): FromSnapshotProcessorFn | undefined {
|
|
385
|
+
const propsWithFromSnapshotProcessor = Object.entries(composedModelProps).filter(
|
|
386
|
+
([_propName, propData]) => propData._fromSnapshotProcessor
|
|
387
|
+
)
|
|
388
|
+
if (propsWithFromSnapshotProcessor.length <= 0) {
|
|
389
|
+
return undefined
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return (sn) => {
|
|
393
|
+
const newSn = { ...sn }
|
|
394
|
+
for (const [propName, propData] of propsWithFromSnapshotProcessor) {
|
|
395
|
+
if (propData._fromSnapshotProcessor) {
|
|
396
|
+
newSn[propName] = withErrorPathSegment(propName, () =>
|
|
397
|
+
propData._fromSnapshotProcessor!(sn[propName])
|
|
398
|
+
)
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return newSn
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function getModelPropsToSnapshotProcessor(
|
|
406
|
+
composedModelProps: ModelProps
|
|
407
|
+
): ToSnapshotProcessorFn | undefined {
|
|
408
|
+
const propsWithToSnapshotProcessor = Object.entries(composedModelProps).filter(
|
|
409
|
+
([_propName, propData]) => propData._toSnapshotProcessor
|
|
410
|
+
)
|
|
411
|
+
|
|
412
|
+
if (propsWithToSnapshotProcessor.length <= 0) {
|
|
413
|
+
return undefined
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
return (sn) => {
|
|
417
|
+
const newSn = { ...sn }
|
|
418
|
+
for (const [propName, propData] of propsWithToSnapshotProcessor) {
|
|
419
|
+
if (propData._toSnapshotProcessor) {
|
|
420
|
+
newSn[propName] = withErrorPathSegment(propName, () =>
|
|
421
|
+
propData._toSnapshotProcessor!(sn[propName])
|
|
422
|
+
)
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return newSn
|
|
426
|
+
}
|
|
427
|
+
}
|