@jbrowse/mobx-state-tree 5.7.0 → 5.8.6

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