@jbrowse/mobx-state-tree 5.7.1 → 5.8.7

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,393 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CustomReferenceType = exports.IdentifierReferenceType = exports.BaseReferenceType = exports.InvalidReferenceError = void 0;
4
- exports.reference = reference;
5
- exports.isReferenceType = isReferenceType;
6
- exports.safeReference = safeReference;
7
- const type_ts_1 = require("../../core/type/type.js");
8
- const internal_ts_1 = require("../../internal.js");
9
- function getInvalidationCause(hook) {
10
- switch (hook) {
11
- case internal_ts_1.Hook.beforeDestroy:
12
- return "destroy";
13
- case internal_ts_1.Hook.beforeDetach:
14
- return "detach";
15
- default:
16
- return undefined;
17
- }
18
- }
19
- class StoredReference {
20
- targetType;
21
- identifier;
22
- node;
23
- resolvedReference;
24
- constructor(value, targetType) {
25
- this.targetType = targetType;
26
- if ((0, internal_ts_1.isValidIdentifier)(value)) {
27
- this.identifier = value;
28
- }
29
- else if ((0, internal_ts_1.isStateTreeNode)(value)) {
30
- const targetNode = (0, internal_ts_1.getStateTreeNode)(value);
31
- if (!targetNode.identifierAttribute) {
32
- throw (0, internal_ts_1.fail)(`Can only store references with a defined identifier attribute.`);
33
- }
34
- const id = targetNode.unnormalizedIdentifier;
35
- if (id === null || id === undefined) {
36
- throw (0, internal_ts_1.fail)(`Can only store references to tree nodes with a defined identifier.`);
37
- }
38
- this.identifier = id;
39
- }
40
- else {
41
- throw (0, internal_ts_1.fail)(`Can only store references to tree nodes or identifiers, got: '${value}'`);
42
- }
43
- }
44
- updateResolvedReference(node) {
45
- const normalizedId = (0, internal_ts_1.normalizeIdentifier)(this.identifier);
46
- const root = node.root;
47
- const lastCacheModification = root.identifierCache.getLastCacheModificationPerId(normalizedId);
48
- if (!this.resolvedReference ||
49
- this.resolvedReference.lastCacheModification !== lastCacheModification) {
50
- const { targetType } = this;
51
- // reference was initialized with the identifier of the target
52
- const target = root.identifierCache.resolve(targetType, normalizedId);
53
- if (!target) {
54
- throw new InvalidReferenceError(`[mobx-state-tree] Failed to resolve reference '${this.identifier}' to type '${this.targetType.name}' (from node: ${node.path})`);
55
- }
56
- this.resolvedReference = {
57
- node: target,
58
- lastCacheModification: lastCacheModification
59
- };
60
- }
61
- }
62
- get resolvedValue() {
63
- this.updateResolvedReference(this.node);
64
- return this.resolvedReference.node.value;
65
- }
66
- }
67
- /**
68
- * @internal
69
- * @hidden
70
- */
71
- class InvalidReferenceError extends Error {
72
- constructor(m) {
73
- super(m);
74
- Object.setPrototypeOf(this, InvalidReferenceError.prototype);
75
- }
76
- }
77
- exports.InvalidReferenceError = InvalidReferenceError;
78
- /**
79
- * @internal
80
- * @hidden
81
- */
82
- class BaseReferenceType extends type_ts_1.SimpleType {
83
- targetType;
84
- onInvalidated;
85
- flags = internal_ts_1.TypeFlags.Reference;
86
- constructor(targetType, onInvalidated) {
87
- super(`reference(${targetType.name})`);
88
- this.targetType = targetType;
89
- this.onInvalidated = onInvalidated;
90
- }
91
- describe() {
92
- return this.name;
93
- }
94
- isAssignableFrom(type) {
95
- return this.targetType.isAssignableFrom(type);
96
- }
97
- isValidSnapshot(value, context) {
98
- return (0, internal_ts_1.isValidIdentifier)(value)
99
- ? (0, internal_ts_1.typeCheckSuccess)()
100
- : (0, internal_ts_1.typeCheckFailure)(context, value, "Value is not a valid identifier, which is a string or a number");
101
- }
102
- fireInvalidated(cause, storedRefNode, referenceId, refTargetNode) {
103
- // to actually invalidate a reference we need an alive parent,
104
- // since it is a scalar value (immutable-ish) and we need to change it
105
- // from the parent
106
- const storedRefParentNode = storedRefNode.parent;
107
- if (!storedRefParentNode || !storedRefParentNode.isAlive) {
108
- return;
109
- }
110
- const storedRefParentValue = storedRefParentNode.storedValue;
111
- if (!storedRefParentValue) {
112
- return;
113
- }
114
- this.onInvalidated({
115
- cause,
116
- parent: storedRefParentValue,
117
- invalidTarget: refTargetNode ? refTargetNode.storedValue : undefined,
118
- invalidId: referenceId,
119
- replaceRef(newRef) {
120
- (0, internal_ts_1.applyPatch)(storedRefNode.root.storedValue, {
121
- op: "replace",
122
- value: newRef,
123
- path: storedRefNode.path
124
- });
125
- },
126
- removeRef() {
127
- if ((0, internal_ts_1.isModelType)(storedRefParentNode.type)) {
128
- this.replaceRef(undefined);
129
- }
130
- else {
131
- (0, internal_ts_1.applyPatch)(storedRefNode.root.storedValue, {
132
- op: "remove",
133
- path: storedRefNode.path
134
- });
135
- }
136
- }
137
- });
138
- }
139
- addTargetNodeWatcher(storedRefNode, referenceId) {
140
- // this will make sure the target node becomes created
141
- const refTargetValue = this.getValue(storedRefNode);
142
- if (!refTargetValue) {
143
- return undefined;
144
- }
145
- const refTargetNode = (0, internal_ts_1.getStateTreeNode)(refTargetValue);
146
- const hookHandler = (_, refTargetNodeHook) => {
147
- const cause = getInvalidationCause(refTargetNodeHook);
148
- if (!cause) {
149
- return;
150
- }
151
- this.fireInvalidated(cause, storedRefNode, referenceId, refTargetNode);
152
- };
153
- const refTargetDetachHookDisposer = refTargetNode.registerHook(internal_ts_1.Hook.beforeDetach, hookHandler);
154
- const refTargetDestroyHookDisposer = refTargetNode.registerHook(internal_ts_1.Hook.beforeDestroy, hookHandler);
155
- return () => {
156
- refTargetDetachHookDisposer();
157
- refTargetDestroyHookDisposer();
158
- };
159
- }
160
- watchTargetNodeForInvalidations(storedRefNode, identifier, customGetSet) {
161
- if (!this.onInvalidated) {
162
- return;
163
- }
164
- let onRefTargetDestroyedHookDisposer;
165
- // get rid of the watcher hook when the stored ref node is destroyed
166
- // detached is ignored since scalar nodes (where the reference resides) cannot be detached
167
- storedRefNode.registerHook(internal_ts_1.Hook.beforeDestroy, () => {
168
- if (onRefTargetDestroyedHookDisposer) {
169
- onRefTargetDestroyedHookDisposer();
170
- }
171
- });
172
- const startWatching = (sync) => {
173
- // re-create hook in case the stored ref gets reattached
174
- if (onRefTargetDestroyedHookDisposer) {
175
- onRefTargetDestroyedHookDisposer();
176
- }
177
- // make sure the target node is actually there and initialized
178
- const storedRefParentNode = storedRefNode.parent;
179
- const storedRefParentValue = storedRefParentNode && storedRefParentNode.storedValue;
180
- if (storedRefParentNode &&
181
- storedRefParentNode.isAlive &&
182
- storedRefParentValue) {
183
- let refTargetNodeExists;
184
- if (customGetSet) {
185
- refTargetNodeExists = !!customGetSet.get(identifier, storedRefParentValue);
186
- }
187
- else {
188
- refTargetNodeExists = storedRefNode.root.identifierCache.has(this.targetType, (0, internal_ts_1.normalizeIdentifier)(identifier));
189
- }
190
- if (!refTargetNodeExists) {
191
- // we cannot change the reference in sync mode
192
- // since we are in the middle of a reconciliation/instantiation and the change would be overwritten
193
- // for those cases just let the wrong reference be assigned and fail upon usage
194
- // (like current references do)
195
- // this means that effectively this code will only run when it is created from a snapshot
196
- if (!sync) {
197
- this.fireInvalidated("invalidSnapshotReference", storedRefNode, identifier, null);
198
- }
199
- }
200
- else {
201
- onRefTargetDestroyedHookDisposer = this.addTargetNodeWatcher(storedRefNode, identifier);
202
- }
203
- }
204
- };
205
- if (storedRefNode.state === internal_ts_1.NodeLifeCycle.FINALIZED) {
206
- // already attached, so the whole tree is ready
207
- startWatching(true);
208
- }
209
- else {
210
- if (!storedRefNode.isRoot) {
211
- // start watching once the whole tree is ready
212
- storedRefNode.root.registerHook(internal_ts_1.Hook.afterCreationFinalization, () => {
213
- // make sure to attach it so it can start listening
214
- if (storedRefNode.parent) {
215
- storedRefNode.parent.createObservableInstanceIfNeeded();
216
- }
217
- });
218
- }
219
- // start watching once the node is attached somewhere / parent changes
220
- storedRefNode.registerHook(internal_ts_1.Hook.afterAttach, () => {
221
- startWatching(false);
222
- });
223
- }
224
- }
225
- }
226
- exports.BaseReferenceType = BaseReferenceType;
227
- /**
228
- * @internal
229
- * @hidden
230
- */
231
- class IdentifierReferenceType extends BaseReferenceType {
232
- constructor(targetType, onInvalidated) {
233
- super(targetType, onInvalidated);
234
- }
235
- getValue(storedRefNode) {
236
- if (!storedRefNode.isAlive) {
237
- return undefined;
238
- }
239
- const storedRef = storedRefNode.storedValue;
240
- return storedRef.resolvedValue;
241
- }
242
- getSnapshot(storedRefNode) {
243
- const ref = storedRefNode.storedValue;
244
- return ref.identifier;
245
- }
246
- instantiate(parent, subpath, environment, initialValue) {
247
- const identifier = (0, internal_ts_1.isStateTreeNode)(initialValue)
248
- ? (0, internal_ts_1.getIdentifier)(initialValue)
249
- : initialValue;
250
- const storedRef = new StoredReference(initialValue, this.targetType);
251
- const storedRefNode = (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, storedRef);
252
- storedRef.node = storedRefNode;
253
- this.watchTargetNodeForInvalidations(storedRefNode, identifier, undefined);
254
- return storedRefNode;
255
- }
256
- reconcile(current, newValue, parent, subpath) {
257
- if (!current.isDetaching && current.type === this) {
258
- const compareByValue = (0, internal_ts_1.isStateTreeNode)(newValue);
259
- const ref = current.storedValue;
260
- if ((!compareByValue && ref.identifier === newValue) ||
261
- (compareByValue && ref.resolvedValue === newValue)) {
262
- current.setParent(parent, subpath);
263
- return current;
264
- }
265
- }
266
- const newNode = this.instantiate(parent, subpath, undefined, newValue);
267
- current.die(); // noop if detaching
268
- return newNode;
269
- }
270
- }
271
- exports.IdentifierReferenceType = IdentifierReferenceType;
272
- /**
273
- * @internal
274
- * @hidden
275
- */
276
- class CustomReferenceType extends BaseReferenceType {
277
- options;
278
- constructor(targetType, options, onInvalidated) {
279
- super(targetType, onInvalidated);
280
- this.options = options;
281
- }
282
- getValue(storedRefNode) {
283
- if (!storedRefNode.isAlive) {
284
- return undefined;
285
- }
286
- const referencedNode = this.options.get(storedRefNode.storedValue, storedRefNode.parent ? storedRefNode.parent.storedValue : null);
287
- return referencedNode;
288
- }
289
- getSnapshot(storedRefNode) {
290
- return storedRefNode.storedValue;
291
- }
292
- instantiate(parent, subpath, environment, newValue) {
293
- const identifier = (0, internal_ts_1.isStateTreeNode)(newValue)
294
- ? this.options.set(newValue, parent ? parent.storedValue : null)
295
- : newValue;
296
- const storedRefNode = (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, identifier);
297
- this.watchTargetNodeForInvalidations(storedRefNode, identifier, this.options);
298
- return storedRefNode;
299
- }
300
- reconcile(current, newValue, parent, subpath) {
301
- const newIdentifier = (0, internal_ts_1.isStateTreeNode)(newValue)
302
- ? this.options.set(newValue, current ? current.storedValue : null)
303
- : newValue;
304
- if (!current.isDetaching &&
305
- current.type === this &&
306
- current.storedValue === newIdentifier) {
307
- current.setParent(parent, subpath);
308
- return current;
309
- }
310
- const newNode = this.instantiate(parent, subpath, undefined, newIdentifier);
311
- current.die(); // noop if detaching
312
- return newNode;
313
- }
314
- }
315
- exports.CustomReferenceType = CustomReferenceType;
316
- /**
317
- * `types.reference` - Creates a reference to another type, which should have defined an identifier.
318
- * See also the [reference and identifiers](https://github.com/mobxjs/mobx-state-tree#references-and-identifiers) section.
319
- */
320
- function reference(subType, options) {
321
- (0, internal_ts_1.assertIsType)(subType, 1);
322
- if ((0, internal_ts_1.devMode)()) {
323
- if (options && typeof options === "string") {
324
- // istanbul ignore next
325
- throw (0, internal_ts_1.fail)("References with base path are no longer supported. Please remove the base path.");
326
- }
327
- }
328
- const getSetOptions = options
329
- ? options
330
- : undefined;
331
- const onInvalidated = options
332
- ? options.onInvalidated
333
- : undefined;
334
- if (getSetOptions && (getSetOptions.get || getSetOptions.set)) {
335
- if ((0, internal_ts_1.devMode)()) {
336
- if (!getSetOptions.get || !getSetOptions.set) {
337
- throw (0, internal_ts_1.fail)("reference options must either contain both a 'get' and a 'set' method or none of them");
338
- }
339
- }
340
- return new CustomReferenceType(subType, {
341
- get: getSetOptions.get,
342
- set: getSetOptions.set
343
- }, onInvalidated);
344
- }
345
- else {
346
- return new IdentifierReferenceType(subType, onInvalidated);
347
- }
348
- }
349
- /**
350
- * Returns if a given value represents a reference type.
351
- *
352
- * @param type
353
- * @returns
354
- */
355
- function isReferenceType(type) {
356
- return (type.flags & internal_ts_1.TypeFlags.Reference) > 0;
357
- }
358
- /**
359
- * `types.safeReference` - A safe reference is like a standard reference, except that it accepts the undefined value by default
360
- * and automatically sets itself to undefined (when the parent is a model) / removes itself from arrays and maps
361
- * when the reference it is pointing to gets detached/destroyed.
362
- *
363
- * The optional options parameter object accepts a parameter named `acceptsUndefined`, which is set to true by default, so it is suitable
364
- * for model properties.
365
- * When used inside collections (arrays/maps), it is recommended to set this option to false so it can't take undefined as value,
366
- * which is usually the desired in those cases.
367
- * 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
368
- *
369
- * Strictly speaking it is a `types.maybe(types.reference(X))` (when `acceptsUndefined` is set to true, the default) and
370
- * `types.reference(X)` (when `acceptsUndefined` is set to false), both of them with a customized `onInvalidated` option.
371
- *
372
- * @param subType
373
- * @param options
374
- * @returns
375
- */
376
- function safeReference(subType, options) {
377
- const refType = reference(subType, {
378
- ...options,
379
- onInvalidated(ev) {
380
- if (options && options.onInvalidated) {
381
- options.onInvalidated(ev);
382
- }
383
- ev.removeRef();
384
- }
385
- });
386
- if (options && options.acceptsUndefined === false) {
387
- return refType;
388
- }
389
- else {
390
- return (0, internal_ts_1.maybe)(refType);
391
- }
392
- }
393
- //# sourceMappingURL=reference.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"reference.js","sourceRoot":"","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AAG,CAAC,EAAE,CAAC;QAC9D,IAAI,IAAA,qBAAO,GAAE,EAAE,CAAC;YACd,IAAI,CAAC,aAAa,CAAC,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC;gBAC7C,MAAM,IAAA,kBAAI,EACR,uFAAuF,CACxF,CAAA;YACH,CAAC;QACH,CAAC;QAED,OAAO,IAAI,mBAAmB,CAC5B,OAAO,EACP;YACE,GAAG,EAAE,aAAa,CAAC,GAAG;YACtB,GAAG,EAAE,aAAa,CAAC,GAAG;SACvB,EACD,aAAa,CACd,CAAA;IACH,CAAC;SAAM,CAAC;QACN,OAAO,IAAI,uBAAuB,CAAC,OAAO,EAAE,aAAa,CAAC,CAAA;IAC5D,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,SAAgB,eAAe,CAC7B,IAAQ;IAER,OAAO,CAAC,IAAI,CAAC,KAAK,GAAG,uBAAS,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;AAC/C,CAAC;AAgBD;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,aAAa,CAC3B,OAAW,EACX,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,IAAA,mBAAK,EAAC,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"]}
@@ -1,10 +0,0 @@
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;
@@ -1,86 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.refinement = refinement;
4
- exports.isRefinementType = isRefinementType;
5
- const type_ts_1 = require("../../core/type/type.js");
6
- const internal_ts_1 = require("../../internal.js");
7
- const utils_ts_1 = require("../../utils.js");
8
- class Refinement extends type_ts_1.BaseType {
9
- _subtype;
10
- _predicate;
11
- _message;
12
- get flags() {
13
- return this._subtype.flags | internal_ts_1.TypeFlags.Refinement;
14
- }
15
- constructor(name, _subtype, _predicate, _message) {
16
- super(name);
17
- this._subtype = _subtype;
18
- this._predicate = _predicate;
19
- this._message = _message;
20
- }
21
- describe() {
22
- return this.name;
23
- }
24
- instantiate(parent, subpath, environment, initialValue) {
25
- // create the child type
26
- return this._subtype.instantiate(parent, subpath, environment, initialValue);
27
- }
28
- isAssignableFrom(type) {
29
- return this._subtype.isAssignableFrom(type);
30
- }
31
- isValidSnapshot(value, context) {
32
- const subtypeErrors = this._subtype.validate(value, context);
33
- if (subtypeErrors.length > 0) {
34
- return subtypeErrors;
35
- }
36
- const snapshot = (0, internal_ts_1.isStateTreeNode)(value)
37
- ? (0, internal_ts_1.getStateTreeNode)(value).snapshot
38
- : value;
39
- if (!this._predicate(snapshot)) {
40
- return (0, internal_ts_1.typeCheckFailure)(context, value, this._message(value));
41
- }
42
- return (0, internal_ts_1.typeCheckSuccess)();
43
- }
44
- reconcile(current, newValue, parent, subpath) {
45
- return this._subtype.reconcile(current, newValue, parent, subpath);
46
- }
47
- getSubTypes() {
48
- return this._subtype;
49
- }
50
- }
51
- /**
52
- * `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.
53
- *
54
- * @param name
55
- * @param type
56
- * @param predicate
57
- * @returns
58
- */
59
- function refinement(...args) {
60
- const name = typeof args[0] === "string"
61
- ? args.shift()
62
- : (0, internal_ts_1.isType)(args[0])
63
- ? args[0].name
64
- : null;
65
- const type = args[0];
66
- const predicate = args[1];
67
- const message = args[2]
68
- ? args[2]
69
- : (_v) => "Value does not respect the refinement predicate";
70
- // ensures all parameters are correct
71
- (0, internal_ts_1.assertIsType)(type, [1, 2]);
72
- (0, utils_ts_1.assertIsString)(name, 1);
73
- (0, utils_ts_1.assertIsFunction)(predicate, [2, 3]);
74
- (0, utils_ts_1.assertIsFunction)(message, [3, 4]);
75
- return new Refinement(name, type, predicate, message);
76
- }
77
- /**
78
- * Returns if a given value is a refinement type.
79
- *
80
- * @param type
81
- * @returns
82
- */
83
- function isRefinementType(type) {
84
- return (type.flags & internal_ts_1.TypeFlags.Refinement) > 0;
85
- }
86
- //# sourceMappingURL=refinement.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"refinement.js","sourceRoot":"","sources":["../../../src/types/utility-types/refinement.ts"],"names":[],"mappings":";;AAiHA,gCAmBC;AAQD,4CAEC;AA9ID,qDAAkD;AAClD,mDAa0B;AAC1B,6CAAiE;AAEjE,MAAM,UAAgC,SAAQ,kBAK7C;IAOoB;IACA;IACA;IARnB,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,GAAG,uBAAS,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;IAED,eAAe,CACb,KAAgB,EAChB,OAA2B;QAE3B,MAAM,aAAa,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,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B,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;QACT,CAAC,CAAC,CAAC,EAAO,EAAE,EAAE,CAAC,iDAAiD,CAAA;IAClE,qCAAqC;IACrC,IAAA,0BAAY,EAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IAC1B,IAAA,yBAAc,EAAC,IAAI,EAAE,CAAC,CAAC,CAAA;IACvB,IAAA,2BAAgB,EAAC,SAAS,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IACnC,IAAA,2BAAgB,EAAC,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,SAAgB,gBAAgB,CAAsB,IAAQ;IAC5D,OAAO,CAAC,IAAI,CAAC,KAAK,GAAG,uBAAS,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"]}
@@ -1,18 +0,0 @@
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>;