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/utils/index.ts
CHANGED
|
@@ -1,372 +1,443 @@
|
|
|
1
|
-
import * as mobx from "mobx"
|
|
2
|
-
import {
|
|
3
|
-
IObservableArray,
|
|
4
|
-
isObservableArray,
|
|
5
|
-
isObservableMap,
|
|
6
|
-
isObservableObject,
|
|
7
|
-
isObservableSet,
|
|
8
|
-
ObservableMap,
|
|
9
|
-
ObservableSet,
|
|
10
|
-
} from "mobx"
|
|
11
|
-
import { JSONPrimitiveValue, PrimitiveValue } from "./types"
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* A mobx-keystone error.
|
|
15
|
-
*/
|
|
16
|
-
export class MobxKeystoneError extends Error {
|
|
17
|
-
constructor(msg: string) {
|
|
18
|
-
super(msg)
|
|
19
|
-
|
|
20
|
-
// Set the prototype explicitly.
|
|
21
|
-
Object.setPrototypeOf(this, new.target.prototype)
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @internal
|
|
27
|
-
*/
|
|
28
|
-
export function failure(msg: string) {
|
|
29
|
-
return new MobxKeystoneError(msg)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const writableHiddenPropDescriptor: PropertyDescriptor = {
|
|
33
|
-
enumerable: false,
|
|
34
|
-
writable: true,
|
|
35
|
-
configurable: false,
|
|
36
|
-
value: undefined,
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* @internal
|
|
41
|
-
*/
|
|
42
|
-
export function addHiddenProp(object: any, propName: PropertyKey, value: any, writable = true) {
|
|
43
|
-
if (writable) {
|
|
44
|
-
Object.defineProperty(object, propName, writableHiddenPropDescriptor)
|
|
45
|
-
object[propName] = value
|
|
46
|
-
} else {
|
|
47
|
-
Object.defineProperty(object, propName, {
|
|
48
|
-
enumerable: false,
|
|
49
|
-
writable,
|
|
50
|
-
configurable: true,
|
|
51
|
-
value,
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @internal
|
|
58
|
-
*/
|
|
59
|
-
export function makePropReadonly<T>(object: T, propName: keyof T, enumerable: boolean) {
|
|
60
|
-
const propDesc = Object.getOwnPropertyDescriptor(object, propName)
|
|
61
|
-
if (propDesc) {
|
|
62
|
-
propDesc.enumerable = enumerable
|
|
63
|
-
if (propDesc.get) {
|
|
64
|
-
delete propDesc.set
|
|
65
|
-
} else {
|
|
66
|
-
propDesc.writable = false
|
|
67
|
-
}
|
|
68
|
-
Object.defineProperty(object, propName, propDesc)
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* @internal
|
|
74
|
-
*/
|
|
75
|
-
export function isPlainObject(value: unknown): value is Record<PropertyKey, unknown> {
|
|
76
|
-
if (!isObject(value)) {
|
|
77
|
-
return false
|
|
78
|
-
}
|
|
79
|
-
const proto = Object.getPrototypeOf(value)
|
|
80
|
-
return proto === Object.prototype || proto === null
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* @internal
|
|
85
|
-
*/
|
|
86
|
-
export function isObject(value: unknown): value is Record<PropertyKey, unknown> {
|
|
87
|
-
return value !== null && typeof value === "object"
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* @internal
|
|
92
|
-
*/
|
|
93
|
-
export
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
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
|
-
* @internal
|
|
170
|
-
*/
|
|
171
|
-
export function
|
|
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
|
-
|
|
222
|
-
/**
|
|
223
|
-
* @internal
|
|
224
|
-
*/
|
|
225
|
-
export
|
|
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
|
-
if (!(
|
|
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
|
-
* @internal
|
|
353
|
-
*/
|
|
354
|
-
export function
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
1
|
+
import * as mobx from "mobx"
|
|
2
|
+
import {
|
|
3
|
+
type IObservableArray,
|
|
4
|
+
isObservableArray,
|
|
5
|
+
isObservableMap,
|
|
6
|
+
isObservableObject,
|
|
7
|
+
isObservableSet,
|
|
8
|
+
type ObservableMap,
|
|
9
|
+
type ObservableSet,
|
|
10
|
+
} from "mobx"
|
|
11
|
+
import type { JSONPrimitiveValue, PrimitiveValue } from "./types"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A mobx-keystone error.
|
|
15
|
+
*/
|
|
16
|
+
export class MobxKeystoneError extends Error {
|
|
17
|
+
constructor(msg: string) {
|
|
18
|
+
super(msg)
|
|
19
|
+
|
|
20
|
+
// Set the prototype explicitly.
|
|
21
|
+
Object.setPrototypeOf(this, new.target.prototype)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
export function failure(msg: string) {
|
|
29
|
+
return new MobxKeystoneError(msg)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const writableHiddenPropDescriptor: PropertyDescriptor = {
|
|
33
|
+
enumerable: false,
|
|
34
|
+
writable: true,
|
|
35
|
+
configurable: false,
|
|
36
|
+
value: undefined,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
export function addHiddenProp(object: any, propName: PropertyKey, value: any, writable = true) {
|
|
43
|
+
if (writable) {
|
|
44
|
+
Object.defineProperty(object, propName, writableHiddenPropDescriptor)
|
|
45
|
+
object[propName] = value
|
|
46
|
+
} else {
|
|
47
|
+
Object.defineProperty(object, propName, {
|
|
48
|
+
enumerable: false,
|
|
49
|
+
writable,
|
|
50
|
+
configurable: true,
|
|
51
|
+
value,
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
export function makePropReadonly<T>(object: T, propName: keyof T, enumerable: boolean) {
|
|
60
|
+
const propDesc = Object.getOwnPropertyDescriptor(object, propName)
|
|
61
|
+
if (propDesc) {
|
|
62
|
+
propDesc.enumerable = enumerable
|
|
63
|
+
if (propDesc.get) {
|
|
64
|
+
delete propDesc.set
|
|
65
|
+
} else {
|
|
66
|
+
propDesc.writable = false
|
|
67
|
+
}
|
|
68
|
+
Object.defineProperty(object, propName, propDesc)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @internal
|
|
74
|
+
*/
|
|
75
|
+
export function isPlainObject(value: unknown): value is Record<PropertyKey, unknown> {
|
|
76
|
+
if (!isObject(value)) {
|
|
77
|
+
return false
|
|
78
|
+
}
|
|
79
|
+
const proto = Object.getPrototypeOf(value)
|
|
80
|
+
return proto === Object.prototype || proto === null
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
86
|
+
export function isObject(value: unknown): value is Record<PropertyKey, unknown> {
|
|
87
|
+
return value !== null && typeof value === "object"
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @internal
|
|
92
|
+
*/
|
|
93
|
+
export let hasOwnProp: (object: object, propName: PropertyKey) => boolean = (
|
|
94
|
+
object,
|
|
95
|
+
propName
|
|
96
|
+
): boolean => {
|
|
97
|
+
const impl: (object: object, propName: PropertyKey) => boolean =
|
|
98
|
+
typeof Object.hasOwn === "function"
|
|
99
|
+
? Object.hasOwn
|
|
100
|
+
: (object, propName) => {
|
|
101
|
+
// biome-ignore lint/suspicious/noPrototypeBuiltins: legacy fallback for runtimes without Object.hasOwn.
|
|
102
|
+
return Object.prototype.hasOwnProperty.call(object, propName)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
hasOwnProp = impl
|
|
106
|
+
return impl(object, propName)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @internal
|
|
111
|
+
*/
|
|
112
|
+
export function setOwnProp(target: any, propName: PropertyKey, value: unknown): void {
|
|
113
|
+
if (propName !== "__proto__") {
|
|
114
|
+
target[propName] = value
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
Object.defineProperty(target, "__proto__", {
|
|
119
|
+
enumerable: true,
|
|
120
|
+
writable: true,
|
|
121
|
+
configurable: true,
|
|
122
|
+
value,
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @internal
|
|
128
|
+
*/
|
|
129
|
+
export function copyOwnEnumerableProps<TTarget extends Record<string, any>>(
|
|
130
|
+
target: TTarget,
|
|
131
|
+
source: Record<string, any>,
|
|
132
|
+
mapValue?: (value: unknown, key: string) => unknown
|
|
133
|
+
): TTarget {
|
|
134
|
+
const keys = Object.keys(source)
|
|
135
|
+
const len = keys.length
|
|
136
|
+
for (let i = 0; i < len; i++) {
|
|
137
|
+
const key = keys[i]
|
|
138
|
+
const value = source[key]
|
|
139
|
+
setOwnProp(target, key, mapValue ? mapValue(value, key) : value)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return target
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @internal
|
|
147
|
+
*/
|
|
148
|
+
export function clonePlainObject<T extends Record<string, any>>(source: T): T {
|
|
149
|
+
return copyOwnEnumerableProps({} as T, source)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @internal
|
|
154
|
+
*/
|
|
155
|
+
export function isPrimitive(value: unknown): value is PrimitiveValue {
|
|
156
|
+
switch (typeof value) {
|
|
157
|
+
case "number":
|
|
158
|
+
case "string":
|
|
159
|
+
case "boolean":
|
|
160
|
+
case "undefined":
|
|
161
|
+
case "bigint":
|
|
162
|
+
return true
|
|
163
|
+
default:
|
|
164
|
+
return value === null
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @internal
|
|
170
|
+
*/
|
|
171
|
+
export function isJSONPrimitive(value: unknown): value is JSONPrimitiveValue {
|
|
172
|
+
switch (typeof value) {
|
|
173
|
+
case "number":
|
|
174
|
+
return Number.isFinite(value)
|
|
175
|
+
case "string":
|
|
176
|
+
case "boolean":
|
|
177
|
+
return true
|
|
178
|
+
default:
|
|
179
|
+
return value === null
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* @internal
|
|
185
|
+
*/
|
|
186
|
+
export function deleteFromArray<T>(array: T[], value: T): boolean {
|
|
187
|
+
const index = array.indexOf(value)
|
|
188
|
+
if (index >= 0) {
|
|
189
|
+
array.splice(index, 1)
|
|
190
|
+
return true
|
|
191
|
+
}
|
|
192
|
+
return false
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @internal
|
|
197
|
+
*/
|
|
198
|
+
export function isMap(val: unknown): val is Map<any, any> | ObservableMap {
|
|
199
|
+
return val instanceof Map || isObservableMap(val)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @internal
|
|
204
|
+
*/
|
|
205
|
+
export function isSet(val: unknown): val is Set<any> | ObservableSet {
|
|
206
|
+
return val instanceof Set || isObservableSet(val)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* @internal
|
|
211
|
+
*/
|
|
212
|
+
export function isArray(val: unknown): val is any[] | IObservableArray {
|
|
213
|
+
return Array.isArray(val) || isObservableArray(val)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
declare const process:
|
|
217
|
+
| {
|
|
218
|
+
env?: Record<string, string | undefined>
|
|
219
|
+
}
|
|
220
|
+
| undefined
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* @internal
|
|
224
|
+
*/
|
|
225
|
+
export const inDevMode =
|
|
226
|
+
process?.env?.NODE_ENV !== undefined && process.env.NODE_ENV !== "production"
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* @internal
|
|
230
|
+
*/
|
|
231
|
+
export function assertIsObject(value: unknown, argName: string): asserts value is object {
|
|
232
|
+
if (!isObject(value)) {
|
|
233
|
+
throw failure(`${argName} must be an object`)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* @internal
|
|
239
|
+
*/
|
|
240
|
+
export function assertIsPlainObject(value: unknown, argName: string): asserts value is object {
|
|
241
|
+
if (!isPlainObject(value)) {
|
|
242
|
+
throw failure(`${argName} must be a plain object`)
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* @internal
|
|
248
|
+
*/
|
|
249
|
+
export function assertIsObservableObject(value: unknown, argName: string): asserts value is object {
|
|
250
|
+
if (!isObservableObject(value)) {
|
|
251
|
+
throw failure(`${argName} must be an observable object`)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* @internal
|
|
257
|
+
*/
|
|
258
|
+
export function assertIsObservableArray(
|
|
259
|
+
value: unknown,
|
|
260
|
+
argName: string
|
|
261
|
+
): asserts value is IObservableArray {
|
|
262
|
+
if (!isObservableArray(value)) {
|
|
263
|
+
throw failure(`${argName} must be an observable array`)
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* @internal
|
|
269
|
+
*/
|
|
270
|
+
export function assertIsMap(
|
|
271
|
+
value: unknown,
|
|
272
|
+
argName: string
|
|
273
|
+
): asserts value is Map<any, any> | ObservableMap {
|
|
274
|
+
if (!isMap(value)) {
|
|
275
|
+
throw failure(`${argName} must be a map`)
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* @internal
|
|
281
|
+
*/
|
|
282
|
+
export function assertIsSet(
|
|
283
|
+
value: unknown,
|
|
284
|
+
argName: string
|
|
285
|
+
): asserts value is Set<any> | ObservableSet {
|
|
286
|
+
if (!isSet(value)) {
|
|
287
|
+
throw failure(`${argName} must be a set`)
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* @internal
|
|
293
|
+
*/
|
|
294
|
+
export function assertIsFunction(value: unknown, argName: string): asserts value is Function {
|
|
295
|
+
if (typeof value !== "function") {
|
|
296
|
+
throw failure(`${argName} must be a function`)
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* @internal
|
|
302
|
+
*/
|
|
303
|
+
export function assertIsPrimitive(
|
|
304
|
+
value: unknown,
|
|
305
|
+
argName: string
|
|
306
|
+
): asserts value is PrimitiveValue {
|
|
307
|
+
if (!isPrimitive(value)) {
|
|
308
|
+
throw failure(`${argName} must be a primitive`)
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* @internal
|
|
314
|
+
*/
|
|
315
|
+
export function assertIsString(value: unknown, argName: string): asserts value is string {
|
|
316
|
+
if (typeof value !== "string") {
|
|
317
|
+
throw failure(`${argName} must be a string`)
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* @internal
|
|
323
|
+
*/
|
|
324
|
+
export const runAfterNewSymbol = Symbol("runAfterNew")
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* @internal
|
|
328
|
+
*/
|
|
329
|
+
export const runBeforeOnInitSymbol = Symbol("runBeforeOnInit")
|
|
330
|
+
|
|
331
|
+
type LateInitializationFunctionsArray = ((instance: any) => void)[]
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* @internal
|
|
335
|
+
*/
|
|
336
|
+
export function addLateInitializationFunction(
|
|
337
|
+
target: any,
|
|
338
|
+
symbol: symbol,
|
|
339
|
+
fn: (instance: any) => void
|
|
340
|
+
) {
|
|
341
|
+
let array: LateInitializationFunctionsArray | undefined = target[symbol]
|
|
342
|
+
// biome-ignore lint/suspicious/noPrototypeBuiltins: support old browsers
|
|
343
|
+
if (!(array && Object.prototype.hasOwnProperty.call(target, symbol))) {
|
|
344
|
+
// leave base array unmodified, create new array in the derived class
|
|
345
|
+
array = array ? array.slice() : []
|
|
346
|
+
addHiddenProp(target, symbol, array)
|
|
347
|
+
}
|
|
348
|
+
array.push(fn)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* @internal
|
|
353
|
+
*/
|
|
354
|
+
export function runLateInitializationFunctions(target: any, symbol: symbol): void {
|
|
355
|
+
const fns: LateInitializationFunctionsArray | undefined = target[symbol]
|
|
356
|
+
if (fns) {
|
|
357
|
+
for (const fn of fns) {
|
|
358
|
+
fn(target)
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const warningsAlreadyDisplayed = new Set<string>()
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* @internal
|
|
367
|
+
*/
|
|
368
|
+
export function logWarning(type: "warn" | "error", msg: string, uniqueKey?: string): void {
|
|
369
|
+
if (uniqueKey) {
|
|
370
|
+
if (warningsAlreadyDisplayed.has(uniqueKey)) {
|
|
371
|
+
return
|
|
372
|
+
}
|
|
373
|
+
warningsAlreadyDisplayed.add(uniqueKey)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
msg = "[mobx-keystone] " + msg
|
|
377
|
+
switch (type) {
|
|
378
|
+
case "warn":
|
|
379
|
+
// biome-ignore lint/suspicious/noConsole: this internal helper intentionally emits user-facing warnings.
|
|
380
|
+
console.warn(msg)
|
|
381
|
+
break
|
|
382
|
+
case "error":
|
|
383
|
+
// biome-ignore lint/suspicious/noConsole: this internal helper intentionally emits user-facing errors.
|
|
384
|
+
console.error(msg)
|
|
385
|
+
break
|
|
386
|
+
default:
|
|
387
|
+
throw failure(`unknown log type - ${type}`)
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* @internal
|
|
393
|
+
*/
|
|
394
|
+
export function lazy<A extends unknown[], R>(getter: (...args: A) => R): typeof getter {
|
|
395
|
+
let memoizedValue: R
|
|
396
|
+
let memoized = false
|
|
397
|
+
|
|
398
|
+
return (...args: A): R => {
|
|
399
|
+
if (!memoized) {
|
|
400
|
+
memoizedValue = getter(...args)
|
|
401
|
+
memoized = true
|
|
402
|
+
}
|
|
403
|
+
return memoizedValue
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* @internal
|
|
409
|
+
*/
|
|
410
|
+
export const identityFn = <T>(x: T): T => x
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* @internal
|
|
414
|
+
*/
|
|
415
|
+
export const mobx6 = {
|
|
416
|
+
makeObservable: (mobx as any)[
|
|
417
|
+
// just to ensure import * is kept properly
|
|
418
|
+
String.fromCharCode("l".charCodeAt(0) + 1) + "akeObservable"
|
|
419
|
+
],
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* @internal
|
|
424
|
+
*/
|
|
425
|
+
export function propNameToSetterName(propName: string): string {
|
|
426
|
+
return `set${propName[0].toUpperCase()}${propName.slice(1)}`
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* @internal
|
|
431
|
+
*/
|
|
432
|
+
export function getMobxVersion(): number {
|
|
433
|
+
if (mobx6.makeObservable!) {
|
|
434
|
+
return 6
|
|
435
|
+
} else {
|
|
436
|
+
return 5
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* @internal
|
|
442
|
+
*/
|
|
443
|
+
export const namespace = "mobx-keystone"
|