@jbrowse/mobx-state-tree 5.6.10 → 5.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/action.js.map +1 -1
- package/dist/core/actionContext.js.map +1 -1
- package/dist/core/flow.js.map +1 -1
- package/dist/core/json-patch.js.map +1 -1
- package/dist/core/mst-operations.js.map +1 -1
- package/dist/core/node/BaseNode.js.map +1 -1
- package/dist/core/node/Hook.js.map +1 -1
- package/dist/core/node/create-node.js.map +1 -1
- package/dist/core/node/identifier-cache.js.map +1 -1
- package/dist/core/node/livelinessChecking.js.map +1 -1
- package/dist/core/node/node-utils.js.map +1 -1
- package/dist/core/node/object-node.d.ts +3 -1
- package/dist/core/node/object-node.js +2 -1
- package/dist/core/node/object-node.js.map +1 -1
- package/dist/core/node/scalar-node.d.ts +3 -1
- package/dist/core/node/scalar-node.js +2 -1
- package/dist/core/node/scalar-node.js.map +1 -1
- package/dist/core/process.js.map +1 -1
- package/dist/core/type/type-checker.js.map +1 -1
- package/dist/core/type/type.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/internal.js.map +1 -1
- package/dist/middlewares/create-action-tracking-middleware.js.map +1 -1
- package/dist/middlewares/createActionTrackingMiddleware2.js.map +1 -1
- package/dist/middlewares/on-action.js.map +1 -1
- package/dist/package.json +1 -0
- package/dist/types/complex-types/array.d.ts +2 -1
- package/dist/types/complex-types/array.js +2 -1
- package/dist/types/complex-types/array.js.map +1 -1
- package/dist/types/complex-types/map.d.ts +2 -1
- package/dist/types/complex-types/map.js +2 -1
- package/dist/types/complex-types/map.js.map +1 -1
- package/dist/types/complex-types/model.d.ts +2 -1
- package/dist/types/complex-types/model.js +2 -1
- package/dist/types/complex-types/model.js.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/primitives.d.ts +2 -1
- package/dist/types/primitives.js +2 -1
- package/dist/types/primitives.js.map +1 -1
- package/dist/types/utility-types/custom.d.ts +2 -1
- package/dist/types/utility-types/custom.js +2 -1
- package/dist/types/utility-types/custom.js.map +1 -1
- package/dist/types/utility-types/enumeration.js.map +1 -1
- package/dist/types/utility-types/frozen.d.ts +2 -1
- package/dist/types/utility-types/frozen.js +2 -1
- package/dist/types/utility-types/frozen.js.map +1 -1
- package/dist/types/utility-types/identifier.d.ts +2 -1
- package/dist/types/utility-types/identifier.js +2 -1
- package/dist/types/utility-types/identifier.js.map +1 -1
- package/dist/types/utility-types/late.js +2 -1
- package/dist/types/utility-types/late.js.map +1 -1
- package/dist/types/utility-types/lazy.d.ts +2 -1
- package/dist/types/utility-types/lazy.js +2 -1
- package/dist/types/utility-types/lazy.js.map +1 -1
- package/dist/types/utility-types/literal.d.ts +2 -1
- package/dist/types/utility-types/literal.js +2 -1
- package/dist/types/utility-types/literal.js.map +1 -1
- package/dist/types/utility-types/maybe.js.map +1 -1
- package/dist/types/utility-types/optional.d.ts +2 -1
- package/dist/types/utility-types/optional.js +2 -1
- package/dist/types/utility-types/optional.js.map +1 -1
- package/dist/types/utility-types/reference.d.ts +2 -1
- package/dist/types/utility-types/reference.js +2 -1
- package/dist/types/utility-types/reference.js.map +1 -1
- package/dist/types/utility-types/refinement.js +2 -1
- package/dist/types/utility-types/refinement.js.map +1 -1
- package/dist/types/utility-types/resilient.js +2 -1
- package/dist/types/utility-types/resilient.js.map +1 -1
- package/dist/types/utility-types/snapshotProcessor.js +3 -2
- package/dist/types/utility-types/snapshotProcessor.js.map +1 -1
- package/dist/types/utility-types/union.d.ts +2 -1
- package/dist/types/utility-types/union.js +2 -1
- package/dist/types/utility-types/union.js.map +1 -1
- package/dist/utils.js.map +1 -1
- package/esm/core/action.d.ts +87 -0
- package/esm/core/action.js +219 -0
- package/esm/core/action.js.map +1 -0
- package/esm/core/actionContext.d.ts +27 -0
- package/esm/core/actionContext.js +37 -0
- package/esm/core/actionContext.js.map +1 -0
- package/esm/core/flow.d.ts +69 -0
- package/esm/core/flow.js +173 -0
- package/esm/core/flow.js.map +1 -0
- package/esm/core/json-patch.d.ts +46 -0
- package/esm/core/json-patch.js +125 -0
- package/esm/core/json-patch.js.map +1 -0
- package/esm/core/mst-operations.d.ts +459 -0
- package/esm/core/mst-operations.js +844 -0
- package/esm/core/mst-operations.js.map +1 -0
- package/esm/core/node/BaseNode.d.ts +62 -0
- package/esm/core/node/BaseNode.js +148 -0
- package/esm/core/node/BaseNode.js.map +1 -0
- package/esm/core/node/Hook.d.ts +17 -0
- package/esm/core/node/Hook.js +12 -0
- package/esm/core/node/Hook.js.map +1 -0
- package/esm/core/node/create-node.d.ts +16 -0
- package/esm/core/node/create-node.js +36 -0
- package/esm/core/node/create-node.js.map +1 -0
- package/esm/core/node/identifier-cache.d.ts +19 -0
- package/esm/core/node/identifier-cache.js +111 -0
- package/esm/core/node/identifier-cache.js.map +1 -0
- package/esm/core/node/livelinessChecking.d.ts +37 -0
- package/esm/core/node/livelinessChecking.js +33 -0
- package/esm/core/node/livelinessChecking.js.map +1 -0
- package/esm/core/node/node-utils.d.ts +83 -0
- package/esm/core/node/node-utils.js +153 -0
- package/esm/core/node/node-utils.js.map +1 -0
- package/esm/core/node/object-node.d.ts +101 -0
- package/esm/core/node/object-node.js +542 -0
- package/esm/core/node/object-node.js.map +1 -0
- package/esm/core/node/scalar-node.d.ts +21 -0
- package/esm/core/node/scalar-node.js +86 -0
- package/esm/core/node/scalar-node.js.map +1 -0
- package/esm/core/process.d.ts +50 -0
- package/esm/core/process.js +35 -0
- package/esm/core/process.js.map +1 -0
- package/esm/core/type/type-checker.d.ts +69 -0
- package/esm/core/type/type-checker.js +144 -0
- package/esm/core/type/type-checker.js.map +1 -0
- package/esm/core/type/type.d.ts +317 -0
- package/esm/core/type/type.js +243 -0
- package/esm/core/type/type.js.map +1 -0
- package/esm/index.d.ts +2 -0
- package/esm/index.js +2 -0
- package/esm/index.js.map +1 -0
- package/esm/internal.d.ts +39 -0
- package/esm/internal.js +44 -0
- package/esm/internal.js.map +1 -0
- package/esm/middlewares/create-action-tracking-middleware.d.ts +24 -0
- package/esm/middlewares/create-action-tracking-middleware.js +78 -0
- package/esm/middlewares/create-action-tracking-middleware.js.map +1 -0
- package/esm/middlewares/createActionTrackingMiddleware2.d.ts +34 -0
- package/esm/middlewares/createActionTrackingMiddleware2.js +130 -0
- package/esm/middlewares/createActionTrackingMiddleware2.js.map +1 -0
- package/esm/middlewares/on-action.d.ts +87 -0
- package/esm/middlewares/on-action.js +210 -0
- package/esm/middlewares/on-action.js.map +1 -0
- package/esm/types/complex-types/array.d.ts +81 -0
- package/esm/types/complex-types/array.js +347 -0
- package/esm/types/complex-types/array.js.map +1 -0
- package/esm/types/complex-types/map.d.ts +111 -0
- package/esm/types/complex-types/map.js +356 -0
- package/esm/types/complex-types/map.js.map +1 -0
- package/esm/types/complex-types/model.d.ts +193 -0
- package/esm/types/complex-types/model.js +471 -0
- package/esm/types/complex-types/model.js.map +1 -0
- package/esm/types/index.d.ts +33 -0
- package/esm/types/index.js +35 -0
- package/esm/types/index.js.map +1 -0
- package/esm/types/primitives.d.ts +125 -0
- package/esm/types/primitives.js +177 -0
- package/esm/types/primitives.js.map +1 -0
- package/esm/types/utility-types/custom.d.ts +75 -0
- package/esm/types/utility-types/custom.js +106 -0
- package/esm/types/utility-types/custom.js.map +1 -0
- package/esm/types/utility-types/enumeration.d.ts +5 -0
- package/esm/types/utility-types/enumeration.js +31 -0
- package/esm/types/utility-types/enumeration.js.map +1 -0
- package/esm/types/utility-types/frozen.d.ts +24 -0
- package/esm/types/utility-types/frozen.js +92 -0
- package/esm/types/utility-types/frozen.js.map +1 -0
- package/esm/types/utility-types/identifier.d.ts +87 -0
- package/esm/types/utility-types/identifier.js +121 -0
- package/esm/types/utility-types/identifier.js.map +1 -0
- package/esm/types/utility-types/late.d.ts +10 -0
- package/esm/types/utility-types/late.js +106 -0
- package/esm/types/utility-types/late.js.map +1 -0
- package/esm/types/utility-types/lazy.d.ts +23 -0
- package/esm/types/utility-types/lazy.js +72 -0
- package/esm/types/utility-types/lazy.js.map +1 -0
- package/esm/types/utility-types/literal.d.ts +38 -0
- package/esm/types/utility-types/literal.js +58 -0
- package/esm/types/utility-types/literal.js.map +1 -0
- package/esm/types/utility-types/maybe.d.ts +26 -0
- package/esm/types/utility-types/maybe.js +26 -0
- package/esm/types/utility-types/maybe.js.map +1 -0
- package/esm/types/utility-types/optional.d.ts +42 -0
- package/esm/types/utility-types/optional.js +135 -0
- package/esm/types/utility-types/optional.js.map +1 -0
- package/esm/types/utility-types/reference.d.ts +90 -0
- package/esm/types/utility-types/reference.js +383 -0
- package/esm/types/utility-types/reference.js.map +1 -0
- package/esm/types/utility-types/refinement.d.ts +10 -0
- package/esm/types/utility-types/refinement.js +82 -0
- package/esm/types/utility-types/refinement.js.map +1 -0
- package/esm/types/utility-types/resilient.d.ts +18 -0
- package/esm/types/utility-types/resilient.js +118 -0
- package/esm/types/utility-types/resilient.js.map +1 -0
- package/esm/types/utility-types/snapshotProcessor.d.ts +63 -0
- package/esm/types/utility-types/snapshotProcessor.js +159 -0
- package/esm/types/utility-types/snapshotProcessor.js.map +1 -0
- package/esm/types/utility-types/union.d.ts +78 -0
- package/esm/types/utility-types/union.js +240 -0
- package/esm/types/utility-types/union.js.map +1 -0
- package/esm/utils.d.ts +230 -0
- package/esm/utils.js +449 -0
- package/esm/utils.js.map +1 -0
- package/package.json +23 -22
- package/dist/mobx-state-tree.js +0 -86
- package/dist/mobx-state-tree.js.map +0 -1
- package/dist/mobx-state-tree.module.js +0 -84
- package/dist/mobx-state-tree.module.js.map +0 -1
|
@@ -0,0 +1,58 @@
|
|
|
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
|
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,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"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
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>;
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,135 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optional.js","sourceRoot":"","sources":["../../../src/types/utility-types/optional.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAClD,OAAO,EAOL,SAAS,EACT,YAAY,EACZ,OAAO,EACP,IAAI,EACJ,eAAe,EACf,MAAM,EACN,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,mBAAmB,CAAA;AAY1B;;;GAGG;AACH,MAAM,OAAO,aAGX,SAAQ,QAIT;IAMoB;IACA;IAIR;IAVX,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAA;IACjD,CAAC;IAED,YACmB,QAAY,EACZ,aAGhB,EACQ,cAA4B;QAErC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;QAPH,aAAQ,GAAR,QAAQ,CAAI;QACZ,kBAAa,GAAb,aAAa,CAG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,EACtB,cAAc,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,yBAAyB,CAC5D,CAAA;AACH,CAAC;AAED,MAAM,yBAAyB,GAAgB,CAAC,SAAS,CAAC,CAAA;AAE1D;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAsB,IAAQ;IAC1D,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;AAC9D,CAAC","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"]}
|
|
@@ -0,0 +1,90 @@
|
|
|
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>>;
|