@jbrowse/mobx-state-tree 5.7.0 → 5.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (253) hide show
  1. package/README.md +0 -10
  2. package/dist/index.d.ts +2135 -2
  3. package/dist/mobx-state-tree.cjs +6894 -0
  4. package/dist/mobx-state-tree.cjs.map +1 -0
  5. package/dist/mobx-state-tree.mjs +6814 -0
  6. package/dist/mobx-state-tree.mjs.map +1 -0
  7. package/package.json +30 -26
  8. package/dist/core/action.d.ts +0 -87
  9. package/dist/core/action.js +0 -228
  10. package/dist/core/action.js.map +0 -1
  11. package/dist/core/actionContext.d.ts +0 -27
  12. package/dist/core/actionContext.js +0 -42
  13. package/dist/core/actionContext.js.map +0 -1
  14. package/dist/core/flow.d.ts +0 -69
  15. package/dist/core/flow.js +0 -180
  16. package/dist/core/flow.js.map +0 -1
  17. package/dist/core/json-patch.d.ts +0 -46
  18. package/dist/core/json-patch.js +0 -133
  19. package/dist/core/json-patch.js.map +0 -1
  20. package/dist/core/mst-operations.d.ts +0 -459
  21. package/dist/core/mst-operations.js +0 -885
  22. package/dist/core/mst-operations.js.map +0 -1
  23. package/dist/core/node/BaseNode.d.ts +0 -62
  24. package/dist/core/node/BaseNode.js +0 -152
  25. package/dist/core/node/BaseNode.js.map +0 -1
  26. package/dist/core/node/Hook.d.ts +0 -17
  27. package/dist/core/node/Hook.js +0 -15
  28. package/dist/core/node/Hook.js.map +0 -1
  29. package/dist/core/node/create-node.d.ts +0 -16
  30. package/dist/core/node/create-node.js +0 -41
  31. package/dist/core/node/create-node.js.map +0 -1
  32. package/dist/core/node/identifier-cache.d.ts +0 -19
  33. package/dist/core/node/identifier-cache.js +0 -115
  34. package/dist/core/node/identifier-cache.js.map +0 -1
  35. package/dist/core/node/livelinessChecking.d.ts +0 -37
  36. package/dist/core/node/livelinessChecking.js +0 -38
  37. package/dist/core/node/livelinessChecking.js.map +0 -1
  38. package/dist/core/node/node-utils.d.ts +0 -83
  39. package/dist/core/node/node-utils.js +0 -165
  40. package/dist/core/node/node-utils.js.map +0 -1
  41. package/dist/core/node/object-node.d.ts +0 -101
  42. package/dist/core/node/object-node.js +0 -546
  43. package/dist/core/node/object-node.js.map +0 -1
  44. package/dist/core/node/scalar-node.d.ts +0 -21
  45. package/dist/core/node/scalar-node.js +0 -90
  46. package/dist/core/node/scalar-node.js.map +0 -1
  47. package/dist/core/process.d.ts +0 -50
  48. package/dist/core/process.js +0 -39
  49. package/dist/core/process.js.map +0 -1
  50. package/dist/core/type/type-checker.d.ts +0 -69
  51. package/dist/core/type/type-checker.js +0 -154
  52. package/dist/core/type/type-checker.js.map +0 -1
  53. package/dist/core/type/type.d.ts +0 -317
  54. package/dist/core/type/type.js +0 -251
  55. package/dist/core/type/type.js.map +0 -1
  56. package/dist/index.js +0 -85
  57. package/dist/index.js.map +0 -1
  58. package/dist/internal.d.ts +0 -39
  59. package/dist/internal.js +0 -60
  60. package/dist/internal.js.map +0 -1
  61. package/dist/middlewares/create-action-tracking-middleware.d.ts +0 -24
  62. package/dist/middlewares/create-action-tracking-middleware.js +0 -81
  63. package/dist/middlewares/create-action-tracking-middleware.js.map +0 -1
  64. package/dist/middlewares/createActionTrackingMiddleware2.d.ts +0 -34
  65. package/dist/middlewares/createActionTrackingMiddleware2.js +0 -133
  66. package/dist/middlewares/createActionTrackingMiddleware2.js.map +0 -1
  67. package/dist/middlewares/on-action.d.ts +0 -87
  68. package/dist/middlewares/on-action.js +0 -215
  69. package/dist/middlewares/on-action.js.map +0 -1
  70. package/dist/package.json +0 -1
  71. package/dist/types/complex-types/array.d.ts +0 -81
  72. package/dist/types/complex-types/array.js +0 -353
  73. package/dist/types/complex-types/array.js.map +0 -1
  74. package/dist/types/complex-types/map.d.ts +0 -111
  75. package/dist/types/complex-types/map.js +0 -362
  76. package/dist/types/complex-types/map.js.map +0 -1
  77. package/dist/types/complex-types/model.d.ts +0 -193
  78. package/dist/types/complex-types/model.js +0 -478
  79. package/dist/types/complex-types/model.js.map +0 -1
  80. package/dist/types/index.d.ts +0 -33
  81. package/dist/types/index.js +0 -38
  82. package/dist/types/index.js.map +0 -1
  83. package/dist/types/primitives.d.ts +0 -125
  84. package/dist/types/primitives.js +0 -183
  85. package/dist/types/primitives.js.map +0 -1
  86. package/dist/types/utility-types/custom.d.ts +0 -75
  87. package/dist/types/utility-types/custom.js +0 -111
  88. package/dist/types/utility-types/custom.js.map +0 -1
  89. package/dist/types/utility-types/enumeration.d.ts +0 -5
  90. package/dist/types/utility-types/enumeration.js +0 -34
  91. package/dist/types/utility-types/enumeration.js.map +0 -1
  92. package/dist/types/utility-types/frozen.d.ts +0 -24
  93. package/dist/types/utility-types/frozen.js +0 -98
  94. package/dist/types/utility-types/frozen.js.map +0 -1
  95. package/dist/types/utility-types/identifier.d.ts +0 -87
  96. package/dist/types/utility-types/identifier.js +0 -130
  97. package/dist/types/utility-types/identifier.js.map +0 -1
  98. package/dist/types/utility-types/late.d.ts +0 -10
  99. package/dist/types/utility-types/late.js +0 -110
  100. package/dist/types/utility-types/late.js.map +0 -1
  101. package/dist/types/utility-types/lazy.d.ts +0 -23
  102. package/dist/types/utility-types/lazy.js +0 -77
  103. package/dist/types/utility-types/lazy.js.map +0 -1
  104. package/dist/types/utility-types/literal.d.ts +0 -38
  105. package/dist/types/utility-types/literal.js +0 -64
  106. package/dist/types/utility-types/literal.js.map +0 -1
  107. package/dist/types/utility-types/maybe.d.ts +0 -26
  108. package/dist/types/utility-types/maybe.js +0 -30
  109. package/dist/types/utility-types/maybe.js.map +0 -1
  110. package/dist/types/utility-types/optional.d.ts +0 -42
  111. package/dist/types/utility-types/optional.js +0 -141
  112. package/dist/types/utility-types/optional.js.map +0 -1
  113. package/dist/types/utility-types/reference.d.ts +0 -90
  114. package/dist/types/utility-types/reference.js +0 -393
  115. package/dist/types/utility-types/reference.js.map +0 -1
  116. package/dist/types/utility-types/refinement.d.ts +0 -10
  117. package/dist/types/utility-types/refinement.js +0 -86
  118. package/dist/types/utility-types/refinement.js.map +0 -1
  119. package/dist/types/utility-types/resilient.d.ts +0 -18
  120. package/dist/types/utility-types/resilient.js +0 -121
  121. package/dist/types/utility-types/resilient.js.map +0 -1
  122. package/dist/types/utility-types/snapshotProcessor.d.ts +0 -63
  123. package/dist/types/utility-types/snapshotProcessor.js +0 -162
  124. package/dist/types/utility-types/snapshotProcessor.js.map +0 -1
  125. package/dist/types/utility-types/union.d.ts +0 -78
  126. package/dist/types/utility-types/union.js +0 -246
  127. package/dist/types/utility-types/union.js.map +0 -1
  128. package/dist/utils.d.ts +0 -230
  129. package/dist/utils.js +0 -483
  130. package/dist/utils.js.map +0 -1
  131. package/esm/core/action.d.ts +0 -87
  132. package/esm/core/action.js +0 -219
  133. package/esm/core/action.js.map +0 -1
  134. package/esm/core/actionContext.d.ts +0 -27
  135. package/esm/core/actionContext.js +0 -37
  136. package/esm/core/actionContext.js.map +0 -1
  137. package/esm/core/flow.d.ts +0 -69
  138. package/esm/core/flow.js +0 -173
  139. package/esm/core/flow.js.map +0 -1
  140. package/esm/core/json-patch.d.ts +0 -46
  141. package/esm/core/json-patch.js +0 -125
  142. package/esm/core/json-patch.js.map +0 -1
  143. package/esm/core/mst-operations.d.ts +0 -459
  144. package/esm/core/mst-operations.js +0 -844
  145. package/esm/core/mst-operations.js.map +0 -1
  146. package/esm/core/node/BaseNode.d.ts +0 -62
  147. package/esm/core/node/BaseNode.js +0 -148
  148. package/esm/core/node/BaseNode.js.map +0 -1
  149. package/esm/core/node/Hook.d.ts +0 -17
  150. package/esm/core/node/Hook.js +0 -12
  151. package/esm/core/node/Hook.js.map +0 -1
  152. package/esm/core/node/create-node.d.ts +0 -16
  153. package/esm/core/node/create-node.js +0 -36
  154. package/esm/core/node/create-node.js.map +0 -1
  155. package/esm/core/node/identifier-cache.d.ts +0 -19
  156. package/esm/core/node/identifier-cache.js +0 -111
  157. package/esm/core/node/identifier-cache.js.map +0 -1
  158. package/esm/core/node/livelinessChecking.d.ts +0 -37
  159. package/esm/core/node/livelinessChecking.js +0 -33
  160. package/esm/core/node/livelinessChecking.js.map +0 -1
  161. package/esm/core/node/node-utils.d.ts +0 -83
  162. package/esm/core/node/node-utils.js +0 -153
  163. package/esm/core/node/node-utils.js.map +0 -1
  164. package/esm/core/node/object-node.d.ts +0 -101
  165. package/esm/core/node/object-node.js +0 -542
  166. package/esm/core/node/object-node.js.map +0 -1
  167. package/esm/core/node/scalar-node.d.ts +0 -21
  168. package/esm/core/node/scalar-node.js +0 -86
  169. package/esm/core/node/scalar-node.js.map +0 -1
  170. package/esm/core/process.d.ts +0 -50
  171. package/esm/core/process.js +0 -35
  172. package/esm/core/process.js.map +0 -1
  173. package/esm/core/type/type-checker.d.ts +0 -69
  174. package/esm/core/type/type-checker.js +0 -144
  175. package/esm/core/type/type-checker.js.map +0 -1
  176. package/esm/core/type/type.d.ts +0 -317
  177. package/esm/core/type/type.js +0 -243
  178. package/esm/core/type/type.js.map +0 -1
  179. package/esm/index.d.ts +0 -2
  180. package/esm/index.js +0 -2
  181. package/esm/index.js.map +0 -1
  182. package/esm/internal.d.ts +0 -39
  183. package/esm/internal.js +0 -44
  184. package/esm/internal.js.map +0 -1
  185. package/esm/middlewares/create-action-tracking-middleware.d.ts +0 -24
  186. package/esm/middlewares/create-action-tracking-middleware.js +0 -78
  187. package/esm/middlewares/create-action-tracking-middleware.js.map +0 -1
  188. package/esm/middlewares/createActionTrackingMiddleware2.d.ts +0 -34
  189. package/esm/middlewares/createActionTrackingMiddleware2.js +0 -130
  190. package/esm/middlewares/createActionTrackingMiddleware2.js.map +0 -1
  191. package/esm/middlewares/on-action.d.ts +0 -87
  192. package/esm/middlewares/on-action.js +0 -210
  193. package/esm/middlewares/on-action.js.map +0 -1
  194. package/esm/types/complex-types/array.d.ts +0 -81
  195. package/esm/types/complex-types/array.js +0 -347
  196. package/esm/types/complex-types/array.js.map +0 -1
  197. package/esm/types/complex-types/map.d.ts +0 -111
  198. package/esm/types/complex-types/map.js +0 -356
  199. package/esm/types/complex-types/map.js.map +0 -1
  200. package/esm/types/complex-types/model.d.ts +0 -193
  201. package/esm/types/complex-types/model.js +0 -471
  202. package/esm/types/complex-types/model.js.map +0 -1
  203. package/esm/types/index.d.ts +0 -33
  204. package/esm/types/index.js +0 -35
  205. package/esm/types/index.js.map +0 -1
  206. package/esm/types/primitives.d.ts +0 -125
  207. package/esm/types/primitives.js +0 -177
  208. package/esm/types/primitives.js.map +0 -1
  209. package/esm/types/utility-types/custom.d.ts +0 -75
  210. package/esm/types/utility-types/custom.js +0 -106
  211. package/esm/types/utility-types/custom.js.map +0 -1
  212. package/esm/types/utility-types/enumeration.d.ts +0 -5
  213. package/esm/types/utility-types/enumeration.js +0 -31
  214. package/esm/types/utility-types/enumeration.js.map +0 -1
  215. package/esm/types/utility-types/frozen.d.ts +0 -24
  216. package/esm/types/utility-types/frozen.js +0 -92
  217. package/esm/types/utility-types/frozen.js.map +0 -1
  218. package/esm/types/utility-types/identifier.d.ts +0 -87
  219. package/esm/types/utility-types/identifier.js +0 -121
  220. package/esm/types/utility-types/identifier.js.map +0 -1
  221. package/esm/types/utility-types/late.d.ts +0 -10
  222. package/esm/types/utility-types/late.js +0 -106
  223. package/esm/types/utility-types/late.js.map +0 -1
  224. package/esm/types/utility-types/lazy.d.ts +0 -23
  225. package/esm/types/utility-types/lazy.js +0 -72
  226. package/esm/types/utility-types/lazy.js.map +0 -1
  227. package/esm/types/utility-types/literal.d.ts +0 -38
  228. package/esm/types/utility-types/literal.js +0 -58
  229. package/esm/types/utility-types/literal.js.map +0 -1
  230. package/esm/types/utility-types/maybe.d.ts +0 -26
  231. package/esm/types/utility-types/maybe.js +0 -26
  232. package/esm/types/utility-types/maybe.js.map +0 -1
  233. package/esm/types/utility-types/optional.d.ts +0 -42
  234. package/esm/types/utility-types/optional.js +0 -135
  235. package/esm/types/utility-types/optional.js.map +0 -1
  236. package/esm/types/utility-types/reference.d.ts +0 -90
  237. package/esm/types/utility-types/reference.js +0 -383
  238. package/esm/types/utility-types/reference.js.map +0 -1
  239. package/esm/types/utility-types/refinement.d.ts +0 -10
  240. package/esm/types/utility-types/refinement.js +0 -82
  241. package/esm/types/utility-types/refinement.js.map +0 -1
  242. package/esm/types/utility-types/resilient.d.ts +0 -18
  243. package/esm/types/utility-types/resilient.js +0 -118
  244. package/esm/types/utility-types/resilient.js.map +0 -1
  245. package/esm/types/utility-types/snapshotProcessor.d.ts +0 -63
  246. package/esm/types/utility-types/snapshotProcessor.js +0 -159
  247. package/esm/types/utility-types/snapshotProcessor.js.map +0 -1
  248. package/esm/types/utility-types/union.d.ts +0 -78
  249. package/esm/types/utility-types/union.js +0 -240
  250. package/esm/types/utility-types/union.js.map +0 -1
  251. package/esm/utils.d.ts +0 -230
  252. package/esm/utils.js +0 -449
  253. package/esm/utils.js.map +0 -1
@@ -1,58 +0,0 @@
1
- import { SimpleType } from "../../core/type/type.js";
2
- import { TypeFlags, createScalarNode, isPrimitive, isType, typeCheckFailure, typeCheckSuccess } from "../../internal.js";
3
- import { assertArg } from "../../utils.js";
4
- /**
5
- * @internal
6
- * @hidden
7
- */
8
- export class Literal extends SimpleType {
9
- value;
10
- flags = TypeFlags.Literal;
11
- constructor(value) {
12
- super(JSON.stringify(value));
13
- this.value = value;
14
- }
15
- instantiate(parent, subpath, environment, initialValue) {
16
- return createScalarNode(this, parent, subpath, environment, initialValue);
17
- }
18
- describe() {
19
- return JSON.stringify(this.value);
20
- }
21
- isValidSnapshot(value, context) {
22
- if (isPrimitive(value) && value === this.value) {
23
- return typeCheckSuccess();
24
- }
25
- return typeCheckFailure(context, value, `Value is not a literal ${JSON.stringify(this.value)}`);
26
- }
27
- }
28
- /**
29
- * `types.literal` - The literal type will return a type that will match only the exact given type.
30
- * The given value must be a primitive, in order to be serialized to a snapshot correctly.
31
- * You can use literal to match exact strings for example the exact male or female string.
32
- *
33
- * Example:
34
- * ```ts
35
- * const Person = types.model({
36
- * name: types.string,
37
- * gender: types.union(types.literal('male'), types.literal('female'))
38
- * })
39
- * ```
40
- *
41
- * @param value The value to use in the strict equal check
42
- * @returns
43
- */
44
- export function literal(value) {
45
- // check that the given value is a primitive
46
- assertArg(value, isPrimitive, "primitive", 1);
47
- return new Literal(value);
48
- }
49
- /**
50
- * Returns if a given value represents a literal type.
51
- *
52
- * @param type
53
- * @returns
54
- */
55
- export function isLiteralType(type) {
56
- return isType(type) && (type.flags & TypeFlags.Literal) > 0;
57
- }
58
- //# sourceMappingURL=literal.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"literal.js","sourceRoot":"","sources":["../../../src/types/utility-types/literal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,EAML,SAAS,EACT,gBAAgB,EAChB,WAAW,EACX,MAAM,EACN,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE1C;;;GAGG;AACH,MAAM,OAAO,OAAW,SAAQ,UAAmB;IACxC,KAAK,CAAG;IACR,KAAK,GAAG,SAAS,CAAC,OAAO,CAAA;IAElC,YAAY,KAAQ;QAClB,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAA;QAC5B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACpB,CAAC;IAED,WAAW,CACT,MAA4B,EAC5B,OAAe,EACf,WAAgB,EAChB,YAAuB;QAEvB,OAAO,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,YAAY,CAAC,CAAA;IAC3E,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IACnC,CAAC;IAED,eAAe,CACb,KAAgB,EAChB,OAA2B;QAE3B,IAAI,WAAW,CAAC,KAAK,CAAC,IAAI,KAAK,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;YAC/C,OAAO,gBAAgB,EAAE,CAAA;QAC3B,CAAC;QACD,OAAO,gBAAgB,CACrB,OAAO,EACP,KAAK,EACL,0BAA0B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CACvD,CAAA;IACH,CAAC;CACF;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,OAAO,CAAuB,KAAQ;IACpD,4CAA4C;IAC5C,SAAS,CAAC,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,CAAC,CAAC,CAAA;IAE7C,OAAO,IAAI,OAAO,CAAI,KAAK,CAAC,CAAA;AAC9B,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAC3B,IAAQ;IAER,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;AAC7D,CAAC","sourcesContent":["import { SimpleType } from \"../../core/type/type.ts\"\nimport {\n type AnyObjectNode,\n type ISimpleType,\n type IValidationContext,\n type IValidationResult,\n type Primitives,\n TypeFlags,\n createScalarNode,\n isPrimitive,\n isType,\n typeCheckFailure,\n typeCheckSuccess\n} from \"../../internal.ts\"\nimport { assertArg } from \"../../utils.ts\"\n\n/**\n * @internal\n * @hidden\n */\nexport class Literal<T> extends SimpleType<T, T, T> {\n readonly value: T\n readonly flags = TypeFlags.Literal\n\n constructor(value: T) {\n super(JSON.stringify(value))\n this.value = value\n }\n\n instantiate(\n parent: AnyObjectNode | null,\n subpath: string,\n environment: any,\n initialValue: this[\"C\"]\n ): this[\"N\"] {\n return createScalarNode(this, parent, subpath, environment, initialValue)\n }\n\n describe() {\n return JSON.stringify(this.value)\n }\n\n isValidSnapshot(\n value: this[\"C\"],\n context: IValidationContext\n ): IValidationResult {\n if (isPrimitive(value) && value === this.value) {\n return typeCheckSuccess()\n }\n return typeCheckFailure(\n context,\n value,\n `Value is not a literal ${JSON.stringify(this.value)}`\n )\n }\n}\n\n/**\n * `types.literal` - The literal type will return a type that will match only the exact given type.\n * The given value must be a primitive, in order to be serialized to a snapshot correctly.\n * You can use literal to match exact strings for example the exact male or female string.\n *\n * Example:\n * ```ts\n * const Person = types.model({\n * name: types.string,\n * gender: types.union(types.literal('male'), types.literal('female'))\n * })\n * ```\n *\n * @param value The value to use in the strict equal check\n * @returns\n */\nexport function literal<S extends Primitives>(value: S): ISimpleType<S> {\n // check that the given value is a primitive\n assertArg(value, isPrimitive, \"primitive\", 1)\n\n return new Literal<S>(value)\n}\n\n/**\n * Returns if a given value represents a literal type.\n *\n * @param type\n * @returns\n */\nexport function isLiteralType<IT extends ISimpleType<any>>(\n type: IT\n): type is IT {\n return isType(type) && (type.flags & TypeFlags.Literal) > 0\n}\n"]}
@@ -1,26 +0,0 @@
1
- import { type IAnyType, type IType } from "../../internal.ts";
2
- /** @hidden */
3
- export interface IMaybeIType<IT extends IAnyType, C, O> extends IType<IT["CreationType"] | C, IT["SnapshotType"] | O, IT["TypeWithoutSTN"] | O> {
4
- }
5
- /** @hidden */
6
- export interface IMaybe<IT extends IAnyType> extends IMaybeIType<IT, undefined, undefined> {
7
- }
8
- /** @hidden */
9
- export interface IMaybeNull<IT extends IAnyType> extends IMaybeIType<IT, null | undefined, null> {
10
- }
11
- /**
12
- * `types.maybe` - Maybe will make a type nullable, and also optional.
13
- * The value `undefined` will be used to represent nullability.
14
- *
15
- * @param type
16
- * @returns
17
- */
18
- export declare function maybe<IT extends IAnyType>(type: IT): IMaybe<IT>;
19
- /**
20
- * `types.maybeNull` - Maybe will make a type nullable, and also optional.
21
- * The value `null` will be used to represent no value.
22
- *
23
- * @param type
24
- * @returns
25
- */
26
- export declare function maybeNull<IT extends IAnyType>(type: IT): IMaybeNull<IT>;
@@ -1,26 +0,0 @@
1
- import { assertIsType, nullType, optional, undefinedType, union } from "../../internal.js";
2
- const optionalUndefinedType = optional(undefinedType, undefined);
3
- const optionalNullType = optional(nullType, null);
4
- /**
5
- * `types.maybe` - Maybe will make a type nullable, and also optional.
6
- * The value `undefined` will be used to represent nullability.
7
- *
8
- * @param type
9
- * @returns
10
- */
11
- export function maybe(type) {
12
- assertIsType(type, 1);
13
- return union(type, optionalUndefinedType);
14
- }
15
- /**
16
- * `types.maybeNull` - Maybe will make a type nullable, and also optional.
17
- * The value `null` will be used to represent no value.
18
- *
19
- * @param type
20
- * @returns
21
- */
22
- export function maybeNull(type) {
23
- assertIsType(type, 1);
24
- return union(type, optionalNullType);
25
- }
26
- //# sourceMappingURL=maybe.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"maybe.js","sourceRoot":"","sources":["../../../src/types/utility-types/maybe.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,YAAY,EACZ,QAAQ,EACR,QAAQ,EACR,aAAa,EACb,KAAK,EACN,MAAM,mBAAmB,CAAA;AAE1B,MAAM,qBAAqB,GAAG,QAAQ,CAAC,aAAa,EAAE,SAAS,CAAC,CAAA;AAChE,MAAM,gBAAgB,GAAG,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;AAuBjD;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CAAsB,IAAQ;IACjD,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC,CAAA;IAErB,OAAO,KAAK,CAAC,IAAI,EAAE,qBAAqB,CAAC,CAAA;AAC3C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAsB,IAAQ;IACrD,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC,CAAA;IAErB,OAAO,KAAK,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAA;AACtC,CAAC","sourcesContent":["import {\n type IAnyType,\n type IType,\n assertIsType,\n nullType,\n optional,\n undefinedType,\n union\n} from \"../../internal.ts\"\n\nconst optionalUndefinedType = optional(undefinedType, undefined)\nconst optionalNullType = optional(nullType, null)\n\n/** @hidden */\nexport interface IMaybeIType<IT extends IAnyType, C, O> extends IType<\n IT[\"CreationType\"] | C,\n IT[\"SnapshotType\"] | O,\n IT[\"TypeWithoutSTN\"] | O\n> {}\n\n/** @hidden */\nexport interface IMaybe<IT extends IAnyType> extends IMaybeIType<\n IT,\n undefined,\n undefined\n> {}\n\n/** @hidden */\nexport interface IMaybeNull<IT extends IAnyType> extends IMaybeIType<\n IT,\n null | undefined,\n null\n> {}\n\n/**\n * `types.maybe` - Maybe will make a type nullable, and also optional.\n * The value `undefined` will be used to represent nullability.\n *\n * @param type\n * @returns\n */\nexport function maybe<IT extends IAnyType>(type: IT): IMaybe<IT> {\n assertIsType(type, 1)\n\n return union(type, optionalUndefinedType)\n}\n\n/**\n * `types.maybeNull` - Maybe will make a type nullable, and also optional.\n * The value `null` will be used to represent no value.\n *\n * @param type\n * @returns\n */\nexport function maybeNull<IT extends IAnyType>(type: IT): IMaybeNull<IT> {\n assertIsType(type, 1)\n\n return union(type, optionalNullType)\n}\n"]}
@@ -1,42 +0,0 @@
1
- import { BaseType } from "../../core/type/type.ts";
2
- import { type AnyObjectNode, type ExtractCSTWithSTN, type IAnyType, type IType, type IValidationContext, type IValidationResult } from "../../internal.ts";
3
- type IFunctionReturn<T> = () => T;
4
- type IOptionalValue<C, T> = C | IFunctionReturn<C | T>;
5
- /** @hidden */
6
- export type ValidOptionalValue = string | boolean | number | null | undefined;
7
- /** @hidden */
8
- export type ValidOptionalValues = [ValidOptionalValue, ...ValidOptionalValue[]];
9
- /**
10
- * @hidden
11
- * @internal
12
- */
13
- export declare class OptionalValue<IT extends IAnyType, OptionalVals extends ValidOptionalValues> extends BaseType<IT["CreationType"] | OptionalVals[number], IT["SnapshotType"], IT["TypeWithoutSTN"]> {
14
- private readonly _subtype;
15
- private readonly _defaultValue;
16
- readonly optionalValues: OptionalVals;
17
- get flags(): number;
18
- constructor(_subtype: IT, _defaultValue: IOptionalValue<IT["CreationType"], IT["Type"]>, optionalValues: OptionalVals);
19
- describe(): string;
20
- instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: this["C"] | this["T"]): this["N"];
21
- reconcile(current: this["N"], newValue: this["C"] | this["T"], parent: AnyObjectNode, subpath: string): this["N"];
22
- getDefaultInstanceOrSnapshot(): this["C"] | this["T"];
23
- isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
24
- isAssignableFrom(type: IAnyType): boolean;
25
- getSubTypes(): IT;
26
- }
27
- /** @hidden */
28
- export type OptionalDefaultValueOrFunction<IT extends IAnyType> = IT["CreationType"] | IT["SnapshotType"] | (() => ExtractCSTWithSTN<IT>);
29
- /** @hidden */
30
- export interface IOptionalIType<IT extends IAnyType, OptionalVals extends ValidOptionalValues> extends IType<IT["CreationType"] | OptionalVals[number], IT["SnapshotType"], IT["TypeWithoutSTN"]> {
31
- }
32
- export declare function optional<IT extends IAnyType>(type: IT, defaultValueOrFunction: OptionalDefaultValueOrFunction<IT>): IOptionalIType<IT, [undefined]>;
33
- export declare function optional<IT extends IAnyType, OptionalVals extends ValidOptionalValues>(type: IT, defaultValueOrFunction: OptionalDefaultValueOrFunction<IT>, optionalValues: OptionalVals): IOptionalIType<IT, OptionalVals>;
34
- /**
35
- * Returns if a value represents an optional type.
36
- *
37
- * @template IT
38
- * @param type
39
- * @returns
40
- */
41
- export declare function isOptionalType<IT extends IAnyType>(type: IT): type is IT;
42
- export {};
@@ -1,135 +0,0 @@
1
- import { BaseType } from "../../core/type/type.js";
2
- import { TypeFlags, assertIsType, devMode, fail, isStateTreeNode, isType, typeCheckSuccess, typecheckInternal } from "../../internal.js";
3
- /**
4
- * @hidden
5
- * @internal
6
- */
7
- export class OptionalValue extends BaseType {
8
- _subtype;
9
- _defaultValue;
10
- optionalValues;
11
- get flags() {
12
- return this._subtype.flags | TypeFlags.Optional;
13
- }
14
- constructor(_subtype, _defaultValue, optionalValues) {
15
- super(_subtype.name);
16
- this._subtype = _subtype;
17
- this._defaultValue = _defaultValue;
18
- this.optionalValues = optionalValues;
19
- }
20
- describe() {
21
- return this._subtype.describe() + "?";
22
- }
23
- instantiate(parent, subpath, environment, initialValue) {
24
- if (this.optionalValues.indexOf(initialValue) >= 0) {
25
- const defaultInstanceOrSnapshot = this.getDefaultInstanceOrSnapshot();
26
- return this._subtype.instantiate(parent, subpath, environment, defaultInstanceOrSnapshot);
27
- }
28
- return this._subtype.instantiate(parent, subpath, environment, initialValue);
29
- }
30
- reconcile(current, newValue, parent, subpath) {
31
- return this._subtype.reconcile(current, this.optionalValues.indexOf(newValue) < 0 && this._subtype.is(newValue)
32
- ? newValue
33
- : this.getDefaultInstanceOrSnapshot(), parent, subpath);
34
- }
35
- getDefaultInstanceOrSnapshot() {
36
- const defaultInstanceOrSnapshot = typeof this._defaultValue === "function"
37
- ? this._defaultValue()
38
- : this._defaultValue;
39
- // while static values are already snapshots and checked on types.optional
40
- // generator functions must always be rechecked just in case
41
- if (typeof this._defaultValue === "function") {
42
- typecheckInternal(this, defaultInstanceOrSnapshot);
43
- }
44
- return defaultInstanceOrSnapshot;
45
- }
46
- isValidSnapshot(value, context) {
47
- // defaulted values can be skipped
48
- if (this.optionalValues.indexOf(value) >= 0) {
49
- return typeCheckSuccess();
50
- }
51
- // bounce validation to the sub-type
52
- return this._subtype.validate(value, context);
53
- }
54
- isAssignableFrom(type) {
55
- return this._subtype.isAssignableFrom(type);
56
- }
57
- getSubTypes() {
58
- return this._subtype;
59
- }
60
- }
61
- function checkOptionalPreconditions(type, defaultValueOrFunction) {
62
- // make sure we never pass direct instances
63
- if (typeof defaultValueOrFunction !== "function" &&
64
- isStateTreeNode(defaultValueOrFunction)) {
65
- throw fail("default value cannot be an instance, pass a snapshot or a function that creates an instance/snapshot instead");
66
- }
67
- assertIsType(type, 1);
68
- if (devMode()) {
69
- // we only check default values if they are passed directly
70
- // if they are generator functions they will be checked once they are generated
71
- // we don't check generator function results here to avoid generating a node just for type-checking purposes
72
- // which might generate side-effects
73
- if (typeof defaultValueOrFunction !== "function") {
74
- typecheckInternal(type, defaultValueOrFunction);
75
- }
76
- }
77
- }
78
- /**
79
- * `types.optional` - Can be used to create a property with a default value.
80
- *
81
- * Depending on the third argument (`optionalValues`) there are two ways of operation:
82
- * - If the argument is not provided, then if a value is not provided in the snapshot (`undefined` or missing),
83
- * it will default to the provided `defaultValue`
84
- * - If the argument is provided, then if the value in the snapshot matches one of the optional values inside the array then it will
85
- * default to the provided `defaultValue`. Additionally, if one of the optional values inside the array is `undefined` then a missing
86
- * property is also valid.
87
- *
88
- * Note that it is also possible to include values of the same type as the intended subtype as optional values,
89
- * in this case the optional value will be transformed into the `defaultValue` (e.g. `types.optional(types.string, "unnamed", [undefined, ""])`
90
- * will transform the snapshot values `undefined` (and therefore missing) and empty strings into the string `"unnamed"` when it gets
91
- * instantiated).
92
- *
93
- * If `defaultValue` is a function, the function will be invoked for every new instance.
94
- * Applying a snapshot in which the optional value is one of the optional values (or `undefined`/_not_ present if none are provided) causes the
95
- * value to be reset.
96
- *
97
- * Example:
98
- * ```ts
99
- * const Todo = types.model({
100
- * title: types.string,
101
- * subtitle1: types.optional(types.string, "", [null]),
102
- * subtitle2: types.optional(types.string, "", [null, undefined]),
103
- * done: types.optional(types.boolean, false),
104
- * created: types.optional(types.Date, () => new Date()),
105
- * })
106
- *
107
- * // if done is missing / undefined it will become false
108
- * // if created is missing / undefined it will get a freshly generated timestamp
109
- * // if subtitle1 is null it will default to "", but it cannot be missing or undefined
110
- * // if subtitle2 is null or undefined it will default to ""; since it can be undefined it can also be missing
111
- * const todo = Todo.create({ title: "Get coffee", subtitle1: null })
112
- * ```
113
- *
114
- * @param type
115
- * @param defaultValueOrFunction
116
- * @param optionalValues an optional array with zero or more primitive values (string, number, boolean, null or undefined)
117
- * that will be converted into the default. `[ undefined ]` is assumed when none is provided
118
- * @returns
119
- */
120
- export function optional(type, defaultValueOrFunction, optionalValues) {
121
- checkOptionalPreconditions(type, defaultValueOrFunction);
122
- return new OptionalValue(type, defaultValueOrFunction, optionalValues ? optionalValues : undefinedAsOptionalValues);
123
- }
124
- const undefinedAsOptionalValues = [undefined];
125
- /**
126
- * Returns if a value represents an optional type.
127
- *
128
- * @template IT
129
- * @param type
130
- * @returns
131
- */
132
- export function isOptionalType(type) {
133
- return isType(type) && (type.flags & TypeFlags.Optional) > 0;
134
- }
135
- //# sourceMappingURL=optional.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"optional.js","sourceRoot":"","sources":["../../../src/types/utility-types/optional.ts"],"names":[],"mappings":"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","sourcesContent":["import { BaseType } from \"../../core/type/type.ts\"\nimport {\n type AnyObjectNode,\n type ExtractCSTWithSTN,\n type IAnyType,\n type IType,\n type IValidationContext,\n type IValidationResult,\n TypeFlags,\n assertIsType,\n devMode,\n fail,\n isStateTreeNode,\n isType,\n typeCheckSuccess,\n typecheckInternal\n} from \"../../internal.ts\"\n\ntype IFunctionReturn<T> = () => T\n\ntype IOptionalValue<C, T> = C | IFunctionReturn<C | T>\n\n/** @hidden */\nexport type ValidOptionalValue = string | boolean | number | null | undefined\n\n/** @hidden */\nexport type ValidOptionalValues = [ValidOptionalValue, ...ValidOptionalValue[]]\n\n/**\n * @hidden\n * @internal\n */\nexport class OptionalValue<\n IT extends IAnyType,\n OptionalVals extends ValidOptionalValues\n> extends BaseType<\n IT[\"CreationType\"] | OptionalVals[number],\n IT[\"SnapshotType\"],\n IT[\"TypeWithoutSTN\"]\n> {\n get flags() {\n return this._subtype.flags | TypeFlags.Optional\n }\n\n constructor(\n private readonly _subtype: IT,\n private readonly _defaultValue: IOptionalValue<\n IT[\"CreationType\"],\n IT[\"Type\"]\n >,\n readonly optionalValues: OptionalVals\n ) {\n super(_subtype.name)\n }\n\n describe() {\n return this._subtype.describe() + \"?\"\n }\n\n instantiate(\n parent: AnyObjectNode | null,\n subpath: string,\n environment: any,\n initialValue: this[\"C\"] | this[\"T\"]\n ): this[\"N\"] {\n if (this.optionalValues.indexOf(initialValue) >= 0) {\n const defaultInstanceOrSnapshot = this.getDefaultInstanceOrSnapshot()\n return this._subtype.instantiate(\n parent,\n subpath,\n environment,\n defaultInstanceOrSnapshot\n )\n }\n return this._subtype.instantiate(parent, subpath, environment, initialValue)\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(\n current,\n this.optionalValues.indexOf(newValue) < 0 && this._subtype.is(newValue)\n ? newValue\n : this.getDefaultInstanceOrSnapshot(),\n parent,\n subpath\n )\n }\n\n getDefaultInstanceOrSnapshot(): this[\"C\"] | this[\"T\"] {\n const defaultInstanceOrSnapshot =\n typeof this._defaultValue === \"function\"\n ? (this._defaultValue as IFunctionReturn<this[\"C\"] | this[\"T\"]>)()\n : this._defaultValue\n\n // while static values are already snapshots and checked on types.optional\n // generator functions must always be rechecked just in case\n if (typeof this._defaultValue === \"function\") {\n typecheckInternal(this, defaultInstanceOrSnapshot)\n }\n\n return defaultInstanceOrSnapshot\n }\n\n isValidSnapshot(\n value: this[\"C\"],\n context: IValidationContext\n ): IValidationResult {\n // defaulted values can be skipped\n if (this.optionalValues.indexOf(value) >= 0) {\n return typeCheckSuccess()\n }\n // bounce validation to the sub-type\n return this._subtype.validate(value, context)\n }\n\n isAssignableFrom(type: IAnyType) {\n return this._subtype.isAssignableFrom(type)\n }\n\n getSubTypes() {\n return this._subtype\n }\n}\n\n/** @hidden */\nexport type OptionalDefaultValueOrFunction<IT extends IAnyType> =\n | IT[\"CreationType\"]\n | IT[\"SnapshotType\"]\n | (() => ExtractCSTWithSTN<IT>)\n\n/** @hidden */\nexport interface IOptionalIType<\n IT extends IAnyType,\n OptionalVals extends ValidOptionalValues\n> extends IType<\n IT[\"CreationType\"] | OptionalVals[number],\n IT[\"SnapshotType\"],\n IT[\"TypeWithoutSTN\"]\n> {}\n\nfunction checkOptionalPreconditions<IT extends IAnyType>(\n type: IAnyType,\n defaultValueOrFunction: OptionalDefaultValueOrFunction<IT>\n) {\n // make sure we never pass direct instances\n if (\n typeof defaultValueOrFunction !== \"function\" &&\n isStateTreeNode(defaultValueOrFunction)\n ) {\n throw fail(\n \"default value cannot be an instance, pass a snapshot or a function that creates an instance/snapshot instead\"\n )\n }\n assertIsType(type, 1)\n if (devMode()) {\n // we only check default values if they are passed directly\n // if they are generator functions they will be checked once they are generated\n // we don't check generator function results here to avoid generating a node just for type-checking purposes\n // which might generate side-effects\n if (typeof defaultValueOrFunction !== \"function\") {\n typecheckInternal(type, defaultValueOrFunction)\n }\n }\n}\n\nexport function optional<IT extends IAnyType>(\n type: IT,\n defaultValueOrFunction: OptionalDefaultValueOrFunction<IT>\n): IOptionalIType<IT, [undefined]>\nexport function optional<\n IT extends IAnyType,\n OptionalVals extends ValidOptionalValues\n>(\n type: IT,\n defaultValueOrFunction: OptionalDefaultValueOrFunction<IT>,\n optionalValues: OptionalVals\n): IOptionalIType<IT, OptionalVals>\n/**\n * `types.optional` - Can be used to create a property with a default value.\n *\n * Depending on the third argument (`optionalValues`) there are two ways of operation:\n * - If the argument is not provided, then if a value is not provided in the snapshot (`undefined` or missing),\n * it will default to the provided `defaultValue`\n * - If the argument is provided, then if the value in the snapshot matches one of the optional values inside the array then it will\n * default to the provided `defaultValue`. Additionally, if one of the optional values inside the array is `undefined` then a missing\n * property is also valid.\n *\n * Note that it is also possible to include values of the same type as the intended subtype as optional values,\n * in this case the optional value will be transformed into the `defaultValue` (e.g. `types.optional(types.string, \"unnamed\", [undefined, \"\"])`\n * will transform the snapshot values `undefined` (and therefore missing) and empty strings into the string `\"unnamed\"` when it gets\n * instantiated).\n *\n * If `defaultValue` is a function, the function will be invoked for every new instance.\n * Applying a snapshot in which the optional value is one of the optional values (or `undefined`/_not_ present if none are provided) causes the\n * value to be reset.\n *\n * Example:\n * ```ts\n * const Todo = types.model({\n * title: types.string,\n * subtitle1: types.optional(types.string, \"\", [null]),\n * subtitle2: types.optional(types.string, \"\", [null, undefined]),\n * done: types.optional(types.boolean, false),\n * created: types.optional(types.Date, () => new Date()),\n * })\n *\n * // if done is missing / undefined it will become false\n * // if created is missing / undefined it will get a freshly generated timestamp\n * // if subtitle1 is null it will default to \"\", but it cannot be missing or undefined\n * // if subtitle2 is null or undefined it will default to \"\"; since it can be undefined it can also be missing\n * const todo = Todo.create({ title: \"Get coffee\", subtitle1: null })\n * ```\n *\n * @param type\n * @param defaultValueOrFunction\n * @param optionalValues an optional array with zero or more primitive values (string, number, boolean, null or undefined)\n * that will be converted into the default. `[ undefined ]` is assumed when none is provided\n * @returns\n */\nexport function optional<\n IT extends IAnyType,\n OptionalVals extends ValidOptionalValues\n>(\n type: IT,\n defaultValueOrFunction: OptionalDefaultValueOrFunction<IT>,\n optionalValues?: OptionalVals\n): IOptionalIType<IT, OptionalVals> {\n checkOptionalPreconditions(type, defaultValueOrFunction)\n\n return new OptionalValue(\n type,\n defaultValueOrFunction,\n optionalValues ? optionalValues : undefinedAsOptionalValues\n )\n}\n\nconst undefinedAsOptionalValues: [undefined] = [undefined]\n\n/**\n * Returns if a value represents an optional type.\n *\n * @template IT\n * @param type\n * @returns\n */\nexport function isOptionalType<IT extends IAnyType>(type: IT): type is IT {\n return isType(type) && (type.flags & TypeFlags.Optional) > 0\n}\n"]}
@@ -1,90 +0,0 @@
1
- import { SimpleType } from "../../core/type/type.ts";
2
- import { type AnyObjectNode, type IAnyComplexType, type IAnyStateTreeNode, type IAnyType, type IMaybe, type IStateTreeNode, type IType, type IValidationContext, type IValidationResult, type ReferenceIdentifier, TypeFlags } from "../../internal.ts";
3
- export type OnReferenceInvalidatedEvent<STN extends IAnyStateTreeNode> = {
4
- parent: IAnyStateTreeNode;
5
- invalidTarget: STN | undefined;
6
- invalidId: ReferenceIdentifier;
7
- replaceRef: (newRef: STN | null | undefined) => void;
8
- removeRef: () => void;
9
- cause: "detach" | "destroy" | "invalidSnapshotReference";
10
- };
11
- export type OnReferenceInvalidated<STN extends IAnyStateTreeNode> = (event: OnReferenceInvalidatedEvent<STN>) => void;
12
- /** @hidden */
13
- export type ReferenceT<IT extends IAnyType> = IT["TypeWithoutSTN"] & IStateTreeNode<IReferenceType<IT>>;
14
- /**
15
- * @internal
16
- * @hidden
17
- */
18
- export declare class InvalidReferenceError extends Error {
19
- constructor(m: string);
20
- }
21
- /**
22
- * @internal
23
- * @hidden
24
- */
25
- export declare abstract class BaseReferenceType<IT extends IAnyComplexType> extends SimpleType<ReferenceIdentifier, ReferenceIdentifier, IT["TypeWithoutSTN"]> {
26
- protected readonly targetType: IT;
27
- private readonly onInvalidated?;
28
- readonly flags = TypeFlags.Reference;
29
- constructor(targetType: IT, onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>> | undefined);
30
- describe(): string;
31
- isAssignableFrom(type: IAnyType): boolean;
32
- isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
33
- private fireInvalidated;
34
- private addTargetNodeWatcher;
35
- protected watchTargetNodeForInvalidations(storedRefNode: this["N"], identifier: ReferenceIdentifier, customGetSet: ReferenceOptionsGetSet<IT> | undefined): void;
36
- }
37
- /**
38
- * @internal
39
- * @hidden
40
- */
41
- export declare class IdentifierReferenceType<IT extends IAnyComplexType> extends BaseReferenceType<IT> {
42
- constructor(targetType: IT, onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>>);
43
- getValue(storedRefNode: this["N"]): any;
44
- getSnapshot(storedRefNode: this["N"]): ReferenceIdentifier;
45
- instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: this["C"] | this["T"]): this["N"];
46
- reconcile(current: this["N"], newValue: this["C"] | this["T"], parent: AnyObjectNode, subpath: string): this["N"];
47
- }
48
- /**
49
- * @internal
50
- * @hidden
51
- */
52
- export declare class CustomReferenceType<IT extends IAnyComplexType> extends BaseReferenceType<IT> {
53
- private readonly options;
54
- constructor(targetType: IT, options: ReferenceOptionsGetSet<IT>, onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>>);
55
- getValue(storedRefNode: this["N"]): any;
56
- getSnapshot(storedRefNode: this["N"]): any;
57
- instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, newValue: this["C"] | this["T"]): this["N"];
58
- reconcile(current: this["N"], newValue: this["C"] | this["T"], parent: AnyObjectNode, subpath: string): this["N"];
59
- }
60
- export interface ReferenceOptionsGetSet<IT extends IAnyComplexType> {
61
- get(identifier: ReferenceIdentifier, parent: IAnyStateTreeNode | null): ReferenceT<IT>;
62
- set(value: ReferenceT<IT>, parent: IAnyStateTreeNode | null): ReferenceIdentifier;
63
- }
64
- export interface ReferenceOptionsOnInvalidated<IT extends IAnyComplexType> {
65
- onInvalidated: OnReferenceInvalidated<ReferenceT<IT>>;
66
- }
67
- export type ReferenceOptions<IT extends IAnyComplexType> = ReferenceOptionsGetSet<IT> | ReferenceOptionsOnInvalidated<IT> | (ReferenceOptionsGetSet<IT> & ReferenceOptionsOnInvalidated<IT>);
68
- /** @hidden */
69
- export interface IReferenceType<IT extends IAnyComplexType> extends IType<ReferenceIdentifier, ReferenceIdentifier, IT["TypeWithoutSTN"]> {
70
- }
71
- /**
72
- * `types.reference` - Creates a reference to another type, which should have defined an identifier.
73
- * See also the [reference and identifiers](https://github.com/mobxjs/mobx-state-tree#references-and-identifiers) section.
74
- */
75
- export declare function reference<IT extends IAnyComplexType>(subType: IT, options?: ReferenceOptions<IT>): IReferenceType<IT>;
76
- /**
77
- * Returns if a given value represents a reference type.
78
- *
79
- * @param type
80
- * @returns
81
- */
82
- export declare function isReferenceType<IT extends IReferenceType<any>>(type: IT): type is IT;
83
- export declare function safeReference<IT extends IAnyComplexType>(subType: IT, options: (ReferenceOptionsGetSet<IT> | object) & {
84
- acceptsUndefined: false;
85
- onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>>;
86
- }): IReferenceType<IT>;
87
- export declare function safeReference<IT extends IAnyComplexType>(subType: IT, options?: (ReferenceOptionsGetSet<IT> | object) & {
88
- acceptsUndefined?: boolean;
89
- onInvalidated?: OnReferenceInvalidated<ReferenceT<IT>>;
90
- }): IMaybe<IReferenceType<IT>>;