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/wrappers/asMap.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
action,
|
|
3
|
-
IMapWillChange,
|
|
4
|
-
IObjectDidChange,
|
|
5
|
-
IObservableArray,
|
|
3
|
+
type IMapWillChange,
|
|
4
|
+
type IObjectDidChange,
|
|
5
|
+
type IObservableArray,
|
|
6
6
|
intercept,
|
|
7
7
|
isObservableArray,
|
|
8
8
|
isObservableObject,
|
|
9
|
-
ObservableMap,
|
|
9
|
+
type ObservableMap,
|
|
10
10
|
observable,
|
|
11
11
|
observe,
|
|
12
12
|
remove,
|
|
@@ -26,12 +26,47 @@ import {
|
|
|
26
26
|
import { setIfDifferent } from "../utils/setIfDifferent"
|
|
27
27
|
import { tag } from "../utils/tag"
|
|
28
28
|
|
|
29
|
+
type Ts6MapExtras<K, V> = {
|
|
30
|
+
getOrInsert(key: K, defaultValue: V): V
|
|
31
|
+
getOrInsertComputed(key: K, callback: (key: K) => V): V
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type ObservableMapWithDataObject<K, V, TDataObject> = ObservableMap<K, V> &
|
|
35
|
+
Ts6MapExtras<K, V> & {
|
|
36
|
+
dataObject: TDataObject
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function addTs6MapExtras<K, V>(map: ObservableMap<K, V>): ObservableMap<K, V> & Ts6MapExtras<K, V> {
|
|
40
|
+
const mapWithExtras = map as ObservableMap<K, V> & Partial<Ts6MapExtras<K, V>>
|
|
41
|
+
|
|
42
|
+
if (!mapWithExtras.getOrInsert) {
|
|
43
|
+
mapWithExtras.getOrInsert = function (key: K, defaultValue: V): V {
|
|
44
|
+
if (this.has(key)) {
|
|
45
|
+
return this.get(key) as V
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this.set(key, defaultValue)
|
|
49
|
+
return defaultValue
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!mapWithExtras.getOrInsertComputed) {
|
|
54
|
+
mapWithExtras.getOrInsertComputed = function (key: K, callback: (key: K) => V): V {
|
|
55
|
+
if (this.has(key)) {
|
|
56
|
+
return this.get(key) as V
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const value = callback(key)
|
|
60
|
+
this.set(key, value)
|
|
61
|
+
return value
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return mapWithExtras as ObservableMap<K, V> & Ts6MapExtras<K, V>
|
|
66
|
+
}
|
|
67
|
+
|
|
29
68
|
const observableMapBackedByObservableObject = action(
|
|
30
|
-
<T>(
|
|
31
|
-
obj: object
|
|
32
|
-
): ObservableMap<string, T> & {
|
|
33
|
-
dataObject: typeof obj
|
|
34
|
-
} => {
|
|
69
|
+
<T>(obj: object): ObservableMapWithDataObject<string, T, typeof obj> => {
|
|
35
70
|
if (inDevMode) {
|
|
36
71
|
if (!isObservableObject(obj)) {
|
|
37
72
|
throw failure("assertion failed: expected an observable object")
|
|
@@ -40,7 +75,7 @@ const observableMapBackedByObservableObject = action(
|
|
|
40
75
|
|
|
41
76
|
const map = transaction(() =>
|
|
42
77
|
untracked(() => {
|
|
43
|
-
const map = observable.map()
|
|
78
|
+
const map = observable.map<string, T>()
|
|
44
79
|
|
|
45
80
|
const keys = Object.keys(obj)
|
|
46
81
|
for (let i = 0; i < keys.length; i++) {
|
|
@@ -51,7 +86,8 @@ const observableMapBackedByObservableObject = action(
|
|
|
51
86
|
return map
|
|
52
87
|
})
|
|
53
88
|
)
|
|
54
|
-
|
|
89
|
+
const mapWithExtras = addTs6MapExtras(map)
|
|
90
|
+
;(mapWithExtras as any).dataObject = obj
|
|
55
91
|
|
|
56
92
|
let mapAlreadyChanged = false
|
|
57
93
|
let objectAlreadyChanged = false
|
|
@@ -70,12 +106,12 @@ const observableMapBackedByObservableObject = action(
|
|
|
70
106
|
switch (change.type) {
|
|
71
107
|
case "add":
|
|
72
108
|
case "update": {
|
|
73
|
-
|
|
109
|
+
mapWithExtras.set(change.name as string, change.newValue)
|
|
74
110
|
break
|
|
75
111
|
}
|
|
76
112
|
|
|
77
113
|
case "remove": {
|
|
78
|
-
|
|
114
|
+
mapWithExtras.delete(change.name as string)
|
|
79
115
|
break
|
|
80
116
|
}
|
|
81
117
|
|
|
@@ -126,24 +162,24 @@ const observableMapBackedByObservableObject = action(
|
|
|
126
162
|
})
|
|
127
163
|
)
|
|
128
164
|
|
|
129
|
-
return
|
|
165
|
+
return mapWithExtras as any
|
|
130
166
|
}
|
|
131
167
|
)
|
|
132
168
|
|
|
133
169
|
const observableMapBackedByObservableArray = <T>(
|
|
134
170
|
array: IObservableArray<[string, T]>
|
|
135
|
-
):
|
|
171
|
+
): ObservableMapWithDataObject<string, T, typeof array> => {
|
|
136
172
|
if (inDevMode) {
|
|
137
173
|
if (!isObservableArray(array)) {
|
|
138
174
|
throw failure("assertion failed: expected an observable array")
|
|
139
175
|
}
|
|
140
176
|
}
|
|
141
177
|
|
|
142
|
-
const map = untracked(() => {
|
|
178
|
+
const map: ObservableMap<string, T> = untracked(() => {
|
|
143
179
|
if (getMobxVersion() >= 6) {
|
|
144
|
-
return observable.map(array)
|
|
180
|
+
return observable.map<string, T>(array)
|
|
145
181
|
} else {
|
|
146
|
-
const map = observable.map()
|
|
182
|
+
const map = observable.map<string, T>()
|
|
147
183
|
runInAction(() => {
|
|
148
184
|
array.forEach(([k, v]) => {
|
|
149
185
|
map.set(k, v)
|
|
@@ -152,9 +188,10 @@ const observableMapBackedByObservableArray = <T>(
|
|
|
152
188
|
return map
|
|
153
189
|
}
|
|
154
190
|
})
|
|
155
|
-
|
|
191
|
+
const mapWithExtras = addTs6MapExtras(map)
|
|
192
|
+
;(mapWithExtras as any).dataObject = array
|
|
156
193
|
|
|
157
|
-
if (
|
|
194
|
+
if (mapWithExtras.size !== array.length) {
|
|
158
195
|
throw failure("arrays backing a map cannot contain duplicate keys")
|
|
159
196
|
}
|
|
160
197
|
|
|
@@ -179,14 +216,14 @@ const observableMapBackedByObservableArray = <T>(
|
|
|
179
216
|
{
|
|
180
217
|
const removed = change.removed
|
|
181
218
|
for (let i = 0; i < removed.length; i++) {
|
|
182
|
-
|
|
219
|
+
mapWithExtras.delete(removed[i][0])
|
|
183
220
|
}
|
|
184
221
|
}
|
|
185
222
|
|
|
186
223
|
{
|
|
187
224
|
const added = change.added
|
|
188
225
|
for (let i = 0; i < added.length; i++) {
|
|
189
|
-
|
|
226
|
+
mapWithExtras.set(added[i][0], added[i][1])
|
|
190
227
|
}
|
|
191
228
|
}
|
|
192
229
|
|
|
@@ -194,8 +231,8 @@ const observableMapBackedByObservableArray = <T>(
|
|
|
194
231
|
}
|
|
195
232
|
|
|
196
233
|
case "update": {
|
|
197
|
-
|
|
198
|
-
|
|
234
|
+
mapWithExtras.delete(change.oldValue[0])
|
|
235
|
+
mapWithExtras.set(change.newValue[0], change.newValue[1])
|
|
199
236
|
break
|
|
200
237
|
}
|
|
201
238
|
|
|
@@ -255,7 +292,7 @@ const observableMapBackedByObservableArray = <T>(
|
|
|
255
292
|
})
|
|
256
293
|
)
|
|
257
294
|
|
|
258
|
-
return
|
|
295
|
+
return mapWithExtras as any
|
|
259
296
|
}
|
|
260
297
|
|
|
261
298
|
const asMapTag = tag((objOrArray: Record<string, any> | Array<[string, any]>) => {
|
|
@@ -273,9 +310,7 @@ const asMapTag = tag((objOrArray: Record<string, any> | Array<[string, any]>) =>
|
|
|
273
310
|
*
|
|
274
311
|
* @param array Array.
|
|
275
312
|
*/
|
|
276
|
-
export function asMap<K, V>(
|
|
277
|
-
array: Array<[K, V]>
|
|
278
|
-
): ObservableMap<K, V> & { dataObject: Array<[K, V]> }
|
|
313
|
+
export function asMap<K, V>(array: Array<[K, V]>): ObservableMapWithDataObject<K, V, Array<[K, V]>>
|
|
279
314
|
|
|
280
315
|
/**
|
|
281
316
|
* Wraps an observable object or a tuple array to offer a map like interface.
|
|
@@ -284,7 +319,7 @@ export function asMap<K, V>(
|
|
|
284
319
|
*/
|
|
285
320
|
export function asMap<T>(
|
|
286
321
|
object: Record<string, T>
|
|
287
|
-
):
|
|
322
|
+
): ObservableMapWithDataObject<string, T, Record<string, T>>
|
|
288
323
|
|
|
289
324
|
/**
|
|
290
325
|
* Wraps an observable object or a tuple array to offer a map like interface.
|
|
@@ -293,7 +328,7 @@ export function asMap<T>(
|
|
|
293
328
|
*/
|
|
294
329
|
export function asMap(
|
|
295
330
|
objOrArray: Record<string, unknown> | Array<[unknown, unknown]>
|
|
296
|
-
):
|
|
331
|
+
): ObservableMapWithDataObject<unknown, unknown, typeof objOrArray> {
|
|
297
332
|
return asMapTag.for(objOrArray) as any
|
|
298
333
|
}
|
|
299
334
|
|
package/src/wrappers/asSet.ts
CHANGED