@jbrowse/mobx-state-tree 5.6.3 → 5.6.9
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.d.ts +87 -0
- package/dist/core/action.js +228 -0
- package/dist/core/action.js.map +1 -0
- package/dist/core/actionContext.d.ts +27 -0
- package/dist/core/actionContext.js +42 -0
- package/dist/core/actionContext.js.map +1 -0
- package/dist/core/flow.d.ts +69 -0
- package/dist/core/flow.js +180 -0
- package/dist/core/flow.js.map +1 -0
- package/dist/core/json-patch.d.ts +46 -0
- package/dist/core/json-patch.js +133 -0
- package/dist/core/json-patch.js.map +1 -0
- package/dist/core/mst-operations.d.ts +459 -0
- package/dist/core/mst-operations.js +885 -0
- package/dist/core/mst-operations.js.map +1 -0
- package/dist/core/node/BaseNode.d.ts +62 -0
- package/dist/core/node/BaseNode.js +152 -0
- package/dist/core/node/BaseNode.js.map +1 -0
- package/dist/core/node/Hook.d.ts +17 -0
- package/dist/core/node/Hook.js +15 -0
- package/dist/core/node/Hook.js.map +1 -0
- package/dist/core/node/create-node.d.ts +16 -0
- package/dist/core/node/create-node.js +41 -0
- package/dist/core/node/create-node.js.map +1 -0
- package/dist/core/node/identifier-cache.d.ts +19 -0
- package/dist/core/node/identifier-cache.js +115 -0
- package/dist/core/node/identifier-cache.js.map +1 -0
- package/dist/core/node/livelinessChecking.d.ts +37 -0
- package/dist/core/node/livelinessChecking.js +38 -0
- package/dist/core/node/livelinessChecking.js.map +1 -0
- package/dist/core/node/node-utils.d.ts +83 -0
- package/dist/core/node/node-utils.js +165 -0
- package/dist/core/node/node-utils.js.map +1 -0
- package/dist/core/node/object-node.d.ts +99 -0
- package/dist/core/node/object-node.js +545 -0
- package/dist/core/node/object-node.js.map +1 -0
- package/dist/core/node/scalar-node.d.ts +19 -0
- package/dist/core/node/scalar-node.js +89 -0
- package/dist/core/node/scalar-node.js.map +1 -0
- package/dist/core/process.d.ts +50 -0
- package/dist/core/process.js +39 -0
- package/dist/core/process.js.map +1 -0
- package/dist/core/type/type-checker.d.ts +69 -0
- package/dist/core/type/type-checker.js +154 -0
- package/dist/core/type/type-checker.js.map +1 -0
- package/dist/core/type/type.d.ts +317 -0
- package/dist/core/type/type.js +251 -0
- package/dist/core/type/type.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +85 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +39 -0
- package/dist/internal.js +60 -0
- package/dist/internal.js.map +1 -0
- package/dist/middlewares/create-action-tracking-middleware.d.ts +24 -0
- package/dist/middlewares/create-action-tracking-middleware.js +81 -0
- package/dist/middlewares/create-action-tracking-middleware.js.map +1 -0
- package/dist/middlewares/createActionTrackingMiddleware2.d.ts +34 -0
- package/dist/middlewares/createActionTrackingMiddleware2.js +133 -0
- package/dist/middlewares/createActionTrackingMiddleware2.js.map +1 -0
- package/dist/middlewares/on-action.d.ts +87 -0
- package/dist/middlewares/on-action.js +215 -0
- package/dist/middlewares/on-action.js.map +1 -0
- package/dist/mobx-state-tree.js +85 -0
- package/dist/mobx-state-tree.module.js +83 -0
- package/dist/types/complex-types/array.d.ts +80 -0
- package/dist/types/complex-types/array.js +352 -0
- package/dist/types/complex-types/array.js.map +1 -0
- package/dist/types/complex-types/map.d.ts +110 -0
- package/dist/types/complex-types/map.js +361 -0
- package/dist/types/complex-types/map.js.map +1 -0
- package/dist/types/complex-types/model.d.ts +192 -0
- package/dist/types/complex-types/model.js +477 -0
- package/dist/types/complex-types/model.js.map +1 -0
- package/dist/types/index.d.ts +33 -0
- package/dist/types/index.js +38 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/primitives.d.ts +124 -0
- package/dist/types/primitives.js +182 -0
- package/dist/types/primitives.js.map +1 -0
- package/dist/types/utility-types/custom.d.ts +74 -0
- package/dist/types/utility-types/custom.js +110 -0
- package/dist/types/utility-types/custom.js.map +1 -0
- package/dist/types/utility-types/enumeration.d.ts +5 -0
- package/dist/types/utility-types/enumeration.js +34 -0
- package/dist/types/utility-types/enumeration.js.map +1 -0
- package/dist/types/utility-types/frozen.d.ts +23 -0
- package/dist/types/utility-types/frozen.js +97 -0
- package/dist/types/utility-types/frozen.js.map +1 -0
- package/dist/types/utility-types/identifier.d.ts +86 -0
- package/dist/types/utility-types/identifier.js +129 -0
- package/dist/types/utility-types/identifier.js.map +1 -0
- package/dist/types/utility-types/late.d.ts +10 -0
- package/dist/types/utility-types/late.js +109 -0
- package/dist/types/utility-types/late.js.map +1 -0
- package/dist/types/utility-types/lazy.d.ts +22 -0
- package/dist/types/utility-types/lazy.js +76 -0
- package/dist/types/utility-types/lazy.js.map +1 -0
- package/dist/types/utility-types/literal.d.ts +37 -0
- package/dist/types/utility-types/literal.js +63 -0
- package/dist/types/utility-types/literal.js.map +1 -0
- package/dist/types/utility-types/maybe.d.ts +26 -0
- package/dist/types/utility-types/maybe.js +30 -0
- package/dist/types/utility-types/maybe.js.map +1 -0
- package/dist/types/utility-types/optional.d.ts +41 -0
- package/dist/types/utility-types/optional.js +140 -0
- package/dist/types/utility-types/optional.js.map +1 -0
- package/dist/types/utility-types/reference.d.ts +89 -0
- package/dist/types/utility-types/reference.js +392 -0
- package/dist/types/utility-types/reference.js.map +1 -0
- package/dist/types/utility-types/refinement.d.ts +10 -0
- package/dist/types/utility-types/refinement.js +85 -0
- package/dist/types/utility-types/refinement.js.map +1 -0
- package/dist/types/utility-types/resilient.d.ts +18 -0
- package/dist/types/utility-types/resilient.js +120 -0
- package/dist/types/utility-types/resilient.js.map +1 -0
- package/dist/types/utility-types/snapshotProcessor.d.ts +63 -0
- package/dist/types/utility-types/snapshotProcessor.js +161 -0
- package/dist/types/utility-types/snapshotProcessor.js.map +1 -0
- package/dist/types/utility-types/union.d.ts +77 -0
- package/dist/types/utility-types/union.js +245 -0
- package/dist/types/utility-types/union.js.map +1 -0
- package/dist/utils.d.ts +230 -0
- package/dist/utils.js +483 -0
- package/dist/utils.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,885 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getType = getType;
|
|
4
|
+
exports.getChildType = getChildType;
|
|
5
|
+
exports.onPatch = onPatch;
|
|
6
|
+
exports.onSnapshot = onSnapshot;
|
|
7
|
+
exports.applyPatch = applyPatch;
|
|
8
|
+
exports.recordPatches = recordPatches;
|
|
9
|
+
exports.protect = protect;
|
|
10
|
+
exports.unprotect = unprotect;
|
|
11
|
+
exports.isProtected = isProtected;
|
|
12
|
+
exports.applySnapshot = applySnapshot;
|
|
13
|
+
exports.getSnapshot = getSnapshot;
|
|
14
|
+
exports.hasParent = hasParent;
|
|
15
|
+
exports.getParent = getParent;
|
|
16
|
+
exports.hasParentOfType = hasParentOfType;
|
|
17
|
+
exports.getParentOfType = getParentOfType;
|
|
18
|
+
exports.getRoot = getRoot;
|
|
19
|
+
exports.getPath = getPath;
|
|
20
|
+
exports.getPathParts = getPathParts;
|
|
21
|
+
exports.isRoot = isRoot;
|
|
22
|
+
exports.resolvePath = resolvePath;
|
|
23
|
+
exports.resolveIdentifier = resolveIdentifier;
|
|
24
|
+
exports.getIdentifier = getIdentifier;
|
|
25
|
+
exports.tryReference = tryReference;
|
|
26
|
+
exports.isValidReference = isValidReference;
|
|
27
|
+
exports.tryResolve = tryResolve;
|
|
28
|
+
exports.getRelativePath = getRelativePath;
|
|
29
|
+
exports.clone = clone;
|
|
30
|
+
exports.detach = detach;
|
|
31
|
+
exports.destroy = destroy;
|
|
32
|
+
exports.isAlive = isAlive;
|
|
33
|
+
exports.addDisposer = addDisposer;
|
|
34
|
+
exports.getEnv = getEnv;
|
|
35
|
+
exports.walk = walk;
|
|
36
|
+
exports.getPropertyMembers = getPropertyMembers;
|
|
37
|
+
exports.getMembers = getMembers;
|
|
38
|
+
exports.cast = cast;
|
|
39
|
+
exports.castToSnapshot = castToSnapshot;
|
|
40
|
+
exports.castToReferenceSnapshot = castToReferenceSnapshot;
|
|
41
|
+
exports.getNodeId = getNodeId;
|
|
42
|
+
const mobx_1 = require("mobx");
|
|
43
|
+
const internal_ts_1 = require("../internal.js");
|
|
44
|
+
/**
|
|
45
|
+
* Returns the _actual_ type of the given tree node. (Or throws)
|
|
46
|
+
*
|
|
47
|
+
* @param object
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
function getType(object) {
|
|
51
|
+
(0, internal_ts_1.assertIsStateTreeNode)(object, 1);
|
|
52
|
+
return (0, internal_ts_1.getStateTreeNode)(object).type;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns the _declared_ type of the given sub property of an object, array or map.
|
|
56
|
+
* In the case of arrays and maps the property name is optional and will be ignored.
|
|
57
|
+
*
|
|
58
|
+
* Example:
|
|
59
|
+
* ```ts
|
|
60
|
+
* const Box = types.model({ x: 0, y: 0 })
|
|
61
|
+
* const box = Box.create()
|
|
62
|
+
*
|
|
63
|
+
* console.log(getChildType(box, "x").name) // 'number'
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @param object
|
|
67
|
+
* @param propertyName
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
function getChildType(object, propertyName) {
|
|
71
|
+
(0, internal_ts_1.assertIsStateTreeNode)(object, 1);
|
|
72
|
+
return (0, internal_ts_1.getStateTreeNode)(object).getChildType(propertyName);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Registers a function that will be invoked for each mutation that is applied to the provided model instance, or to any of its children.
|
|
76
|
+
* See [patches](https://github.com/mobxjs/mobx-state-tree#patches) for more details. onPatch events are emitted immediately and will not await the end of a transaction.
|
|
77
|
+
* Patches can be used to deeply observe a model tree.
|
|
78
|
+
*
|
|
79
|
+
* @param target the model instance from which to receive patches
|
|
80
|
+
* @param callback the callback that is invoked for each patch. The reversePatch is a patch that would actually undo the emitted patch
|
|
81
|
+
* @returns function to remove the listener
|
|
82
|
+
*/
|
|
83
|
+
function onPatch(target, callback) {
|
|
84
|
+
// check all arguments
|
|
85
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
86
|
+
(0, internal_ts_1.assertIsFunction)(callback, 2);
|
|
87
|
+
return (0, internal_ts_1.getStateTreeNode)(target).onPatch(callback);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Registers a function that is invoked whenever a new snapshot for the given model instance is available.
|
|
91
|
+
* The listener will only be fire at the end of the current MobX (trans)action.
|
|
92
|
+
* See [snapshots](https://github.com/mobxjs/mobx-state-tree#snapshots) for more details.
|
|
93
|
+
*
|
|
94
|
+
* @param target
|
|
95
|
+
* @param callback
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
|
+
function onSnapshot(target, callback) {
|
|
99
|
+
// check all arguments
|
|
100
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
101
|
+
(0, internal_ts_1.assertIsFunction)(callback, 2);
|
|
102
|
+
return (0, internal_ts_1.getStateTreeNode)(target).onSnapshot(callback);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Applies a JSON-patch to the given model instance or bails out if the patch couldn't be applied
|
|
106
|
+
* See [patches](https://github.com/mobxjs/mobx-state-tree#patches) for more details.
|
|
107
|
+
*
|
|
108
|
+
* Can apply a single past, or an array of patches.
|
|
109
|
+
*
|
|
110
|
+
* @param target
|
|
111
|
+
* @param patch
|
|
112
|
+
* @returns
|
|
113
|
+
*/
|
|
114
|
+
function applyPatch(target, patch) {
|
|
115
|
+
// check all arguments
|
|
116
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
117
|
+
(0, internal_ts_1.assertArg)(patch, p => typeof p === "object", "object or array", 2);
|
|
118
|
+
(0, internal_ts_1.getStateTreeNode)(target).applyPatches((0, internal_ts_1.asArray)(patch));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Small abstraction around `onPatch` and `applyPatch`, attaches a patch listener to a tree and records all the patches.
|
|
122
|
+
* Returns a recorder object with the following signature:
|
|
123
|
+
*
|
|
124
|
+
* Example:
|
|
125
|
+
* ```ts
|
|
126
|
+
* export interface IPatchRecorder {
|
|
127
|
+
* // the recorded patches
|
|
128
|
+
* patches: IJsonPatch[]
|
|
129
|
+
* // the inverse of the recorded patches
|
|
130
|
+
* inversePatches: IJsonPatch[]
|
|
131
|
+
* // true if currently recording
|
|
132
|
+
* recording: boolean
|
|
133
|
+
* // stop recording patches
|
|
134
|
+
* stop(): void
|
|
135
|
+
* // resume recording patches
|
|
136
|
+
* resume(): void
|
|
137
|
+
* // apply all the recorded patches on the given target (the original subject if omitted)
|
|
138
|
+
* replay(target?: IAnyStateTreeNode): void
|
|
139
|
+
* // reverse apply the recorded patches on the given target (the original subject if omitted)
|
|
140
|
+
* // stops the recorder if not already stopped
|
|
141
|
+
* undo(): void
|
|
142
|
+
* }
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* The optional filter function allows to skip recording certain patches.
|
|
146
|
+
*
|
|
147
|
+
* @param subject
|
|
148
|
+
* @param filter
|
|
149
|
+
* @returns
|
|
150
|
+
*/
|
|
151
|
+
function recordPatches(subject, filter) {
|
|
152
|
+
// check all arguments
|
|
153
|
+
(0, internal_ts_1.assertIsStateTreeNode)(subject, 1);
|
|
154
|
+
const data = {
|
|
155
|
+
patches: [],
|
|
156
|
+
inversePatches: []
|
|
157
|
+
};
|
|
158
|
+
// we will generate the immutable copy of patches on demand for public consumption
|
|
159
|
+
const publicData = {};
|
|
160
|
+
let disposer;
|
|
161
|
+
const recorder = {
|
|
162
|
+
get recording() {
|
|
163
|
+
return !!disposer;
|
|
164
|
+
},
|
|
165
|
+
get patches() {
|
|
166
|
+
if (!publicData.patches) {
|
|
167
|
+
publicData.patches = data.patches.slice();
|
|
168
|
+
}
|
|
169
|
+
return publicData.patches;
|
|
170
|
+
},
|
|
171
|
+
get reversedInversePatches() {
|
|
172
|
+
if (!publicData.reversedInversePatches) {
|
|
173
|
+
publicData.reversedInversePatches = data.inversePatches
|
|
174
|
+
.slice()
|
|
175
|
+
.reverse();
|
|
176
|
+
}
|
|
177
|
+
return publicData.reversedInversePatches;
|
|
178
|
+
},
|
|
179
|
+
get inversePatches() {
|
|
180
|
+
if (!publicData.inversePatches) {
|
|
181
|
+
publicData.inversePatches = data.inversePatches.slice();
|
|
182
|
+
}
|
|
183
|
+
return publicData.inversePatches;
|
|
184
|
+
},
|
|
185
|
+
stop() {
|
|
186
|
+
if (disposer) {
|
|
187
|
+
disposer();
|
|
188
|
+
disposer = undefined;
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
resume() {
|
|
192
|
+
if (disposer) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
disposer = onPatch(subject, (patch, inversePatch) => {
|
|
196
|
+
// skip patches that are asked to be filtered if there's a filter in place
|
|
197
|
+
if (filter && !filter(patch, inversePatch, (0, internal_ts_1.getRunningActionContext)())) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
data.patches.push(patch);
|
|
201
|
+
data.inversePatches.push(inversePatch);
|
|
202
|
+
// mark immutable public patches as dirty
|
|
203
|
+
publicData.patches = undefined;
|
|
204
|
+
publicData.inversePatches = undefined;
|
|
205
|
+
publicData.reversedInversePatches = undefined;
|
|
206
|
+
});
|
|
207
|
+
},
|
|
208
|
+
replay(target) {
|
|
209
|
+
applyPatch(target || subject, data.patches);
|
|
210
|
+
},
|
|
211
|
+
undo(target) {
|
|
212
|
+
applyPatch(target || subject, data.inversePatches.slice().reverse());
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
recorder.resume();
|
|
216
|
+
return recorder;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The inverse of `unprotect`.
|
|
220
|
+
*
|
|
221
|
+
* @param target
|
|
222
|
+
*/
|
|
223
|
+
function protect(target) {
|
|
224
|
+
// check all arguments
|
|
225
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
226
|
+
const node = (0, internal_ts_1.getStateTreeNode)(target);
|
|
227
|
+
if (!node.isRoot) {
|
|
228
|
+
throw (0, internal_ts_1.fail)("`protect` can only be invoked on root nodes");
|
|
229
|
+
}
|
|
230
|
+
node.isProtectionEnabled = true;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* By default it is not allowed to directly modify a model. Models can only be modified through actions.
|
|
234
|
+
* However, in some cases you don't care about the advantages (like replayability, traceability, etc) this yields.
|
|
235
|
+
* For example because you are building a PoC or don't have any middleware attached to your tree.
|
|
236
|
+
*
|
|
237
|
+
* In that case you can disable this protection by calling `unprotect` on the root of your tree.
|
|
238
|
+
*
|
|
239
|
+
* Example:
|
|
240
|
+
* ```ts
|
|
241
|
+
* const Todo = types.model({
|
|
242
|
+
* done: false
|
|
243
|
+
* }).actions(self => ({
|
|
244
|
+
* toggle() {
|
|
245
|
+
* self.done = !self.done
|
|
246
|
+
* }
|
|
247
|
+
* }))
|
|
248
|
+
*
|
|
249
|
+
* const todo = Todo.create()
|
|
250
|
+
* todo.done = true // throws!
|
|
251
|
+
* todo.toggle() // OK
|
|
252
|
+
* unprotect(todo)
|
|
253
|
+
* todo.done = false // OK
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
function unprotect(target) {
|
|
257
|
+
// check all arguments
|
|
258
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
259
|
+
const node = (0, internal_ts_1.getStateTreeNode)(target);
|
|
260
|
+
if (!node.isRoot) {
|
|
261
|
+
throw (0, internal_ts_1.fail)("`unprotect` can only be invoked on root nodes");
|
|
262
|
+
}
|
|
263
|
+
node.isProtectionEnabled = false;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Returns true if the object is in protected mode, @see protect
|
|
267
|
+
*/
|
|
268
|
+
function isProtected(target) {
|
|
269
|
+
return (0, internal_ts_1.getStateTreeNode)(target).isProtected;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Applies a snapshot to a given model instances. Patch and snapshot listeners will be invoked as usual.
|
|
273
|
+
*
|
|
274
|
+
* @param target
|
|
275
|
+
* @param snapshot
|
|
276
|
+
* @returns
|
|
277
|
+
*/
|
|
278
|
+
function applySnapshot(target, snapshot) {
|
|
279
|
+
// check all arguments
|
|
280
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
281
|
+
return (0, internal_ts_1.getStateTreeNode)(target).applySnapshot(snapshot);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Calculates a snapshot from the given model instance. The snapshot will always reflect the latest state but use
|
|
285
|
+
* structural sharing where possible. Doesn't require MobX transactions to be completed.
|
|
286
|
+
*
|
|
287
|
+
* @param target
|
|
288
|
+
* @param applyPostProcess If true (the default) then postProcessSnapshot gets applied.
|
|
289
|
+
* @returns
|
|
290
|
+
*/
|
|
291
|
+
function getSnapshot(target, applyPostProcess = true) {
|
|
292
|
+
// check all arguments
|
|
293
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
294
|
+
const node = (0, internal_ts_1.getStateTreeNode)(target);
|
|
295
|
+
if (applyPostProcess) {
|
|
296
|
+
return node.snapshot;
|
|
297
|
+
}
|
|
298
|
+
return (0, internal_ts_1.freeze)(node.type.getSnapshot(node, false));
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Given a model instance, returns `true` if the object has a parent, that is, is part of another object, map or array.
|
|
302
|
+
*
|
|
303
|
+
* @param target
|
|
304
|
+
* @param depth How far should we look upward? 1 by default.
|
|
305
|
+
* @returns
|
|
306
|
+
*/
|
|
307
|
+
function hasParent(target, depth = 1) {
|
|
308
|
+
// check all arguments
|
|
309
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
310
|
+
(0, internal_ts_1.assertIsNumber)(depth, 2, 0);
|
|
311
|
+
let parent = (0, internal_ts_1.getStateTreeNode)(target).parent;
|
|
312
|
+
while (parent) {
|
|
313
|
+
if (--depth === 0) {
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
parent = parent.parent;
|
|
317
|
+
}
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Returns the immediate parent of this object, or throws.
|
|
322
|
+
*
|
|
323
|
+
* Note that the immediate parent can be either an object, map or array, and
|
|
324
|
+
* doesn't necessarily refer to the parent model.
|
|
325
|
+
*
|
|
326
|
+
* Please note that in child nodes access to the root is only possible
|
|
327
|
+
* once the `afterAttach` hook has fired.
|
|
328
|
+
*
|
|
329
|
+
* @param target
|
|
330
|
+
* @param depth How far should we look upward? 1 by default.
|
|
331
|
+
* @returns
|
|
332
|
+
*/
|
|
333
|
+
function getParent(target, depth = 1) {
|
|
334
|
+
// check all arguments
|
|
335
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
336
|
+
(0, internal_ts_1.assertIsNumber)(depth, 2, 0);
|
|
337
|
+
let d = depth;
|
|
338
|
+
let parent = (0, internal_ts_1.getStateTreeNode)(target).parent;
|
|
339
|
+
while (parent) {
|
|
340
|
+
if (--d === 0) {
|
|
341
|
+
return parent.storedValue;
|
|
342
|
+
}
|
|
343
|
+
parent = parent.parent;
|
|
344
|
+
}
|
|
345
|
+
throw (0, internal_ts_1.fail)(`Failed to find the parent of ${(0, internal_ts_1.getStateTreeNode)(target)} at depth ${depth}`);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Given a model instance, returns `true` if the object has a parent of given type, that is, is part of another object, map or array
|
|
349
|
+
*
|
|
350
|
+
* @param target
|
|
351
|
+
* @param type
|
|
352
|
+
* @returns
|
|
353
|
+
*/
|
|
354
|
+
function hasParentOfType(target, type) {
|
|
355
|
+
// check all arguments
|
|
356
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
357
|
+
(0, internal_ts_1.assertIsType)(type, 2);
|
|
358
|
+
let parent = (0, internal_ts_1.getStateTreeNode)(target).parent;
|
|
359
|
+
while (parent) {
|
|
360
|
+
if (type.is(parent.storedValue)) {
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
parent = parent.parent;
|
|
364
|
+
}
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Returns the target's parent of a given type, or throws.
|
|
369
|
+
*
|
|
370
|
+
* @param target
|
|
371
|
+
* @param type
|
|
372
|
+
* @returns
|
|
373
|
+
*/
|
|
374
|
+
function getParentOfType(target, type) {
|
|
375
|
+
// check all arguments
|
|
376
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
377
|
+
(0, internal_ts_1.assertIsType)(type, 2);
|
|
378
|
+
let parent = (0, internal_ts_1.getStateTreeNode)(target).parent;
|
|
379
|
+
while (parent) {
|
|
380
|
+
if (type.is(parent.storedValue)) {
|
|
381
|
+
return parent.storedValue;
|
|
382
|
+
}
|
|
383
|
+
parent = parent.parent;
|
|
384
|
+
}
|
|
385
|
+
throw (0, internal_ts_1.fail)(`Failed to find the parent of ${(0, internal_ts_1.getStateTreeNode)(target)} of a given type`);
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Given an object in a model tree, returns the root object of that tree.
|
|
389
|
+
*
|
|
390
|
+
* Please note that in child nodes access to the root is only possible
|
|
391
|
+
* once the `afterAttach` hook has fired.
|
|
392
|
+
*
|
|
393
|
+
* @param target
|
|
394
|
+
* @returns
|
|
395
|
+
*/
|
|
396
|
+
function getRoot(target) {
|
|
397
|
+
// check all arguments
|
|
398
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
399
|
+
return (0, internal_ts_1.getStateTreeNode)(target).root.storedValue;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Returns the path of the given object in the model tree
|
|
403
|
+
*
|
|
404
|
+
* @param target
|
|
405
|
+
* @returns
|
|
406
|
+
*/
|
|
407
|
+
function getPath(target) {
|
|
408
|
+
// check all arguments
|
|
409
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
410
|
+
return (0, internal_ts_1.getStateTreeNode)(target).path;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Returns the path of the given object as unescaped string array.
|
|
414
|
+
*
|
|
415
|
+
* @param target
|
|
416
|
+
* @returns
|
|
417
|
+
*/
|
|
418
|
+
function getPathParts(target) {
|
|
419
|
+
// check all arguments
|
|
420
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
421
|
+
return (0, internal_ts_1.splitJsonPath)((0, internal_ts_1.getStateTreeNode)(target).path);
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Returns true if the given object is the root of a model tree.
|
|
425
|
+
*
|
|
426
|
+
* @param target
|
|
427
|
+
* @returns
|
|
428
|
+
*/
|
|
429
|
+
function isRoot(target) {
|
|
430
|
+
// check all arguments
|
|
431
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
432
|
+
return (0, internal_ts_1.getStateTreeNode)(target).isRoot;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Resolves a path relatively to a given object.
|
|
436
|
+
* Returns undefined if no value can be found.
|
|
437
|
+
*
|
|
438
|
+
* @param target
|
|
439
|
+
* @param path escaped json path
|
|
440
|
+
* @returns
|
|
441
|
+
*/
|
|
442
|
+
function resolvePath(target, path) {
|
|
443
|
+
// check all arguments
|
|
444
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
445
|
+
(0, internal_ts_1.assertIsString)(path, 2);
|
|
446
|
+
const node = (0, internal_ts_1.resolveNodeByPath)((0, internal_ts_1.getStateTreeNode)(target), path);
|
|
447
|
+
return node ? node.value : undefined;
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Resolves a model instance given a root target, the type and the identifier you are searching for.
|
|
451
|
+
* Returns undefined if no value can be found.
|
|
452
|
+
*
|
|
453
|
+
* @param type
|
|
454
|
+
* @param target
|
|
455
|
+
* @param identifier
|
|
456
|
+
* @returns
|
|
457
|
+
*/
|
|
458
|
+
function resolveIdentifier(type, target, identifier) {
|
|
459
|
+
// check all arguments
|
|
460
|
+
(0, internal_ts_1.assertIsType)(type, 1);
|
|
461
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 2);
|
|
462
|
+
(0, internal_ts_1.assertIsValidIdentifier)(identifier, 3);
|
|
463
|
+
const node = (0, internal_ts_1.getStateTreeNode)(target).root.identifierCache.resolve(type, (0, internal_ts_1.normalizeIdentifier)(identifier));
|
|
464
|
+
return node?.value;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Returns the identifier of the target node.
|
|
468
|
+
* This is the *string normalized* identifier, which might not match the type of the identifier attribute
|
|
469
|
+
*
|
|
470
|
+
* @param target
|
|
471
|
+
* @returns
|
|
472
|
+
*/
|
|
473
|
+
function getIdentifier(target) {
|
|
474
|
+
// check all arguments
|
|
475
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
476
|
+
return (0, internal_ts_1.getStateTreeNode)(target).identifier;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if the check passes,
|
|
480
|
+
* else it returns undefined.
|
|
481
|
+
*
|
|
482
|
+
* @param getter Function to access the reference.
|
|
483
|
+
* @param checkIfAlive true to also make sure the referenced node is alive (default), false to skip this check.
|
|
484
|
+
* @returns
|
|
485
|
+
*/
|
|
486
|
+
function tryReference(getter, checkIfAlive = true) {
|
|
487
|
+
try {
|
|
488
|
+
const node = getter();
|
|
489
|
+
if (node === undefined || node === null) {
|
|
490
|
+
return undefined;
|
|
491
|
+
}
|
|
492
|
+
else if ((0, internal_ts_1.isStateTreeNode)(node)) {
|
|
493
|
+
if (!checkIfAlive) {
|
|
494
|
+
return node;
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
return isAlive(node) ? node : undefined;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
else {
|
|
501
|
+
throw (0, internal_ts_1.fail)("The reference to be checked is not one of node, null or undefined");
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
catch (e) {
|
|
505
|
+
if (e instanceof internal_ts_1.InvalidReferenceError) {
|
|
506
|
+
return undefined;
|
|
507
|
+
}
|
|
508
|
+
throw e;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns if the check passes or not.
|
|
513
|
+
*
|
|
514
|
+
* @param getter Function to access the reference.
|
|
515
|
+
* @param checkIfAlive true to also make sure the referenced node is alive (default), false to skip this check.
|
|
516
|
+
* @returns
|
|
517
|
+
*/
|
|
518
|
+
function isValidReference(getter, checkIfAlive = true) {
|
|
519
|
+
try {
|
|
520
|
+
const node = getter();
|
|
521
|
+
if (node === undefined || node === null) {
|
|
522
|
+
return false;
|
|
523
|
+
}
|
|
524
|
+
else if ((0, internal_ts_1.isStateTreeNode)(node)) {
|
|
525
|
+
return checkIfAlive ? isAlive(node) : true;
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
throw (0, internal_ts_1.fail)("The reference to be checked is not one of node, null or undefined");
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
catch (e) {
|
|
532
|
+
if (e instanceof internal_ts_1.InvalidReferenceError) {
|
|
533
|
+
return false;
|
|
534
|
+
}
|
|
535
|
+
throw e;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Try to resolve a given path relative to a given node.
|
|
540
|
+
*
|
|
541
|
+
* @param target
|
|
542
|
+
* @param path
|
|
543
|
+
* @returns
|
|
544
|
+
*/
|
|
545
|
+
function tryResolve(target, path) {
|
|
546
|
+
// check all arguments
|
|
547
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
548
|
+
(0, internal_ts_1.assertIsString)(path, 2);
|
|
549
|
+
const node = (0, internal_ts_1.resolveNodeByPath)((0, internal_ts_1.getStateTreeNode)(target), path, false);
|
|
550
|
+
if (node === undefined) {
|
|
551
|
+
return undefined;
|
|
552
|
+
}
|
|
553
|
+
try {
|
|
554
|
+
return node.value;
|
|
555
|
+
}
|
|
556
|
+
catch (_e) {
|
|
557
|
+
// For what ever reason not resolvable (e.g. totally not existing path, or value that cannot be fetched)
|
|
558
|
+
// see test / issue: 'try resolve doesn't work #686'
|
|
559
|
+
return undefined;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Given two state tree nodes that are part of the same tree,
|
|
564
|
+
* returns the shortest jsonpath needed to navigate from the one to the other
|
|
565
|
+
*
|
|
566
|
+
* @param base
|
|
567
|
+
* @param target
|
|
568
|
+
* @returns
|
|
569
|
+
*/
|
|
570
|
+
function getRelativePath(base, target) {
|
|
571
|
+
// check all arguments
|
|
572
|
+
(0, internal_ts_1.assertIsStateTreeNode)(base, 1);
|
|
573
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 2);
|
|
574
|
+
return (0, internal_ts_1.getRelativePathBetweenNodes)((0, internal_ts_1.getStateTreeNode)(base), (0, internal_ts_1.getStateTreeNode)(target));
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Returns a deep copy of the given state tree node as new tree.
|
|
578
|
+
* Shorthand for `snapshot(x) = getType(x).create(getSnapshot(x))`
|
|
579
|
+
*
|
|
580
|
+
* _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc. during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
|
|
581
|
+
*
|
|
582
|
+
* @param source
|
|
583
|
+
* @param keepEnvironment indicates whether the clone should inherit the same environment (`true`, the default), or not have an environment (`false`). If an object is passed in as second argument, that will act as the environment for the cloned tree.
|
|
584
|
+
* @returns
|
|
585
|
+
*/
|
|
586
|
+
function clone(source, keepEnvironment = true) {
|
|
587
|
+
// check all arguments
|
|
588
|
+
(0, internal_ts_1.assertIsStateTreeNode)(source, 1);
|
|
589
|
+
const node = (0, internal_ts_1.getStateTreeNode)(source);
|
|
590
|
+
return node.type.create(node.snapshot, keepEnvironment === true
|
|
591
|
+
? node.root.environment
|
|
592
|
+
: keepEnvironment === false
|
|
593
|
+
? undefined
|
|
594
|
+
: keepEnvironment); // it's an object or something else
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Removes a model element from the state tree, and let it live on as a new state tree
|
|
598
|
+
*/
|
|
599
|
+
function detach(target) {
|
|
600
|
+
// check all arguments
|
|
601
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
602
|
+
(0, internal_ts_1.getStateTreeNode)(target).detach();
|
|
603
|
+
return target;
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Removes a model element from the state tree, and mark it as end-of-life; the element should not be used anymore
|
|
607
|
+
*/
|
|
608
|
+
function destroy(target) {
|
|
609
|
+
// check all arguments
|
|
610
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
611
|
+
const node = (0, internal_ts_1.getStateTreeNode)(target);
|
|
612
|
+
if (node.isRoot) {
|
|
613
|
+
node.die();
|
|
614
|
+
}
|
|
615
|
+
else {
|
|
616
|
+
node.parent.removeChild(node.subpath);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Returns true if the given state tree node is not killed yet.
|
|
621
|
+
* This means that the node is still a part of a tree, and that `destroy`
|
|
622
|
+
* has not been called. If a node is not alive anymore, the only thing one can do with it
|
|
623
|
+
* is requesting it's last path and snapshot
|
|
624
|
+
*
|
|
625
|
+
* @param target
|
|
626
|
+
* @returns
|
|
627
|
+
*/
|
|
628
|
+
function isAlive(target) {
|
|
629
|
+
// check all arguments
|
|
630
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
631
|
+
return (0, internal_ts_1.getStateTreeNode)(target).observableIsAlive;
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Use this utility to register a function that should be called whenever the
|
|
635
|
+
* targeted state tree node is destroyed. This is a useful alternative to managing
|
|
636
|
+
* cleanup methods yourself using the `beforeDestroy` hook.
|
|
637
|
+
*
|
|
638
|
+
* This methods returns the same disposer that was passed as argument.
|
|
639
|
+
*
|
|
640
|
+
* Example:
|
|
641
|
+
* ```ts
|
|
642
|
+
* const Todo = types.model({
|
|
643
|
+
* title: types.string
|
|
644
|
+
* }).actions(self => ({
|
|
645
|
+
* afterCreate() {
|
|
646
|
+
* const autoSaveDisposer = reaction(
|
|
647
|
+
* () => getSnapshot(self),
|
|
648
|
+
* snapshot => sendSnapshotToServerSomehow(snapshot)
|
|
649
|
+
* )
|
|
650
|
+
* // stop sending updates to server if this
|
|
651
|
+
* // instance is destroyed
|
|
652
|
+
* addDisposer(self, autoSaveDisposer)
|
|
653
|
+
* }
|
|
654
|
+
* }))
|
|
655
|
+
* ```
|
|
656
|
+
*
|
|
657
|
+
* @param target
|
|
658
|
+
* @param disposer
|
|
659
|
+
* @returns The same disposer that was passed as argument
|
|
660
|
+
*/
|
|
661
|
+
function addDisposer(target, disposer) {
|
|
662
|
+
// check all arguments
|
|
663
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
664
|
+
(0, internal_ts_1.assertIsFunction)(disposer, 2);
|
|
665
|
+
const node = (0, internal_ts_1.getStateTreeNode)(target);
|
|
666
|
+
node.addDisposer(disposer);
|
|
667
|
+
return disposer;
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* Returns the environment of the current state tree. For more info on environments,
|
|
671
|
+
* see [Dependency injection](https://github.com/mobxjs/mobx-state-tree#dependency-injection)
|
|
672
|
+
*
|
|
673
|
+
* Please note that in child nodes access to the root is only possible
|
|
674
|
+
* once the `afterAttach` hook has fired
|
|
675
|
+
*
|
|
676
|
+
* Returns an empty environment if the tree wasn't initialized with an environment
|
|
677
|
+
*
|
|
678
|
+
* @param target
|
|
679
|
+
* @returns
|
|
680
|
+
*/
|
|
681
|
+
function getEnv(target) {
|
|
682
|
+
// check all arguments
|
|
683
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
684
|
+
const node = (0, internal_ts_1.getStateTreeNode)(target);
|
|
685
|
+
const env = node.root.environment;
|
|
686
|
+
if (!env) {
|
|
687
|
+
return internal_ts_1.EMPTY_OBJECT;
|
|
688
|
+
}
|
|
689
|
+
return env;
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Performs a depth first walk through a tree.
|
|
693
|
+
*/
|
|
694
|
+
function walk(target, processor) {
|
|
695
|
+
// check all arguments
|
|
696
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
697
|
+
(0, internal_ts_1.assertIsFunction)(processor, 2);
|
|
698
|
+
const node = (0, internal_ts_1.getStateTreeNode)(target);
|
|
699
|
+
// tslint:disable-next-line:no_unused-variable
|
|
700
|
+
node.getChildren().forEach(child => {
|
|
701
|
+
if ((0, internal_ts_1.isStateTreeNode)(child.storedValue)) {
|
|
702
|
+
walk(child.storedValue, processor);
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
processor(node.storedValue);
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* Returns a reflection of the model type properties and name for either a model type or model node.
|
|
709
|
+
*
|
|
710
|
+
* @param typeOrNode
|
|
711
|
+
* @returns
|
|
712
|
+
*/
|
|
713
|
+
function getPropertyMembers(typeOrNode) {
|
|
714
|
+
let type;
|
|
715
|
+
if ((0, internal_ts_1.isStateTreeNode)(typeOrNode)) {
|
|
716
|
+
type = getType(typeOrNode);
|
|
717
|
+
}
|
|
718
|
+
else {
|
|
719
|
+
type = typeOrNode;
|
|
720
|
+
}
|
|
721
|
+
(0, internal_ts_1.assertArg)(type, t => (0, internal_ts_1.isModelType)(t), "model type or model instance", 1);
|
|
722
|
+
return {
|
|
723
|
+
name: type.name,
|
|
724
|
+
properties: { ...type.properties }
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Returns a reflection of the model node, including name, properties, views, volatile state,
|
|
729
|
+
* and actions. `flowActions` is also provided as a separate array of names for any action that
|
|
730
|
+
* came from a flow generator as well.
|
|
731
|
+
*
|
|
732
|
+
* In the case where a model has two actions: `doSomething` and `doSomethingWithFlow`, where
|
|
733
|
+
* `doSomethingWithFlow` is a flow generator, the `actions` array will contain both actions,
|
|
734
|
+
* i.e. ["doSomething", "doSomethingWithFlow"], and the `flowActions` array will contain only
|
|
735
|
+
* the flow action, i.e. ["doSomethingWithFlow"].
|
|
736
|
+
*
|
|
737
|
+
* @param target
|
|
738
|
+
* @returns
|
|
739
|
+
*/
|
|
740
|
+
function getMembers(target) {
|
|
741
|
+
const type = (0, internal_ts_1.getStateTreeNode)(target).type;
|
|
742
|
+
const reflected = {
|
|
743
|
+
...getPropertyMembers(type),
|
|
744
|
+
actions: [],
|
|
745
|
+
volatile: [],
|
|
746
|
+
views: [],
|
|
747
|
+
flowActions: []
|
|
748
|
+
};
|
|
749
|
+
const props = Object.getOwnPropertyNames(target);
|
|
750
|
+
props.forEach(key => {
|
|
751
|
+
if (key in reflected.properties) {
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
const descriptor = Object.getOwnPropertyDescriptor(target, key);
|
|
755
|
+
if (descriptor.get) {
|
|
756
|
+
if ((0, mobx_1.isComputedProp)(target, key)) {
|
|
757
|
+
reflected.views.push(key);
|
|
758
|
+
}
|
|
759
|
+
else {
|
|
760
|
+
reflected.volatile.push(key);
|
|
761
|
+
}
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
if (descriptor.value._isFlowAction === true) {
|
|
765
|
+
reflected.flowActions.push(key);
|
|
766
|
+
}
|
|
767
|
+
if (descriptor.value._isMSTAction === true) {
|
|
768
|
+
reflected.actions.push(key);
|
|
769
|
+
}
|
|
770
|
+
else if ((0, mobx_1.isObservableProp)(target, key)) {
|
|
771
|
+
reflected.volatile.push(key);
|
|
772
|
+
}
|
|
773
|
+
else {
|
|
774
|
+
reflected.views.push(key);
|
|
775
|
+
}
|
|
776
|
+
});
|
|
777
|
+
return reflected;
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
* Casts a node snapshot or instance type to an instance type so it can be assigned to a type instance.
|
|
781
|
+
* Note that this is just a cast for the type system, this is, it won't actually convert a snapshot to an instance,
|
|
782
|
+
* but just fool typescript into thinking so.
|
|
783
|
+
* Either way, casting when outside an assignation operation won't compile.
|
|
784
|
+
*
|
|
785
|
+
* Example:
|
|
786
|
+
* ```ts
|
|
787
|
+
* const ModelA = types.model({
|
|
788
|
+
* n: types.number
|
|
789
|
+
* }).actions(self => ({
|
|
790
|
+
* setN(aNumber: number) {
|
|
791
|
+
* self.n = aNumber
|
|
792
|
+
* }
|
|
793
|
+
* }))
|
|
794
|
+
*
|
|
795
|
+
* const ModelB = types.model({
|
|
796
|
+
* innerModel: ModelA
|
|
797
|
+
* }).actions(self => ({
|
|
798
|
+
* someAction() {
|
|
799
|
+
* // this will allow the compiler to assign a snapshot to the property
|
|
800
|
+
* self.innerModel = cast({ a: 5 })
|
|
801
|
+
* }
|
|
802
|
+
* }))
|
|
803
|
+
* ```
|
|
804
|
+
*
|
|
805
|
+
* @param snapshotOrInstance Snapshot or instance
|
|
806
|
+
* @returns The same object cast as an instance
|
|
807
|
+
*/
|
|
808
|
+
function cast(snapshotOrInstance) {
|
|
809
|
+
return snapshotOrInstance;
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Casts a node instance type to a snapshot type so it can be assigned to a type snapshot (e.g. to be used inside a create call).
|
|
813
|
+
* Note that this is just a cast for the type system, this is, it won't actually convert an instance to a snapshot,
|
|
814
|
+
* but just fool typescript into thinking so.
|
|
815
|
+
*
|
|
816
|
+
* Example:
|
|
817
|
+
* ```ts
|
|
818
|
+
* const ModelA = types.model({
|
|
819
|
+
* n: types.number
|
|
820
|
+
* }).actions(self => ({
|
|
821
|
+
* setN(aNumber: number) {
|
|
822
|
+
* self.n = aNumber
|
|
823
|
+
* }
|
|
824
|
+
* }))
|
|
825
|
+
*
|
|
826
|
+
* const ModelB = types.model({
|
|
827
|
+
* innerModel: ModelA
|
|
828
|
+
* })
|
|
829
|
+
*
|
|
830
|
+
* const a = ModelA.create({ n: 5 });
|
|
831
|
+
* // this will allow the compiler to use a model as if it were a snapshot
|
|
832
|
+
* const b = ModelB.create({ innerModel: castToSnapshot(a)})
|
|
833
|
+
* ```
|
|
834
|
+
*
|
|
835
|
+
* @param snapshotOrInstance Snapshot or instance
|
|
836
|
+
* @returns The same object cast as an input (creation) snapshot
|
|
837
|
+
*/
|
|
838
|
+
function castToSnapshot(snapshotOrInstance) {
|
|
839
|
+
return snapshotOrInstance;
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* Casts a node instance type to a reference snapshot type so it can be assigned to a reference snapshot (e.g. to be used inside a create call).
|
|
843
|
+
* Note that this is just a cast for the type system, this is, it won't actually convert an instance to a reference snapshot,
|
|
844
|
+
* but just fool typescript into thinking so.
|
|
845
|
+
*
|
|
846
|
+
* Example:
|
|
847
|
+
* ```ts
|
|
848
|
+
* const ModelA = types.model({
|
|
849
|
+
* id: types.identifier,
|
|
850
|
+
* n: types.number
|
|
851
|
+
* }).actions(self => ({
|
|
852
|
+
* setN(aNumber: number) {
|
|
853
|
+
* self.n = aNumber
|
|
854
|
+
* }
|
|
855
|
+
* }))
|
|
856
|
+
*
|
|
857
|
+
* const ModelB = types.model({
|
|
858
|
+
* refA: types.reference(ModelA)
|
|
859
|
+
* })
|
|
860
|
+
*
|
|
861
|
+
* const a = ModelA.create({ id: 'someId', n: 5 });
|
|
862
|
+
* // this will allow the compiler to use a model as if it were a reference snapshot
|
|
863
|
+
* const b = ModelB.create({ refA: castToReferenceSnapshot(a)})
|
|
864
|
+
* ```
|
|
865
|
+
*
|
|
866
|
+
* @param instance Instance
|
|
867
|
+
* @returns The same object cast as a reference snapshot (string or number)
|
|
868
|
+
*/
|
|
869
|
+
function castToReferenceSnapshot(instance) {
|
|
870
|
+
return instance;
|
|
871
|
+
}
|
|
872
|
+
/**
|
|
873
|
+
* Returns the unique node id (not to be confused with the instance identifier) for a
|
|
874
|
+
* given instance.
|
|
875
|
+
* This id is a number that is unique for each instance.
|
|
876
|
+
*
|
|
877
|
+
* @export
|
|
878
|
+
* @param target
|
|
879
|
+
* @returns
|
|
880
|
+
*/
|
|
881
|
+
function getNodeId(target) {
|
|
882
|
+
(0, internal_ts_1.assertIsStateTreeNode)(target, 1);
|
|
883
|
+
return (0, internal_ts_1.getStateTreeNode)(target).nodeId;
|
|
884
|
+
}
|
|
885
|
+
//# sourceMappingURL=mst-operations.js.map
|