@jbrowse/mobx-state-tree 5.6.10 → 5.7.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/dist/core/action.js.map +1 -1
- package/dist/core/actionContext.js.map +1 -1
- package/dist/core/flow.js.map +1 -1
- package/dist/core/json-patch.js.map +1 -1
- package/dist/core/mst-operations.js.map +1 -1
- package/dist/core/node/BaseNode.js.map +1 -1
- package/dist/core/node/Hook.js.map +1 -1
- package/dist/core/node/create-node.js.map +1 -1
- package/dist/core/node/identifier-cache.js.map +1 -1
- package/dist/core/node/livelinessChecking.js.map +1 -1
- package/dist/core/node/node-utils.js.map +1 -1
- package/dist/core/node/object-node.d.ts +3 -1
- package/dist/core/node/object-node.js +2 -1
- package/dist/core/node/object-node.js.map +1 -1
- package/dist/core/node/scalar-node.d.ts +3 -1
- package/dist/core/node/scalar-node.js +2 -1
- package/dist/core/node/scalar-node.js.map +1 -1
- package/dist/core/process.js.map +1 -1
- package/dist/core/type/type-checker.js.map +1 -1
- package/dist/core/type/type.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/internal.js.map +1 -1
- package/dist/middlewares/create-action-tracking-middleware.js.map +1 -1
- package/dist/middlewares/createActionTrackingMiddleware2.js.map +1 -1
- package/dist/middlewares/on-action.js.map +1 -1
- package/dist/package.json +1 -0
- package/dist/types/complex-types/array.d.ts +2 -1
- package/dist/types/complex-types/array.js +2 -1
- package/dist/types/complex-types/array.js.map +1 -1
- package/dist/types/complex-types/map.d.ts +2 -1
- package/dist/types/complex-types/map.js +2 -1
- package/dist/types/complex-types/map.js.map +1 -1
- package/dist/types/complex-types/model.d.ts +2 -1
- package/dist/types/complex-types/model.js +2 -1
- package/dist/types/complex-types/model.js.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/primitives.d.ts +2 -1
- package/dist/types/primitives.js +2 -1
- package/dist/types/primitives.js.map +1 -1
- package/dist/types/utility-types/custom.d.ts +2 -1
- package/dist/types/utility-types/custom.js +2 -1
- package/dist/types/utility-types/custom.js.map +1 -1
- package/dist/types/utility-types/enumeration.js.map +1 -1
- package/dist/types/utility-types/frozen.d.ts +2 -1
- package/dist/types/utility-types/frozen.js +2 -1
- package/dist/types/utility-types/frozen.js.map +1 -1
- package/dist/types/utility-types/identifier.d.ts +2 -1
- package/dist/types/utility-types/identifier.js +2 -1
- package/dist/types/utility-types/identifier.js.map +1 -1
- package/dist/types/utility-types/late.js +2 -1
- package/dist/types/utility-types/late.js.map +1 -1
- package/dist/types/utility-types/lazy.d.ts +2 -1
- package/dist/types/utility-types/lazy.js +2 -1
- package/dist/types/utility-types/lazy.js.map +1 -1
- package/dist/types/utility-types/literal.d.ts +2 -1
- package/dist/types/utility-types/literal.js +2 -1
- package/dist/types/utility-types/literal.js.map +1 -1
- package/dist/types/utility-types/maybe.js.map +1 -1
- package/dist/types/utility-types/optional.d.ts +2 -1
- package/dist/types/utility-types/optional.js +2 -1
- package/dist/types/utility-types/optional.js.map +1 -1
- package/dist/types/utility-types/reference.d.ts +2 -1
- package/dist/types/utility-types/reference.js +2 -1
- package/dist/types/utility-types/reference.js.map +1 -1
- package/dist/types/utility-types/refinement.js +2 -1
- package/dist/types/utility-types/refinement.js.map +1 -1
- package/dist/types/utility-types/resilient.js +2 -1
- package/dist/types/utility-types/resilient.js.map +1 -1
- package/dist/types/utility-types/snapshotProcessor.js +3 -2
- package/dist/types/utility-types/snapshotProcessor.js.map +1 -1
- package/dist/types/utility-types/union.d.ts +2 -1
- package/dist/types/utility-types/union.js +2 -1
- package/dist/types/utility-types/union.js.map +1 -1
- package/dist/utils.js.map +1 -1
- package/esm/core/action.d.ts +87 -0
- package/esm/core/action.js +219 -0
- package/esm/core/action.js.map +1 -0
- package/esm/core/actionContext.d.ts +27 -0
- package/esm/core/actionContext.js +37 -0
- package/esm/core/actionContext.js.map +1 -0
- package/esm/core/flow.d.ts +69 -0
- package/esm/core/flow.js +173 -0
- package/esm/core/flow.js.map +1 -0
- package/esm/core/json-patch.d.ts +46 -0
- package/esm/core/json-patch.js +125 -0
- package/esm/core/json-patch.js.map +1 -0
- package/esm/core/mst-operations.d.ts +459 -0
- package/esm/core/mst-operations.js +844 -0
- package/esm/core/mst-operations.js.map +1 -0
- package/esm/core/node/BaseNode.d.ts +62 -0
- package/esm/core/node/BaseNode.js +148 -0
- package/esm/core/node/BaseNode.js.map +1 -0
- package/esm/core/node/Hook.d.ts +17 -0
- package/esm/core/node/Hook.js +12 -0
- package/esm/core/node/Hook.js.map +1 -0
- package/esm/core/node/create-node.d.ts +16 -0
- package/esm/core/node/create-node.js +36 -0
- package/esm/core/node/create-node.js.map +1 -0
- package/esm/core/node/identifier-cache.d.ts +19 -0
- package/esm/core/node/identifier-cache.js +111 -0
- package/esm/core/node/identifier-cache.js.map +1 -0
- package/esm/core/node/livelinessChecking.d.ts +37 -0
- package/esm/core/node/livelinessChecking.js +33 -0
- package/esm/core/node/livelinessChecking.js.map +1 -0
- package/esm/core/node/node-utils.d.ts +83 -0
- package/esm/core/node/node-utils.js +153 -0
- package/esm/core/node/node-utils.js.map +1 -0
- package/esm/core/node/object-node.d.ts +101 -0
- package/esm/core/node/object-node.js +542 -0
- package/esm/core/node/object-node.js.map +1 -0
- package/esm/core/node/scalar-node.d.ts +21 -0
- package/esm/core/node/scalar-node.js +86 -0
- package/esm/core/node/scalar-node.js.map +1 -0
- package/esm/core/process.d.ts +50 -0
- package/esm/core/process.js +35 -0
- package/esm/core/process.js.map +1 -0
- package/esm/core/type/type-checker.d.ts +69 -0
- package/esm/core/type/type-checker.js +144 -0
- package/esm/core/type/type-checker.js.map +1 -0
- package/esm/core/type/type.d.ts +317 -0
- package/esm/core/type/type.js +243 -0
- package/esm/core/type/type.js.map +1 -0
- package/esm/index.d.ts +2 -0
- package/esm/index.js +2 -0
- package/esm/index.js.map +1 -0
- package/esm/internal.d.ts +39 -0
- package/esm/internal.js +44 -0
- package/esm/internal.js.map +1 -0
- package/esm/middlewares/create-action-tracking-middleware.d.ts +24 -0
- package/esm/middlewares/create-action-tracking-middleware.js +78 -0
- package/esm/middlewares/create-action-tracking-middleware.js.map +1 -0
- package/esm/middlewares/createActionTrackingMiddleware2.d.ts +34 -0
- package/esm/middlewares/createActionTrackingMiddleware2.js +130 -0
- package/esm/middlewares/createActionTrackingMiddleware2.js.map +1 -0
- package/esm/middlewares/on-action.d.ts +87 -0
- package/esm/middlewares/on-action.js +210 -0
- package/esm/middlewares/on-action.js.map +1 -0
- package/esm/types/complex-types/array.d.ts +81 -0
- package/esm/types/complex-types/array.js +347 -0
- package/esm/types/complex-types/array.js.map +1 -0
- package/esm/types/complex-types/map.d.ts +111 -0
- package/esm/types/complex-types/map.js +356 -0
- package/esm/types/complex-types/map.js.map +1 -0
- package/esm/types/complex-types/model.d.ts +193 -0
- package/esm/types/complex-types/model.js +471 -0
- package/esm/types/complex-types/model.js.map +1 -0
- package/esm/types/index.d.ts +33 -0
- package/esm/types/index.js +35 -0
- package/esm/types/index.js.map +1 -0
- package/esm/types/primitives.d.ts +125 -0
- package/esm/types/primitives.js +177 -0
- package/esm/types/primitives.js.map +1 -0
- package/esm/types/utility-types/custom.d.ts +75 -0
- package/esm/types/utility-types/custom.js +106 -0
- package/esm/types/utility-types/custom.js.map +1 -0
- package/esm/types/utility-types/enumeration.d.ts +5 -0
- package/esm/types/utility-types/enumeration.js +31 -0
- package/esm/types/utility-types/enumeration.js.map +1 -0
- package/esm/types/utility-types/frozen.d.ts +24 -0
- package/esm/types/utility-types/frozen.js +92 -0
- package/esm/types/utility-types/frozen.js.map +1 -0
- package/esm/types/utility-types/identifier.d.ts +87 -0
- package/esm/types/utility-types/identifier.js +121 -0
- package/esm/types/utility-types/identifier.js.map +1 -0
- package/esm/types/utility-types/late.d.ts +10 -0
- package/esm/types/utility-types/late.js +106 -0
- package/esm/types/utility-types/late.js.map +1 -0
- package/esm/types/utility-types/lazy.d.ts +23 -0
- package/esm/types/utility-types/lazy.js +72 -0
- package/esm/types/utility-types/lazy.js.map +1 -0
- package/esm/types/utility-types/literal.d.ts +38 -0
- package/esm/types/utility-types/literal.js +58 -0
- package/esm/types/utility-types/literal.js.map +1 -0
- package/esm/types/utility-types/maybe.d.ts +26 -0
- package/esm/types/utility-types/maybe.js +26 -0
- package/esm/types/utility-types/maybe.js.map +1 -0
- package/esm/types/utility-types/optional.d.ts +42 -0
- package/esm/types/utility-types/optional.js +135 -0
- package/esm/types/utility-types/optional.js.map +1 -0
- package/esm/types/utility-types/reference.d.ts +90 -0
- package/esm/types/utility-types/reference.js +383 -0
- package/esm/types/utility-types/reference.js.map +1 -0
- package/esm/types/utility-types/refinement.d.ts +10 -0
- package/esm/types/utility-types/refinement.js +82 -0
- package/esm/types/utility-types/refinement.js.map +1 -0
- package/esm/types/utility-types/resilient.d.ts +18 -0
- package/esm/types/utility-types/resilient.js +118 -0
- package/esm/types/utility-types/resilient.js.map +1 -0
- package/esm/types/utility-types/snapshotProcessor.d.ts +63 -0
- package/esm/types/utility-types/snapshotProcessor.js +159 -0
- package/esm/types/utility-types/snapshotProcessor.js.map +1 -0
- package/esm/types/utility-types/union.d.ts +78 -0
- package/esm/types/utility-types/union.js +240 -0
- package/esm/types/utility-types/union.js.map +1 -0
- package/esm/utils.d.ts +230 -0
- package/esm/utils.js +449 -0
- package/esm/utils.js.map +1 -0
- package/package.json +23 -22
- package/dist/mobx-state-tree.js +0 -86
- package/dist/mobx-state-tree.js.map +0 -1
- package/dist/mobx-state-tree.module.js +0 -84
- package/dist/mobx-state-tree.module.js.map +0 -1
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import { SimpleType } from "../../core/type/type.js";
|
|
2
|
+
import { Hook, NodeLifeCycle, TypeFlags, applyPatch, assertIsType, createScalarNode, devMode, fail, getIdentifier, getStateTreeNode, isModelType, isStateTreeNode, isValidIdentifier, maybe, normalizeIdentifier, typeCheckFailure, typeCheckSuccess } from "../../internal.js";
|
|
3
|
+
function getInvalidationCause(hook) {
|
|
4
|
+
switch (hook) {
|
|
5
|
+
case Hook.beforeDestroy:
|
|
6
|
+
return "destroy";
|
|
7
|
+
case Hook.beforeDetach:
|
|
8
|
+
return "detach";
|
|
9
|
+
default:
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
class StoredReference {
|
|
14
|
+
targetType;
|
|
15
|
+
identifier;
|
|
16
|
+
node;
|
|
17
|
+
resolvedReference;
|
|
18
|
+
constructor(value, targetType) {
|
|
19
|
+
this.targetType = targetType;
|
|
20
|
+
if (isValidIdentifier(value)) {
|
|
21
|
+
this.identifier = value;
|
|
22
|
+
}
|
|
23
|
+
else if (isStateTreeNode(value)) {
|
|
24
|
+
const targetNode = getStateTreeNode(value);
|
|
25
|
+
if (!targetNode.identifierAttribute) {
|
|
26
|
+
throw fail(`Can only store references with a defined identifier attribute.`);
|
|
27
|
+
}
|
|
28
|
+
const id = targetNode.unnormalizedIdentifier;
|
|
29
|
+
if (id === null || id === undefined) {
|
|
30
|
+
throw fail(`Can only store references to tree nodes with a defined identifier.`);
|
|
31
|
+
}
|
|
32
|
+
this.identifier = id;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
throw fail(`Can only store references to tree nodes or identifiers, got: '${value}'`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
updateResolvedReference(node) {
|
|
39
|
+
const normalizedId = normalizeIdentifier(this.identifier);
|
|
40
|
+
const root = node.root;
|
|
41
|
+
const lastCacheModification = root.identifierCache.getLastCacheModificationPerId(normalizedId);
|
|
42
|
+
if (!this.resolvedReference ||
|
|
43
|
+
this.resolvedReference.lastCacheModification !== lastCacheModification) {
|
|
44
|
+
const { targetType } = this;
|
|
45
|
+
// reference was initialized with the identifier of the target
|
|
46
|
+
const target = root.identifierCache.resolve(targetType, normalizedId);
|
|
47
|
+
if (!target) {
|
|
48
|
+
throw new InvalidReferenceError(`[mobx-state-tree] Failed to resolve reference '${this.identifier}' to type '${this.targetType.name}' (from node: ${node.path})`);
|
|
49
|
+
}
|
|
50
|
+
this.resolvedReference = {
|
|
51
|
+
node: target,
|
|
52
|
+
lastCacheModification: lastCacheModification
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
get resolvedValue() {
|
|
57
|
+
this.updateResolvedReference(this.node);
|
|
58
|
+
return this.resolvedReference.node.value;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @internal
|
|
63
|
+
* @hidden
|
|
64
|
+
*/
|
|
65
|
+
export class InvalidReferenceError extends Error {
|
|
66
|
+
constructor(m) {
|
|
67
|
+
super(m);
|
|
68
|
+
Object.setPrototypeOf(this, InvalidReferenceError.prototype);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @internal
|
|
73
|
+
* @hidden
|
|
74
|
+
*/
|
|
75
|
+
export class BaseReferenceType extends SimpleType {
|
|
76
|
+
targetType;
|
|
77
|
+
onInvalidated;
|
|
78
|
+
flags = TypeFlags.Reference;
|
|
79
|
+
constructor(targetType, onInvalidated) {
|
|
80
|
+
super(`reference(${targetType.name})`);
|
|
81
|
+
this.targetType = targetType;
|
|
82
|
+
this.onInvalidated = onInvalidated;
|
|
83
|
+
}
|
|
84
|
+
describe() {
|
|
85
|
+
return this.name;
|
|
86
|
+
}
|
|
87
|
+
isAssignableFrom(type) {
|
|
88
|
+
return this.targetType.isAssignableFrom(type);
|
|
89
|
+
}
|
|
90
|
+
isValidSnapshot(value, context) {
|
|
91
|
+
return isValidIdentifier(value)
|
|
92
|
+
? typeCheckSuccess()
|
|
93
|
+
: typeCheckFailure(context, value, "Value is not a valid identifier, which is a string or a number");
|
|
94
|
+
}
|
|
95
|
+
fireInvalidated(cause, storedRefNode, referenceId, refTargetNode) {
|
|
96
|
+
// to actually invalidate a reference we need an alive parent,
|
|
97
|
+
// since it is a scalar value (immutable-ish) and we need to change it
|
|
98
|
+
// from the parent
|
|
99
|
+
const storedRefParentNode = storedRefNode.parent;
|
|
100
|
+
if (!storedRefParentNode || !storedRefParentNode.isAlive) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const storedRefParentValue = storedRefParentNode.storedValue;
|
|
104
|
+
if (!storedRefParentValue) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
this.onInvalidated({
|
|
108
|
+
cause,
|
|
109
|
+
parent: storedRefParentValue,
|
|
110
|
+
invalidTarget: refTargetNode ? refTargetNode.storedValue : undefined,
|
|
111
|
+
invalidId: referenceId,
|
|
112
|
+
replaceRef(newRef) {
|
|
113
|
+
applyPatch(storedRefNode.root.storedValue, {
|
|
114
|
+
op: "replace",
|
|
115
|
+
value: newRef,
|
|
116
|
+
path: storedRefNode.path
|
|
117
|
+
});
|
|
118
|
+
},
|
|
119
|
+
removeRef() {
|
|
120
|
+
if (isModelType(storedRefParentNode.type)) {
|
|
121
|
+
this.replaceRef(undefined);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
applyPatch(storedRefNode.root.storedValue, {
|
|
125
|
+
op: "remove",
|
|
126
|
+
path: storedRefNode.path
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
addTargetNodeWatcher(storedRefNode, referenceId) {
|
|
133
|
+
// this will make sure the target node becomes created
|
|
134
|
+
const refTargetValue = this.getValue(storedRefNode);
|
|
135
|
+
if (!refTargetValue) {
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
const refTargetNode = getStateTreeNode(refTargetValue);
|
|
139
|
+
const hookHandler = (_, refTargetNodeHook) => {
|
|
140
|
+
const cause = getInvalidationCause(refTargetNodeHook);
|
|
141
|
+
if (!cause) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
this.fireInvalidated(cause, storedRefNode, referenceId, refTargetNode);
|
|
145
|
+
};
|
|
146
|
+
const refTargetDetachHookDisposer = refTargetNode.registerHook(Hook.beforeDetach, hookHandler);
|
|
147
|
+
const refTargetDestroyHookDisposer = refTargetNode.registerHook(Hook.beforeDestroy, hookHandler);
|
|
148
|
+
return () => {
|
|
149
|
+
refTargetDetachHookDisposer();
|
|
150
|
+
refTargetDestroyHookDisposer();
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
watchTargetNodeForInvalidations(storedRefNode, identifier, customGetSet) {
|
|
154
|
+
if (!this.onInvalidated) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
let onRefTargetDestroyedHookDisposer;
|
|
158
|
+
// get rid of the watcher hook when the stored ref node is destroyed
|
|
159
|
+
// detached is ignored since scalar nodes (where the reference resides) cannot be detached
|
|
160
|
+
storedRefNode.registerHook(Hook.beforeDestroy, () => {
|
|
161
|
+
if (onRefTargetDestroyedHookDisposer) {
|
|
162
|
+
onRefTargetDestroyedHookDisposer();
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
const startWatching = (sync) => {
|
|
166
|
+
// re-create hook in case the stored ref gets reattached
|
|
167
|
+
if (onRefTargetDestroyedHookDisposer) {
|
|
168
|
+
onRefTargetDestroyedHookDisposer();
|
|
169
|
+
}
|
|
170
|
+
// make sure the target node is actually there and initialized
|
|
171
|
+
const storedRefParentNode = storedRefNode.parent;
|
|
172
|
+
const storedRefParentValue = storedRefParentNode && storedRefParentNode.storedValue;
|
|
173
|
+
if (storedRefParentNode &&
|
|
174
|
+
storedRefParentNode.isAlive &&
|
|
175
|
+
storedRefParentValue) {
|
|
176
|
+
let refTargetNodeExists;
|
|
177
|
+
if (customGetSet) {
|
|
178
|
+
refTargetNodeExists = !!customGetSet.get(identifier, storedRefParentValue);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
refTargetNodeExists = storedRefNode.root.identifierCache.has(this.targetType, normalizeIdentifier(identifier));
|
|
182
|
+
}
|
|
183
|
+
if (!refTargetNodeExists) {
|
|
184
|
+
// we cannot change the reference in sync mode
|
|
185
|
+
// since we are in the middle of a reconciliation/instantiation and the change would be overwritten
|
|
186
|
+
// for those cases just let the wrong reference be assigned and fail upon usage
|
|
187
|
+
// (like current references do)
|
|
188
|
+
// this means that effectively this code will only run when it is created from a snapshot
|
|
189
|
+
if (!sync) {
|
|
190
|
+
this.fireInvalidated("invalidSnapshotReference", storedRefNode, identifier, null);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
onRefTargetDestroyedHookDisposer = this.addTargetNodeWatcher(storedRefNode, identifier);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
if (storedRefNode.state === NodeLifeCycle.FINALIZED) {
|
|
199
|
+
// already attached, so the whole tree is ready
|
|
200
|
+
startWatching(true);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
if (!storedRefNode.isRoot) {
|
|
204
|
+
// start watching once the whole tree is ready
|
|
205
|
+
storedRefNode.root.registerHook(Hook.afterCreationFinalization, () => {
|
|
206
|
+
// make sure to attach it so it can start listening
|
|
207
|
+
if (storedRefNode.parent) {
|
|
208
|
+
storedRefNode.parent.createObservableInstanceIfNeeded();
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
// start watching once the node is attached somewhere / parent changes
|
|
213
|
+
storedRefNode.registerHook(Hook.afterAttach, () => {
|
|
214
|
+
startWatching(false);
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* @internal
|
|
221
|
+
* @hidden
|
|
222
|
+
*/
|
|
223
|
+
export class IdentifierReferenceType extends BaseReferenceType {
|
|
224
|
+
constructor(targetType, onInvalidated) {
|
|
225
|
+
super(targetType, onInvalidated);
|
|
226
|
+
}
|
|
227
|
+
getValue(storedRefNode) {
|
|
228
|
+
if (!storedRefNode.isAlive) {
|
|
229
|
+
return undefined;
|
|
230
|
+
}
|
|
231
|
+
const storedRef = storedRefNode.storedValue;
|
|
232
|
+
return storedRef.resolvedValue;
|
|
233
|
+
}
|
|
234
|
+
getSnapshot(storedRefNode) {
|
|
235
|
+
const ref = storedRefNode.storedValue;
|
|
236
|
+
return ref.identifier;
|
|
237
|
+
}
|
|
238
|
+
instantiate(parent, subpath, environment, initialValue) {
|
|
239
|
+
const identifier = isStateTreeNode(initialValue)
|
|
240
|
+
? getIdentifier(initialValue)
|
|
241
|
+
: initialValue;
|
|
242
|
+
const storedRef = new StoredReference(initialValue, this.targetType);
|
|
243
|
+
const storedRefNode = createScalarNode(this, parent, subpath, environment, storedRef);
|
|
244
|
+
storedRef.node = storedRefNode;
|
|
245
|
+
this.watchTargetNodeForInvalidations(storedRefNode, identifier, undefined);
|
|
246
|
+
return storedRefNode;
|
|
247
|
+
}
|
|
248
|
+
reconcile(current, newValue, parent, subpath) {
|
|
249
|
+
if (!current.isDetaching && current.type === this) {
|
|
250
|
+
const compareByValue = isStateTreeNode(newValue);
|
|
251
|
+
const ref = current.storedValue;
|
|
252
|
+
if ((!compareByValue && ref.identifier === newValue) ||
|
|
253
|
+
(compareByValue && ref.resolvedValue === newValue)) {
|
|
254
|
+
current.setParent(parent, subpath);
|
|
255
|
+
return current;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const newNode = this.instantiate(parent, subpath, undefined, newValue);
|
|
259
|
+
current.die(); // noop if detaching
|
|
260
|
+
return newNode;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* @internal
|
|
265
|
+
* @hidden
|
|
266
|
+
*/
|
|
267
|
+
export class CustomReferenceType extends BaseReferenceType {
|
|
268
|
+
options;
|
|
269
|
+
constructor(targetType, options, onInvalidated) {
|
|
270
|
+
super(targetType, onInvalidated);
|
|
271
|
+
this.options = options;
|
|
272
|
+
}
|
|
273
|
+
getValue(storedRefNode) {
|
|
274
|
+
if (!storedRefNode.isAlive) {
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
const referencedNode = this.options.get(storedRefNode.storedValue, storedRefNode.parent ? storedRefNode.parent.storedValue : null);
|
|
278
|
+
return referencedNode;
|
|
279
|
+
}
|
|
280
|
+
getSnapshot(storedRefNode) {
|
|
281
|
+
return storedRefNode.storedValue;
|
|
282
|
+
}
|
|
283
|
+
instantiate(parent, subpath, environment, newValue) {
|
|
284
|
+
const identifier = isStateTreeNode(newValue)
|
|
285
|
+
? this.options.set(newValue, parent ? parent.storedValue : null)
|
|
286
|
+
: newValue;
|
|
287
|
+
const storedRefNode = createScalarNode(this, parent, subpath, environment, identifier);
|
|
288
|
+
this.watchTargetNodeForInvalidations(storedRefNode, identifier, this.options);
|
|
289
|
+
return storedRefNode;
|
|
290
|
+
}
|
|
291
|
+
reconcile(current, newValue, parent, subpath) {
|
|
292
|
+
const newIdentifier = isStateTreeNode(newValue)
|
|
293
|
+
? this.options.set(newValue, current ? current.storedValue : null)
|
|
294
|
+
: newValue;
|
|
295
|
+
if (!current.isDetaching &&
|
|
296
|
+
current.type === this &&
|
|
297
|
+
current.storedValue === newIdentifier) {
|
|
298
|
+
current.setParent(parent, subpath);
|
|
299
|
+
return current;
|
|
300
|
+
}
|
|
301
|
+
const newNode = this.instantiate(parent, subpath, undefined, newIdentifier);
|
|
302
|
+
current.die(); // noop if detaching
|
|
303
|
+
return newNode;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* `types.reference` - Creates a reference to another type, which should have defined an identifier.
|
|
308
|
+
* See also the [reference and identifiers](https://github.com/mobxjs/mobx-state-tree#references-and-identifiers) section.
|
|
309
|
+
*/
|
|
310
|
+
export function reference(subType, options) {
|
|
311
|
+
assertIsType(subType, 1);
|
|
312
|
+
if (devMode()) {
|
|
313
|
+
if (options && typeof options === "string") {
|
|
314
|
+
// istanbul ignore next
|
|
315
|
+
throw fail("References with base path are no longer supported. Please remove the base path.");
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
const getSetOptions = options
|
|
319
|
+
? options
|
|
320
|
+
: undefined;
|
|
321
|
+
const onInvalidated = options
|
|
322
|
+
? options.onInvalidated
|
|
323
|
+
: undefined;
|
|
324
|
+
if (getSetOptions && (getSetOptions.get || getSetOptions.set)) {
|
|
325
|
+
if (devMode()) {
|
|
326
|
+
if (!getSetOptions.get || !getSetOptions.set) {
|
|
327
|
+
throw fail("reference options must either contain both a 'get' and a 'set' method or none of them");
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return new CustomReferenceType(subType, {
|
|
331
|
+
get: getSetOptions.get,
|
|
332
|
+
set: getSetOptions.set
|
|
333
|
+
}, onInvalidated);
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
return new IdentifierReferenceType(subType, onInvalidated);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Returns if a given value represents a reference type.
|
|
341
|
+
*
|
|
342
|
+
* @param type
|
|
343
|
+
* @returns
|
|
344
|
+
*/
|
|
345
|
+
export function isReferenceType(type) {
|
|
346
|
+
return (type.flags & TypeFlags.Reference) > 0;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* `types.safeReference` - A safe reference is like a standard reference, except that it accepts the undefined value by default
|
|
350
|
+
* and automatically sets itself to undefined (when the parent is a model) / removes itself from arrays and maps
|
|
351
|
+
* when the reference it is pointing to gets detached/destroyed.
|
|
352
|
+
*
|
|
353
|
+
* The optional options parameter object accepts a parameter named `acceptsUndefined`, which is set to true by default, so it is suitable
|
|
354
|
+
* for model properties.
|
|
355
|
+
* When used inside collections (arrays/maps), it is recommended to set this option to false so it can't take undefined as value,
|
|
356
|
+
* which is usually the desired in those cases.
|
|
357
|
+
* Additionally, the optional options parameter object accepts a parameter named `onInvalidated`, which will be called when the reference target node that the reference is pointing to is about to be detached/destroyed
|
|
358
|
+
*
|
|
359
|
+
* Strictly speaking it is a `types.maybe(types.reference(X))` (when `acceptsUndefined` is set to true, the default) and
|
|
360
|
+
* `types.reference(X)` (when `acceptsUndefined` is set to false), both of them with a customized `onInvalidated` option.
|
|
361
|
+
*
|
|
362
|
+
* @param subType
|
|
363
|
+
* @param options
|
|
364
|
+
* @returns
|
|
365
|
+
*/
|
|
366
|
+
export function safeReference(subType, options) {
|
|
367
|
+
const refType = reference(subType, {
|
|
368
|
+
...options,
|
|
369
|
+
onInvalidated(ev) {
|
|
370
|
+
if (options && options.onInvalidated) {
|
|
371
|
+
options.onInvalidated(ev);
|
|
372
|
+
}
|
|
373
|
+
ev.removeRef();
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
if (options && options.acceptsUndefined === false) {
|
|
377
|
+
return refType;
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
return maybe(refType);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
//# sourceMappingURL=reference.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reference.js","sourceRoot":"","sources":["../../../src/types/utility-types/reference.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,EAGL,IAAI,EAUJ,aAAa,EAEb,SAAS,EACT,UAAU,EACV,YAAY,EACZ,gBAAgB,EAChB,OAAO,EACP,IAAI,EACJ,aAAa,EACb,gBAAgB,EAChB,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,KAAK,EACL,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,mBAAmB,CAAA;AAe1B,SAAS,oBAAoB,CAAC,IAAU;IACtC,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,IAAI,CAAC,aAAa;YACrB,OAAO,SAAS,CAAA;QAClB,KAAK,IAAI,CAAC,YAAY;YACpB,OAAO,QAAQ,CAAA;QACjB;YACE,OAAO,SAAS,CAAA;IACpB,CAAC;AACH,CAAC;AAMD,MAAM,eAAe;IAWA;IAVV,UAAU,CAAsB;IACzC,IAAI,CAAU;IAEN,iBAAiB,CAGxB;IAED,YACE,KAA2C,EAC1B,UAAc;QAAd,eAAU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,OAGC;IAED,MAAM,OAAO,GAAG,SAAS,CAAC,OAAO,EAAE;QACjC,GAAG,OAAO;QACV,aAAa,CAAC,EAAE;YACd,IAAI,OAAO,IAAI,OAAO,CAAC,aAAa,EAAE,CAAC;gBACrC,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC,CAAA;YAC3B,CAAC;YACD,EAAE,CAAC,SAAS,EAAE,CAAA;QAChB,CAAC;KACF,CAAC,CAAA;IAEF,IAAI,OAAO,IAAI,OAAO,CAAC,gBAAgB,KAAK,KAAK,EAAE,CAAC;QAClD,OAAO,OAAO,CAAA;IAChB,CAAC;SAAM,CAAC;QACN,OAAO,KAAK,CAAC,OAAO,CAAC,CAAA;IACvB,CAAC;AACH,CAAC","sourcesContent":["import { SimpleType } from \"../../core/type/type.ts\"\nimport {\n type AnyNode,\n type AnyObjectNode,\n Hook,\n type IAnyComplexType,\n type IAnyStateTreeNode,\n type IAnyType,\n type IDisposer,\n type IMaybe,\n type IStateTreeNode,\n type IType,\n type IValidationContext,\n type IValidationResult,\n NodeLifeCycle,\n type ReferenceIdentifier,\n TypeFlags,\n applyPatch,\n assertIsType,\n createScalarNode,\n devMode,\n fail,\n getIdentifier,\n getStateTreeNode,\n isModelType,\n isStateTreeNode,\n isValidIdentifier,\n maybe,\n normalizeIdentifier,\n typeCheckFailure,\n typeCheckSuccess\n} from \"../../internal.ts\"\n\nexport type OnReferenceInvalidatedEvent<STN extends IAnyStateTreeNode> = {\n parent: IAnyStateTreeNode\n invalidTarget: STN | undefined\n invalidId: ReferenceIdentifier\n replaceRef: (newRef: STN | null | undefined) => void\n removeRef: () => void\n cause: \"detach\" | \"destroy\" | \"invalidSnapshotReference\"\n}\n\nexport type OnReferenceInvalidated<STN extends IAnyStateTreeNode> = (\n event: OnReferenceInvalidatedEvent<STN>\n) => void\n\nfunction getInvalidationCause(hook: Hook): \"detach\" | \"destroy\" | undefined {\n switch (hook) {\n case Hook.beforeDestroy:\n return \"destroy\"\n case Hook.beforeDetach:\n return \"detach\"\n default:\n return undefined\n }\n}\n\n/** @hidden */\nexport type ReferenceT<IT extends IAnyType> = IT[\"TypeWithoutSTN\"] &\n IStateTreeNode<IReferenceType<IT>>\n\nclass StoredReference<IT extends IAnyType> {\n readonly identifier!: ReferenceIdentifier\n node!: AnyNode\n\n private resolvedReference?: {\n node: AnyObjectNode\n lastCacheModification: string\n }\n\n constructor(\n value: ReferenceT<IT> | ReferenceIdentifier,\n private readonly targetType: IT\n ) {\n if (isValidIdentifier(value)) {\n this.identifier = value\n } else if (isStateTreeNode(value)) {\n const targetNode = getStateTreeNode(value)\n if (!targetNode.identifierAttribute) {\n throw fail(\n `Can only store references with a defined identifier attribute.`\n )\n }\n const id = targetNode.unnormalizedIdentifier\n if (id === null || id === undefined) {\n throw fail(\n `Can only store references to tree nodes with a defined identifier.`\n )\n }\n this.identifier = id\n } else {\n throw fail(\n `Can only store references to tree nodes or identifiers, got: '${value}'`\n )\n }\n }\n\n private updateResolvedReference(node: AnyNode) {\n const normalizedId = normalizeIdentifier(this.identifier)\n const root = node.root\n const lastCacheModification =\n root.identifierCache!.getLastCacheModificationPerId(normalizedId)\n if (\n !this.resolvedReference ||\n this.resolvedReference.lastCacheModification !== lastCacheModification\n ) {\n const { targetType } = this\n // reference was initialized with the identifier of the target\n\n const target = root.identifierCache!.resolve(targetType, normalizedId)\n\n if (!target) {\n throw new InvalidReferenceError(\n `[mobx-state-tree] Failed to resolve reference '${this.identifier}' to type '${this.targetType.name}' (from node: ${node.path})`\n )\n }\n\n this.resolvedReference = {\n node: target!,\n lastCacheModification: lastCacheModification\n }\n }\n }\n\n get resolvedValue(): ReferenceT<IT> {\n this.updateResolvedReference(this.node)\n return this.resolvedReference!.node.value\n }\n}\n\n/**\n * @internal\n * @hidden\n */\nexport class InvalidReferenceError extends Error {\n constructor(m: string) {\n super(m)\n\n Object.setPrototypeOf(this, InvalidReferenceError.prototype)\n }\n}\n\n/**\n * @internal\n * @hidden\n */\nexport abstract class BaseReferenceType<\n IT extends IAnyComplexType\n> extends SimpleType<\n ReferenceIdentifier,\n ReferenceIdentifier,\n IT[\"TypeWithoutSTN\"]\n> {\n readonly flags = TypeFlags.Reference\n\n constructor(\n protected readonly targetType: IT,\n private readonly onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>>\n ) {\n super(`reference(${targetType.name})`)\n }\n\n describe() {\n return this.name\n }\n\n isAssignableFrom(type: IAnyType): boolean {\n return this.targetType.isAssignableFrom(type)\n }\n\n isValidSnapshot(\n value: this[\"C\"],\n context: IValidationContext\n ): IValidationResult {\n return isValidIdentifier(value)\n ? typeCheckSuccess()\n : typeCheckFailure(\n context,\n value,\n \"Value is not a valid identifier, which is a string or a number\"\n )\n }\n\n private fireInvalidated(\n cause: \"detach\" | \"destroy\" | \"invalidSnapshotReference\",\n storedRefNode: this[\"N\"],\n referenceId: ReferenceIdentifier,\n refTargetNode: AnyObjectNode | null\n ) {\n // to actually invalidate a reference we need an alive parent,\n // since it is a scalar value (immutable-ish) and we need to change it\n // from the parent\n const storedRefParentNode = storedRefNode.parent\n if (!storedRefParentNode || !storedRefParentNode.isAlive) {\n return\n }\n const storedRefParentValue = storedRefParentNode.storedValue\n if (!storedRefParentValue) {\n return\n }\n this.onInvalidated!({\n cause,\n parent: storedRefParentValue,\n invalidTarget: refTargetNode ? refTargetNode.storedValue : undefined,\n invalidId: referenceId,\n replaceRef(newRef) {\n applyPatch(storedRefNode.root.storedValue, {\n op: \"replace\",\n value: newRef,\n path: storedRefNode.path\n })\n },\n removeRef() {\n if (isModelType(storedRefParentNode.type)) {\n this.replaceRef(undefined as any)\n } else {\n applyPatch(storedRefNode.root.storedValue, {\n op: \"remove\",\n path: storedRefNode.path\n })\n }\n }\n })\n }\n\n private addTargetNodeWatcher(\n storedRefNode: this[\"N\"],\n referenceId: ReferenceIdentifier\n ): IDisposer | undefined {\n // this will make sure the target node becomes created\n const refTargetValue = this.getValue(storedRefNode)\n if (!refTargetValue) {\n return undefined\n }\n const refTargetNode = getStateTreeNode(refTargetValue)\n\n const hookHandler = (_: AnyNode, refTargetNodeHook: Hook) => {\n const cause = getInvalidationCause(refTargetNodeHook)\n if (!cause) {\n return\n }\n this.fireInvalidated(cause, storedRefNode, referenceId, refTargetNode)\n }\n\n const refTargetDetachHookDisposer = refTargetNode.registerHook(\n Hook.beforeDetach,\n hookHandler\n )\n const refTargetDestroyHookDisposer = refTargetNode.registerHook(\n Hook.beforeDestroy,\n hookHandler\n )\n\n return () => {\n refTargetDetachHookDisposer()\n refTargetDestroyHookDisposer()\n }\n }\n\n protected watchTargetNodeForInvalidations(\n storedRefNode: this[\"N\"],\n identifier: ReferenceIdentifier,\n customGetSet: ReferenceOptionsGetSet<IT> | undefined\n ) {\n if (!this.onInvalidated) {\n return\n }\n\n let onRefTargetDestroyedHookDisposer: IDisposer | undefined\n\n // get rid of the watcher hook when the stored ref node is destroyed\n // detached is ignored since scalar nodes (where the reference resides) cannot be detached\n storedRefNode.registerHook(Hook.beforeDestroy, () => {\n if (onRefTargetDestroyedHookDisposer) {\n onRefTargetDestroyedHookDisposer()\n }\n })\n\n const startWatching = (sync: boolean) => {\n // re-create hook in case the stored ref gets reattached\n if (onRefTargetDestroyedHookDisposer) {\n onRefTargetDestroyedHookDisposer()\n }\n\n // make sure the target node is actually there and initialized\n const storedRefParentNode = storedRefNode.parent\n const storedRefParentValue =\n storedRefParentNode && storedRefParentNode.storedValue\n if (\n storedRefParentNode &&\n storedRefParentNode.isAlive &&\n storedRefParentValue\n ) {\n let refTargetNodeExists: boolean\n if (customGetSet) {\n refTargetNodeExists = !!customGetSet.get(\n identifier,\n storedRefParentValue\n )\n } else {\n refTargetNodeExists = storedRefNode.root.identifierCache!.has(\n this.targetType,\n normalizeIdentifier(identifier)\n )\n }\n\n if (!refTargetNodeExists) {\n // we cannot change the reference in sync mode\n // since we are in the middle of a reconciliation/instantiation and the change would be overwritten\n // for those cases just let the wrong reference be assigned and fail upon usage\n // (like current references do)\n // this means that effectively this code will only run when it is created from a snapshot\n if (!sync) {\n this.fireInvalidated(\n \"invalidSnapshotReference\",\n storedRefNode,\n identifier,\n null\n )\n }\n } else {\n onRefTargetDestroyedHookDisposer = this.addTargetNodeWatcher(\n storedRefNode,\n identifier\n )\n }\n }\n }\n\n if (storedRefNode.state === NodeLifeCycle.FINALIZED) {\n // already attached, so the whole tree is ready\n startWatching(true)\n } else {\n if (!storedRefNode.isRoot) {\n // start watching once the whole tree is ready\n storedRefNode.root.registerHook(Hook.afterCreationFinalization, () => {\n // make sure to attach it so it can start listening\n if (storedRefNode.parent) {\n storedRefNode.parent.createObservableInstanceIfNeeded()\n }\n })\n }\n // start watching once the node is attached somewhere / parent changes\n storedRefNode.registerHook(Hook.afterAttach, () => {\n startWatching(false)\n })\n }\n }\n}\n\n/**\n * @internal\n * @hidden\n */\nexport class IdentifierReferenceType<\n IT extends IAnyComplexType\n> extends BaseReferenceType<IT> {\n constructor(\n targetType: IT,\n onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>>\n ) {\n super(targetType, onInvalidated)\n }\n\n getValue(storedRefNode: this[\"N\"]) {\n if (!storedRefNode.isAlive) {\n return undefined\n }\n const storedRef: StoredReference<IT> = storedRefNode.storedValue\n return storedRef.resolvedValue as any\n }\n\n getSnapshot(storedRefNode: this[\"N\"]) {\n const ref: StoredReference<IT> = storedRefNode.storedValue\n return ref.identifier\n }\n\n instantiate(\n parent: AnyObjectNode | null,\n subpath: string,\n environment: any,\n initialValue: this[\"C\"] | this[\"T\"]\n ): this[\"N\"] {\n const identifier = isStateTreeNode(initialValue)\n ? getIdentifier(initialValue)!\n : initialValue\n const storedRef = new StoredReference(initialValue, this.targetType as any)\n const storedRefNode: this[\"N\"] = createScalarNode(\n this,\n parent,\n subpath,\n environment,\n storedRef as any\n )\n storedRef.node = storedRefNode\n this.watchTargetNodeForInvalidations(\n storedRefNode,\n identifier as string,\n undefined\n )\n return storedRefNode\n }\n\n reconcile(\n current: this[\"N\"],\n newValue: this[\"C\"] | this[\"T\"],\n parent: AnyObjectNode,\n subpath: string\n ): this[\"N\"] {\n if (!current.isDetaching && current.type === this) {\n const compareByValue = isStateTreeNode(newValue)\n const ref: StoredReference<IT> = current.storedValue\n if (\n (!compareByValue && ref.identifier === newValue) ||\n (compareByValue && ref.resolvedValue === newValue)\n ) {\n current.setParent(parent, subpath)\n return current\n }\n }\n const newNode = this.instantiate(parent, subpath, undefined, newValue)\n current.die() // noop if detaching\n return newNode\n }\n}\n\n/**\n * @internal\n * @hidden\n */\nexport class CustomReferenceType<\n IT extends IAnyComplexType\n> extends BaseReferenceType<IT> {\n constructor(\n targetType: IT,\n private readonly options: ReferenceOptionsGetSet<IT>,\n onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>>\n ) {\n super(targetType, onInvalidated)\n }\n\n getValue(storedRefNode: this[\"N\"]) {\n if (!storedRefNode.isAlive) {\n return undefined as any\n }\n const referencedNode = this.options.get(\n storedRefNode.storedValue,\n storedRefNode.parent ? storedRefNode.parent.storedValue : null\n )\n return referencedNode\n }\n\n getSnapshot(storedRefNode: this[\"N\"]) {\n return storedRefNode.storedValue\n }\n\n instantiate(\n parent: AnyObjectNode | null,\n subpath: string,\n environment: any,\n newValue: this[\"C\"] | this[\"T\"]\n ): this[\"N\"] {\n const identifier = isStateTreeNode(newValue)\n ? this.options.set(newValue as any, parent ? parent.storedValue : null)\n : newValue\n const storedRefNode: this[\"N\"] = createScalarNode(\n this,\n parent,\n subpath,\n environment,\n identifier as any\n )\n this.watchTargetNodeForInvalidations(\n storedRefNode,\n identifier as string,\n this.options\n )\n return storedRefNode\n }\n\n reconcile(\n current: this[\"N\"],\n newValue: this[\"C\"] | this[\"T\"],\n parent: AnyObjectNode,\n subpath: string\n ): this[\"N\"] {\n const newIdentifier = isStateTreeNode(newValue)\n ? this.options.set(newValue as any, current ? current.storedValue : null)\n : newValue\n if (\n !current.isDetaching &&\n current.type === this &&\n current.storedValue === newIdentifier\n ) {\n current.setParent(parent, subpath)\n return current\n }\n const newNode = this.instantiate(parent, subpath, undefined, newIdentifier)\n current.die() // noop if detaching\n return newNode\n }\n}\n\nexport interface ReferenceOptionsGetSet<IT extends IAnyComplexType> {\n get(\n identifier: ReferenceIdentifier,\n parent: IAnyStateTreeNode | null\n ): ReferenceT<IT>\n set(\n value: ReferenceT<IT>,\n parent: IAnyStateTreeNode | null\n ): ReferenceIdentifier\n}\n\nexport interface ReferenceOptionsOnInvalidated<IT extends IAnyComplexType> {\n // called when the current reference is about to become invalid\n onInvalidated: OnReferenceInvalidated<ReferenceT<IT>>\n}\n\nexport type ReferenceOptions<IT extends IAnyComplexType> =\n | ReferenceOptionsGetSet<IT>\n | ReferenceOptionsOnInvalidated<IT>\n | (ReferenceOptionsGetSet<IT> & ReferenceOptionsOnInvalidated<IT>)\n\n/** @hidden */\nexport interface IReferenceType<IT extends IAnyComplexType> extends IType<\n ReferenceIdentifier,\n ReferenceIdentifier,\n IT[\"TypeWithoutSTN\"]\n> {}\n\n/**\n * `types.reference` - Creates a reference to another type, which should have defined an identifier.\n * See also the [reference and identifiers](https://github.com/mobxjs/mobx-state-tree#references-and-identifiers) section.\n */\nexport function reference<IT extends IAnyComplexType>(\n subType: IT,\n options?: ReferenceOptions<IT>\n): IReferenceType<IT> {\n assertIsType(subType, 1)\n if (devMode()) {\n if (options && typeof options === \"string\") {\n // istanbul ignore next\n throw fail(\n \"References with base path are no longer supported. Please remove the base path.\"\n )\n }\n }\n\n const getSetOptions = options\n ? (options as ReferenceOptionsGetSet<IT>)\n : undefined\n const onInvalidated = options\n ? (options as ReferenceOptionsOnInvalidated<IT>).onInvalidated\n : undefined\n\n if (getSetOptions && (getSetOptions.get || getSetOptions.set)) {\n if (devMode()) {\n if (!getSetOptions.get || !getSetOptions.set) {\n throw fail(\n \"reference options must either contain both a 'get' and a 'set' method or none of them\"\n )\n }\n }\n\n return new CustomReferenceType(\n subType,\n {\n get: getSetOptions.get,\n set: getSetOptions.set\n },\n onInvalidated\n )\n } else {\n return new IdentifierReferenceType(subType, onInvalidated)\n }\n}\n\n/**\n * Returns if a given value represents a reference type.\n *\n * @param type\n * @returns\n */\nexport function isReferenceType<IT extends IReferenceType<any>>(\n type: IT\n): type is IT {\n return (type.flags & TypeFlags.Reference) > 0\n}\n\nexport function safeReference<IT extends IAnyComplexType>(\n subType: IT,\n options: (ReferenceOptionsGetSet<IT> | object) & {\n acceptsUndefined: false\n onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>>\n }\n): IReferenceType<IT>\nexport function safeReference<IT extends IAnyComplexType>(\n subType: IT,\n options?: (ReferenceOptionsGetSet<IT> | object) & {\n acceptsUndefined?: boolean\n onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>>\n }\n): IMaybe<IReferenceType<IT>>\n/**\n * `types.safeReference` - A safe reference is like a standard reference, except that it accepts the undefined value by default\n * and automatically sets itself to undefined (when the parent is a model) / removes itself from arrays and maps\n * when the reference it is pointing to gets detached/destroyed.\n *\n * The optional options parameter object accepts a parameter named `acceptsUndefined`, which is set to true by default, so it is suitable\n * for model properties.\n * When used inside collections (arrays/maps), it is recommended to set this option to false so it can't take undefined as value,\n * which is usually the desired in those cases.\n * Additionally, the optional options parameter object accepts a parameter named `onInvalidated`, which will be called when the reference target node that the reference is pointing to is about to be detached/destroyed\n *\n * Strictly speaking it is a `types.maybe(types.reference(X))` (when `acceptsUndefined` is set to true, the default) and\n * `types.reference(X)` (when `acceptsUndefined` is set to false), both of them with a customized `onInvalidated` option.\n *\n * @param subType\n * @param options\n * @returns\n */\nexport function safeReference<IT extends IAnyComplexType>(\n subType: IT,\n options?: (ReferenceOptionsGetSet<IT> | object) & {\n acceptsUndefined?: boolean\n onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>>\n }\n): IReferenceType<IT> | IMaybe<IReferenceType<IT>> {\n const refType = reference(subType, {\n ...options,\n onInvalidated(ev) {\n if (options && options.onInvalidated) {\n options.onInvalidated(ev)\n }\n ev.removeRef()\n }\n })\n\n if (options && options.acceptsUndefined === false) {\n return refType\n } else {\n return maybe(refType)\n }\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type IAnyType } from "../../internal.ts";
|
|
2
|
+
export declare function refinement<IT extends IAnyType>(name: string, type: IT, predicate: (snapshot: IT["CreationType"]) => boolean, message?: string | ((v: IT["CreationType"]) => string)): IT;
|
|
3
|
+
export declare function refinement<IT extends IAnyType>(type: IT, predicate: (snapshot: IT["CreationType"]) => boolean, message?: string | ((v: IT["CreationType"]) => string)): IT;
|
|
4
|
+
/**
|
|
5
|
+
* Returns if a given value is a refinement type.
|
|
6
|
+
*
|
|
7
|
+
* @param type
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare function isRefinementType<IT extends IAnyType>(type: IT): type is IT;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { BaseType } from "../../core/type/type.js";
|
|
2
|
+
import { TypeFlags, assertIsType, getStateTreeNode, isStateTreeNode, isType, typeCheckFailure, typeCheckSuccess } from "../../internal.js";
|
|
3
|
+
import { assertIsFunction, assertIsString } from "../../utils.js";
|
|
4
|
+
class Refinement extends BaseType {
|
|
5
|
+
_subtype;
|
|
6
|
+
_predicate;
|
|
7
|
+
_message;
|
|
8
|
+
get flags() {
|
|
9
|
+
return this._subtype.flags | TypeFlags.Refinement;
|
|
10
|
+
}
|
|
11
|
+
constructor(name, _subtype, _predicate, _message) {
|
|
12
|
+
super(name);
|
|
13
|
+
this._subtype = _subtype;
|
|
14
|
+
this._predicate = _predicate;
|
|
15
|
+
this._message = _message;
|
|
16
|
+
}
|
|
17
|
+
describe() {
|
|
18
|
+
return this.name;
|
|
19
|
+
}
|
|
20
|
+
instantiate(parent, subpath, environment, initialValue) {
|
|
21
|
+
// create the child type
|
|
22
|
+
return this._subtype.instantiate(parent, subpath, environment, initialValue);
|
|
23
|
+
}
|
|
24
|
+
isAssignableFrom(type) {
|
|
25
|
+
return this._subtype.isAssignableFrom(type);
|
|
26
|
+
}
|
|
27
|
+
isValidSnapshot(value, context) {
|
|
28
|
+
const subtypeErrors = this._subtype.validate(value, context);
|
|
29
|
+
if (subtypeErrors.length > 0) {
|
|
30
|
+
return subtypeErrors;
|
|
31
|
+
}
|
|
32
|
+
const snapshot = isStateTreeNode(value)
|
|
33
|
+
? getStateTreeNode(value).snapshot
|
|
34
|
+
: value;
|
|
35
|
+
if (!this._predicate(snapshot)) {
|
|
36
|
+
return typeCheckFailure(context, value, this._message(value));
|
|
37
|
+
}
|
|
38
|
+
return typeCheckSuccess();
|
|
39
|
+
}
|
|
40
|
+
reconcile(current, newValue, parent, subpath) {
|
|
41
|
+
return this._subtype.reconcile(current, newValue, parent, subpath);
|
|
42
|
+
}
|
|
43
|
+
getSubTypes() {
|
|
44
|
+
return this._subtype;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* `types.refinement` - Creates a type that is more specific than the base type, e.g. `types.refinement(types.string, value => value.length > 5)` to create a type of strings that can only be longer then 5.
|
|
49
|
+
*
|
|
50
|
+
* @param name
|
|
51
|
+
* @param type
|
|
52
|
+
* @param predicate
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
export function refinement(...args) {
|
|
56
|
+
const name = typeof args[0] === "string"
|
|
57
|
+
? args.shift()
|
|
58
|
+
: isType(args[0])
|
|
59
|
+
? args[0].name
|
|
60
|
+
: null;
|
|
61
|
+
const type = args[0];
|
|
62
|
+
const predicate = args[1];
|
|
63
|
+
const message = args[2]
|
|
64
|
+
? args[2]
|
|
65
|
+
: (_v) => "Value does not respect the refinement predicate";
|
|
66
|
+
// ensures all parameters are correct
|
|
67
|
+
assertIsType(type, [1, 2]);
|
|
68
|
+
assertIsString(name, 1);
|
|
69
|
+
assertIsFunction(predicate, [2, 3]);
|
|
70
|
+
assertIsFunction(message, [3, 4]);
|
|
71
|
+
return new Refinement(name, type, predicate, message);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns if a given value is a refinement type.
|
|
75
|
+
*
|
|
76
|
+
* @param type
|
|
77
|
+
* @returns
|
|
78
|
+
*/
|
|
79
|
+
export function isRefinementType(type) {
|
|
80
|
+
return (type.flags & TypeFlags.Refinement) > 0;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=refinement.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refinement.js","sourceRoot":"","sources":["../../../src/types/utility-types/refinement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAClD,OAAO,EAML,SAAS,EACT,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,MAAM,EACN,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEjE,MAAM,UAAgC,SAAQ,QAK7C;IAOoB;IACA;IACA;IARnB,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,GAAG,SAAS,CAAC,UAAU,CAAA;IACnD,CAAC;IAED,YACE,IAAY,EACK,QAAY,EACZ,UAA8C,EAC9C,QAA2C;QAE5D,KAAK,CAAC,IAAI,CAAC,CAAA;QAJM,aAAQ,GAAR,QAAQ,CAAI;QACZ,eAAU,GAAV,UAAU,CAAoC;QAC9C,aAAQ,GAAR,QAAQ,CAAmC;IAG9D,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,IAAI,CAAA;IAClB,CAAC;IAED,WAAW,CACT,MAA4B,EAC5B,OAAe,EACf,WAAgB,EAChB,YAAmC;QAEnC,wBAAwB;QACxB,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,CAC9B,MAAM,EACN,OAAO,EACP,WAAW,EACX,YAAY,CACN,CAAA;IACV,CAAC;IAED,gBAAgB,CAAC,IAAc;QAC7B,OAAO,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;IAC7C,CAAC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AC,CAAC,CAAA;IACzB,MAAM,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC;QACrB,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;QACT,CAAC,CAAC,CAAC,EAAO,EAAE,EAAE,CAAC,iDAAiD,CAAA;IAClE,qCAAqC;IACrC,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IAC1B,cAAc,CAAC,IAAI,EAAE,CAAC,CAAC,CAAA;IACvB,gBAAgB,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IACnC,gBAAgB,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IAEjC,OAAO,IAAI,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,CAAC,CAAA;AACvD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAsB,IAAQ;IAC5D,OAAO,CAAC,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;AAChD,CAAC","sourcesContent":["import { BaseType } from \"../../core/type/type.ts\"\nimport {\n type AnyObjectNode,\n type ExtractNodeType,\n type IAnyType,\n type IValidationContext,\n type IValidationResult,\n TypeFlags,\n assertIsType,\n getStateTreeNode,\n isStateTreeNode,\n isType,\n typeCheckFailure,\n typeCheckSuccess\n} from \"../../internal.ts\"\nimport { assertIsFunction, assertIsString } from \"../../utils.ts\"\n\nclass Refinement<IT extends IAnyType> extends BaseType<\n IT[\"CreationType\"],\n IT[\"SnapshotType\"],\n IT[\"TypeWithoutSTN\"],\n ExtractNodeType<IT>\n> {\n get flags() {\n return this._subtype.flags | TypeFlags.Refinement\n }\n\n constructor(\n name: string,\n private readonly _subtype: IT,\n private readonly _predicate: (v: IT[\"CreationType\"]) => boolean,\n private readonly _message: (v: IT[\"CreationType\"]) => string\n ) {\n super(name)\n }\n\n describe() {\n return this.name\n }\n\n instantiate(\n parent: AnyObjectNode | null,\n subpath: string,\n environment: any,\n initialValue: this[\"C\"] | this[\"T\"]\n ): this[\"N\"] {\n // create the child type\n return this._subtype.instantiate(\n parent,\n subpath,\n environment,\n initialValue\n ) as any\n }\n\n isAssignableFrom(type: IAnyType) {\n return this._subtype.isAssignableFrom(type)\n }\n\n isValidSnapshot(\n value: this[\"C\"],\n context: IValidationContext\n ): IValidationResult {\n const subtypeErrors = this._subtype.validate(value, context)\n if (subtypeErrors.length > 0) {\n return subtypeErrors\n }\n\n const snapshot = isStateTreeNode(value)\n ? getStateTreeNode(value).snapshot\n : value\n\n if (!this._predicate(snapshot)) {\n return typeCheckFailure(context, value, this._message(value))\n }\n\n return typeCheckSuccess()\n }\n\n reconcile(\n current: this[\"N\"],\n newValue: this[\"C\"] | this[\"T\"],\n parent: AnyObjectNode,\n subpath: string\n ): this[\"N\"] {\n return this._subtype.reconcile(current, newValue, parent, subpath) as any\n }\n\n getSubTypes() {\n return this._subtype\n }\n}\n\nexport function refinement<IT extends IAnyType>(\n name: string,\n type: IT,\n predicate: (snapshot: IT[\"CreationType\"]) => boolean,\n message?: string | ((v: IT[\"CreationType\"]) => string)\n): IT\nexport function refinement<IT extends IAnyType>(\n type: IT,\n predicate: (snapshot: IT[\"CreationType\"]) => boolean,\n message?: string | ((v: IT[\"CreationType\"]) => string)\n): IT\n\n/**\n * `types.refinement` - Creates a type that is more specific than the base type, e.g. `types.refinement(types.string, value => value.length > 5)` to create a type of strings that can only be longer then 5.\n *\n * @param name\n * @param type\n * @param predicate\n * @returns\n */\nexport function refinement(...args: any[]): IAnyType {\n const name =\n typeof args[0] === \"string\"\n ? args.shift()\n : isType(args[0])\n ? args[0].name\n : null\n const type = args[0]\n const predicate = args[1]\n const message = args[2]\n ? args[2]\n : (_v: any) => \"Value does not respect the refinement predicate\"\n // ensures all parameters are correct\n assertIsType(type, [1, 2])\n assertIsString(name, 1)\n assertIsFunction(predicate, [2, 3])\n assertIsFunction(message, [3, 4])\n\n return new Refinement(name, type, predicate, message)\n}\n\n/**\n * Returns if a given value is a refinement type.\n *\n * @param type\n * @returns\n */\nexport function isRefinementType<IT extends IAnyType>(type: IT): type is IT {\n return (type.flags & TypeFlags.Refinement) > 0\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type IAnyType, type IType } from "../../internal.ts";
|
|
2
|
+
export interface IResilientType<IT extends IAnyType, FT extends IAnyType> extends IType<IT["CreationType"] | FT["CreationType"], IT["SnapshotType"] | FT["SnapshotType"], IT["TypeWithoutSTN"] | FT["TypeWithoutSTN"]> {
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* `types.resilient` - Wraps a type so that instantiation errors are caught
|
|
6
|
+
* and a fallback type is used instead. This is useful for loading data that
|
|
7
|
+
* may contain unknown or invalid subtrees (e.g., plugin types that are not
|
|
8
|
+
* installed) without crashing the entire state tree.
|
|
9
|
+
*
|
|
10
|
+
* The `createFallbackSnapshot` callback receives the caught error and the
|
|
11
|
+
* original snapshot, and must return a valid snapshot for the fallback type.
|
|
12
|
+
*
|
|
13
|
+
* @param type The type to wrap.
|
|
14
|
+
* @param fallbackType The fallback type to use when instantiation fails.
|
|
15
|
+
* @param createFallbackSnapshot Callback that produces a fallback snapshot from the error and original snapshot.
|
|
16
|
+
* @returns A resilient type.
|
|
17
|
+
*/
|
|
18
|
+
export declare function resilient<IT extends IAnyType, FT extends IAnyType>(type: IT, fallbackType: FT, createFallbackSnapshot: (error: unknown, snapshot: any) => FT["CreationType"]): IResilientType<IT, FT>;
|