@jbrowse/mobx-state-tree 5.6.3 → 5.6.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.
- package/dist/core/action.d.ts +87 -0
- package/dist/core/action.js +228 -0
- package/dist/core/action.js.map +1 -0
- package/dist/core/actionContext.d.ts +27 -0
- package/dist/core/actionContext.js +42 -0
- package/dist/core/actionContext.js.map +1 -0
- package/dist/core/flow.d.ts +69 -0
- package/dist/core/flow.js +180 -0
- package/dist/core/flow.js.map +1 -0
- package/dist/core/json-patch.d.ts +46 -0
- package/dist/core/json-patch.js +133 -0
- package/dist/core/json-patch.js.map +1 -0
- package/dist/core/mst-operations.d.ts +459 -0
- package/dist/core/mst-operations.js +885 -0
- package/dist/core/mst-operations.js.map +1 -0
- package/dist/core/node/BaseNode.d.ts +62 -0
- package/dist/core/node/BaseNode.js +152 -0
- package/dist/core/node/BaseNode.js.map +1 -0
- package/dist/core/node/Hook.d.ts +17 -0
- package/dist/core/node/Hook.js +15 -0
- package/dist/core/node/Hook.js.map +1 -0
- package/dist/core/node/create-node.d.ts +16 -0
- package/dist/core/node/create-node.js +41 -0
- package/dist/core/node/create-node.js.map +1 -0
- package/dist/core/node/identifier-cache.d.ts +19 -0
- package/dist/core/node/identifier-cache.js +115 -0
- package/dist/core/node/identifier-cache.js.map +1 -0
- package/dist/core/node/livelinessChecking.d.ts +37 -0
- package/dist/core/node/livelinessChecking.js +38 -0
- package/dist/core/node/livelinessChecking.js.map +1 -0
- package/dist/core/node/node-utils.d.ts +83 -0
- package/dist/core/node/node-utils.js +165 -0
- package/dist/core/node/node-utils.js.map +1 -0
- package/dist/core/node/object-node.d.ts +99 -0
- package/dist/core/node/object-node.js +545 -0
- package/dist/core/node/object-node.js.map +1 -0
- package/dist/core/node/scalar-node.d.ts +19 -0
- package/dist/core/node/scalar-node.js +89 -0
- package/dist/core/node/scalar-node.js.map +1 -0
- package/dist/core/process.d.ts +50 -0
- package/dist/core/process.js +39 -0
- package/dist/core/process.js.map +1 -0
- package/dist/core/type/type-checker.d.ts +69 -0
- package/dist/core/type/type-checker.js +154 -0
- package/dist/core/type/type-checker.js.map +1 -0
- package/dist/core/type/type.d.ts +317 -0
- package/dist/core/type/type.js +251 -0
- package/dist/core/type/type.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +85 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +39 -0
- package/dist/internal.js +60 -0
- package/dist/internal.js.map +1 -0
- package/dist/middlewares/create-action-tracking-middleware.d.ts +24 -0
- package/dist/middlewares/create-action-tracking-middleware.js +81 -0
- package/dist/middlewares/create-action-tracking-middleware.js.map +1 -0
- package/dist/middlewares/createActionTrackingMiddleware2.d.ts +34 -0
- package/dist/middlewares/createActionTrackingMiddleware2.js +133 -0
- package/dist/middlewares/createActionTrackingMiddleware2.js.map +1 -0
- package/dist/middlewares/on-action.d.ts +87 -0
- package/dist/middlewares/on-action.js +215 -0
- package/dist/middlewares/on-action.js.map +1 -0
- package/dist/mobx-state-tree.js +85 -0
- package/dist/mobx-state-tree.module.js +83 -0
- package/dist/types/complex-types/array.d.ts +80 -0
- package/dist/types/complex-types/array.js +352 -0
- package/dist/types/complex-types/array.js.map +1 -0
- package/dist/types/complex-types/map.d.ts +110 -0
- package/dist/types/complex-types/map.js +361 -0
- package/dist/types/complex-types/map.js.map +1 -0
- package/dist/types/complex-types/model.d.ts +192 -0
- package/dist/types/complex-types/model.js +477 -0
- package/dist/types/complex-types/model.js.map +1 -0
- package/dist/types/index.d.ts +33 -0
- package/dist/types/index.js +38 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/primitives.d.ts +124 -0
- package/dist/types/primitives.js +182 -0
- package/dist/types/primitives.js.map +1 -0
- package/dist/types/utility-types/custom.d.ts +74 -0
- package/dist/types/utility-types/custom.js +110 -0
- package/dist/types/utility-types/custom.js.map +1 -0
- package/dist/types/utility-types/enumeration.d.ts +5 -0
- package/dist/types/utility-types/enumeration.js +34 -0
- package/dist/types/utility-types/enumeration.js.map +1 -0
- package/dist/types/utility-types/frozen.d.ts +23 -0
- package/dist/types/utility-types/frozen.js +97 -0
- package/dist/types/utility-types/frozen.js.map +1 -0
- package/dist/types/utility-types/identifier.d.ts +86 -0
- package/dist/types/utility-types/identifier.js +129 -0
- package/dist/types/utility-types/identifier.js.map +1 -0
- package/dist/types/utility-types/late.d.ts +10 -0
- package/dist/types/utility-types/late.js +109 -0
- package/dist/types/utility-types/late.js.map +1 -0
- package/dist/types/utility-types/lazy.d.ts +22 -0
- package/dist/types/utility-types/lazy.js +76 -0
- package/dist/types/utility-types/lazy.js.map +1 -0
- package/dist/types/utility-types/literal.d.ts +37 -0
- package/dist/types/utility-types/literal.js +63 -0
- package/dist/types/utility-types/literal.js.map +1 -0
- package/dist/types/utility-types/maybe.d.ts +26 -0
- package/dist/types/utility-types/maybe.js +30 -0
- package/dist/types/utility-types/maybe.js.map +1 -0
- package/dist/types/utility-types/optional.d.ts +41 -0
- package/dist/types/utility-types/optional.js +140 -0
- package/dist/types/utility-types/optional.js.map +1 -0
- package/dist/types/utility-types/reference.d.ts +89 -0
- package/dist/types/utility-types/reference.js +392 -0
- package/dist/types/utility-types/reference.js.map +1 -0
- package/dist/types/utility-types/refinement.d.ts +10 -0
- package/dist/types/utility-types/refinement.js +85 -0
- package/dist/types/utility-types/refinement.js.map +1 -0
- package/dist/types/utility-types/resilient.d.ts +18 -0
- package/dist/types/utility-types/resilient.js +120 -0
- package/dist/types/utility-types/resilient.js.map +1 -0
- package/dist/types/utility-types/snapshotProcessor.d.ts +63 -0
- package/dist/types/utility-types/snapshotProcessor.js +161 -0
- package/dist/types/utility-types/snapshotProcessor.js.map +1 -0
- package/dist/types/utility-types/union.d.ts +77 -0
- package/dist/types/utility-types/union.js +245 -0
- package/dist/types/utility-types/union.js.map +1 -0
- package/dist/utils.d.ts +230 -0
- package/dist/utils.js +483 -0
- package/dist/utils.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { type AnyObjectNode, type ISimpleType, type IValidationContext, type IValidationResult, type ScalarNode, SimpleType, TypeFlags } from "../../internal.ts";
|
|
2
|
+
declare abstract class BaseIdentifierType<T> extends SimpleType<T, T, T> {
|
|
3
|
+
private readonly validType;
|
|
4
|
+
readonly flags = TypeFlags.Identifier;
|
|
5
|
+
constructor(name: string, validType: "string" | "number");
|
|
6
|
+
instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: this["C"]): this["N"];
|
|
7
|
+
reconcile(current: this["N"], newValue: this["C"], parent: AnyObjectNode, subpath: string): this["N"];
|
|
8
|
+
isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* @internal
|
|
12
|
+
* @hidden
|
|
13
|
+
*/
|
|
14
|
+
export declare class IdentifierType extends BaseIdentifierType<string> {
|
|
15
|
+
readonly flags = TypeFlags.Identifier;
|
|
16
|
+
constructor();
|
|
17
|
+
describe(): string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @internal
|
|
21
|
+
* @hidden
|
|
22
|
+
*/
|
|
23
|
+
export declare class IdentifierNumberType extends BaseIdentifierType<number> {
|
|
24
|
+
constructor();
|
|
25
|
+
getSnapshot(node: ScalarNode<number, number, number>): number;
|
|
26
|
+
describe(): string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* `types.identifier` - Identifiers are used to make references, lifecycle events and reconciling works.
|
|
30
|
+
* Inside a state tree, for each type can exist only one instance for each given identifier.
|
|
31
|
+
* For example there couldn't be 2 instances of user with id 1. If you need more, consider using references.
|
|
32
|
+
* Identifier can be used only as type property of a model.
|
|
33
|
+
* This type accepts as parameter the value type of the identifier field that can be either string or number.
|
|
34
|
+
*
|
|
35
|
+
* Example:
|
|
36
|
+
* ```ts
|
|
37
|
+
* const Todo = types.model("Todo", {
|
|
38
|
+
* id: types.identifier,
|
|
39
|
+
* title: types.string
|
|
40
|
+
* })
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
export declare const identifier: ISimpleType<string>;
|
|
46
|
+
/**
|
|
47
|
+
* `types.identifierNumber` - Similar to `types.identifier`. This one will serialize from / to a number when applying snapshots
|
|
48
|
+
*
|
|
49
|
+
* Example:
|
|
50
|
+
* ```ts
|
|
51
|
+
* const Todo = types.model("Todo", {
|
|
52
|
+
* id: types.identifierNumber,
|
|
53
|
+
* title: types.string
|
|
54
|
+
* })
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
export declare const identifierNumber: ISimpleType<number>;
|
|
60
|
+
/**
|
|
61
|
+
* Returns if a given value represents an identifier type.
|
|
62
|
+
*
|
|
63
|
+
* @param type
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
export declare function isIdentifierType<IT extends typeof identifier | typeof identifierNumber>(type: IT): type is IT;
|
|
67
|
+
/**
|
|
68
|
+
* Valid types for identifiers.
|
|
69
|
+
*/
|
|
70
|
+
export type ReferenceIdentifier = string | number;
|
|
71
|
+
/**
|
|
72
|
+
* @internal
|
|
73
|
+
* @hidden
|
|
74
|
+
*/
|
|
75
|
+
export declare function normalizeIdentifier(id: ReferenceIdentifier): string;
|
|
76
|
+
/**
|
|
77
|
+
* @internal
|
|
78
|
+
* @hidden
|
|
79
|
+
*/
|
|
80
|
+
export declare function isValidIdentifier(id: any): id is ReferenceIdentifier;
|
|
81
|
+
/**
|
|
82
|
+
* @internal
|
|
83
|
+
* @hidden
|
|
84
|
+
*/
|
|
85
|
+
export declare function assertIsValidIdentifier(id: ReferenceIdentifier, argNumber: number | number[]): void;
|
|
86
|
+
export {};
|
|
@@ -0,0 +1,129 @@
|
|
|
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 internal_ts_1 = require("../../internal.js");
|
|
9
|
+
class BaseIdentifierType extends internal_ts_1.SimpleType {
|
|
10
|
+
validType;
|
|
11
|
+
flags = internal_ts_1.TypeFlags.Identifier;
|
|
12
|
+
constructor(name, validType) {
|
|
13
|
+
super(name);
|
|
14
|
+
this.validType = validType;
|
|
15
|
+
}
|
|
16
|
+
instantiate(parent, subpath, environment, initialValue) {
|
|
17
|
+
if (!parent || !(parent.type instanceof internal_ts_1.ModelType)) {
|
|
18
|
+
throw (0, internal_ts_1.fail)(`Identifier types can only be instantiated as direct child of a model type`);
|
|
19
|
+
}
|
|
20
|
+
return (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, initialValue);
|
|
21
|
+
}
|
|
22
|
+
reconcile(current, newValue, parent, subpath) {
|
|
23
|
+
// we don't consider detaching here since identifier are scalar nodes, and scalar nodes cannot be detached
|
|
24
|
+
if (current.storedValue !== newValue) {
|
|
25
|
+
throw (0, internal_ts_1.fail)(`Tried to change identifier from '${current.storedValue}' to '${newValue}'. Changing identifiers is not allowed.`);
|
|
26
|
+
}
|
|
27
|
+
current.setParent(parent, subpath);
|
|
28
|
+
return current;
|
|
29
|
+
}
|
|
30
|
+
isValidSnapshot(value, context) {
|
|
31
|
+
if (typeof value !== this.validType) {
|
|
32
|
+
return (0, internal_ts_1.typeCheckFailure)(context, value, `Value is not a valid ${this.describe()}, expected a ${this.validType}`);
|
|
33
|
+
}
|
|
34
|
+
return (0, internal_ts_1.typeCheckSuccess)();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* @internal
|
|
39
|
+
* @hidden
|
|
40
|
+
*/
|
|
41
|
+
class IdentifierType extends BaseIdentifierType {
|
|
42
|
+
flags = internal_ts_1.TypeFlags.Identifier;
|
|
43
|
+
constructor() {
|
|
44
|
+
super(`identifier`, "string");
|
|
45
|
+
}
|
|
46
|
+
describe() {
|
|
47
|
+
return `identifier`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.IdentifierType = IdentifierType;
|
|
51
|
+
/**
|
|
52
|
+
* @internal
|
|
53
|
+
* @hidden
|
|
54
|
+
*/
|
|
55
|
+
class IdentifierNumberType extends BaseIdentifierType {
|
|
56
|
+
constructor() {
|
|
57
|
+
super("identifierNumber", "number");
|
|
58
|
+
}
|
|
59
|
+
getSnapshot(node) {
|
|
60
|
+
return node.storedValue;
|
|
61
|
+
}
|
|
62
|
+
describe() {
|
|
63
|
+
return `identifierNumber`;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.IdentifierNumberType = IdentifierNumberType;
|
|
67
|
+
/**
|
|
68
|
+
* `types.identifier` - Identifiers are used to make references, lifecycle events and reconciling works.
|
|
69
|
+
* Inside a state tree, for each type can exist only one instance for each given identifier.
|
|
70
|
+
* For example there couldn't be 2 instances of user with id 1. If you need more, consider using references.
|
|
71
|
+
* Identifier can be used only as type property of a model.
|
|
72
|
+
* This type accepts as parameter the value type of the identifier field that can be either string or number.
|
|
73
|
+
*
|
|
74
|
+
* Example:
|
|
75
|
+
* ```ts
|
|
76
|
+
* const Todo = types.model("Todo", {
|
|
77
|
+
* id: types.identifier,
|
|
78
|
+
* title: types.string
|
|
79
|
+
* })
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @returns
|
|
83
|
+
*/
|
|
84
|
+
exports.identifier = new IdentifierType();
|
|
85
|
+
/**
|
|
86
|
+
* `types.identifierNumber` - Similar to `types.identifier`. This one will serialize from / to a number when applying snapshots
|
|
87
|
+
*
|
|
88
|
+
* Example:
|
|
89
|
+
* ```ts
|
|
90
|
+
* const Todo = types.model("Todo", {
|
|
91
|
+
* id: types.identifierNumber,
|
|
92
|
+
* title: types.string
|
|
93
|
+
* })
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
|
+
exports.identifierNumber = new IdentifierNumberType();
|
|
99
|
+
/**
|
|
100
|
+
* Returns if a given value represents an identifier type.
|
|
101
|
+
*
|
|
102
|
+
* @param type
|
|
103
|
+
* @returns
|
|
104
|
+
*/
|
|
105
|
+
function isIdentifierType(type) {
|
|
106
|
+
return (0, internal_ts_1.isType)(type) && (type.flags & internal_ts_1.TypeFlags.Identifier) > 0;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* @internal
|
|
110
|
+
* @hidden
|
|
111
|
+
*/
|
|
112
|
+
function normalizeIdentifier(id) {
|
|
113
|
+
return "" + id;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* @internal
|
|
117
|
+
* @hidden
|
|
118
|
+
*/
|
|
119
|
+
function isValidIdentifier(id) {
|
|
120
|
+
return typeof id === "string" || typeof id === "number";
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* @internal
|
|
124
|
+
* @hidden
|
|
125
|
+
*/
|
|
126
|
+
function assertIsValidIdentifier(id, argNumber) {
|
|
127
|
+
(0, internal_ts_1.assertArg)(id, isValidIdentifier, "string or number (identifier)", argNumber);
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=identifier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,mDAe0B;AAE1B,MAAe,kBAAsB,SAAQ,wBAAmB;IAK3C;IAJV,KAAK,GAAG,uBAAS,CAAC,UAAU,CAAA;IAErC,YACE,IAAY,EACK,SAA8B;QAE/C,KAAK,CAAC,IAAI,CAAC,CAAA;QAFM,cAAS,GAAT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,SAAQ,kBAA0B;IAClE;QACE,KAAK,CAAC,kBAAkB,EAAE,QAAQ,CAAC,CAAA;IACrC,CAAC;IAED,WAAW,CAAC,IAAwC;QAClD,OAAO,IAAI,CAAC,WAAW,CAAA;IACzB,CAAC;IAED,QAAQ;QACN,OAAO,kBAAkB,CAAA;IAC3B,CAAC;CACF;AAZD,oDAYC;AAED;;;;;;;;;;;;;;;;GAgBG;AACU,QAAA,UAAU,GAAwB,IAAI,cAAc,EAAE,CAAA;AAEnE;;;;;;;;;;;;GAYG;AACU,QAAA,gBAAgB,GAAwB,IAAI,oBAAoB,EAAE,CAAA;AAE/E;;;;;GAKG;AACH,SAAgB,gBAAgB,CAE9B,IAAQ;IACR,OAAO,IAAA,oBAAM,EAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,uBAAS,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;AAChE,CAAC;AAOD;;;GAGG;AACH,SAAgB,mBAAmB,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"}
|
|
@@ -0,0 +1,10 @@
|
|
|
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;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.late = late;
|
|
4
|
+
exports.isLateType = isLateType;
|
|
5
|
+
const internal_ts_1 = require("../../internal.js");
|
|
6
|
+
class Late extends internal_ts_1.BaseType {
|
|
7
|
+
_definition;
|
|
8
|
+
_subType;
|
|
9
|
+
get flags() {
|
|
10
|
+
return (this._subType ? this._subType.flags : 0) | internal_ts_1.TypeFlags.Late;
|
|
11
|
+
}
|
|
12
|
+
getSubType(mustSucceed) {
|
|
13
|
+
if (!this._subType) {
|
|
14
|
+
let t = undefined;
|
|
15
|
+
try {
|
|
16
|
+
t = this._definition();
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
if (e instanceof ReferenceError) // can happen in strict ES5 code when a definition is self refering
|
|
20
|
+
{
|
|
21
|
+
t = undefined;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
throw e;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (mustSucceed && t === undefined) {
|
|
28
|
+
throw (0, internal_ts_1.fail)("Late type seems to be used too early, the definition (still) returns undefined");
|
|
29
|
+
}
|
|
30
|
+
if (t) {
|
|
31
|
+
if ((0, internal_ts_1.devMode)() && !(0, internal_ts_1.isType)(t)) {
|
|
32
|
+
throw (0, internal_ts_1.fail)("Failed to determine subtype, make sure types.late returns a type definition.");
|
|
33
|
+
}
|
|
34
|
+
this._subType = t;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return this._subType;
|
|
38
|
+
}
|
|
39
|
+
constructor(name, _definition) {
|
|
40
|
+
super(name);
|
|
41
|
+
this._definition = _definition;
|
|
42
|
+
}
|
|
43
|
+
instantiate(parent, subpath, environment, initialValue) {
|
|
44
|
+
return this.getSubType(true).instantiate(parent, subpath, environment, initialValue);
|
|
45
|
+
}
|
|
46
|
+
reconcile(current, newValue, parent, subpath) {
|
|
47
|
+
return this.getSubType(true).reconcile(current, newValue, parent, subpath);
|
|
48
|
+
}
|
|
49
|
+
describe() {
|
|
50
|
+
const t = this.getSubType(false);
|
|
51
|
+
return t ? t.name : "<uknown late type>";
|
|
52
|
+
}
|
|
53
|
+
isValidSnapshot(value, context) {
|
|
54
|
+
const t = this.getSubType(false);
|
|
55
|
+
if (!t) {
|
|
56
|
+
// See #916; the variable the definition closure is pointing to wasn't defined yet, so can't be evaluted yet here
|
|
57
|
+
return (0, internal_ts_1.typeCheckSuccess)();
|
|
58
|
+
}
|
|
59
|
+
return t.validate(value, context);
|
|
60
|
+
}
|
|
61
|
+
isAssignableFrom(type) {
|
|
62
|
+
const t = this.getSubType(false);
|
|
63
|
+
return t ? t.isAssignableFrom(type) : false;
|
|
64
|
+
}
|
|
65
|
+
getSubTypes() {
|
|
66
|
+
const subtype = this.getSubType(false);
|
|
67
|
+
return subtype ? subtype : internal_ts_1.cannotDetermineSubtype;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* `types.late` - Defines a type that gets implemented later. This is useful when you have to deal with circular dependencies.
|
|
72
|
+
* Please notice that when defining circular dependencies TypeScript isn't smart enough to inference them.
|
|
73
|
+
*
|
|
74
|
+
* Example:
|
|
75
|
+
* ```ts
|
|
76
|
+
* // TypeScript isn't smart enough to infer self referencing types.
|
|
77
|
+
* const Node = types.model({
|
|
78
|
+
* children: types.array(types.late((): IAnyModelType => Node)) // then typecast each array element to Instance<typeof Node>
|
|
79
|
+
* })
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @param name The name to use for the type that will be returned.
|
|
83
|
+
* @param type A function that returns the type that will be defined.
|
|
84
|
+
* @returns
|
|
85
|
+
*/
|
|
86
|
+
function late(nameOrType, maybeType) {
|
|
87
|
+
const name = typeof nameOrType === "string"
|
|
88
|
+
? nameOrType
|
|
89
|
+
: `late(${nameOrType.toString()})`;
|
|
90
|
+
const type = typeof nameOrType === "string" ? maybeType : nameOrType;
|
|
91
|
+
// checks that the type is actually a late type
|
|
92
|
+
if ((0, internal_ts_1.devMode)()) {
|
|
93
|
+
if (!(typeof type === "function" && type.length === 0)) {
|
|
94
|
+
throw (0, internal_ts_1.fail)("Invalid late type, expected a function with zero arguments that returns a type, got: " +
|
|
95
|
+
type);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return new Late(name, type);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Returns if a given value represents a late type.
|
|
102
|
+
*
|
|
103
|
+
* @param type
|
|
104
|
+
* @returns
|
|
105
|
+
*/
|
|
106
|
+
function isLateType(type) {
|
|
107
|
+
return (0, internal_ts_1.isType)(type) && (type.flags & internal_ts_1.TypeFlags.Late) > 0;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=late.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"late.js","sourceRoot":"","sources":["../../../src/types/utility-types/late.ts"],"names":[],"mappings":";;AA8IA,oBAgBC;AAQD,gCAEC;AAxKD,mDAa0B;AAE1B,MAAM,IAA0B,SAAQ,sBAKvC;IA2CoB;IA1CX,QAAQ,CAAK;IAErB,IAAI,KAAK;QACP,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,uBAAS,CAAC,IAAI,CAAA;IACnE,CAAC;IAID,UAAU,CAAC,WAAoB;QAC7B,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnB,IAAI,CAAC,GAAG,SAAS,CAAA;YACjB,IAAI,CAAC;gBACH,CAAC,GAAG,IAAI,CAAC,WAAW,EAAE,CAAA;YACxB,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,IACE,CAAC,YAAY,cAAc,EAC3B,mEAAmE;iBACrE,CAAC;oBACC,CAAC,GA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AKG;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"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type AnyObjectNode, type IType, type IValidationContext, type IValidationResult, SimpleType, TypeFlags } from "../../internal.ts";
|
|
2
|
+
interface LazyOptions<T extends IType<any, any, any>, U> {
|
|
3
|
+
loadType: () => Promise<T>;
|
|
4
|
+
shouldLoadPredicate: (parent: U) => boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare function lazy<T extends IType<any, any, any>, U>(name: string, options: LazyOptions<T, U>): T;
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
* @hidden
|
|
10
|
+
*/
|
|
11
|
+
export declare class Lazy<T extends IType<any, any, any>, U> extends SimpleType<T, T, T> {
|
|
12
|
+
private readonly options;
|
|
13
|
+
flags: TypeFlags;
|
|
14
|
+
private loadedType;
|
|
15
|
+
private pendingNodeList;
|
|
16
|
+
constructor(name: string, options: LazyOptions<T, U>);
|
|
17
|
+
describe(): string;
|
|
18
|
+
instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, value: this["C"]): this["N"];
|
|
19
|
+
isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
|
|
20
|
+
reconcile(current: this["N"], value: T, parent: AnyObjectNode, subpath: string): this["N"];
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
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 internal_ts_1 = require("../../internal.js");
|
|
7
|
+
function lazy(name, options) {
|
|
8
|
+
// TODO: fix this unknown casting to be stricter
|
|
9
|
+
return new Lazy(name, options);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
* @hidden
|
|
14
|
+
*/
|
|
15
|
+
class Lazy extends internal_ts_1.SimpleType {
|
|
16
|
+
options;
|
|
17
|
+
flags = internal_ts_1.TypeFlags.Lazy;
|
|
18
|
+
loadedType = null;
|
|
19
|
+
pendingNodeList = mobx_1.observable.array();
|
|
20
|
+
constructor(name, options) {
|
|
21
|
+
super(name);
|
|
22
|
+
this.options = options;
|
|
23
|
+
(0, mobx_1.when)(() => this.pendingNodeList.length > 0 &&
|
|
24
|
+
this.pendingNodeList.some(node => node.isAlive &&
|
|
25
|
+
this.options.shouldLoadPredicate(node.parent ? node.parent.value : null)), () => {
|
|
26
|
+
this.options.loadType().then((0, mobx_1.action)((type) => {
|
|
27
|
+
this.loadedType = type;
|
|
28
|
+
this.pendingNodeList.forEach(node => {
|
|
29
|
+
if (!node.parent) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (!this.loadedType) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
node.parent.applyPatches([
|
|
36
|
+
{
|
|
37
|
+
op: "replace",
|
|
38
|
+
path: `/${node.subpath}`,
|
|
39
|
+
value: node.snapshot
|
|
40
|
+
}
|
|
41
|
+
]);
|
|
42
|
+
});
|
|
43
|
+
}));
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
describe() {
|
|
47
|
+
return `<lazy ${this.name}>`;
|
|
48
|
+
}
|
|
49
|
+
instantiate(parent, subpath, environment, value) {
|
|
50
|
+
if (this.loadedType) {
|
|
51
|
+
return this.loadedType.instantiate(parent, subpath, environment, value);
|
|
52
|
+
}
|
|
53
|
+
const node = (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, (0, internal_ts_1.deepFreeze)(value));
|
|
54
|
+
this.pendingNodeList.push(node);
|
|
55
|
+
(0, mobx_1.when)(() => !node.isAlive, () => this.pendingNodeList.splice(this.pendingNodeList.indexOf(node), 1));
|
|
56
|
+
return node;
|
|
57
|
+
}
|
|
58
|
+
isValidSnapshot(value, context) {
|
|
59
|
+
if (this.loadedType) {
|
|
60
|
+
return this.loadedType.validate(value, context);
|
|
61
|
+
}
|
|
62
|
+
if (!(0, internal_ts_1.isSerializable)(value)) {
|
|
63
|
+
return (0, internal_ts_1.typeCheckFailure)(context, value, "Value is not serializable and cannot be lazy");
|
|
64
|
+
}
|
|
65
|
+
return (0, internal_ts_1.typeCheckSuccess)();
|
|
66
|
+
}
|
|
67
|
+
reconcile(current, value, parent, subpath) {
|
|
68
|
+
if (this.loadedType) {
|
|
69
|
+
current.die();
|
|
70
|
+
return this.loadedType.instantiate(parent, subpath, parent.environment, value);
|
|
71
|
+
}
|
|
72
|
+
return super.reconcile(current, value, parent, subpath);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.Lazy = Lazy;
|
|
76
|
+
//# sourceMappingURL=lazy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lazy.js","sourceRoot":"","sources":["../../../src/types/utility-types/lazy.ts"],"names":[],"mappings":";;;AAsBA,oBAMC;AA5BD,+BAAsE;AAEtE,mDAa0B;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,wBAI5D;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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I,EACJ,MAAM,EACN,OAAO,EACP,WAAW,EACX,IAAA,wBAAU,EAAC,KAAK,CAAC,CAClB,CAAA;QACD,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAE/B,IAAA,WAAI,EACF,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,OAAO,EACnB,GAAG,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CACzE,CAAA;QAED,OAAO,IAAI,CAAA;IACb,CAAC;IAED,eAAe,CACb,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"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type AnyObjectNode, type ISimpleType, type IValidationContext, type IValidationResult, type Primitives, SimpleType, TypeFlags } from "../../internal.ts";
|
|
2
|
+
/**
|
|
3
|
+
* @internal
|
|
4
|
+
* @hidden
|
|
5
|
+
*/
|
|
6
|
+
export declare class Literal<T> extends SimpleType<T, T, T> {
|
|
7
|
+
readonly value: T;
|
|
8
|
+
readonly flags = TypeFlags.Literal;
|
|
9
|
+
constructor(value: T);
|
|
10
|
+
instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: this["C"]): this["N"];
|
|
11
|
+
describe(): string;
|
|
12
|
+
isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* `types.literal` - The literal type will return a type that will match only the exact given type.
|
|
16
|
+
* The given value must be a primitive, in order to be serialized to a snapshot correctly.
|
|
17
|
+
* You can use literal to match exact strings for example the exact male or female string.
|
|
18
|
+
*
|
|
19
|
+
* Example:
|
|
20
|
+
* ```ts
|
|
21
|
+
* const Person = types.model({
|
|
22
|
+
* name: types.string,
|
|
23
|
+
* gender: types.union(types.literal('male'), types.literal('female'))
|
|
24
|
+
* })
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param value The value to use in the strict equal check
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
export declare function literal<S extends Primitives>(value: S): ISimpleType<S>;
|
|
31
|
+
/**
|
|
32
|
+
* Returns if a given value represents a literal type.
|
|
33
|
+
*
|
|
34
|
+
* @param type
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
export declare function isLiteralType<IT extends ISimpleType<any>>(type: IT): type is IT;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Literal = void 0;
|
|
4
|
+
exports.literal = literal;
|
|
5
|
+
exports.isLiteralType = isLiteralType;
|
|
6
|
+
const internal_ts_1 = require("../../internal.js");
|
|
7
|
+
const utils_ts_1 = require("../../utils.js");
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
* @hidden
|
|
11
|
+
*/
|
|
12
|
+
class Literal extends internal_ts_1.SimpleType {
|
|
13
|
+
value;
|
|
14
|
+
flags = internal_ts_1.TypeFlags.Literal;
|
|
15
|
+
constructor(value) {
|
|
16
|
+
super(JSON.stringify(value));
|
|
17
|
+
this.value = value;
|
|
18
|
+
}
|
|
19
|
+
instantiate(parent, subpath, environment, initialValue) {
|
|
20
|
+
return (0, internal_ts_1.createScalarNode)(this, parent, subpath, environment, initialValue);
|
|
21
|
+
}
|
|
22
|
+
describe() {
|
|
23
|
+
return JSON.stringify(this.value);
|
|
24
|
+
}
|
|
25
|
+
isValidSnapshot(value, context) {
|
|
26
|
+
if ((0, internal_ts_1.isPrimitive)(value) && value === this.value) {
|
|
27
|
+
return (0, internal_ts_1.typeCheckSuccess)();
|
|
28
|
+
}
|
|
29
|
+
return (0, internal_ts_1.typeCheckFailure)(context, value, `Value is not a literal ${JSON.stringify(this.value)}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.Literal = Literal;
|
|
33
|
+
/**
|
|
34
|
+
* `types.literal` - The literal type will return a type that will match only the exact given type.
|
|
35
|
+
* The given value must be a primitive, in order to be serialized to a snapshot correctly.
|
|
36
|
+
* You can use literal to match exact strings for example the exact male or female string.
|
|
37
|
+
*
|
|
38
|
+
* Example:
|
|
39
|
+
* ```ts
|
|
40
|
+
* const Person = types.model({
|
|
41
|
+
* name: types.string,
|
|
42
|
+
* gender: types.union(types.literal('male'), types.literal('female'))
|
|
43
|
+
* })
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @param value The value to use in the strict equal check
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
function literal(value) {
|
|
50
|
+
// check that the given value is a primitive
|
|
51
|
+
(0, utils_ts_1.assertArg)(value, internal_ts_1.isPrimitive, "primitive", 1);
|
|
52
|
+
return new Literal(value);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns if a given value represents a literal type.
|
|
56
|
+
*
|
|
57
|
+
* @param type
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
function isLiteralType(type) {
|
|
61
|
+
return (0, internal_ts_1.isType)(type) && (type.flags & internal_ts_1.TypeFlags.Literal) > 0;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=literal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"literal.js","sourceRoot":"","sources":["../../../src/types/utility-types/literal.ts"],"names":[],"mappings":";;;AAyEA,0BAKC;AAQD,sCAIC;AA1FD,mDAa0B;AAC1B,6CAA0C;AAE1C;;;GAGG;AACH,MAAa,OAAW,SAAQ,wBAAmB;IACxC,KAAK,CAAG;IACR,KAAK,GAAG,uBAAS,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,IAAA,8BAAgB,EAAC,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,IAAA,yBAAW,EAAC,KAAK,CAAC,IAAI,KAAK,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;YAC/C,OAAO,IAAA,8BAAgB,GAAE,CAAA;QAC3B,CAAC;QACD,OAAO,IAAA,8BAAgB,EACrB,OAAO,EACP,KAAK,EACL,0BAA0B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CACvD,CAAA;IACH,CAAC;CACF;AAnCD,0BAmCC;AAED;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,OAAO,CAAuB,KAAQ;IACpD,4CAA4C;IAC5C,IAAA,oBAAS,EAAC,KAAK,EAAE,yBAAW,EAAE,WAAW,EAAE,CAAC,CAAC,CAAA;IAE7C,OAAO,IAAI,OAAO,CAAI,KAAK,CAAC,CAAA;AAC9B,CAAC;AAED;;;;;GAKG;AACH,SAAgB,aAAa,CAC3B,IAAQ;IAER,OAAO,IAAA,oBAAM,EAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,uBAAS,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;AAC7D,CAAC"}
|
|
@@ -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,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.maybe = maybe;
|
|
4
|
+
exports.maybeNull = maybeNull;
|
|
5
|
+
const internal_ts_1 = require("../../internal.js");
|
|
6
|
+
const optionalUndefinedType = (0, internal_ts_1.optional)(internal_ts_1.undefinedType, undefined);
|
|
7
|
+
const optionalNullType = (0, internal_ts_1.optional)(internal_ts_1.nullType, null);
|
|
8
|
+
/**
|
|
9
|
+
* `types.maybe` - Maybe will make a type nullable, and also optional.
|
|
10
|
+
* The value `undefined` will be used to represent nullability.
|
|
11
|
+
*
|
|
12
|
+
* @param type
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
function maybe(type) {
|
|
16
|
+
(0, internal_ts_1.assertIsType)(type, 1);
|
|
17
|
+
return (0, internal_ts_1.union)(type, optionalUndefinedType);
|
|
18
|
+
}
|
|
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
|
+
function maybeNull(type) {
|
|
27
|
+
(0, internal_ts_1.assertIsType)(type, 1);
|
|
28
|
+
return (0, internal_ts_1.union)(type, optionalNullType);
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=maybe.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"maybe.js","sourceRoot":"","sources":["../../../src/types/utility-types/maybe.ts"],"names":[],"mappings":";;AAyCA,sBAIC;AASD,8BAIC;AA1DD,mDAQ0B;AAE1B,MAAM,qBAAqB,GAAG,IAAA,sBAAQ,EAAC,2BAAa,EAAE,SAAS,CAAC,CAAA;AAChE,MAAM,gBAAgB,GAAG,IAAA,sBAAQ,EAAC,sBAAQ,EAAE,IAAI,CAAC,CAAA;AAuBjD;;;;;;GAMG;AACH,SAAgB,KAAK,CAAsB,IAAQ;IACjD,IAAA,0BAAY,EAAC,IAAI,EAAE,CAAC,CAAC,CAAA;IAErB,OAAO,IAAA,mBAAK,EAAC,IAAI,EAAE,qBAAqB,CAAC,CAAA;AAC3C,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,SAAS,CAAsB,IAAQ;IACrD,IAAA,0BAAY,EAAC,IAAI,EAAE,CAAC,CAAC,CAAA;IAErB,OAAO,IAAA,mBAAK,EAAC,IAAI,EAAE,gBAAgB,CAAC,CAAA;AACtC,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type AnyObjectNode, BaseType, type ExtractCSTWithSTN, type IAnyType, type IType, type IValidationContext, type IValidationResult } from "../../internal.ts";
|
|
2
|
+
type IFunctionReturn<T> = () => T;
|
|
3
|
+
type IOptionalValue<C, T> = C | IFunctionReturn<C | T>;
|
|
4
|
+
/** @hidden */
|
|
5
|
+
export type ValidOptionalValue = string | boolean | number | null | undefined;
|
|
6
|
+
/** @hidden */
|
|
7
|
+
export type ValidOptionalValues = [ValidOptionalValue, ...ValidOptionalValue[]];
|
|
8
|
+
/**
|
|
9
|
+
* @hidden
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export declare class OptionalValue<IT extends IAnyType, OptionalVals extends ValidOptionalValues> extends BaseType<IT["CreationType"] | OptionalVals[number], IT["SnapshotType"], IT["TypeWithoutSTN"]> {
|
|
13
|
+
private readonly _subtype;
|
|
14
|
+
private readonly _defaultValue;
|
|
15
|
+
readonly optionalValues: OptionalVals;
|
|
16
|
+
get flags(): number;
|
|
17
|
+
constructor(_subtype: IT, _defaultValue: IOptionalValue<IT["CreationType"], IT["Type"]>, optionalValues: OptionalVals);
|
|
18
|
+
describe(): string;
|
|
19
|
+
instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: this["C"] | this["T"]): this["N"];
|
|
20
|
+
reconcile(current: this["N"], newValue: this["C"] | this["T"], parent: AnyObjectNode, subpath: string): this["N"];
|
|
21
|
+
getDefaultInstanceOrSnapshot(): this["C"] | this["T"];
|
|
22
|
+
isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
|
|
23
|
+
isAssignableFrom(type: IAnyType): boolean;
|
|
24
|
+
getSubTypes(): IT;
|
|
25
|
+
}
|
|
26
|
+
/** @hidden */
|
|
27
|
+
export type OptionalDefaultValueOrFunction<IT extends IAnyType> = IT["CreationType"] | IT["SnapshotType"] | (() => ExtractCSTWithSTN<IT>);
|
|
28
|
+
/** @hidden */
|
|
29
|
+
export interface IOptionalIType<IT extends IAnyType, OptionalVals extends ValidOptionalValues> extends IType<IT["CreationType"] | OptionalVals[number], IT["SnapshotType"], IT["TypeWithoutSTN"]> {
|
|
30
|
+
}
|
|
31
|
+
export declare function optional<IT extends IAnyType>(type: IT, defaultValueOrFunction: OptionalDefaultValueOrFunction<IT>): IOptionalIType<IT, [undefined]>;
|
|
32
|
+
export declare function optional<IT extends IAnyType, OptionalVals extends ValidOptionalValues>(type: IT, defaultValueOrFunction: OptionalDefaultValueOrFunction<IT>, optionalValues: OptionalVals): IOptionalIType<IT, OptionalVals>;
|
|
33
|
+
/**
|
|
34
|
+
* Returns if a value represents an optional type.
|
|
35
|
+
*
|
|
36
|
+
* @template IT
|
|
37
|
+
* @param type
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
export declare function isOptionalType<IT extends IAnyType>(type: IT): type is IT;
|
|
41
|
+
export {};
|