mobx-state-tree 7.0.0-pre.2 → 7.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mobx-state-tree.js +62 -34
- package/dist/mobx-state-tree.min.js +1 -1
- package/dist/mobx-state-tree.module.js +62 -34
- package/dist/mobx-state-tree.umd.js +62 -34
- package/dist/mobx-state-tree.umd.min.js +1 -1
- package/dist/types/complex-types/model.d.ts +1 -1
- package/dist/types/utility-types/union.d.ts +13 -4
- package/package.json +111 -109
package/dist/mobx-state-tree.js
CHANGED
|
@@ -63,7 +63,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
63
63
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
64
64
|
PERFORMANCE OF THIS SOFTWARE.
|
|
65
65
|
***************************************************************************** */
|
|
66
|
-
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
66
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
67
67
|
|
|
68
68
|
var extendStatics = function(d, b) {
|
|
69
69
|
extendStatics = Object.setPrototypeOf ||
|
|
@@ -104,8 +104,8 @@ function __rest(s, e) {
|
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
function __generator(thisArg, body) {
|
|
107
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
108
|
-
return g =
|
|
107
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
108
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
109
109
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
110
110
|
function step(op) {
|
|
111
111
|
if (f) throw new TypeError("Generator is already executing.");
|
|
@@ -1801,7 +1801,9 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1801
1801
|
if (!this.parent && newParent.root === this) {
|
|
1802
1802
|
throw new MstError("A state tree is not allowed to contain itself. Cannot assign ".concat(this, " to path '").concat(newParent.path, "/").concat(subpath, "'"));
|
|
1803
1803
|
}
|
|
1804
|
-
if (!this.parent &&
|
|
1804
|
+
if (!this.parent &&
|
|
1805
|
+
!!this.environment &&
|
|
1806
|
+
this.environment !== newParent.root.environment) {
|
|
1805
1807
|
throw new MstError("A state tree cannot be made part of another state tree as long as their environments are different.");
|
|
1806
1808
|
}
|
|
1807
1809
|
}
|
|
@@ -1825,7 +1827,9 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1825
1827
|
value: function (name) {
|
|
1826
1828
|
var _this = this;
|
|
1827
1829
|
this.fireInternalHook(name);
|
|
1828
|
-
var fn = this.storedValue &&
|
|
1830
|
+
var fn = this.storedValue &&
|
|
1831
|
+
typeof this.storedValue === "object" &&
|
|
1832
|
+
this.storedValue[name];
|
|
1829
1833
|
if (typeof fn === "function") {
|
|
1830
1834
|
// we check for it to allow old mobx peer dependencies that don't have the method to work (even when still bugged)
|
|
1831
1835
|
if (mobx._allowStateChangesInsideComputed) {
|
|
@@ -1930,7 +1934,8 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1930
1934
|
var actionFullPath = "";
|
|
1931
1935
|
if (actionContext && actionContext.name != null) {
|
|
1932
1936
|
// try to use the context, and if it not available use the node one
|
|
1933
|
-
var actionPath = (actionContext && actionContext.context && getPath(actionContext.context)) ||
|
|
1937
|
+
var actionPath = (actionContext && actionContext.context && getPath(actionContext.context)) ||
|
|
1938
|
+
escapedPath;
|
|
1934
1939
|
actionFullPath = "".concat(actionPath, ".").concat(actionContext.name, "()");
|
|
1935
1940
|
}
|
|
1936
1941
|
return "You are trying to read or write to an object that is no longer part of a state tree. (Object type: '".concat(this.type.name, "', Path upon death: '").concat(escapedPath, "', Subpath: '").concat(subpath, "', Action: '").concat(actionFullPath, "'). Either detach nodes first, or don't use objects after removing / replacing them in the tree.");
|
|
@@ -2565,7 +2570,8 @@ var ComplexType = /** @class */ (function (_super) {
|
|
|
2565
2570
|
writable: true,
|
|
2566
2571
|
value: function (current, snapshot) {
|
|
2567
2572
|
return (!current.identifierAttribute ||
|
|
2568
|
-
current.identifier ===
|
|
2573
|
+
current.identifier ===
|
|
2574
|
+
normalizeIdentifier(snapshot[current.identifierAttribute]));
|
|
2569
2575
|
}
|
|
2570
2576
|
});
|
|
2571
2577
|
Object.defineProperty(ComplexType.prototype, "tryToReconcileNode", {
|
|
@@ -2963,7 +2969,8 @@ function serializeArgument(node, actionName, index, arg) {
|
|
|
2963
2969
|
if (typeof arg === "function")
|
|
2964
2970
|
return serializeTheUnserializable("[function]");
|
|
2965
2971
|
if (typeof arg === "object" && !isPlainObject(arg) && !isArray(arg))
|
|
2966
|
-
return serializeTheUnserializable("[object ".concat((arg && arg.constructor && arg.constructor.name) ||
|
|
2972
|
+
return serializeTheUnserializable("[object ".concat((arg && arg.constructor && arg.constructor.name) ||
|
|
2973
|
+
"Complex Object", "]"));
|
|
2967
2974
|
try {
|
|
2968
2975
|
// Check if serializable, cycle free etc...
|
|
2969
2976
|
// MWE: there must be a better way....
|
|
@@ -3219,7 +3226,8 @@ function createActionInvoker(target, name, fn) {
|
|
|
3219
3226
|
tree: getRoot(target),
|
|
3220
3227
|
rootId: parentContext ? parentContext.rootId : id,
|
|
3221
3228
|
parentId: parentContext ? parentContext.id : 0,
|
|
3222
|
-
allParentIds: parentContext
|
|
3229
|
+
allParentIds: parentContext
|
|
3230
|
+
? __spreadArray(__spreadArray([], __read(parentContext.allParentIds), false), [parentContext.id], false) : [],
|
|
3223
3231
|
parentEvent: parentContext,
|
|
3224
3232
|
parentActionEvent: parentActionContext
|
|
3225
3233
|
}, fn);
|
|
@@ -3813,7 +3821,7 @@ function getRelativePathBetweenNodes(base, target) {
|
|
|
3813
3821
|
break;
|
|
3814
3822
|
}
|
|
3815
3823
|
// TODO: assert that no targetParts paths are "..", "." or ""!
|
|
3816
|
-
return baseParts.slice(common).map(doubleDot).join("/") + joinJsonPath(targetParts.slice(common));
|
|
3824
|
+
return (baseParts.slice(common).map(doubleDot).join("/") + joinJsonPath(targetParts.slice(common)));
|
|
3817
3825
|
}
|
|
3818
3826
|
/**
|
|
3819
3827
|
* @internal
|
|
@@ -4061,7 +4069,8 @@ function deepFreeze(value) {
|
|
|
4061
4069
|
freeze(value);
|
|
4062
4070
|
if (isPlainObject(value)) {
|
|
4063
4071
|
Object.keys(value).forEach(function (propKey) {
|
|
4064
|
-
if (!isPrimitive(value[propKey]) &&
|
|
4072
|
+
if (!isPrimitive(value[propKey]) &&
|
|
4073
|
+
!Object.isFrozen(value[propKey])) {
|
|
4065
4074
|
deepFreeze(value[propKey]);
|
|
4066
4075
|
}
|
|
4067
4076
|
});
|
|
@@ -6129,11 +6138,16 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6129
6138
|
configurable: true,
|
|
6130
6139
|
writable: true,
|
|
6131
6140
|
value: function (self, actions) {
|
|
6141
|
+
var _this = this;
|
|
6132
6142
|
// check if return is correct
|
|
6133
|
-
if (!isPlainObject(actions))
|
|
6143
|
+
if (!isPlainObject(actions)) {
|
|
6134
6144
|
throw new MstError("actions initializer should return a plain object containing actions");
|
|
6145
|
+
}
|
|
6135
6146
|
// bind actions to the object created
|
|
6136
|
-
Object.
|
|
6147
|
+
Object.getOwnPropertyNames(actions).forEach(function (name) {
|
|
6148
|
+
if (name in _this.properties) {
|
|
6149
|
+
throw new MstError("'".concat(name, "' is a property and cannot be declared as an action"));
|
|
6150
|
+
}
|
|
6137
6151
|
// warn if preprocessor was given
|
|
6138
6152
|
if (name === PRE_PROCESS_SNAPSHOT)
|
|
6139
6153
|
throw new MstError("Cannot define action '".concat(PRE_PROCESS_SNAPSHOT, "', it should be defined using 'type.preProcessSnapshot(fn)' instead"));
|
|
@@ -6183,10 +6197,17 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6183
6197
|
configurable: true,
|
|
6184
6198
|
writable: true,
|
|
6185
6199
|
value: function (self, state) {
|
|
6200
|
+
var _this = this;
|
|
6186
6201
|
// check views return
|
|
6187
|
-
if (!isPlainObject(state))
|
|
6202
|
+
if (!isPlainObject(state)) {
|
|
6188
6203
|
throw new MstError("volatile state initializer should return a plain object containing state");
|
|
6189
|
-
|
|
6204
|
+
}
|
|
6205
|
+
Object.getOwnPropertyNames(state).forEach(function (name) {
|
|
6206
|
+
if (name in _this.properties) {
|
|
6207
|
+
throw new MstError("'".concat(name, "' is a property and cannot be declared as volatile state"));
|
|
6208
|
+
}
|
|
6209
|
+
mobx.set(self, name, state[name]);
|
|
6210
|
+
});
|
|
6190
6211
|
}
|
|
6191
6212
|
});
|
|
6192
6213
|
Object.defineProperty(ModelType.prototype, "extend", {
|
|
@@ -6228,19 +6249,24 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6228
6249
|
configurable: true,
|
|
6229
6250
|
writable: true,
|
|
6230
6251
|
value: function (self, views) {
|
|
6252
|
+
var _this = this;
|
|
6231
6253
|
// check views return
|
|
6232
|
-
if (!isPlainObject(views))
|
|
6254
|
+
if (!isPlainObject(views)) {
|
|
6233
6255
|
throw new MstError("views initializer should return a plain object containing views");
|
|
6234
|
-
|
|
6256
|
+
}
|
|
6257
|
+
Object.getOwnPropertyNames(views).forEach(function (name) {
|
|
6235
6258
|
var _a;
|
|
6259
|
+
if (name in _this.properties) {
|
|
6260
|
+
throw new MstError("'".concat(name, "' is a property and cannot be declared as a view"));
|
|
6261
|
+
}
|
|
6236
6262
|
// is this a computed property?
|
|
6237
|
-
var descriptor = Object.getOwnPropertyDescriptor(views,
|
|
6263
|
+
var descriptor = Object.getOwnPropertyDescriptor(views, name);
|
|
6238
6264
|
if ("get" in descriptor) {
|
|
6239
|
-
mobx.defineProperty(self,
|
|
6240
|
-
mobx.makeObservable(self, (_a = {}, _a[
|
|
6265
|
+
mobx.defineProperty(self, name, descriptor);
|
|
6266
|
+
mobx.makeObservable(self, (_a = {}, _a[name] = mobx.computed, _a));
|
|
6241
6267
|
}
|
|
6242
6268
|
else if (typeof descriptor.value === "function") {
|
|
6243
|
-
(!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(self,
|
|
6269
|
+
(!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(self, name, descriptor.value);
|
|
6244
6270
|
}
|
|
6245
6271
|
else {
|
|
6246
6272
|
throw new MstError("A view member should either be a function or getter based property");
|
|
@@ -6376,14 +6402,9 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6376
6402
|
if (applyPostProcess === void 0) { applyPostProcess = true; }
|
|
6377
6403
|
var res = {};
|
|
6378
6404
|
this.forAllProps(function (name, type) {
|
|
6379
|
-
|
|
6380
|
-
|
|
6381
|
-
|
|
6382
|
-
atom.reportObserved();
|
|
6383
|
-
}
|
|
6384
|
-
catch (e) {
|
|
6385
|
-
throw new MstError("".concat(name, " property is declared twice"));
|
|
6386
|
-
}
|
|
6405
|
+
// TODO: FIXME, make sure the observable ref is used!
|
|
6406
|
+
var atom = mobx.getAtom(node.storedValue, name);
|
|
6407
|
+
atom.reportObserved();
|
|
6387
6408
|
res[name] = _this.getChildNode(node, name).snapshot;
|
|
6388
6409
|
});
|
|
6389
6410
|
if (applyPostProcess) {
|
|
@@ -6976,7 +6997,9 @@ function refinement() {
|
|
|
6976
6997
|
var name = typeof args[0] === "string" ? args.shift() : isType(args[0]) ? args[0].name : null;
|
|
6977
6998
|
var type = args[0];
|
|
6978
6999
|
var predicate = args[1];
|
|
6979
|
-
var message = args[2]
|
|
7000
|
+
var message = args[2]
|
|
7001
|
+
? args[2]
|
|
7002
|
+
: function (v) { return "Value does not respect the refinement predicate"; };
|
|
6980
7003
|
// ensures all parameters are correct
|
|
6981
7004
|
assertIsType(type, [1, 2]);
|
|
6982
7005
|
assertIsString(name, 1);
|
|
@@ -7177,7 +7200,7 @@ function union(optionsOrType) {
|
|
|
7177
7200
|
otherTypes[_i - 1] = arguments[_i];
|
|
7178
7201
|
}
|
|
7179
7202
|
var options = isType(optionsOrType) ? undefined : optionsOrType;
|
|
7180
|
-
var types = isType(optionsOrType) ? __spreadArray([optionsOrType], __read(otherTypes), false) : otherTypes;
|
|
7203
|
+
var types = (isType(optionsOrType) ? __spreadArray([optionsOrType], __read(otherTypes), false) : otherTypes);
|
|
7181
7204
|
var name = "(" + types.map(function (type) { return type.name; }).join(" | ") + ")";
|
|
7182
7205
|
// check all options
|
|
7183
7206
|
if (devMode()) {
|
|
@@ -7598,7 +7621,8 @@ var Lazy = /** @class */ (function (_super) {
|
|
|
7598
7621
|
mobx.when(function () {
|
|
7599
7622
|
return _this.pendingNodeList.length > 0 &&
|
|
7600
7623
|
_this.pendingNodeList.some(function (node) {
|
|
7601
|
-
return node.isAlive &&
|
|
7624
|
+
return node.isAlive &&
|
|
7625
|
+
_this.options.shouldLoadPredicate(node.parent ? node.parent.value : null);
|
|
7602
7626
|
});
|
|
7603
7627
|
}, function () {
|
|
7604
7628
|
_this.options.loadType().then(mobx.action(function (type) {
|
|
@@ -8112,7 +8136,9 @@ var IdentifierReferenceType = /** @class */ (function (_super) {
|
|
|
8112
8136
|
configurable: true,
|
|
8113
8137
|
writable: true,
|
|
8114
8138
|
value: function (parent, subpath, environment, initialValue) {
|
|
8115
|
-
var identifier = isStateTreeNode(initialValue)
|
|
8139
|
+
var identifier = isStateTreeNode(initialValue)
|
|
8140
|
+
? getIdentifier(initialValue)
|
|
8141
|
+
: initialValue;
|
|
8116
8142
|
var storedRef = new StoredReference(initialValue, this.targetType);
|
|
8117
8143
|
var storedRefNode = createScalarNode(this, parent, subpath, environment, storedRef);
|
|
8118
8144
|
storedRef.node = storedRefNode;
|
|
@@ -8197,7 +8223,9 @@ var CustomReferenceType = /** @class */ (function (_super) {
|
|
|
8197
8223
|
var newIdentifier = isStateTreeNode(newValue)
|
|
8198
8224
|
? this.options.set(newValue, current ? current.storedValue : null)
|
|
8199
8225
|
: newValue;
|
|
8200
|
-
if (!current.isDetaching &&
|
|
8226
|
+
if (!current.isDetaching &&
|
|
8227
|
+
current.type === this &&
|
|
8228
|
+
current.storedValue === newIdentifier) {
|
|
8201
8229
|
current.setParent(parent, subpath);
|
|
8202
8230
|
return current;
|
|
8203
8231
|
}
|