@jbrowse/mobx-state-tree 5.6.3 → 5.6.9
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 +2 -2
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ScalarNode = void 0;
|
|
4
|
+
const mobx_1 = require("mobx");
|
|
5
|
+
const internal_ts_1 = require("../../internal.js");
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
* @hidden
|
|
9
|
+
*/
|
|
10
|
+
class ScalarNode extends internal_ts_1.BaseNode {
|
|
11
|
+
constructor(simpleType, parent, subpath, environment, initialSnapshot) {
|
|
12
|
+
super(simpleType, parent, subpath, environment);
|
|
13
|
+
try {
|
|
14
|
+
this.storedValue = simpleType.createNewInstance(initialSnapshot);
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
// short-cut to die the instance, to avoid the snapshot computed starting to throw...
|
|
18
|
+
this.state = internal_ts_1.NodeLifeCycle.DEAD;
|
|
19
|
+
throw e;
|
|
20
|
+
}
|
|
21
|
+
this.state = internal_ts_1.NodeLifeCycle.CREATED;
|
|
22
|
+
// for scalar nodes there's no point in firing this event since it would fire on the constructor, before
|
|
23
|
+
// anybody can actually register for/listen to it
|
|
24
|
+
// this.fireHook(Hook.AfterCreate)
|
|
25
|
+
this.finalizeCreation();
|
|
26
|
+
}
|
|
27
|
+
get root() {
|
|
28
|
+
// future optimization: store root ref in the node and maintain it
|
|
29
|
+
if (!this.parent) {
|
|
30
|
+
throw (0, internal_ts_1.fail)(`This scalar node is not part of a tree`);
|
|
31
|
+
}
|
|
32
|
+
return this.parent.root;
|
|
33
|
+
}
|
|
34
|
+
setParent(newParent, subpath) {
|
|
35
|
+
const parentChanged = this.parent !== newParent;
|
|
36
|
+
const subpathChanged = this.subpath !== subpath;
|
|
37
|
+
if (!parentChanged && !subpathChanged) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if ((0, internal_ts_1.devMode)()) {
|
|
41
|
+
if (!subpath) {
|
|
42
|
+
// istanbul ignore next
|
|
43
|
+
throw (0, internal_ts_1.fail)("assertion failed: subpath expected");
|
|
44
|
+
}
|
|
45
|
+
if (!newParent) {
|
|
46
|
+
// istanbul ignore next
|
|
47
|
+
throw (0, internal_ts_1.fail)("assertion failed: parent expected");
|
|
48
|
+
}
|
|
49
|
+
if (parentChanged) {
|
|
50
|
+
// istanbul ignore next
|
|
51
|
+
throw (0, internal_ts_1.fail)("assertion failed: scalar nodes cannot change their parent");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
this.environment = undefined; // use parent's
|
|
55
|
+
this.baseSetParent(this.parent, subpath);
|
|
56
|
+
}
|
|
57
|
+
get snapshot() {
|
|
58
|
+
return (0, internal_ts_1.freeze)(this.getSnapshot());
|
|
59
|
+
}
|
|
60
|
+
getSnapshot() {
|
|
61
|
+
return this.type.getSnapshot(this);
|
|
62
|
+
}
|
|
63
|
+
toString() {
|
|
64
|
+
const path = (this.isAlive ? this.path : this.pathUponDeath) || "<root>";
|
|
65
|
+
return `${this.type.name}@${path}${this.isAlive ? "" : " [dead]"}`;
|
|
66
|
+
}
|
|
67
|
+
die() {
|
|
68
|
+
if (!this.isAlive || this.state === internal_ts_1.NodeLifeCycle.DETACHING) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
this.aboutToDie();
|
|
72
|
+
this.finalizeDeath();
|
|
73
|
+
}
|
|
74
|
+
finalizeCreation() {
|
|
75
|
+
this.baseFinalizeCreation();
|
|
76
|
+
}
|
|
77
|
+
aboutToDie() {
|
|
78
|
+
this.baseAboutToDie();
|
|
79
|
+
}
|
|
80
|
+
finalizeDeath() {
|
|
81
|
+
this.baseFinalizeDeath();
|
|
82
|
+
}
|
|
83
|
+
fireHook(name) {
|
|
84
|
+
this.fireInternalHook(name);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.ScalarNode = ScalarNode;
|
|
88
|
+
ScalarNode.prototype.die = (0, mobx_1.action)(ScalarNode.prototype.die);
|
|
89
|
+
//# sourceMappingURL=scalar-node.js.map
|
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ED,UAAU;QACR,IAAI,CAAC,cAAc,EAAE,CAAA;IACvB,CAAC;IAED,aAAa;QACX,IAAI,CAAC,iBAAiB,EAAE,CAAA;IAC1B,CAAC;IAES,QAAQ,CAAC,IAAU;QAC3B,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAA;IAC7B,CAAC;CACF;AAxGD,gCAwGC;AACD,UAAU,CAAC,SAAS,CAAC,GAAG,GAAG,IAAA,aAAM,EAAC,UAAU,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated has been renamed to `flow()`.
|
|
3
|
+
* @hidden
|
|
4
|
+
*/
|
|
5
|
+
export declare function process<R>(generator: () => IterableIterator<any>): () => Promise<R>;
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated has been renamed to `flow()`.
|
|
8
|
+
* @hidden
|
|
9
|
+
*/
|
|
10
|
+
export declare function process<A1>(generator: (a1: A1) => IterableIterator<any>): (a1: A1) => Promise<any>;
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated has been renamed to `flow()`.
|
|
13
|
+
* @hidden
|
|
14
|
+
*/
|
|
15
|
+
export declare function process<A1, A2>(generator: (a1: A1, a2: A2) => IterableIterator<any>): (a1: A1, a2: A2) => Promise<any>;
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated has been renamed to `flow()`.
|
|
18
|
+
* @hidden
|
|
19
|
+
*/
|
|
20
|
+
export declare function process<A1, A2, A3>(generator: (a1: A1, a2: A2, a3: A3) => IterableIterator<any>): (a1: A1, a2: A2, a3: A3) => Promise<any>;
|
|
21
|
+
/**
|
|
22
|
+
* @deprecated has been renamed to `flow()`.
|
|
23
|
+
* @hidden
|
|
24
|
+
*/
|
|
25
|
+
export declare function process<A1, A2, A3, A4>(generator: (a1: A1, a2: A2, a3: A3, a4: A4) => IterableIterator<any>): (a1: A1, a2: A2, a3: A3, a4: A4) => Promise<any>;
|
|
26
|
+
/**
|
|
27
|
+
* @deprecated has been renamed to `flow()`.
|
|
28
|
+
* @hidden
|
|
29
|
+
*/
|
|
30
|
+
export declare function process<A1, A2, A3, A4, A5>(generator: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5) => IterableIterator<any>): (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5) => Promise<any>;
|
|
31
|
+
/**
|
|
32
|
+
* @deprecated has been renamed to `flow()`.
|
|
33
|
+
* @hidden
|
|
34
|
+
*/
|
|
35
|
+
export declare function process<A1, A2, A3, A4, A5, A6>(generator: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6) => IterableIterator<any>): (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6) => Promise<any>;
|
|
36
|
+
/**
|
|
37
|
+
* @deprecated has been renamed to `flow()`.
|
|
38
|
+
* @hidden
|
|
39
|
+
*/
|
|
40
|
+
export declare function process<A1, A2, A3, A4, A5, A6, A7>(generator: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7) => IterableIterator<any>): (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7) => Promise<any>;
|
|
41
|
+
/**
|
|
42
|
+
* @deprecated has been renamed to `flow()`.
|
|
43
|
+
* @hidden
|
|
44
|
+
*/
|
|
45
|
+
export declare function process<A1, A2, A3, A4, A5, A6, A7, A8>(generator: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7, a8: A8) => IterableIterator<any>): (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7, a8: A8) => Promise<any>;
|
|
46
|
+
/**
|
|
47
|
+
* @internal
|
|
48
|
+
* @hidden
|
|
49
|
+
*/
|
|
50
|
+
export declare function createProcessSpawner(name: string, generator: Function): (this: any, ...flowArgs: any[]) => Promise<unknown>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.process = process;
|
|
4
|
+
exports.createProcessSpawner = createProcessSpawner;
|
|
5
|
+
const internal_ts_1 = require("../internal.js");
|
|
6
|
+
// based on: https://github.com/mobxjs/mobx-utils/blob/master/src/async-action.ts
|
|
7
|
+
/*
|
|
8
|
+
All contents of this file are deprecated.
|
|
9
|
+
|
|
10
|
+
The term `process` has been replaced with `flow` to avoid conflicts with the
|
|
11
|
+
global `process` object.
|
|
12
|
+
|
|
13
|
+
Refer to `flow.ts` for any further changes to this implementation.
|
|
14
|
+
*/
|
|
15
|
+
const DEPRECATION_MESSAGE = "See https://github.com/mobxjs/mobx-state-tree/issues/399 for more information. " +
|
|
16
|
+
"Note that the middleware event types starting with `process` now start with `flow`.";
|
|
17
|
+
/**
|
|
18
|
+
* @hidden
|
|
19
|
+
*
|
|
20
|
+
* @deprecated has been renamed to `flow()`.
|
|
21
|
+
* See https://github.com/mobxjs/mobx-state-tree/issues/399 for more information.
|
|
22
|
+
* Note that the middleware event types starting with `process` now start with `flow`.
|
|
23
|
+
*
|
|
24
|
+
* @returns {Promise}
|
|
25
|
+
*/
|
|
26
|
+
function process(asyncAction) {
|
|
27
|
+
(0, internal_ts_1.deprecated)("process", "`process()` has been renamed to `flow()`. " + DEPRECATION_MESSAGE);
|
|
28
|
+
return (0, internal_ts_1.flow)(asyncAction);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @internal
|
|
32
|
+
* @hidden
|
|
33
|
+
*/
|
|
34
|
+
function createProcessSpawner(name, generator) {
|
|
35
|
+
(0, internal_ts_1.deprecated)("process", "`createProcessSpawner()` has been renamed to `createFlowSpawner()`. " +
|
|
36
|
+
DEPRECATION_MESSAGE);
|
|
37
|
+
return (0, internal_ts_1.createFlowSpawner)(name, generator);
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=process.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.js","sourceRoot":"","sources":["../../src/core/process.ts"],"names":[],"mappings":";;AAyHA,0BAMC;AAMD,oDAOC;AA5ID,gDAAoE;AAEpE,iFAAiF;AACjF;;;;;;;EAOE;AAEF,MAAM,mBAAmB,GACvB,iFAAiF;IACjF,qFAAqF,CAAA;AAkGvF;;;;;;;;GAQG;AACH,SAAgB,OAAO,CAAC,WAAgB;IACtC,IAAA,wBAAU,EACR,SAAS,EACT,4CAA4C,GAAG,mBAAmB,CACnE,CAAA;IACD,OAAO,IAAA,kBAAI,EAAC,WAAW,CAAC,CAAA;AAC1B,CAAC;AAED;;;GAGG;AACH,SAAgB,oBAAoB,CAAC,IAAY,EAAE,SAAmB;IACpE,IAAA,wBAAU,EACR,SAAS,EACT,sEAAsE;QACpE,mBAAmB,CACtB,CAAA;IACD,OAAO,IAAA,+BAAiB,EAAC,IAAI,EAAE,SAAS,CAAC,CAAA;AAC3C,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { type ExtractCSTWithSTN, type IAnyType } from "../../internal.ts";
|
|
2
|
+
/** Validation context entry, this is, where the validation should run against which type */
|
|
3
|
+
export interface IValidationContextEntry {
|
|
4
|
+
/** Subpath where the validation should be run, or an empty string to validate it all */
|
|
5
|
+
path: string;
|
|
6
|
+
/** Type to validate the subpath against */
|
|
7
|
+
type: IAnyType;
|
|
8
|
+
}
|
|
9
|
+
/** Array of validation context entries */
|
|
10
|
+
export type IValidationContext = IValidationContextEntry[];
|
|
11
|
+
/** Type validation error */
|
|
12
|
+
export interface IValidationError {
|
|
13
|
+
/** Validation context */
|
|
14
|
+
context: IValidationContext;
|
|
15
|
+
/** Value that was being validated, either a snapshot or an instance */
|
|
16
|
+
value: any;
|
|
17
|
+
/** Error message */
|
|
18
|
+
message?: string;
|
|
19
|
+
}
|
|
20
|
+
/** Type validation result, which is an array of type validation errors */
|
|
21
|
+
export type IValidationResult = IValidationError[];
|
|
22
|
+
/**
|
|
23
|
+
* @internal
|
|
24
|
+
* @hidden
|
|
25
|
+
*/
|
|
26
|
+
export declare function prettyPrintValue(value: any): string;
|
|
27
|
+
/**
|
|
28
|
+
* @internal
|
|
29
|
+
* @hidden
|
|
30
|
+
* Pushes a new entry onto the context array (mutates in place for performance).
|
|
31
|
+
* Returns the same context array for chaining.
|
|
32
|
+
*/
|
|
33
|
+
export declare function getContextForPath(context: IValidationContext, path: string, type: IAnyType): IValidationContext;
|
|
34
|
+
/**
|
|
35
|
+
* @internal
|
|
36
|
+
* @hidden
|
|
37
|
+
* Pops the last entry from the context array (mutates in place).
|
|
38
|
+
* Must be called after validation to restore context state.
|
|
39
|
+
*/
|
|
40
|
+
export declare function popContext(context: IValidationContext): void;
|
|
41
|
+
/**
|
|
42
|
+
* @internal
|
|
43
|
+
* @hidden
|
|
44
|
+
*/
|
|
45
|
+
export declare function typeCheckSuccess(): IValidationResult;
|
|
46
|
+
/**
|
|
47
|
+
* @internal
|
|
48
|
+
* @hidden
|
|
49
|
+
*/
|
|
50
|
+
export declare function typeCheckFailure(context: IValidationContext, value: any, message?: string): IValidationResult;
|
|
51
|
+
/**
|
|
52
|
+
* @internal
|
|
53
|
+
* @hidden
|
|
54
|
+
*/
|
|
55
|
+
export declare function flattenTypeErrors(errors: IValidationResult[]): IValidationResult;
|
|
56
|
+
/**
|
|
57
|
+
* @internal
|
|
58
|
+
* @hidden
|
|
59
|
+
*/
|
|
60
|
+
export declare function typecheckInternal<IT extends IAnyType>(type: IAnyType, value: ExtractCSTWithSTN<IT>): void;
|
|
61
|
+
/**
|
|
62
|
+
* Run's the typechecker for the given type on the given value, which can be a snapshot or an instance.
|
|
63
|
+
* Throws if the given value is not according the provided type specification.
|
|
64
|
+
* Use this if you need typechecks even in a production build (by default all automatic runtime type checks will be skipped in production builds)
|
|
65
|
+
*
|
|
66
|
+
* @param type Type to check against.
|
|
67
|
+
* @param value Value to be checked, either a snapshot or an instance.
|
|
68
|
+
*/
|
|
69
|
+
export declare function typecheck<IT extends IAnyType>(type: IT, value: ExtractCSTWithSTN<IT>): void;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prettyPrintValue = prettyPrintValue;
|
|
4
|
+
exports.getContextForPath = getContextForPath;
|
|
5
|
+
exports.popContext = popContext;
|
|
6
|
+
exports.typeCheckSuccess = typeCheckSuccess;
|
|
7
|
+
exports.typeCheckFailure = typeCheckFailure;
|
|
8
|
+
exports.flattenTypeErrors = flattenTypeErrors;
|
|
9
|
+
exports.typecheckInternal = typecheckInternal;
|
|
10
|
+
exports.typecheck = typecheck;
|
|
11
|
+
const internal_ts_1 = require("../../internal.js");
|
|
12
|
+
const MAX_STRINGIFY_DEPTH = 3;
|
|
13
|
+
function safeStringify(value) {
|
|
14
|
+
try {
|
|
15
|
+
const ancestors = [];
|
|
16
|
+
return JSON.stringify(value, function (_key, val) {
|
|
17
|
+
if (val !== null && typeof val === "object") {
|
|
18
|
+
while (ancestors.length > 0 &&
|
|
19
|
+
ancestors[ancestors.length - 1] !== this) {
|
|
20
|
+
ancestors.pop();
|
|
21
|
+
}
|
|
22
|
+
if (ancestors.length >= MAX_STRINGIFY_DEPTH) {
|
|
23
|
+
return Array.isArray(val) ? "[…]" : "{…}";
|
|
24
|
+
}
|
|
25
|
+
ancestors.push(val);
|
|
26
|
+
}
|
|
27
|
+
return val;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
// istanbul ignore next
|
|
32
|
+
return `<Unserializable: ${e}>`;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @internal
|
|
37
|
+
* @hidden
|
|
38
|
+
*/
|
|
39
|
+
function prettyPrintValue(value) {
|
|
40
|
+
return typeof value === "function"
|
|
41
|
+
? `<function${value.name ? " " + value.name : ""}>`
|
|
42
|
+
: (0, internal_ts_1.isStateTreeNode)(value)
|
|
43
|
+
? `<${value}>`
|
|
44
|
+
: `\`${safeStringify(value)}\``;
|
|
45
|
+
}
|
|
46
|
+
function shortenPrintValue(valueInString) {
|
|
47
|
+
return valueInString.length < 280
|
|
48
|
+
? valueInString
|
|
49
|
+
: `${valueInString.substring(0, 272)}......${valueInString.substring(valueInString.length - 8)}`;
|
|
50
|
+
}
|
|
51
|
+
function toErrorString(error) {
|
|
52
|
+
const { value } = error;
|
|
53
|
+
const type = error.context[error.context.length - 1].type;
|
|
54
|
+
const fullPath = error.context
|
|
55
|
+
.map(({ path }) => path)
|
|
56
|
+
.filter(path => path.length > 0)
|
|
57
|
+
.join("/");
|
|
58
|
+
const pathPrefix = fullPath.length > 0 ? `at path "/${fullPath}" ` : ``;
|
|
59
|
+
const currentTypename = (0, internal_ts_1.isStateTreeNode)(value)
|
|
60
|
+
? `value of type ${(0, internal_ts_1.getStateTreeNode)(value).type.name}:`
|
|
61
|
+
: (0, internal_ts_1.isPrimitive)(value)
|
|
62
|
+
? "value"
|
|
63
|
+
: "snapshot";
|
|
64
|
+
const isSnapshotCompatible = type && (0, internal_ts_1.isStateTreeNode)(value) && type.is((0, internal_ts_1.getStateTreeNode)(value).snapshot);
|
|
65
|
+
return (`${pathPrefix}${currentTypename} ${shortenPrintValue(prettyPrintValue(value))} is not assignable ${type ? `to type: \`${type.name}\`` : ``}` +
|
|
66
|
+
(error.message ? ` (${error.message})` : "") +
|
|
67
|
+
(type
|
|
68
|
+
? (0, internal_ts_1.isPrimitiveType)(type) || (0, internal_ts_1.isPrimitive)(value)
|
|
69
|
+
? `.`
|
|
70
|
+
: `, expected an instance of \`${type.name}\` or a snapshot like \`${shortenPrintValue(type.describe())}\` instead.` +
|
|
71
|
+
(isSnapshotCompatible
|
|
72
|
+
? " (Note that a snapshot of the provided value is compatible with the targeted type)"
|
|
73
|
+
: "")
|
|
74
|
+
: `.`));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* @internal
|
|
78
|
+
* @hidden
|
|
79
|
+
* Pushes a new entry onto the context array (mutates in place for performance).
|
|
80
|
+
* Returns the same context array for chaining.
|
|
81
|
+
*/
|
|
82
|
+
function getContextForPath(context, path, type) {
|
|
83
|
+
context.push({ path, type });
|
|
84
|
+
return context;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @internal
|
|
88
|
+
* @hidden
|
|
89
|
+
* Pops the last entry from the context array (mutates in place).
|
|
90
|
+
* Must be called after validation to restore context state.
|
|
91
|
+
*/
|
|
92
|
+
function popContext(context) {
|
|
93
|
+
context.pop();
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* @internal
|
|
97
|
+
* @hidden
|
|
98
|
+
*/
|
|
99
|
+
function typeCheckSuccess() {
|
|
100
|
+
return internal_ts_1.EMPTY_ARRAY;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* @internal
|
|
104
|
+
* @hidden
|
|
105
|
+
*/
|
|
106
|
+
function typeCheckFailure(context, value, message) {
|
|
107
|
+
// Clone context since it may be mutated after this error is created
|
|
108
|
+
return [{ context: context.slice(), value, message }];
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* @internal
|
|
112
|
+
* @hidden
|
|
113
|
+
*/
|
|
114
|
+
function flattenTypeErrors(errors) {
|
|
115
|
+
return errors.flat();
|
|
116
|
+
}
|
|
117
|
+
// TODO; doublecheck: typecheck should only needed to be invoked from: type.create and array / map / value.property will change
|
|
118
|
+
/**
|
|
119
|
+
* @internal
|
|
120
|
+
* @hidden
|
|
121
|
+
*/
|
|
122
|
+
function typecheckInternal(type, value) {
|
|
123
|
+
// runs typeChecking if it is in dev-mode or through a process.env.ENABLE_TYPE_CHECK flag
|
|
124
|
+
if ((0, internal_ts_1.isTypeCheckingEnabled)()) {
|
|
125
|
+
typecheck(type, value);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Run's the typechecker for the given type on the given value, which can be a snapshot or an instance.
|
|
130
|
+
* Throws if the given value is not according the provided type specification.
|
|
131
|
+
* Use this if you need typechecks even in a production build (by default all automatic runtime type checks will be skipped in production builds)
|
|
132
|
+
*
|
|
133
|
+
* @param type Type to check against.
|
|
134
|
+
* @param value Value to be checked, either a snapshot or an instance.
|
|
135
|
+
*/
|
|
136
|
+
function typecheck(type, value) {
|
|
137
|
+
const errors = type.validate(value, [{ path: "", type }]);
|
|
138
|
+
if (errors.length > 0) {
|
|
139
|
+
throw (0, internal_ts_1.fail)(validationErrorsToString(type, value, errors));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const MAX_ERRORS_REPORTED = 10;
|
|
143
|
+
function validationErrorsToString(type, value, errors) {
|
|
144
|
+
if (errors.length === 0) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
const shown = errors.slice(0, MAX_ERRORS_REPORTED).map(toErrorString);
|
|
148
|
+
const overflow = errors.length - shown.length;
|
|
149
|
+
if (overflow > 0) {
|
|
150
|
+
shown.push(`(… and ${overflow} more error${overflow === 1 ? "" : "s"})`);
|
|
151
|
+
}
|
|
152
|
+
return (`Error while converting ${shortenPrintValue(prettyPrintValue(value))} to \`${type.name}\`:\n\n ` + shown.join("\n "));
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=type-checker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type-checker.js","sourceRoot":"","sources":["../../../src/core/type/type-checker.ts"],"names":[],"mappings":";;AAkEA,4CAMC;AAkDD,8CAOC;AAQD,gCAEC;AAMD,4CAEC;AAMD,4CAOC;AAMD,8CAIC;AAOD,8CAQC;AAUD,8BASC;AA5MD,mDAU0B;AA0B1B,MAAM,mBAAmB,GAAG,CAAC,CAAA;AAE7B,SAAS,aAAa,CAAC,KAAU;IAC/B,IAAI,CAAC;QACH,MAAM,SAAS,GAAU,EAAE,CAAA;QAC3B,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,IAAI,EAAE,GAAG;YAC9C,IAAI,GAAG,KAAK,IAAI,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,C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