@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,98 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Frozen = void 0;
4
- exports.frozen = frozen;
5
- exports.isFrozenType = isFrozenType;
6
- const type_ts_1 = require("../../core/type/type.js");
7
- const internal_ts_1 = require("../../internal.js");
8
- /**
9
- * @internal
10
- * @hidden
11
- */
12
- class Frozen extends type_ts_1.SimpleType {
13
- subType;
14
- flags = internal_ts_1.TypeFlags.Frozen;
15
- constructor(subType) {
16
- super(subType ? `frozen(${subType.name})` : "frozen");
17
- this.subType = subType;
18
- }
19
- describe() {
20
- return "<any immutable value>";
21
- }
22
- instantiate(parent, subpath, environment, value) {
23
- // create the node
24
- return (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, (0, internal_ts_1.deepFreeze)(value));
25
- }
26
- isValidSnapshot(value, context) {
27
- if (!(0, internal_ts_1.isSerializable)(value)) {
28
- return (0, internal_ts_1.typeCheckFailure)(context, value, "Value is not serializable and cannot be frozen");
29
- }
30
- if (this.subType) {
31
- return this.subType.validate(value, context);
32
- }
33
- return (0, internal_ts_1.typeCheckSuccess)();
34
- }
35
- }
36
- exports.Frozen = Frozen;
37
- const untypedFrozenInstance = new Frozen();
38
- /**
39
- * `types.frozen` - Frozen can be used to store any value that is serializable in itself (that is valid JSON).
40
- * Frozen values need to be immutable or treated as if immutable. They need be serializable as well.
41
- * Values stored in frozen will snapshotted as-is by MST, and internal changes will not be tracked.
42
- *
43
- * This is useful to store complex, but immutable values like vectors etc. It can form a powerful bridge to parts of your application that should be immutable, or that assume data to be immutable.
44
- *
45
- * Note: if you want to store free-form state that is mutable, or not serializeable, consider using volatile state instead.
46
- *
47
- * Frozen properties can be defined in three different ways
48
- * 1. `types.frozen(SubType)` - provide a valid MST type and frozen will check if the provided data conforms the snapshot for that type
49
- * 2. `types.frozen({ someDefaultValue: true})` - provide a primitive value, object or array, and MST will infer the type from that object, and also make it the default value for the field
50
- * 3. `types.frozen<TypeScriptType>()` - provide a typescript type, to help in strongly typing the field (design time only)
51
- *
52
- * Example:
53
- * ```ts
54
- * const GameCharacter = types.model({
55
- * name: string,
56
- * location: types.frozen({ x: 0, y: 0})
57
- * })
58
- *
59
- * const hero = GameCharacter.create({
60
- * name: "Mario",
61
- * location: { x: 7, y: 4 }
62
- * })
63
- *
64
- * hero.location = { x: 10, y: 2 } // OK
65
- * hero.location.x = 7 // Not ok!
66
- * ```
67
- *
68
- * ```ts
69
- * type Point = { x: number, y: number }
70
- * const Mouse = types.model({
71
- * loc: types.frozen<Point>()
72
- * })
73
- * ```
74
- *
75
- * @param defaultValueOrType
76
- * @returns
77
- */
78
- function frozen(arg) {
79
- if (arguments.length === 0) {
80
- return untypedFrozenInstance;
81
- }
82
- else if ((0, internal_ts_1.isType)(arg)) {
83
- return new Frozen(arg);
84
- }
85
- else {
86
- return (0, internal_ts_1.optional)(untypedFrozenInstance, arg);
87
- }
88
- }
89
- /**
90
- * Returns if a given value represents a frozen type.
91
- *
92
- * @param type
93
- * @returns
94
- */
95
- function isFrozenType(type) {
96
- return (0, internal_ts_1.isType)(type) && (type.flags & internal_ts_1.TypeFlags.Frozen) > 0;
97
- }
98
- //# sourceMappingURL=frozen.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"frozen.js","sourceRoot":"","sources":["../../../src/types/utility-types/frozen.ts"],"names":[],"mappings":";;;AA+GA,wBAQC;AAQD,oCAIC;AAnID,qDAAoD;AACpD,mDAc0B;AAE1B;;;GAGG;AACH,MAAa,MAAU,SAAQ,oBAAmB;IAG5B;IAFpB,KAAK,GAAG,uBAAS,CAAC,MAAM,CAAA;IAExB,YAAoB,OAAkB;QACpC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU,OAAO,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAA;QADnC,YAAO,GAAP,OAAO,CAAW;IAEtC,CAAC;IAED,QAAQ;QACN,OAAO,uBAAuB,CAAA;IAChC,CAAC;IAED,WAAW,CACT,MAA4B,EAC5B,OAAe,EACf,WAAgB,EAChB,KAAgB;QAEhB,kBAAkB;QAClB,OAAO,IAAA,8BAAgB,EACrB,IAAI,EACJ,MAAM,EACN,OAAO,EACP,WAAW,EACX,IAAA,wBAAU,EAAC,KAAK,CAAC,CAClB,CAAA;IACH,CAAC;IAED,eAAe,CACb,KAAgB,EAChB,OAA2B;QAE3B,IAAI,CAAC,IAAA,4BAAc,EAAC,KAAK,CAAC,EAAE,CAAC;YAC3B,OAAO,IAAA,8BAAgB,EACrB,OAAO,EACP,KAAK,EACL,gDAAgD,CACjD,CAAA;QACH,CAAC;QACD,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QAC9C,CAAC;QACD,OAAO,IAAA,8BAAgB,GAAE,CAAA;IAC3B,CAAC;CACF;AA3CD,wBA2CC;AAED,MAAM,qBAAqB,GAAG,IAAI,MAAM,EAAE,CAAA;AAK1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,SAAgB,MAAM,CAAC,GAAS;IAC9B,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC3B,OAAO,qBAAqB,CAAA;IAC9B,CAAC;SAAM,IAAI,IAAA,oBAAM,EAAC,GAAG,CAAC,EAAE,CAAC;QACvB,OAAO,IAAI,MAAM,CAAC,GAAG,CAAC,CAAA;IACxB,CAAC;SAAM,CAAC;QACN,OAAO,IAAA,sBAAQ,EAAC,qBAAqB,EAAE,GAAG,CAAC,CAAA;IAC7C,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,SAAgB,YAAY,CAC1B,IAAQ;IAER,OAAO,IAAA,oBAAM,EAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,uBAAS,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;AAC5D,CAAC","sourcesContent":["import { SimpleType } from \"../../core/type/type.ts\"\nimport {\n type AnyObjectNode,\n type IAnyType,\n type IType,\n type IValidationContext,\n type IValidationResult,\n TypeFlags,\n createScalarNode,\n deepFreeze,\n isSerializable,\n isType,\n optional,\n typeCheckFailure,\n typeCheckSuccess\n} from \"../../internal.ts\"\n\n/**\n * @internal\n * @hidden\n */\nexport class Frozen<T> extends SimpleType<T, T, T> {\n flags = TypeFlags.Frozen\n\n constructor(private subType?: IAnyType) {\n super(subType ? `frozen(${subType.name})` : \"frozen\")\n }\n\n describe() {\n return \"<any immutable value>\"\n }\n\n instantiate(\n parent: AnyObjectNode | null,\n subpath: string,\n environment: any,\n value: this[\"C\"]\n ): this[\"N\"] {\n // create the node\n return createScalarNode(\n this,\n parent,\n subpath,\n environment,\n deepFreeze(value)\n )\n }\n\n isValidSnapshot(\n value: this[\"C\"],\n context: IValidationContext\n ): IValidationResult {\n if (!isSerializable(value)) {\n return typeCheckFailure(\n context,\n value,\n \"Value is not serializable and cannot be frozen\"\n )\n }\n if (this.subType) {\n return this.subType.validate(value, context)\n }\n return typeCheckSuccess()\n }\n}\n\nconst untypedFrozenInstance = new Frozen()\n\nexport function frozen<C>(subType: IType<C, any, any>): IType<C, C, C>\nexport function frozen<T>(defaultValue: T): IType<T | undefined | null, T, T>\nexport function frozen<T = any>(): IType<T, T, T> // do not assume undefined by default, let the user specify it if needed\n/**\n * `types.frozen` - Frozen can be used to store any value that is serializable in itself (that is valid JSON).\n * Frozen values need to be immutable or treated as if immutable. They need be serializable as well.\n * Values stored in frozen will snapshotted as-is by MST, and internal changes will not be tracked.\n *\n * This is useful to store complex, but immutable values like vectors etc. It can form a powerful bridge to parts of your application that should be immutable, or that assume data to be immutable.\n *\n * Note: if you want to store free-form state that is mutable, or not serializeable, consider using volatile state instead.\n *\n * Frozen properties can be defined in three different ways\n * 1. `types.frozen(SubType)` - provide a valid MST type and frozen will check if the provided data conforms the snapshot for that type\n * 2. `types.frozen({ someDefaultValue: true})` - provide a primitive value, object or array, and MST will infer the type from that object, and also make it the default value for the field\n * 3. `types.frozen<TypeScriptType>()` - provide a typescript type, to help in strongly typing the field (design time only)\n *\n * Example:\n * ```ts\n * const GameCharacter = types.model({\n * name: string,\n * location: types.frozen({ x: 0, y: 0})\n * })\n *\n * const hero = GameCharacter.create({\n * name: \"Mario\",\n * location: { x: 7, y: 4 }\n * })\n *\n * hero.location = { x: 10, y: 2 } // OK\n * hero.location.x = 7 // Not ok!\n * ```\n *\n * ```ts\n * type Point = { x: number, y: number }\n * const Mouse = types.model({\n * loc: types.frozen<Point>()\n * })\n * ```\n *\n * @param defaultValueOrType\n * @returns\n */\nexport function frozen(arg?: any): any {\n if (arguments.length === 0) {\n return untypedFrozenInstance\n } else if (isType(arg)) {\n return new Frozen(arg)\n } else {\n return optional(untypedFrozenInstance, arg)\n }\n}\n\n/**\n * Returns if a given value represents a frozen type.\n *\n * @param type\n * @returns\n */\nexport function isFrozenType<IT extends IType<T | any, T, T>, T = any>(\n type: IT\n): type is IT {\n return isType(type) && (type.flags & TypeFlags.Frozen) > 0\n}\n"]}
@@ -1,87 +0,0 @@
1
- import { SimpleType } from "../../core/type/type.ts";
2
- import { type AnyObjectNode, type ISimpleType, type IValidationContext, type IValidationResult, type ScalarNode, TypeFlags } from "../../internal.ts";
3
- declare abstract class BaseIdentifierType<T> extends SimpleType<T, T, T> {
4
- private readonly validType;
5
- readonly flags = TypeFlags.Identifier;
6
- constructor(name: string, validType: "string" | "number");
7
- instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: this["C"]): this["N"];
8
- reconcile(current: this["N"], newValue: this["C"], parent: AnyObjectNode, subpath: string): this["N"];
9
- isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
10
- }
11
- /**
12
- * @internal
13
- * @hidden
14
- */
15
- export declare class IdentifierType extends BaseIdentifierType<string> {
16
- readonly flags = TypeFlags.Identifier;
17
- constructor();
18
- describe(): string;
19
- }
20
- /**
21
- * @internal
22
- * @hidden
23
- */
24
- export declare class IdentifierNumberType extends BaseIdentifierType<number> {
25
- constructor();
26
- getSnapshot(node: ScalarNode<number, number, number>): number;
27
- describe(): string;
28
- }
29
- /**
30
- * `types.identifier` - Identifiers are used to make references, lifecycle events and reconciling works.
31
- * Inside a state tree, for each type can exist only one instance for each given identifier.
32
- * For example there couldn't be 2 instances of user with id 1. If you need more, consider using references.
33
- * Identifier can be used only as type property of a model.
34
- * This type accepts as parameter the value type of the identifier field that can be either string or number.
35
- *
36
- * Example:
37
- * ```ts
38
- * const Todo = types.model("Todo", {
39
- * id: types.identifier,
40
- * title: types.string
41
- * })
42
- * ```
43
- *
44
- * @returns
45
- */
46
- export declare const identifier: ISimpleType<string>;
47
- /**
48
- * `types.identifierNumber` - Similar to `types.identifier`. This one will serialize from / to a number when applying snapshots
49
- *
50
- * Example:
51
- * ```ts
52
- * const Todo = types.model("Todo", {
53
- * id: types.identifierNumber,
54
- * title: types.string
55
- * })
56
- * ```
57
- *
58
- * @returns
59
- */
60
- export declare const identifierNumber: ISimpleType<number>;
61
- /**
62
- * Returns if a given value represents an identifier type.
63
- *
64
- * @param type
65
- * @returns
66
- */
67
- export declare function isIdentifierType<IT extends typeof identifier | typeof identifierNumber>(type: IT): type is IT;
68
- /**
69
- * Valid types for identifiers.
70
- */
71
- export type ReferenceIdentifier = string | number;
72
- /**
73
- * @internal
74
- * @hidden
75
- */
76
- export declare function normalizeIdentifier(id: ReferenceIdentifier): string;
77
- /**
78
- * @internal
79
- * @hidden
80
- */
81
- export declare function isValidIdentifier(id: any): id is ReferenceIdentifier;
82
- /**
83
- * @internal
84
- * @hidden
85
- */
86
- export declare function assertIsValidIdentifier(id: ReferenceIdentifier, argNumber: number | number[]): void;
87
- export {};
@@ -1,130 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.identifierNumber = exports.identifier = exports.IdentifierNumberType = exports.IdentifierType = void 0;
4
- exports.isIdentifierType = isIdentifierType;
5
- exports.normalizeIdentifier = normalizeIdentifier;
6
- exports.isValidIdentifier = isValidIdentifier;
7
- exports.assertIsValidIdentifier = assertIsValidIdentifier;
8
- const type_ts_1 = require("../../core/type/type.js");
9
- const internal_ts_1 = require("../../internal.js");
10
- class BaseIdentifierType extends type_ts_1.SimpleType {
11
- validType;
12
- flags = internal_ts_1.TypeFlags.Identifier;
13
- constructor(name, validType) {
14
- super(name);
15
- this.validType = validType;
16
- }
17
- instantiate(parent, subpath, environment, initialValue) {
18
- if (!parent || !(parent.type instanceof internal_ts_1.ModelType)) {
19
- throw (0, internal_ts_1.fail)(`Identifier types can only be instantiated as direct child of a model type`);
20
- }
21
- return (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, initialValue);
22
- }
23
- reconcile(current, newValue, parent, subpath) {
24
- // we don't consider detaching here since identifier are scalar nodes, and scalar nodes cannot be detached
25
- if (current.storedValue !== newValue) {
26
- throw (0, internal_ts_1.fail)(`Tried to change identifier from '${current.storedValue}' to '${newValue}'. Changing identifiers is not allowed.`);
27
- }
28
- current.setParent(parent, subpath);
29
- return current;
30
- }
31
- isValidSnapshot(value, context) {
32
- if (typeof value !== this.validType) {
33
- return (0, internal_ts_1.typeCheckFailure)(context, value, `Value is not a valid ${this.describe()}, expected a ${this.validType}`);
34
- }
35
- return (0, internal_ts_1.typeCheckSuccess)();
36
- }
37
- }
38
- /**
39
- * @internal
40
- * @hidden
41
- */
42
- class IdentifierType extends BaseIdentifierType {
43
- flags = internal_ts_1.TypeFlags.Identifier;
44
- constructor() {
45
- super(`identifier`, "string");
46
- }
47
- describe() {
48
- return `identifier`;
49
- }
50
- }
51
- exports.IdentifierType = IdentifierType;
52
- /**
53
- * @internal
54
- * @hidden
55
- */
56
- class IdentifierNumberType extends BaseIdentifierType {
57
- constructor() {
58
- super("identifierNumber", "number");
59
- }
60
- getSnapshot(node) {
61
- return node.storedValue;
62
- }
63
- describe() {
64
- return `identifierNumber`;
65
- }
66
- }
67
- exports.IdentifierNumberType = IdentifierNumberType;
68
- /**
69
- * `types.identifier` - Identifiers are used to make references, lifecycle events and reconciling works.
70
- * Inside a state tree, for each type can exist only one instance for each given identifier.
71
- * For example there couldn't be 2 instances of user with id 1. If you need more, consider using references.
72
- * Identifier can be used only as type property of a model.
73
- * This type accepts as parameter the value type of the identifier field that can be either string or number.
74
- *
75
- * Example:
76
- * ```ts
77
- * const Todo = types.model("Todo", {
78
- * id: types.identifier,
79
- * title: types.string
80
- * })
81
- * ```
82
- *
83
- * @returns
84
- */
85
- exports.identifier = new IdentifierType();
86
- /**
87
- * `types.identifierNumber` - Similar to `types.identifier`. This one will serialize from / to a number when applying snapshots
88
- *
89
- * Example:
90
- * ```ts
91
- * const Todo = types.model("Todo", {
92
- * id: types.identifierNumber,
93
- * title: types.string
94
- * })
95
- * ```
96
- *
97
- * @returns
98
- */
99
- exports.identifierNumber = new IdentifierNumberType();
100
- /**
101
- * Returns if a given value represents an identifier type.
102
- *
103
- * @param type
104
- * @returns
105
- */
106
- function isIdentifierType(type) {
107
- return (0, internal_ts_1.isType)(type) && (type.flags & internal_ts_1.TypeFlags.Identifier) > 0;
108
- }
109
- /**
110
- * @internal
111
- * @hidden
112
- */
113
- function normalizeIdentifier(id) {
114
- return "" + id;
115
- }
116
- /**
117
- * @internal
118
- * @hidden
119
- */
120
- function isValidIdentifier(id) {
121
- return typeof id === "string" || typeof id === "number";
122
- }
123
- /**
124
- * @internal
125
- * @hidden
126
- */
127
- function assertIsValidIdentifier(id, argNumber) {
128
- (0, internal_ts_1.assertArg)(id, isValidIdentifier, "string or number (identifier)", argNumber);
129
- }
130
- //# sourceMappingURL=identifier.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../src/types/utility-types/identifier.ts"],"names":[],"mappings":";;;AAmJA,4CAIC;AAWD,kDAEC;AAMD,8CAEC;AAMD,0DAKC;AAvLD,qDAAoD;AACpD,mDAc0B;AAE1B,MAAe,kBAAsB,SAAQ,oBAAmB;IAK3C;IAJV,KAAK,GAAG,uBAAS,CAAC,UAAU,CAAA;IAErC,YACE,IAAY,EACK,SAA8B;QAE/C,KAAK,CAAC,IAAI,CAAC,CAAA;QAFM,cAAS,GAAT,SAAS,CAAqB;IAGjD,CAAC;IAED,WAAW,CACT,MAA4B,EAC5B,OAAe,EACf,WAAgB,EAChB,YAAuB;QAEvB,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,IAAI,YAAY,uBAAS,CAAC,EAAE,CAAC;YACnD,MAAM,IAAA,kBAAI,EACR,2EAA2E,CAC5E,CAAA;QACH,CAAC;QAED,OAAO,IAAA,8BAAgB,EAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,YAAY,CAAC,CAAA;IAC3E,CAAC;IAED,SAAS,CACP,OAAkB,EAClB,QAAmB,EACnB,MAAqB,EACrB,OAAe;QAEf,0GAA0G;QAC1G,IAAI,OAAO,CAAC,WAAW,KAAK,QAAQ,EAAE,CAAC;YACrC,MAAM,IAAA,kBAAI,EACR,oCAAoC,OAAO,CAAC,WAAW,SAAS,QAAQ,yCAAyC,CAClH,CAAA;QACH,CAAC;QACD,OAAO,CAA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B,CAAC,EAAuB;IACzD,OAAO,EAAE,GAAG,EAAE,CAAA;AAChB,CAAC;AAED;;;GAGG;AACH,SAAgB,iBAAiB,CAAC,EAAO;IACvC,OAAO,OAAO,EAAE,KAAK,QAAQ,IAAI,OAAO,EAAE,KAAK,QAAQ,CAAA;AACzD,CAAC;AAED;;;GAGG;AACH,SAAgB,uBAAuB,CACrC,EAAuB,EACvB,SAA4B;IAE5B,IAAA,uBAAS,EAAC,EAAE,EAAE,iBAAiB,EAAE,+BAA+B,EAAE,SAAS,CAAC,CAAA;AAC9E,CAAC","sourcesContent":["import { SimpleType } from \"../../core/type/type.ts\"\nimport {\n type AnyObjectNode,\n type ISimpleType,\n type IValidationContext,\n type IValidationResult,\n ModelType,\n type ScalarNode,\n TypeFlags,\n assertArg,\n createScalarNode,\n fail,\n isType,\n typeCheckFailure,\n typeCheckSuccess\n} from \"../../internal.ts\"\n\nabstract class BaseIdentifierType<T> extends SimpleType<T, T, T> {\n readonly flags = TypeFlags.Identifier\n\n constructor(\n name: string,\n private readonly validType: \"string\" | \"number\"\n ) {\n super(name)\n }\n\n instantiate(\n parent: AnyObjectNode | null,\n subpath: string,\n environment: any,\n initialValue: this[\"C\"]\n ): this[\"N\"] {\n if (!parent || !(parent.type instanceof ModelType)) {\n throw fail(\n `Identifier types can only be instantiated as direct child of a model type`\n )\n }\n\n return createScalarNode(this, parent, subpath, environment, initialValue)\n }\n\n reconcile(\n current: this[\"N\"],\n newValue: this[\"C\"],\n parent: AnyObjectNode,\n subpath: string\n ) {\n // we don't consider detaching here since identifier are scalar nodes, and scalar nodes cannot be detached\n if (current.storedValue !== newValue) {\n throw fail(\n `Tried to change identifier from '${current.storedValue}' to '${newValue}'. Changing identifiers is not allowed.`\n )\n }\n current.setParent(parent, subpath)\n return current\n }\n\n isValidSnapshot(\n value: this[\"C\"],\n context: IValidationContext\n ): IValidationResult {\n if (typeof value !== this.validType) {\n return typeCheckFailure(\n context,\n value,\n `Value is not a valid ${this.describe()}, expected a ${this.validType}`\n )\n }\n return typeCheckSuccess()\n }\n}\n\n/**\n * @internal\n * @hidden\n */\nexport class IdentifierType extends BaseIdentifierType<string> {\n readonly flags = TypeFlags.Identifier\n\n constructor() {\n super(`identifier`, \"string\")\n }\n\n describe() {\n return `identifier`\n }\n}\n\n/**\n * @internal\n * @hidden\n */\nexport class IdentifierNumberType extends BaseIdentifierType<number> {\n constructor() {\n super(\"identifierNumber\", \"number\")\n }\n\n getSnapshot(node: ScalarNode<number, number, number>): number {\n return node.storedValue\n }\n\n describe() {\n return `identifierNumber`\n }\n}\n\n/**\n * `types.identifier` - Identifiers are used to make references, lifecycle events and reconciling works.\n * Inside a state tree, for each type can exist only one instance for each given identifier.\n * For example there couldn't be 2 instances of user with id 1. If you need more, consider using references.\n * Identifier can be used only as type property of a model.\n * This type accepts as parameter the value type of the identifier field that can be either string or number.\n *\n * Example:\n * ```ts\n * const Todo = types.model(\"Todo\", {\n * id: types.identifier,\n * title: types.string\n * })\n * ```\n *\n * @returns\n */\nexport const identifier: ISimpleType<string> = new IdentifierType()\n\n/**\n * `types.identifierNumber` - Similar to `types.identifier`. This one will serialize from / to a number when applying snapshots\n *\n * Example:\n * ```ts\n * const Todo = types.model(\"Todo\", {\n * id: types.identifierNumber,\n * title: types.string\n * })\n * ```\n *\n * @returns\n */\nexport const identifierNumber: ISimpleType<number> = new IdentifierNumberType()\n\n/**\n * Returns if a given value represents an identifier type.\n *\n * @param type\n * @returns\n */\nexport function isIdentifierType<\n IT extends typeof identifier | typeof identifierNumber\n>(type: IT): type is IT {\n return isType(type) && (type.flags & TypeFlags.Identifier) > 0\n}\n\n/**\n * Valid types for identifiers.\n */\nexport type ReferenceIdentifier = string | number\n\n/**\n * @internal\n * @hidden\n */\nexport function normalizeIdentifier(id: ReferenceIdentifier): string {\n return \"\" + id\n}\n\n/**\n * @internal\n * @hidden\n */\nexport function isValidIdentifier(id: any): id is ReferenceIdentifier {\n return typeof id === \"string\" || typeof id === \"number\"\n}\n\n/**\n * @internal\n * @hidden\n */\nexport function assertIsValidIdentifier(\n id: ReferenceIdentifier,\n argNumber: number | number[]\n) {\n assertArg(id, isValidIdentifier, \"string or number (identifier)\", argNumber)\n}\n"]}
@@ -1,10 +0,0 @@
1
- import { type IAnyType } from "../../internal.ts";
2
- export declare function late<T extends IAnyType>(type: () => T): T;
3
- export declare function late<T extends IAnyType>(name: string, type: () => T): T;
4
- /**
5
- * Returns if a given value represents a late type.
6
- *
7
- * @param type
8
- * @returns
9
- */
10
- export declare function isLateType<IT extends IAnyType>(type: IT): type is IT;
@@ -1,110 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.late = late;
4
- exports.isLateType = isLateType;
5
- const type_ts_1 = require("../../core/type/type.js");
6
- const internal_ts_1 = require("../../internal.js");
7
- class Late extends type_ts_1.BaseType {
8
- _definition;
9
- _subType;
10
- get flags() {
11
- return (this._subType ? this._subType.flags : 0) | internal_ts_1.TypeFlags.Late;
12
- }
13
- getSubType(mustSucceed) {
14
- if (!this._subType) {
15
- let t = undefined;
16
- try {
17
- t = this._definition();
18
- }
19
- catch (e) {
20
- if (e instanceof ReferenceError) // can happen in strict ES5 code when a definition is self refering
21
- {
22
- t = undefined;
23
- }
24
- else {
25
- throw e;
26
- }
27
- }
28
- if (mustSucceed && t === undefined) {
29
- throw (0, internal_ts_1.fail)("Late type seems to be used too early, the definition (still) returns undefined");
30
- }
31
- if (t) {
32
- if ((0, internal_ts_1.devMode)() && !(0, internal_ts_1.isType)(t)) {
33
- throw (0, internal_ts_1.fail)("Failed to determine subtype, make sure types.late returns a type definition.");
34
- }
35
- this._subType = t;
36
- }
37
- }
38
- return this._subType;
39
- }
40
- constructor(name, _definition) {
41
- super(name);
42
- this._definition = _definition;
43
- }
44
- instantiate(parent, subpath, environment, initialValue) {
45
- return this.getSubType(true).instantiate(parent, subpath, environment, initialValue);
46
- }
47
- reconcile(current, newValue, parent, subpath) {
48
- return this.getSubType(true).reconcile(current, newValue, parent, subpath);
49
- }
50
- describe() {
51
- const t = this.getSubType(false);
52
- return t ? t.name : "<uknown late type>";
53
- }
54
- isValidSnapshot(value, context) {
55
- const t = this.getSubType(false);
56
- if (!t) {
57
- // See #916; the variable the definition closure is pointing to wasn't defined yet, so can't be evaluted yet here
58
- return (0, internal_ts_1.typeCheckSuccess)();
59
- }
60
- return t.validate(value, context);
61
- }
62
- isAssignableFrom(type) {
63
- const t = this.getSubType(false);
64
- return t ? t.isAssignableFrom(type) : false;
65
- }
66
- getSubTypes() {
67
- const subtype = this.getSubType(false);
68
- return subtype ? subtype : internal_ts_1.cannotDetermineSubtype;
69
- }
70
- }
71
- /**
72
- * `types.late` - Defines a type that gets implemented later. This is useful when you have to deal with circular dependencies.
73
- * Please notice that when defining circular dependencies TypeScript isn't smart enough to inference them.
74
- *
75
- * Example:
76
- * ```ts
77
- * // TypeScript isn't smart enough to infer self referencing types.
78
- * const Node = types.model({
79
- * children: types.array(types.late((): IAnyModelType => Node)) // then typecast each array element to Instance<typeof Node>
80
- * })
81
- * ```
82
- *
83
- * @param name The name to use for the type that will be returned.
84
- * @param type A function that returns the type that will be defined.
85
- * @returns
86
- */
87
- function late(nameOrType, maybeType) {
88
- const name = typeof nameOrType === "string"
89
- ? nameOrType
90
- : `late(${nameOrType.toString()})`;
91
- const type = typeof nameOrType === "string" ? maybeType : nameOrType;
92
- // checks that the type is actually a late type
93
- if ((0, internal_ts_1.devMode)()) {
94
- if (!(typeof type === "function" && type.length === 0)) {
95
- throw (0, internal_ts_1.fail)("Invalid late type, expected a function with zero arguments that returns a type, got: " +
96
- type);
97
- }
98
- }
99
- return new Late(name, type);
100
- }
101
- /**
102
- * Returns if a given value represents a late type.
103
- *
104
- * @param type
105
- * @returns
106
- */
107
- function isLateType(type) {
108
- return (0, internal_ts_1.isType)(type) && (type.flags & internal_ts_1.TypeFlags.Late) > 0;
109
- }
110
- //# sourceMappingURL=late.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"late.js","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CACF;AAID;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,IAAI,CAAC,UAAe,EAAE,SAA0B;IAC9D,MAAM,IAAI,GACR,OAAO,UAAU,KAAK,QAAQ;QAC5B,CAAC,CAAC,UAAU;QACZ,CAAC,CAAC,QAAQ,UAAU,CAAC,QAAQ,EAAE,GAAG,CAAA;IACtC,MAAM,IAAI,GAAG,OAAO,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAA;IACpE,+CAA+C;IAC/C,IAAI,IAAA,qBAAO,GAAE,EAAE,CAAC;QACd,IAAI,CAAC,CAAC,OAAO,IAAI,KAAK,UAAU,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC;YACvD,MAAM,IAAA,kBAAI,EACR,uFAAuF;gBACrF,IAAI,CACP,CAAA;QACH,CAAC;IACH,CAAC;IACD,OAAO,IAAI,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;AAC7B,CAAC;AAED;;;;;GAKG;AACH,SAAgB,UAAU,CAAsB,IAAQ;IACtD,OAAO,IAAA,oBAAM,EAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,uBAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AAC1D,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 cannotDetermineSubtype,\n devMode,\n fail,\n isType,\n typeCheckSuccess\n} from \"../../internal.ts\"\n\nclass Late<IT extends IAnyType> extends BaseType<\n IT[\"CreationType\"],\n IT[\"SnapshotType\"],\n IT[\"TypeWithoutSTN\"],\n ExtractNodeType<IT>\n> {\n private _subType?: IT\n\n get flags() {\n return (this._subType ? this._subType.flags : 0) | TypeFlags.Late\n }\n\n getSubType(mustSucceed: true): IT\n getSubType(mustSucceed: false): IT | undefined\n getSubType(mustSucceed: boolean): IT | undefined {\n if (!this._subType) {\n let t = undefined\n try {\n t = this._definition()\n } catch (e) {\n if (\n e instanceof ReferenceError\n ) // can happen in strict ES5 code when a definition is self refering\n {\n t = undefined\n } else {\n throw e\n }\n }\n if (mustSucceed && t === undefined) {\n throw fail(\n \"Late type seems to be used too early, the definition (still) returns undefined\"\n )\n }\n if (t) {\n if (devMode() && !isType(t)) {\n throw fail(\n \"Failed to determine subtype, make sure types.late returns a type definition.\"\n )\n }\n this._subType = t\n }\n }\n return this._subType\n }\n\n constructor(\n name: string,\n private readonly _definition: () => IT\n ) {\n super(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 return this.getSubType(true).instantiate(\n parent,\n subpath,\n environment,\n initialValue\n ) as any\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.getSubType(true).reconcile(\n current,\n newValue,\n parent,\n subpath\n ) as any\n }\n\n describe() {\n const t = this.getSubType(false)\n return t ? t.name : \"<uknown late type>\"\n }\n\n isValidSnapshot(\n value: this[\"C\"],\n context: IValidationContext\n ): IValidationResult {\n const t = this.getSubType(false)\n if (!t) {\n // See #916; the variable the definition closure is pointing to wasn't defined yet, so can't be evaluted yet here\n return typeCheckSuccess()\n }\n return t.validate(value, context)\n }\n\n isAssignableFrom(type: IAnyType) {\n const t = this.getSubType(false)\n return t ? t.isAssignableFrom(type) : false\n }\n\n getSubTypes() {\n const subtype = this.getSubType(false)\n return subtype ? subtype : cannotDetermineSubtype\n }\n}\n\nexport function late<T extends IAnyType>(type: () => T): T\nexport function late<T extends IAnyType>(name: string, type: () => T): T\n/**\n * `types.late` - Defines a type that gets implemented later. This is useful when you have to deal with circular dependencies.\n * Please notice that when defining circular dependencies TypeScript isn't smart enough to inference them.\n *\n * Example:\n * ```ts\n * // TypeScript isn't smart enough to infer self referencing types.\n * const Node = types.model({\n * children: types.array(types.late((): IAnyModelType => Node)) // then typecast each array element to Instance<typeof Node>\n * })\n * ```\n *\n * @param name The name to use for the type that will be returned.\n * @param type A function that returns the type that will be defined.\n * @returns\n */\nexport function late(nameOrType: any, maybeType?: () => IAnyType): IAnyType {\n const name =\n typeof nameOrType === \"string\"\n ? nameOrType\n : `late(${nameOrType.toString()})`\n const type = typeof nameOrType === \"string\" ? maybeType : nameOrType\n // checks that the type is actually a late type\n if (devMode()) {\n if (!(typeof type === \"function\" && type.length === 0)) {\n throw fail(\n \"Invalid late type, expected a function with zero arguments that returns a type, got: \" +\n type\n )\n }\n }\n return new Late(name, type)\n}\n\n/**\n * Returns if a given value represents a late type.\n *\n * @param type\n * @returns\n */\nexport function isLateType<IT extends IAnyType>(type: IT): type is IT {\n return isType(type) && (type.flags & TypeFlags.Late) > 0\n}\n"]}
@@ -1,23 +0,0 @@
1
- import { SimpleType } from "../../core/type/type.ts";
2
- import { type AnyObjectNode, type IType, type IValidationContext, type IValidationResult, TypeFlags } from "../../internal.ts";
3
- interface LazyOptions<T extends IType<any, any, any>, U> {
4
- loadType: () => Promise<T>;
5
- shouldLoadPredicate: (parent: U) => boolean;
6
- }
7
- export declare function lazy<T extends IType<any, any, any>, U>(name: string, options: LazyOptions<T, U>): T;
8
- /**
9
- * @internal
10
- * @hidden
11
- */
12
- export declare class Lazy<T extends IType<any, any, any>, U> extends SimpleType<T, T, T> {
13
- private readonly options;
14
- flags: TypeFlags;
15
- private loadedType;
16
- private pendingNodeList;
17
- constructor(name: string, options: LazyOptions<T, U>);
18
- describe(): string;
19
- instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, value: this["C"]): this["N"];
20
- isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
21
- reconcile(current: this["N"], value: T, parent: AnyObjectNode, subpath: string): this["N"];
22
- }
23
- export {};
@@ -1,77 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Lazy = void 0;
4
- exports.lazy = lazy;
5
- const mobx_1 = require("mobx");
6
- const type_ts_1 = require("../../core/type/type.js");
7
- const internal_ts_1 = require("../../internal.js");
8
- function lazy(name, options) {
9
- // TODO: fix this unknown casting to be stricter
10
- return new Lazy(name, options);
11
- }
12
- /**
13
- * @internal
14
- * @hidden
15
- */
16
- class Lazy extends type_ts_1.SimpleType {
17
- options;
18
- flags = internal_ts_1.TypeFlags.Lazy;
19
- loadedType = null;
20
- pendingNodeList = mobx_1.observable.array();
21
- constructor(name, options) {
22
- super(name);
23
- this.options = options;
24
- (0, mobx_1.when)(() => this.pendingNodeList.length > 0 &&
25
- this.pendingNodeList.some(node => node.isAlive &&
26
- this.options.shouldLoadPredicate(node.parent ? node.parent.value : null)), () => {
27
- this.options.loadType().then((0, mobx_1.action)((type) => {
28
- this.loadedType = type;
29
- this.pendingNodeList.forEach(node => {
30
- if (!node.parent) {
31
- return;
32
- }
33
- if (!this.loadedType) {
34
- return;
35
- }
36
- node.parent.applyPatches([
37
- {
38
- op: "replace",
39
- path: `/${node.subpath}`,
40
- value: node.snapshot
41
- }
42
- ]);
43
- });
44
- }));
45
- });
46
- }
47
- describe() {
48
- return `<lazy ${this.name}>`;
49
- }
50
- instantiate(parent, subpath, environment, value) {
51
- if (this.loadedType) {
52
- return this.loadedType.instantiate(parent, subpath, environment, value);
53
- }
54
- const node = (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, (0, internal_ts_1.deepFreeze)(value));
55
- this.pendingNodeList.push(node);
56
- (0, mobx_1.when)(() => !node.isAlive, () => this.pendingNodeList.splice(this.pendingNodeList.indexOf(node), 1));
57
- return node;
58
- }
59
- isValidSnapshot(value, context) {
60
- if (this.loadedType) {
61
- return this.loadedType.validate(value, context);
62
- }
63
- if (!(0, internal_ts_1.isSerializable)(value)) {
64
- return (0, internal_ts_1.typeCheckFailure)(context, value, "Value is not serializable and cannot be lazy");
65
- }
66
- return (0, internal_ts_1.typeCheckSuccess)();
67
- }
68
- reconcile(current, value, parent, subpath) {
69
- if (this.loadedType) {
70
- current.die();
71
- return this.loadedType.instantiate(parent, subpath, parent.environment, value);
72
- }
73
- return super.reconcile(current, value, parent, subpath);
74
- }
75
- }
76
- exports.Lazy = Lazy;
77
- //# sourceMappingURL=lazy.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"lazy.js","sourceRoot":"","sources":["../../../src/types/utility-types/lazy.ts"],"names":[],"mappings":";;;AAsBA,oBAMC;AA5BD,+BAAsE;AAEtE,qDAAoD;AACpD,mDAY0B;AAO1B,SAAgB,IAAI,CAClB,IAAY,EACZ,OAA0B;IAE1B,gDAAgD;IAChD,OAAO,IAAI,IAAI,CAAC,IAAI,EAAE,OAAO,CAAiB,CAAA;AAChD,CAAC;AAED;;;GAGG;AACH,MAAa,IAAwC,SAAQ,oBAI5D;IAQoB;IAPnB,KAAK,GAAG,uBAAS,CAAC,IAAI,CAAA;IAEd,UAAU,GAAa,IAAI,CAAA;IAC3B,eAAe,GAA8B,iBAAU,CAAC,KAAK,EAAE,CAAA;IAEvE,YACE,IAAY,EACK,OAA0B;QAE3C,KAAK,CAAC,IAAI,CAAC,CAAA;QAFM,YAAO,GAAP,OAAO,CAAmB;QAI3C,IAAA,WAAI,EACF,GAAG,EAAE,CACH,IAAI,CAAC,eAAe,CAAC,MAAM,GAAG,CAAC;YAC/B,IAAI,CAAC,eAAe,CAAC,IAAI,CACvB,IAAI,CAAC,EAAE,CACL,IAAI,CAAC,OAAO;gBACZ,IAAI,CAAC,OAAO,CAAC,mBAAmB,CAC9B,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CACvC,CACJ,EACH,GAAG,EAAE;YACH,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,IAAI,CAC1B,IAAA,aAAM,EAAC,CAAC,IAAO,EAAE,EAAE;gBACjB,IAAI,CAAC,UAAU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KAAgB,EAChB,OAA2B;QAE3B,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACpB,OAAO,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QACjD,CAAC;QACD,IAAI,CAAC,IAAA,4BAAc,EAAC,KAAK,CAAC,EAAE,CAAC;YAC3B,OAAO,IAAA,8BAAgB,EACrB,OAAO,EACP,KAAK,EACL,8CAA8C,CAC/C,CAAA;QACH,CAAC;QACD,OAAO,IAAA,8BAAgB,GAAE,CAAA;IAC3B,CAAC;IAED,SAAS,CACP,OAAkB,EAClB,KAAQ,EACR,MAAqB,EACrB,OAAe;QAEf,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACpB,OAAO,CAAC,GAAG,EAAE,CAAA;YACb,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAChC,MAAM,EACN,OAAO,EACP,MAAM,CAAC,WAAW,EAClB,KAAK,CACO,CAAA;QAChB,CAAC;QACD,OAAO,KAAK,CAAC,SAAS,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAA;IACzD,CAAC;CACF;AA1HD,oBA0HC","sourcesContent":["import { type IObservableArray, action, observable, when } from \"mobx\"\n\nimport { SimpleType } from \"../../core/type/type.ts\"\nimport {\n type AnyNode,\n type AnyObjectNode,\n type IType,\n type IValidationContext,\n type IValidationResult,\n TypeFlags,\n createScalarNode,\n deepFreeze,\n isSerializable,\n typeCheckFailure,\n typeCheckSuccess\n} from \"../../internal.ts\"\n\ninterface LazyOptions<T extends IType<any, any, any>, U> {\n loadType: () => Promise<T>\n shouldLoadPredicate: (parent: U) => boolean\n}\n\nexport function lazy<T extends IType<any, any, any>, U>(\n name: string,\n options: LazyOptions<T, U>\n): T {\n // TODO: fix this unknown casting to be stricter\n return new Lazy(name, options) as unknown as T\n}\n\n/**\n * @internal\n * @hidden\n */\nexport class Lazy<T extends IType<any, any, any>, U> extends SimpleType<\n T,\n T,\n T\n> {\n flags = TypeFlags.Lazy\n\n private loadedType: T | null = null\n private pendingNodeList: IObservableArray<AnyNode> = observable.array()\n\n constructor(\n name: string,\n private readonly options: LazyOptions<T, U>\n ) {\n super(name)\n\n when(\n () =>\n this.pendingNodeList.length > 0 &&\n this.pendingNodeList.some(\n node =>\n node.isAlive &&\n this.options.shouldLoadPredicate(\n node.parent ? node.parent.value : null\n )\n ),\n () => {\n this.options.loadType().then(\n action((type: T) => {\n this.loadedType = type\n this.pendingNodeList.forEach(node => {\n if (!node.parent) {\n return\n }\n if (!this.loadedType) {\n return\n }\n\n node.parent.applyPatches([\n {\n op: \"replace\",\n path: `/${node.subpath}`,\n value: node.snapshot\n }\n ])\n })\n })\n )\n }\n )\n }\n\n describe() {\n return `<lazy ${this.name}>`\n }\n\n instantiate(\n parent: AnyObjectNode | null,\n subpath: string,\n environment: any,\n value: this[\"C\"]\n ): this[\"N\"] {\n if (this.loadedType) {\n return this.loadedType.instantiate(\n parent,\n subpath,\n environment,\n value\n ) as this[\"N\"]\n }\n\n const node = createScalarNode(\n this,\n parent,\n subpath,\n environment,\n deepFreeze(value)\n )\n this.pendingNodeList.push(node)\n\n when(\n () => !node.isAlive,\n () => this.pendingNodeList.splice(this.pendingNodeList.indexOf(node), 1)\n )\n\n return node\n }\n\n isValidSnapshot(\n value: this[\"C\"],\n context: IValidationContext\n ): IValidationResult {\n if (this.loadedType) {\n return this.loadedType.validate(value, context)\n }\n if (!isSerializable(value)) {\n return typeCheckFailure(\n context,\n value,\n \"Value is not serializable and cannot be lazy\"\n )\n }\n return typeCheckSuccess()\n }\n\n reconcile(\n current: this[\"N\"],\n value: T,\n parent: AnyObjectNode,\n subpath: string\n ): this[\"N\"] {\n if (this.loadedType) {\n current.die()\n return this.loadedType.instantiate(\n parent,\n subpath,\n parent.environment,\n value\n ) as this[\"N\"]\n }\n return super.reconcile(current, value, parent, subpath)\n }\n}\n"]}
@@ -1,38 +0,0 @@
1
- import { SimpleType } from "../../core/type/type.ts";
2
- import { type AnyObjectNode, type ISimpleType, type IValidationContext, type IValidationResult, type Primitives, TypeFlags } from "../../internal.ts";
3
- /**
4
- * @internal
5
- * @hidden
6
- */
7
- export declare class Literal<T> extends SimpleType<T, T, T> {
8
- readonly value: T;
9
- readonly flags = TypeFlags.Literal;
10
- constructor(value: T);
11
- instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: this["C"]): this["N"];
12
- describe(): string;
13
- isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
14
- }
15
- /**
16
- * `types.literal` - The literal type will return a type that will match only the exact given type.
17
- * The given value must be a primitive, in order to be serialized to a snapshot correctly.
18
- * You can use literal to match exact strings for example the exact male or female string.
19
- *
20
- * Example:
21
- * ```ts
22
- * const Person = types.model({
23
- * name: types.string,
24
- * gender: types.union(types.literal('male'), types.literal('female'))
25
- * })
26
- * ```
27
- *
28
- * @param value The value to use in the strict equal check
29
- * @returns
30
- */
31
- export declare function literal<S extends Primitives>(value: S): ISimpleType<S>;
32
- /**
33
- * Returns if a given value represents a literal type.
34
- *
35
- * @param type
36
- * @returns
37
- */
38
- export declare function isLiteralType<IT extends ISimpleType<any>>(type: IT): type is IT;