@jbrowse/mobx-state-tree 5.6.0 → 5.6.2
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/README.md +18 -0
- package/dist/core/action.d.ts +1 -1
- package/dist/core/action.js +228 -0
- package/dist/core/action.js.map +1 -1
- package/dist/core/actionContext.js +42 -0
- package/dist/core/actionContext.js.map +1 -1
- package/dist/core/flow.d.ts +1 -1
- package/dist/core/flow.js +180 -0
- package/dist/core/flow.js.map +1 -1
- package/dist/core/json-patch.js +133 -0
- package/dist/core/json-patch.js.map +1 -1
- package/dist/core/mst-operations.js +885 -0
- package/dist/core/mst-operations.js.map +1 -1
- package/dist/core/node/BaseNode.js +152 -0
- package/dist/core/node/BaseNode.js.map +1 -1
- package/dist/core/node/Hook.js +15 -0
- package/dist/core/node/Hook.js.map +1 -1
- package/dist/core/node/create-node.js +41 -0
- package/dist/core/node/create-node.js.map +1 -1
- package/dist/core/node/identifier-cache.js +115 -0
- package/dist/core/node/identifier-cache.js.map +1 -1
- package/dist/core/node/livelinessChecking.js +38 -0
- package/dist/core/node/livelinessChecking.js.map +1 -1
- package/dist/core/node/node-utils.js +165 -0
- package/dist/core/node/node-utils.js.map +1 -1
- package/dist/core/node/object-node.js +545 -0
- package/dist/core/node/object-node.js.map +1 -1
- package/dist/core/node/scalar-node.js +89 -0
- package/dist/core/node/scalar-node.js.map +1 -1
- package/dist/core/process.d.ts +1 -1
- package/dist/core/process.js +39 -0
- package/dist/core/process.js.map +1 -1
- package/dist/core/type/type-checker.js +154 -0
- package/dist/core/type/type-checker.js.map +1 -1
- package/dist/core/type/type.d.ts +1 -1
- package/dist/core/type/type.js +251 -0
- package/dist/core/type/type.js.map +1 -1
- package/dist/index.js +85 -0
- package/dist/index.js.map +1 -1
- package/dist/internal.js +60 -0
- package/dist/internal.js.map +1 -1
- package/dist/middlewares/create-action-tracking-middleware.js +81 -0
- package/dist/middlewares/create-action-tracking-middleware.js.map +1 -1
- package/dist/middlewares/createActionTrackingMiddleware2.js +133 -0
- package/dist/middlewares/createActionTrackingMiddleware2.js.map +1 -1
- package/dist/middlewares/on-action.js +215 -0
- package/dist/middlewares/on-action.js.map +1 -1
- package/dist/mobx-state-tree.js +83 -6867
- package/dist/mobx-state-tree.module.js +83 -6789
- package/dist/types/complex-types/array.d.ts +1 -1
- package/dist/types/complex-types/array.js +352 -0
- package/dist/types/complex-types/array.js.map +1 -1
- package/dist/types/complex-types/map.d.ts +1 -1
- package/dist/types/complex-types/map.js +361 -0
- package/dist/types/complex-types/map.js.map +1 -1
- package/dist/types/complex-types/model.js +477 -0
- package/dist/types/complex-types/model.js.map +1 -1
- package/dist/types/index.js +38 -0
- package/dist/types/index.js.map +1 -1
- package/dist/types/primitives.js +182 -0
- package/dist/types/primitives.js.map +1 -1
- package/dist/types/utility-types/custom.js +110 -0
- package/dist/types/utility-types/custom.js.map +1 -1
- package/dist/types/utility-types/enumeration.js +34 -0
- package/dist/types/utility-types/enumeration.js.map +1 -1
- package/dist/types/utility-types/frozen.js +97 -0
- package/dist/types/utility-types/frozen.js.map +1 -1
- package/dist/types/utility-types/identifier.js +129 -0
- package/dist/types/utility-types/identifier.js.map +1 -1
- package/dist/types/utility-types/late.js +109 -0
- package/dist/types/utility-types/late.js.map +1 -1
- package/dist/types/utility-types/lazy.js +76 -0
- package/dist/types/utility-types/lazy.js.map +1 -1
- package/dist/types/utility-types/literal.js +63 -0
- package/dist/types/utility-types/literal.js.map +1 -1
- package/dist/types/utility-types/maybe.js +30 -0
- package/dist/types/utility-types/maybe.js.map +1 -1
- package/dist/types/utility-types/optional.js +140 -0
- package/dist/types/utility-types/optional.js.map +1 -1
- package/dist/types/utility-types/reference.d.ts +2 -2
- package/dist/types/utility-types/reference.js +392 -0
- package/dist/types/utility-types/reference.js.map +1 -1
- package/dist/types/utility-types/refinement.js +85 -0
- package/dist/types/utility-types/refinement.js.map +1 -1
- package/dist/types/utility-types/resilient.js +120 -0
- package/dist/types/utility-types/resilient.js.map +1 -1
- package/dist/types/utility-types/snapshotProcessor.js +161 -0
- package/dist/types/utility-types/snapshotProcessor.js.map +1 -1
- package/dist/types/utility-types/union.d.ts +1 -0
- package/dist/types/utility-types/union.js +245 -0
- package/dist/types/utility-types/union.js.map +1 -1
- package/dist/utils.js +483 -0
- package/dist/utils.js.map +1 -1
- package/package.json +25 -24
- package/changelog.md +0 -677
- package/dist/mobx-state-tree.umd.js +0 -6873
|
@@ -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
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scalar-node.js","sourceRoot":"","sources":["../../../src/core/node/scalar-node.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"scalar-node.js","sourceRoot":"","sources":["../../../src/core/node/scalar-node.ts"],"names":[],"mappings":";;;AAAA,+BAA6B;AAE7B,mDAS0B;AAE1B;;;GAGG;AACH,MAAa,UAAoB,SAAQ,sBAAiB;IASxD,YACE,UAA+B,EAC/B,MAA4B,EAC5B,OAAe,EACf,WAAgB,EAChB,eAAkB;QAElB,KAAK,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,CAAC,CAAA;QAC/C,IAAI,CAAC;YACH,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC,iBAAiB,CAAC,eAAe,CAAC,CAAA;QAClE,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,qFAAqF;YACrF,IAAI,CAAC,KAAK,GAAG,2BAAa,CAAC,IAAI,CAAA;YAC/B,MAAM,CAAC,CAAA;QACT,CAAC;QAED,IAAI,CAAC,KAAK,GAAG,2BAAa,CAAC,OAAO,CAAA;QAClC,wGAAwG;QACxG,iDAAiD;QACjD,kCAAkC;QAElC,IAAI,CAAC,gBAAgB,EAAE,CAAA;IACzB,CAAC;IAED,IAAI,IAAI;QACN,kEAAkE;QAClE,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,MAAM,IAAA,kBAAI,EAAC,wCAAwC,CAAC,CAAA;QACtD,C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AAG,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,EAAE,CAAA;IACpE,CAAC;IAED,GAAG;QACD,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,KAAK,KAAK,2BAAa,CAAC,SAAS,EAAE,CAAC;YAC5D,OAAM;QACR,CAAC;QACD,IAAI,CAAC,UAAU,EAAE,CAAA;QACjB,IAAI,CAAC,aAAa,EAAE,CAAA;IACtB,CAAC;IAED,gBAAgB;QACd,IAAI,CAAC,oBAAoB,EAAE,CAAA;IAC7B,CAAC;IAED,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"}
|
package/dist/core/process.d.ts
CHANGED
|
@@ -47,4 +47,4 @@ export declare function process<A1, A2, A3, A4, A5, A6, A7, A8>(generator: (a1:
|
|
|
47
47
|
* @internal
|
|
48
48
|
* @hidden
|
|
49
49
|
*/
|
|
50
|
-
export declare function createProcessSpawner(name: string, generator: Function): (this: any) => Promise<unknown>;
|
|
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
|
package/dist/core/process.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process.js","sourceRoot":"","sources":["../../src/core/process.ts"],"names":[],"mappings":"
|
|
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,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
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-checker.js","sourceRoot":"","sources":["../../../src/core/type/type-checker.ts"],"names":[],"mappings":"
|
|
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,CAAC;gBAC5C,OACE,SAAS,CAAC,MAAM,GAAG,CAAC;oBACpB,SAAS,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,IAAI,EACxC,CAAC;oBACD,SAAS,CAAC,GAAG,EAAE,CAAA;gBACjB,CAAC;gBACD,IAAI,SAAS,CAAC,MAAM,IAAI,mBAAmB,EAAE,CAAC;oBAC5C,OAAO,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAA;gBAC3C,CAAC;gBACD,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;YACrB,CAAC;YACD,OAAO,GAAG,CAAA;QACZ,CAAC,CAAC,CAAA;IACJ,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,uBAAuB;QACvB,OAAO,oBAAoB,CAAC,GAAG,CAAA;IACjC,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,KAAU;IACzC,OAAO,OAAO,KAAK,KAAK,UAAU;Q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E,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAA;IAC7C,IAAI,QAAQ,GAAG,CAAC,EAAE,CAAC;QACjB,KAAK,CAAC,IAAI,CAAC,UAAU,QAAQ,cAAc,QAAQ,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAA;IAC1E,CAAC;IAED,OAAO,CACL,0BAA0B,iBAAiB,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,SAClE,IAAI,CAAC,IACP,aAAa,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CACrC,CAAA;AACH,CAAC"}
|
package/dist/core/type/type.d.ts
CHANGED
|
@@ -238,7 +238,7 @@ export declare abstract class BaseType<C, S, T, N extends BaseNode<any, any, any
|
|
|
238
238
|
readonly name: string;
|
|
239
239
|
constructor(name: string);
|
|
240
240
|
create(snapshot?: C, environment?: any): any;
|
|
241
|
-
getSnapshot(
|
|
241
|
+
getSnapshot(_node: N, _applyPostProcess?: boolean): S;
|
|
242
242
|
abstract reconcile(current: N, newValue: C | T, parent: AnyObjectNode, subpath: string): N;
|
|
243
243
|
abstract instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: C | T): N;
|
|
244
244
|
abstract flags: TypeFlags;
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SimpleType = exports.ComplexType = exports.BaseType = exports.cannotDetermineSubtype = exports.TypeFlags = void 0;
|
|
4
|
+
exports.isType = isType;
|
|
5
|
+
exports.assertIsType = assertIsType;
|
|
6
|
+
const mobx_1 = require("mobx");
|
|
7
|
+
const internal_ts_1 = require("../../internal.js");
|
|
8
|
+
// Cache for validation results to avoid re-validating the same object against the same type
|
|
9
|
+
// Uses WeakMap so cached objects can be garbage collected
|
|
10
|
+
const validationCache = new WeakMap();
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
* @hidden
|
|
14
|
+
*/
|
|
15
|
+
var TypeFlags;
|
|
16
|
+
(function (TypeFlags) {
|
|
17
|
+
TypeFlags[TypeFlags["String"] = 1] = "String";
|
|
18
|
+
TypeFlags[TypeFlags["Number"] = 2] = "Number";
|
|
19
|
+
TypeFlags[TypeFlags["Boolean"] = 4] = "Boolean";
|
|
20
|
+
TypeFlags[TypeFlags["Date"] = 8] = "Date";
|
|
21
|
+
TypeFlags[TypeFlags["Literal"] = 16] = "Literal";
|
|
22
|
+
TypeFlags[TypeFlags["Array"] = 32] = "Array";
|
|
23
|
+
TypeFlags[TypeFlags["Map"] = 64] = "Map";
|
|
24
|
+
TypeFlags[TypeFlags["Object"] = 128] = "Object";
|
|
25
|
+
TypeFlags[TypeFlags["Frozen"] = 256] = "Frozen";
|
|
26
|
+
TypeFlags[TypeFlags["Optional"] = 512] = "Optional";
|
|
27
|
+
TypeFlags[TypeFlags["Reference"] = 1024] = "Reference";
|
|
28
|
+
TypeFlags[TypeFlags["Identifier"] = 2048] = "Identifier";
|
|
29
|
+
TypeFlags[TypeFlags["Late"] = 4096] = "Late";
|
|
30
|
+
TypeFlags[TypeFlags["Refinement"] = 8192] = "Refinement";
|
|
31
|
+
TypeFlags[TypeFlags["Union"] = 16384] = "Union";
|
|
32
|
+
TypeFlags[TypeFlags["Null"] = 32768] = "Null";
|
|
33
|
+
TypeFlags[TypeFlags["Undefined"] = 65536] = "Undefined";
|
|
34
|
+
TypeFlags[TypeFlags["Integer"] = 131072] = "Integer";
|
|
35
|
+
TypeFlags[TypeFlags["Custom"] = 262144] = "Custom";
|
|
36
|
+
TypeFlags[TypeFlags["SnapshotProcessor"] = 524288] = "SnapshotProcessor";
|
|
37
|
+
TypeFlags[TypeFlags["Lazy"] = 1048576] = "Lazy";
|
|
38
|
+
TypeFlags[TypeFlags["Finite"] = 2097152] = "Finite";
|
|
39
|
+
TypeFlags[TypeFlags["Float"] = 4194304] = "Float";
|
|
40
|
+
})(TypeFlags || (exports.TypeFlags = TypeFlags = {}));
|
|
41
|
+
/**
|
|
42
|
+
* @internal
|
|
43
|
+
* @hidden
|
|
44
|
+
*/
|
|
45
|
+
exports.cannotDetermineSubtype = "cannotDetermine";
|
|
46
|
+
/** @hidden */
|
|
47
|
+
const $type = Symbol("$type");
|
|
48
|
+
/**
|
|
49
|
+
* A base type produces a MST node (Node in the state tree)
|
|
50
|
+
*
|
|
51
|
+
* @internal
|
|
52
|
+
* @hidden
|
|
53
|
+
*/
|
|
54
|
+
class BaseType {
|
|
55
|
+
[$type];
|
|
56
|
+
// these are just to make inner types avaialable to inherited classes
|
|
57
|
+
C;
|
|
58
|
+
S;
|
|
59
|
+
T;
|
|
60
|
+
N;
|
|
61
|
+
isType = true;
|
|
62
|
+
name;
|
|
63
|
+
constructor(name) {
|
|
64
|
+
this.name = name;
|
|
65
|
+
}
|
|
66
|
+
create(snapshot, environment) {
|
|
67
|
+
(0, internal_ts_1.typecheckInternal)(this, snapshot);
|
|
68
|
+
return this.instantiate(null, "", environment, snapshot).value;
|
|
69
|
+
}
|
|
70
|
+
getSnapshot(_node, _applyPostProcess) {
|
|
71
|
+
// istanbul ignore next
|
|
72
|
+
throw (0, internal_ts_1.fail)("unimplemented method");
|
|
73
|
+
}
|
|
74
|
+
isAssignableFrom(type) {
|
|
75
|
+
return type === this;
|
|
76
|
+
}
|
|
77
|
+
validate(value, context) {
|
|
78
|
+
const node = (0, internal_ts_1.getStateTreeNodeSafe)(value);
|
|
79
|
+
if (node) {
|
|
80
|
+
const valueType = (0, internal_ts_1.getType)(value);
|
|
81
|
+
return this.isAssignableFrom(valueType)
|
|
82
|
+
? (0, internal_ts_1.typeCheckSuccess)()
|
|
83
|
+
: (0, internal_ts_1.typeCheckFailure)(context, value);
|
|
84
|
+
// it is tempting to compare snapshots, but in that case we should always clone on assignments...
|
|
85
|
+
}
|
|
86
|
+
// check cache for object values (only at root level to avoid context mismatches)
|
|
87
|
+
if (typeof value === "object" && value !== null && context.length === 1) {
|
|
88
|
+
const typeCache = validationCache.get(value);
|
|
89
|
+
if (typeCache) {
|
|
90
|
+
const cached = typeCache.get(this);
|
|
91
|
+
if (cached !== undefined) {
|
|
92
|
+
return cached;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const result = this.isValidSnapshot(value, context);
|
|
97
|
+
// cache result for object values (only at root level)
|
|
98
|
+
if (typeof value === "object" && value !== null && context.length === 1) {
|
|
99
|
+
let typeCache = validationCache.get(value);
|
|
100
|
+
if (!typeCache) {
|
|
101
|
+
typeCache = new WeakMap();
|
|
102
|
+
validationCache.set(value, typeCache);
|
|
103
|
+
}
|
|
104
|
+
typeCache.set(this, result);
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
is(thing) {
|
|
109
|
+
return this.validate(thing, [{ path: "", type: this }]).length === 0;
|
|
110
|
+
}
|
|
111
|
+
get Type() {
|
|
112
|
+
// istanbul ignore next
|
|
113
|
+
throw (0, internal_ts_1.fail)("Factory.Type should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.Type`");
|
|
114
|
+
}
|
|
115
|
+
get TypeWithoutSTN() {
|
|
116
|
+
// istanbul ignore next
|
|
117
|
+
throw (0, internal_ts_1.fail)("Factory.TypeWithoutSTN should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.TypeWithoutSTN`");
|
|
118
|
+
}
|
|
119
|
+
get SnapshotType() {
|
|
120
|
+
// istanbul ignore next
|
|
121
|
+
throw (0, internal_ts_1.fail)("Factory.SnapshotType should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.SnapshotType`");
|
|
122
|
+
}
|
|
123
|
+
get CreationType() {
|
|
124
|
+
// istanbul ignore next
|
|
125
|
+
throw (0, internal_ts_1.fail)("Factory.CreationType should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.CreationType`");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
exports.BaseType = BaseType;
|
|
129
|
+
BaseType.prototype.create = (0, mobx_1.action)(BaseType.prototype.create);
|
|
130
|
+
/**
|
|
131
|
+
* A complex type produces a MST node (Node in the state tree)
|
|
132
|
+
*
|
|
133
|
+
* @internal
|
|
134
|
+
* @hidden
|
|
135
|
+
*/
|
|
136
|
+
class ComplexType extends BaseType {
|
|
137
|
+
identifierAttribute;
|
|
138
|
+
constructor(name) {
|
|
139
|
+
super(name);
|
|
140
|
+
}
|
|
141
|
+
create(snapshot = this.getDefaultSnapshot(), environment) {
|
|
142
|
+
return super.create(snapshot, environment);
|
|
143
|
+
}
|
|
144
|
+
getValue(node) {
|
|
145
|
+
node.createObservableInstanceIfNeeded();
|
|
146
|
+
return node.storedValue;
|
|
147
|
+
}
|
|
148
|
+
isMatchingSnapshotId(current, snapshot) {
|
|
149
|
+
return (!current.identifierAttribute ||
|
|
150
|
+
current.identifier ===
|
|
151
|
+
(0, internal_ts_1.normalizeIdentifier)(snapshot[current.identifierAttribute]));
|
|
152
|
+
}
|
|
153
|
+
tryToReconcileNode(current, newValue) {
|
|
154
|
+
if (current.isDetaching) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
if (current.snapshot === newValue) {
|
|
158
|
+
// newValue is the current snapshot of the node, noop
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
if ((0, internal_ts_1.isStateTreeNode)(newValue) && (0, internal_ts_1.getStateTreeNode)(newValue) === current) {
|
|
162
|
+
// the current node is the same as the new one
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
if (current.type === this &&
|
|
166
|
+
(0, internal_ts_1.isMutable)(newValue) &&
|
|
167
|
+
!(0, internal_ts_1.isStateTreeNode)(newValue) &&
|
|
168
|
+
this.isMatchingSnapshotId(current, newValue)) {
|
|
169
|
+
// the newValue has no node, so can be treated like a snapshot
|
|
170
|
+
// we can reconcile
|
|
171
|
+
current.applySnapshot(newValue);
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
reconcile(current, newValue, parent, subpath) {
|
|
177
|
+
const nodeReconciled = this.tryToReconcileNode(current, newValue);
|
|
178
|
+
if (nodeReconciled) {
|
|
179
|
+
current.setParent(parent, subpath);
|
|
180
|
+
return current;
|
|
181
|
+
}
|
|
182
|
+
// current node cannot be recycled in any way
|
|
183
|
+
current.die(); // noop if detaching
|
|
184
|
+
// attempt to reuse the new one
|
|
185
|
+
if ((0, internal_ts_1.isStateTreeNode)(newValue) && this.isAssignableFrom((0, internal_ts_1.getType)(newValue))) {
|
|
186
|
+
// newValue is a Node as well, move it here..
|
|
187
|
+
const newNode = (0, internal_ts_1.getStateTreeNode)(newValue);
|
|
188
|
+
newNode.setParent(parent, subpath);
|
|
189
|
+
return newNode;
|
|
190
|
+
}
|
|
191
|
+
// nothing to do, we have to create a new node
|
|
192
|
+
return this.instantiate(parent, subpath, undefined, newValue);
|
|
193
|
+
}
|
|
194
|
+
getSubTypes() {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
exports.ComplexType = ComplexType;
|
|
199
|
+
ComplexType.prototype.create = (0, mobx_1.action)(ComplexType.prototype.create);
|
|
200
|
+
/**
|
|
201
|
+
* @internal
|
|
202
|
+
* @hidden
|
|
203
|
+
*/
|
|
204
|
+
class SimpleType extends BaseType {
|
|
205
|
+
createNewInstance(snapshot) {
|
|
206
|
+
return snapshot;
|
|
207
|
+
}
|
|
208
|
+
getValue(node) {
|
|
209
|
+
// if we ever find a case where scalar nodes can be accessed without iterating through its parent
|
|
210
|
+
// uncomment this to make sure the parent chain is created when this is accessed
|
|
211
|
+
// if (node.parent) {
|
|
212
|
+
// node.parent.createObservableInstanceIfNeeded()
|
|
213
|
+
// }
|
|
214
|
+
return node.storedValue;
|
|
215
|
+
}
|
|
216
|
+
getSnapshot(node) {
|
|
217
|
+
return node.storedValue;
|
|
218
|
+
}
|
|
219
|
+
reconcile(current, newValue, parent, subpath) {
|
|
220
|
+
// reconcile only if type and value are still the same, and only if the node is not detaching
|
|
221
|
+
if (!current.isDetaching &&
|
|
222
|
+
current.type === this &&
|
|
223
|
+
current.storedValue === newValue) {
|
|
224
|
+
return current;
|
|
225
|
+
}
|
|
226
|
+
const res = this.instantiate(parent, subpath, undefined, newValue);
|
|
227
|
+
current.die(); // noop if detaching
|
|
228
|
+
return res;
|
|
229
|
+
}
|
|
230
|
+
getSubTypes() {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
exports.SimpleType = SimpleType;
|
|
235
|
+
/**
|
|
236
|
+
* Returns if a given value represents a type.
|
|
237
|
+
*
|
|
238
|
+
* @param value Value to check.
|
|
239
|
+
* @returns `true` if the value is a type.
|
|
240
|
+
*/
|
|
241
|
+
function isType(value) {
|
|
242
|
+
return typeof value === "object" && value && value.isType === true;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* @internal
|
|
246
|
+
* @hidden
|
|
247
|
+
*/
|
|
248
|
+
function assertIsType(type, argNumber) {
|
|
249
|
+
(0, internal_ts_1.assertArg)(type, isType, "mobx-state-tree type", argNumber);
|
|
250
|
+
}
|
|
251
|
+
//# sourceMappingURL=type.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../../src/core/type/type.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../../src/core/type/type.ts"],"names":[],"mappings":";;;AAinBA,wBAEC;AAMD,oCAEC;AA3nBD,+BAA6B;AAE7B,mDAa0B;AAE1B,4FAA4F;AAC5F,0DAA0D;AAC1D,MAAM,eAAe,GAAG,IAAI,OAAO,EAGhC,CAAA;AAeH;;;GAGG;AACH,IAAY,SAwBX;AAxBD,WAAY,SAAS;IACnB,6CAAU,CAAA;IACV,6CAAe,CAAA;IACf,+CAAgB,CAAA;IAChB,yCAA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