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/ref/customRef.ts
CHANGED
|
@@ -2,9 +2,9 @@ import { action } from "mobx"
|
|
|
2
2
|
import {
|
|
3
3
|
getModelRefId,
|
|
4
4
|
internalCustomRef,
|
|
5
|
-
RefIdResolver,
|
|
6
|
-
RefOnResolvedValueChange,
|
|
7
|
-
RefResolver,
|
|
5
|
+
type RefIdResolver,
|
|
6
|
+
type RefOnResolvedValueChange,
|
|
7
|
+
type RefResolver,
|
|
8
8
|
} from "./core"
|
|
9
9
|
import type { RefConstructor } from "./Ref"
|
|
10
10
|
|
package/src/ref/rootRef.ts
CHANGED
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
import { action } from "mobx"
|
|
2
|
-
import { fastGetRoot } from "../parent/path"
|
|
3
|
-
import {
|
|
4
|
-
getModelRefId,
|
|
5
|
-
internalCustomRef,
|
|
6
|
-
RefIdResolver,
|
|
7
|
-
RefOnResolvedValueChange,
|
|
8
|
-
RefResolver,
|
|
9
|
-
resolveId,
|
|
10
|
-
} from "./core"
|
|
11
|
-
import type { Ref, RefConstructor } from "./Ref"
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Custom reference options.
|
|
15
|
-
*/
|
|
16
|
-
export interface RootRefOptions<T extends object> {
|
|
17
|
-
/**
|
|
18
|
-
* Must return the ID associated to the given target object, or `undefined` if it has no ID.
|
|
19
|
-
* If not provided it will try to get the reference id from the model `getRefId()` method.
|
|
20
|
-
*
|
|
21
|
-
* @param target Target object.
|
|
22
|
-
*/
|
|
23
|
-
getId?: RefIdResolver
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* What should happen when the resolved value changes.
|
|
27
|
-
*
|
|
28
|
-
* @param ref Reference object.
|
|
29
|
-
* @param newValue New resolved value.
|
|
30
|
-
* @param oldValue Old resolved value.
|
|
31
|
-
*/
|
|
32
|
-
onResolvedValueChange?: RefOnResolvedValueChange<T>
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Creates a root ref to an object, which in its snapshot form has an id.
|
|
37
|
-
* A root ref will only be able to resolve references as long as both the Ref
|
|
38
|
-
* and the referenced object share a common root.
|
|
39
|
-
*
|
|
40
|
-
* @template T Target object type.
|
|
41
|
-
* @param modelTypeId Unique model type id.
|
|
42
|
-
* @param [options] Root reference options.
|
|
43
|
-
* @returns A function that allows you to construct that type of root reference.
|
|
44
|
-
*/
|
|
45
|
-
export const rootRef: <T extends object>(
|
|
46
|
-
modelTypeId: string,
|
|
47
|
-
options?: RootRefOptions<T>
|
|
48
|
-
) => RefConstructor<T> = action(
|
|
49
|
-
"rootRef",
|
|
50
|
-
<T extends object>(modelTypeId: string, options?: RootRefOptions<T>): RefConstructor<T> => {
|
|
51
|
-
const getId = options?.getId ?? getModelRefId
|
|
52
|
-
const onResolvedValueChange = options?.onResolvedValueChange
|
|
53
|
-
|
|
54
|
-
const resolverGen = (ref: Ref<T>): RefResolver<T> => {
|
|
55
|
-
let cachedTarget: T | undefined
|
|
56
|
-
|
|
57
|
-
return () => {
|
|
58
|
-
const refRoot = fastGetRoot(ref, true)
|
|
59
|
-
|
|
60
|
-
if (isRefRootCachedTargetOk(ref, refRoot, cachedTarget, getId)) {
|
|
61
|
-
return cachedTarget
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Detached refs cannot resolve a rootRef target until they are attached.
|
|
65
|
-
// Skipping resolveId here avoids creating one computed id tree per detached ref.
|
|
66
|
-
if (refRoot === ref) {
|
|
67
|
-
return undefined
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// when not found, everytime a child is added/removed or its id changes we will perform another search
|
|
71
|
-
// this search is only done once for every distinct getId function
|
|
72
|
-
const newTarget = resolveId<T>(refRoot, ref.id, getId)
|
|
73
|
-
if (newTarget) {
|
|
74
|
-
cachedTarget = newTarget
|
|
75
|
-
}
|
|
76
|
-
return newTarget
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return internalCustomRef(modelTypeId, resolverGen, getId, onResolvedValueChange)
|
|
81
|
-
}
|
|
82
|
-
)
|
|
83
|
-
|
|
84
|
-
function isRefRootCachedTargetOk<T extends object>(
|
|
85
|
-
ref: Ref<T>,
|
|
86
|
-
refRoot: object,
|
|
87
|
-
cachedTarget: T | undefined,
|
|
88
|
-
getId: RefIdResolver
|
|
89
|
-
): cachedTarget is T {
|
|
90
|
-
if (!cachedTarget) {
|
|
91
|
-
return false
|
|
92
|
-
}
|
|
93
|
-
if (ref.id !== getId(cachedTarget)) {
|
|
94
|
-
return false
|
|
95
|
-
}
|
|
96
|
-
if (refRoot !== fastGetRoot(cachedTarget, true)) {
|
|
97
|
-
return false
|
|
98
|
-
}
|
|
99
|
-
return true
|
|
100
|
-
}
|
|
1
|
+
import { action } from "mobx"
|
|
2
|
+
import { fastGetRoot } from "../parent/path"
|
|
3
|
+
import {
|
|
4
|
+
getModelRefId,
|
|
5
|
+
internalCustomRef,
|
|
6
|
+
type RefIdResolver,
|
|
7
|
+
type RefOnResolvedValueChange,
|
|
8
|
+
type RefResolver,
|
|
9
|
+
resolveId,
|
|
10
|
+
} from "./core"
|
|
11
|
+
import type { Ref, RefConstructor } from "./Ref"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Custom reference options.
|
|
15
|
+
*/
|
|
16
|
+
export interface RootRefOptions<T extends object> {
|
|
17
|
+
/**
|
|
18
|
+
* Must return the ID associated to the given target object, or `undefined` if it has no ID.
|
|
19
|
+
* If not provided it will try to get the reference id from the model `getRefId()` method.
|
|
20
|
+
*
|
|
21
|
+
* @param target Target object.
|
|
22
|
+
*/
|
|
23
|
+
getId?: RefIdResolver
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What should happen when the resolved value changes.
|
|
27
|
+
*
|
|
28
|
+
* @param ref Reference object.
|
|
29
|
+
* @param newValue New resolved value.
|
|
30
|
+
* @param oldValue Old resolved value.
|
|
31
|
+
*/
|
|
32
|
+
onResolvedValueChange?: RefOnResolvedValueChange<T>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Creates a root ref to an object, which in its snapshot form has an id.
|
|
37
|
+
* A root ref will only be able to resolve references as long as both the Ref
|
|
38
|
+
* and the referenced object share a common root.
|
|
39
|
+
*
|
|
40
|
+
* @template T Target object type.
|
|
41
|
+
* @param modelTypeId Unique model type id.
|
|
42
|
+
* @param [options] Root reference options.
|
|
43
|
+
* @returns A function that allows you to construct that type of root reference.
|
|
44
|
+
*/
|
|
45
|
+
export const rootRef: <T extends object>(
|
|
46
|
+
modelTypeId: string,
|
|
47
|
+
options?: RootRefOptions<T>
|
|
48
|
+
) => RefConstructor<T> = action(
|
|
49
|
+
"rootRef",
|
|
50
|
+
<T extends object>(modelTypeId: string, options?: RootRefOptions<T>): RefConstructor<T> => {
|
|
51
|
+
const getId = options?.getId ?? getModelRefId
|
|
52
|
+
const onResolvedValueChange = options?.onResolvedValueChange
|
|
53
|
+
|
|
54
|
+
const resolverGen = (ref: Ref<T>): RefResolver<T> => {
|
|
55
|
+
let cachedTarget: T | undefined
|
|
56
|
+
|
|
57
|
+
return () => {
|
|
58
|
+
const refRoot = fastGetRoot(ref, true)
|
|
59
|
+
|
|
60
|
+
if (isRefRootCachedTargetOk(ref, refRoot, cachedTarget, getId)) {
|
|
61
|
+
return cachedTarget
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Detached refs cannot resolve a rootRef target until they are attached.
|
|
65
|
+
// Skipping resolveId here avoids creating one computed id tree per detached ref.
|
|
66
|
+
if (refRoot === ref) {
|
|
67
|
+
return undefined
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// when not found, everytime a child is added/removed or its id changes we will perform another search
|
|
71
|
+
// this search is only done once for every distinct getId function
|
|
72
|
+
const newTarget = resolveId<T>(refRoot, ref.id, getId)
|
|
73
|
+
if (newTarget) {
|
|
74
|
+
cachedTarget = newTarget
|
|
75
|
+
}
|
|
76
|
+
return newTarget
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return internalCustomRef(modelTypeId, resolverGen, getId, onResolvedValueChange)
|
|
81
|
+
}
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
function isRefRootCachedTargetOk<T extends object>(
|
|
85
|
+
ref: Ref<T>,
|
|
86
|
+
refRoot: object,
|
|
87
|
+
cachedTarget: T | undefined,
|
|
88
|
+
getId: RefIdResolver
|
|
89
|
+
): cachedTarget is T {
|
|
90
|
+
if (!cachedTarget) {
|
|
91
|
+
return false
|
|
92
|
+
}
|
|
93
|
+
if (ref.id !== getId(cachedTarget)) {
|
|
94
|
+
return false
|
|
95
|
+
}
|
|
96
|
+
if (refRoot !== fastGetRoot(cachedTarget, true)) {
|
|
97
|
+
return false
|
|
98
|
+
}
|
|
99
|
+
return true
|
|
100
|
+
}
|
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
import { action } from "mobx"
|
|
2
|
-
import { ActionContextActionType } from "../action/context"
|
|
3
|
-
import { HookAction } from "../action/hookActions"
|
|
4
|
-
import { wrapInAction, wrapModelMethodInActionIfNeeded } from "../action/wrapInAction"
|
|
5
|
-
import { AnyModel, BaseModel } from "../model/BaseModel"
|
|
6
|
-
import { WalkTreeMode, walkTree } from "../parent/walkTree"
|
|
7
|
-
|
|
8
|
-
const onAttachedDisposers = new WeakMap<object, () => void>()
|
|
9
|
-
const attachedToRootStore = new WeakSet<object>()
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @internal
|
|
13
|
-
*/
|
|
14
|
-
export const attachToRootStore = action(
|
|
15
|
-
"attachToRootStore",
|
|
16
|
-
(rootStore: object, child: object): void => {
|
|
17
|
-
// we use an array to ensure they will get called even if the actual hook modifies the tree
|
|
18
|
-
const childrenToCall: AnyModel[] = []
|
|
19
|
-
|
|
20
|
-
walkTree(
|
|
21
|
-
child,
|
|
22
|
-
(ch) => {
|
|
23
|
-
// we use this to avoid calling onAttachedToRootStore
|
|
24
|
-
// twice
|
|
25
|
-
if (attachedToRootStore.has(ch)) {
|
|
26
|
-
return
|
|
27
|
-
}
|
|
28
|
-
attachedToRootStore.add(ch)
|
|
29
|
-
|
|
30
|
-
if (ch instanceof BaseModel && (ch as any).onAttachedToRootStore) {
|
|
31
|
-
wrapModelMethodInActionIfNeeded(
|
|
32
|
-
ch as any,
|
|
33
|
-
"onAttachedToRootStore",
|
|
34
|
-
HookAction.OnAttachedToRootStore
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
childrenToCall.push(ch as AnyModel)
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
WalkTreeMode.ParentFirst
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
const childrenToCallLen = childrenToCall.length
|
|
44
|
-
for (let i = 0; i < childrenToCallLen; i++) {
|
|
45
|
-
const ch = childrenToCall[i]
|
|
46
|
-
|
|
47
|
-
const disposer = (ch as any).onAttachedToRootStore(rootStore)
|
|
48
|
-
if (disposer) {
|
|
49
|
-
onAttachedDisposers.set(ch, disposer)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @internal
|
|
57
|
-
*/
|
|
58
|
-
export const detachFromRootStore = action("detachFromRootStore", (child: object): void => {
|
|
59
|
-
// we use an array to ensure they will get called even if the actual hook modifies the tree
|
|
60
|
-
const disposersToCall: (() => void)[] = []
|
|
61
|
-
|
|
62
|
-
walkTree(
|
|
63
|
-
child,
|
|
64
|
-
(ch) => {
|
|
65
|
-
if (!attachedToRootStore.delete(ch)) {
|
|
66
|
-
return
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const disposer = onAttachedDisposers.get(ch)
|
|
70
|
-
if (disposer) {
|
|
71
|
-
// wrap disposer in action
|
|
72
|
-
const disposerAction = wrapInAction({
|
|
73
|
-
nameOrNameFn: HookAction.OnAttachedToRootStoreDisposer,
|
|
74
|
-
fn: disposer,
|
|
75
|
-
actionType: ActionContextActionType.Sync,
|
|
76
|
-
}).bind(ch)
|
|
77
|
-
onAttachedDisposers.delete(ch)
|
|
78
|
-
|
|
79
|
-
disposersToCall.push(disposerAction)
|
|
80
|
-
}
|
|
81
|
-
},
|
|
82
|
-
WalkTreeMode.ChildrenFirst
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
const disposersToCallLen = disposersToCall.length
|
|
86
|
-
for (let i = 0; i < disposersToCallLen; i++) {
|
|
87
|
-
disposersToCall[i]()
|
|
88
|
-
}
|
|
89
|
-
})
|
|
1
|
+
import { action } from "mobx"
|
|
2
|
+
import { ActionContextActionType } from "../action/context"
|
|
3
|
+
import { HookAction } from "../action/hookActions"
|
|
4
|
+
import { wrapInAction, wrapModelMethodInActionIfNeeded } from "../action/wrapInAction"
|
|
5
|
+
import { type AnyModel, BaseModel } from "../model/BaseModel"
|
|
6
|
+
import { WalkTreeMode, walkTree } from "../parent/walkTree"
|
|
7
|
+
|
|
8
|
+
const onAttachedDisposers = new WeakMap<object, () => void>()
|
|
9
|
+
const attachedToRootStore = new WeakSet<object>()
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export const attachToRootStore = action(
|
|
15
|
+
"attachToRootStore",
|
|
16
|
+
(rootStore: object, child: object): void => {
|
|
17
|
+
// we use an array to ensure they will get called even if the actual hook modifies the tree
|
|
18
|
+
const childrenToCall: AnyModel[] = []
|
|
19
|
+
|
|
20
|
+
walkTree(
|
|
21
|
+
child,
|
|
22
|
+
(ch) => {
|
|
23
|
+
// we use this to avoid calling onAttachedToRootStore
|
|
24
|
+
// twice
|
|
25
|
+
if (attachedToRootStore.has(ch)) {
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
attachedToRootStore.add(ch)
|
|
29
|
+
|
|
30
|
+
if (ch instanceof BaseModel && (ch as any).onAttachedToRootStore) {
|
|
31
|
+
wrapModelMethodInActionIfNeeded(
|
|
32
|
+
ch as any,
|
|
33
|
+
"onAttachedToRootStore",
|
|
34
|
+
HookAction.OnAttachedToRootStore
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
childrenToCall.push(ch as AnyModel)
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
WalkTreeMode.ParentFirst
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
const childrenToCallLen = childrenToCall.length
|
|
44
|
+
for (let i = 0; i < childrenToCallLen; i++) {
|
|
45
|
+
const ch = childrenToCall[i]
|
|
46
|
+
|
|
47
|
+
const disposer = (ch as any).onAttachedToRootStore(rootStore)
|
|
48
|
+
if (disposer) {
|
|
49
|
+
onAttachedDisposers.set(ch, disposer)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @internal
|
|
57
|
+
*/
|
|
58
|
+
export const detachFromRootStore = action("detachFromRootStore", (child: object): void => {
|
|
59
|
+
// we use an array to ensure they will get called even if the actual hook modifies the tree
|
|
60
|
+
const disposersToCall: (() => void)[] = []
|
|
61
|
+
|
|
62
|
+
walkTree(
|
|
63
|
+
child,
|
|
64
|
+
(ch) => {
|
|
65
|
+
if (!attachedToRootStore.delete(ch)) {
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const disposer = onAttachedDisposers.get(ch)
|
|
70
|
+
if (disposer) {
|
|
71
|
+
// wrap disposer in action
|
|
72
|
+
const disposerAction = wrapInAction({
|
|
73
|
+
nameOrNameFn: HookAction.OnAttachedToRootStoreDisposer,
|
|
74
|
+
fn: disposer,
|
|
75
|
+
actionType: ActionContextActionType.Sync,
|
|
76
|
+
}).bind(ch)
|
|
77
|
+
onAttachedDisposers.delete(ch)
|
|
78
|
+
|
|
79
|
+
disposersToCall.push(disposerAction)
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
WalkTreeMode.ChildrenFirst
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
const disposersToCallLen = disposersToCall.length
|
|
86
|
+
for (let i = 0; i < disposersToCallLen; i++) {
|
|
87
|
+
disposersToCall[i]()
|
|
88
|
+
}
|
|
89
|
+
})
|
|
@@ -1,104 +1,104 @@
|
|
|
1
|
-
import type { Frozen, frozenKey } from "../frozen/Frozen"
|
|
2
|
-
import type { AnyModel } from "../model/BaseModel"
|
|
3
|
-
import type { modelIdKey, modelTypeKey } from "../model/metadata"
|
|
4
|
-
import type { ModelFromSnapshot, ModelToSnapshot } from "../modelShared/BaseModelShared"
|
|
5
|
-
import type { ArraySet, ObjectMap } from "../wrappers"
|
|
6
|
-
|
|
7
|
-
// this must be a type rather than an interface to ensure compatibility with index signatures
|
|
8
|
-
// (e.g. PlainObject)
|
|
9
|
-
export type FrozenData<D> = {
|
|
10
|
-
[frozenKey]: true
|
|
11
|
-
data: D
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// snapshot out
|
|
15
|
-
|
|
16
|
-
// infer is there just to cache type generation
|
|
17
|
-
|
|
18
|
-
export type SnapshotOutOfObject<T> = {
|
|
19
|
-
[k in keyof T]: SnapshotOutOf<T[k]> extends infer R ? R : never
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export type SnapshotOutOfModel<M extends AnyModel> = ModelToSnapshot<M>
|
|
23
|
-
|
|
24
|
-
export interface SnapshotOutOfFrozen<F extends Frozen<any>> extends FrozenData<F["data"]> {}
|
|
25
|
-
|
|
26
|
-
export interface SnapshotOutOfObjectMap<V> {
|
|
27
|
-
items: { [k: string]: SnapshotOutOf<V> }
|
|
28
|
-
[modelTypeKey]?: string
|
|
29
|
-
[modelIdKey]: string
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface SnapshotOutOfArraySet<V> {
|
|
33
|
-
items: SnapshotOutOf<V>[]
|
|
34
|
-
[modelTypeKey]?: string
|
|
35
|
-
[modelIdKey]: string
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export type SnapshotOutOf<T> =
|
|
39
|
-
T extends ObjectMap<infer V>
|
|
40
|
-
? SnapshotOutOfObjectMap<V> extends infer R
|
|
41
|
-
? R
|
|
42
|
-
: never
|
|
43
|
-
: T extends ArraySet<infer V>
|
|
44
|
-
? SnapshotOutOfArraySet<V> extends infer R
|
|
45
|
-
? R
|
|
46
|
-
: never
|
|
47
|
-
: T extends AnyModel
|
|
48
|
-
? SnapshotOutOfModel<T> extends infer R
|
|
49
|
-
? R
|
|
50
|
-
: never
|
|
51
|
-
: T extends Frozen<any>
|
|
52
|
-
? SnapshotOutOfFrozen<T> extends infer R
|
|
53
|
-
? R
|
|
54
|
-
: never
|
|
55
|
-
: T extends object
|
|
56
|
-
? SnapshotOutOfObject<T> extends infer R
|
|
57
|
-
? R
|
|
58
|
-
: never
|
|
59
|
-
: T
|
|
60
|
-
|
|
61
|
-
// snapshot in
|
|
62
|
-
|
|
63
|
-
export type SnapshotInOfObject<T> = {
|
|
64
|
-
[k in keyof T]: SnapshotInOf<T[k]> extends infer R ? R : never
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export type SnapshotInOfModel<M extends AnyModel> = ModelFromSnapshot<M>
|
|
68
|
-
|
|
69
|
-
export interface SnapshotInOfFrozen<F extends Frozen<any>> extends FrozenData<F["data"]> {}
|
|
70
|
-
|
|
71
|
-
export interface SnapshotInOfObjectMap<V> {
|
|
72
|
-
items?: { [k: string]: SnapshotOutOf<V> }
|
|
73
|
-
[modelTypeKey]?: string
|
|
74
|
-
[modelIdKey]: string
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export interface SnapshotInOfArraySet<V> {
|
|
78
|
-
items?: SnapshotOutOf<V>[]
|
|
79
|
-
[modelTypeKey]?: string
|
|
80
|
-
[modelIdKey]: string
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export type SnapshotInOf<T> =
|
|
84
|
-
T extends ObjectMap<infer V>
|
|
85
|
-
? SnapshotInOfObjectMap<V> extends infer R
|
|
86
|
-
? R
|
|
87
|
-
: never
|
|
88
|
-
: T extends ArraySet<infer V>
|
|
89
|
-
? SnapshotInOfArraySet<V> extends infer R
|
|
90
|
-
? R
|
|
91
|
-
: never
|
|
92
|
-
: T extends AnyModel
|
|
93
|
-
? SnapshotInOfModel<T> extends infer R
|
|
94
|
-
? R
|
|
95
|
-
: never
|
|
96
|
-
: T extends Frozen<any>
|
|
97
|
-
? SnapshotInOfFrozen<T> extends infer R
|
|
98
|
-
? R
|
|
99
|
-
: never
|
|
100
|
-
: T extends object
|
|
101
|
-
? SnapshotInOfObject<T> extends infer R
|
|
102
|
-
? R
|
|
103
|
-
: never
|
|
104
|
-
: T
|
|
1
|
+
import type { Frozen, frozenKey } from "../frozen/Frozen"
|
|
2
|
+
import type { AnyModel } from "../model/BaseModel"
|
|
3
|
+
import type { modelIdKey, modelTypeKey } from "../model/metadata"
|
|
4
|
+
import type { ModelFromSnapshot, ModelToSnapshot } from "../modelShared/BaseModelShared"
|
|
5
|
+
import type { ArraySet, ObjectMap } from "../wrappers"
|
|
6
|
+
|
|
7
|
+
// this must be a type rather than an interface to ensure compatibility with index signatures
|
|
8
|
+
// (e.g. PlainObject)
|
|
9
|
+
export type FrozenData<D> = {
|
|
10
|
+
[frozenKey]: true
|
|
11
|
+
data: D
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// snapshot out
|
|
15
|
+
|
|
16
|
+
// infer is there just to cache type generation
|
|
17
|
+
|
|
18
|
+
export type SnapshotOutOfObject<T> = {
|
|
19
|
+
[k in keyof T]: SnapshotOutOf<T[k]> extends infer R ? R : never
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type SnapshotOutOfModel<M extends AnyModel> = ModelToSnapshot<M>
|
|
23
|
+
|
|
24
|
+
export interface SnapshotOutOfFrozen<F extends Frozen<any>> extends FrozenData<F["data"]> {}
|
|
25
|
+
|
|
26
|
+
export interface SnapshotOutOfObjectMap<V> {
|
|
27
|
+
items: { [k: string]: SnapshotOutOf<V> }
|
|
28
|
+
[modelTypeKey]?: string
|
|
29
|
+
[modelIdKey]: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface SnapshotOutOfArraySet<V> {
|
|
33
|
+
items: SnapshotOutOf<V>[]
|
|
34
|
+
[modelTypeKey]?: string
|
|
35
|
+
[modelIdKey]: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type SnapshotOutOf<T> =
|
|
39
|
+
T extends ObjectMap<infer V>
|
|
40
|
+
? SnapshotOutOfObjectMap<V> extends infer R
|
|
41
|
+
? R
|
|
42
|
+
: never
|
|
43
|
+
: T extends ArraySet<infer V>
|
|
44
|
+
? SnapshotOutOfArraySet<V> extends infer R
|
|
45
|
+
? R
|
|
46
|
+
: never
|
|
47
|
+
: T extends AnyModel
|
|
48
|
+
? SnapshotOutOfModel<T> extends infer R
|
|
49
|
+
? R
|
|
50
|
+
: never
|
|
51
|
+
: T extends Frozen<any>
|
|
52
|
+
? SnapshotOutOfFrozen<T> extends infer R
|
|
53
|
+
? R
|
|
54
|
+
: never
|
|
55
|
+
: T extends object
|
|
56
|
+
? SnapshotOutOfObject<T> extends infer R
|
|
57
|
+
? R
|
|
58
|
+
: never
|
|
59
|
+
: T
|
|
60
|
+
|
|
61
|
+
// snapshot in
|
|
62
|
+
|
|
63
|
+
export type SnapshotInOfObject<T> = {
|
|
64
|
+
[k in keyof T]: SnapshotInOf<T[k]> extends infer R ? R : never
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type SnapshotInOfModel<M extends AnyModel> = ModelFromSnapshot<M>
|
|
68
|
+
|
|
69
|
+
export interface SnapshotInOfFrozen<F extends Frozen<any>> extends FrozenData<F["data"]> {}
|
|
70
|
+
|
|
71
|
+
export interface SnapshotInOfObjectMap<V> {
|
|
72
|
+
items?: { [k: string]: SnapshotOutOf<V> }
|
|
73
|
+
[modelTypeKey]?: string
|
|
74
|
+
[modelIdKey]: string
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface SnapshotInOfArraySet<V> {
|
|
78
|
+
items?: SnapshotOutOf<V>[]
|
|
79
|
+
[modelTypeKey]?: string
|
|
80
|
+
[modelIdKey]: string
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export type SnapshotInOf<T> =
|
|
84
|
+
T extends ObjectMap<infer V>
|
|
85
|
+
? SnapshotInOfObjectMap<V> extends infer R
|
|
86
|
+
? R
|
|
87
|
+
: never
|
|
88
|
+
: T extends ArraySet<infer V>
|
|
89
|
+
? SnapshotInOfArraySet<V> extends infer R
|
|
90
|
+
? R
|
|
91
|
+
: never
|
|
92
|
+
: T extends AnyModel
|
|
93
|
+
? SnapshotInOfModel<T> extends infer R
|
|
94
|
+
? R
|
|
95
|
+
: never
|
|
96
|
+
: T extends Frozen<any>
|
|
97
|
+
? SnapshotInOfFrozen<T> extends infer R
|
|
98
|
+
? R
|
|
99
|
+
: never
|
|
100
|
+
: T extends object
|
|
101
|
+
? SnapshotInOfObject<T> extends infer R
|
|
102
|
+
? R
|
|
103
|
+
: never
|
|
104
|
+
: T
|