mobx-state-tree 5.2.0 → 5.3.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Michel Weststrate
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -279,7 +279,7 @@ function recordPatches(subject, filter) {
279
279
  assertIsStateTreeNode(subject, 1);
280
280
  var data = {
281
281
  patches: [],
282
- reversedInversePatches: []
282
+ inversePatches: []
283
283
  };
284
284
  // we will generate the immutable copy of patches on demand for public consumption
285
285
  var publicData = {};
@@ -296,13 +296,13 @@ function recordPatches(subject, filter) {
296
296
  },
297
297
  get reversedInversePatches() {
298
298
  if (!publicData.reversedInversePatches) {
299
- publicData.reversedInversePatches = data.reversedInversePatches.slice();
299
+ publicData.reversedInversePatches = data.inversePatches.slice().reverse();
300
300
  }
301
301
  return publicData.reversedInversePatches;
302
302
  },
303
303
  get inversePatches() {
304
304
  if (!publicData.inversePatches) {
305
- publicData.inversePatches = data.reversedInversePatches.slice().reverse();
305
+ publicData.inversePatches = data.inversePatches.slice();
306
306
  }
307
307
  return publicData.inversePatches;
308
308
  },
@@ -321,7 +321,7 @@ function recordPatches(subject, filter) {
321
321
  return;
322
322
  }
323
323
  data.patches.push(patch);
324
- data.reversedInversePatches.unshift(inversePatch);
324
+ data.inversePatches.push(inversePatch);
325
325
  // mark immutable public patches as dirty
326
326
  publicData.patches = undefined;
327
327
  publicData.inversePatches = undefined;
@@ -332,7 +332,7 @@ function recordPatches(subject, filter) {
332
332
  applyPatch(target || subject, data.patches);
333
333
  },
334
334
  undo: function (target) {
335
- applyPatch(target || subject, data.reversedInversePatches);
335
+ applyPatch(target || subject, data.inversePatches.slice().reverse());
336
336
  }
337
337
  };
338
338
  recorder.resume();
@@ -1751,9 +1751,7 @@ var ObjectNode = /** @class */ (function (_super) {
1751
1751
  if (!this.parent && newParent.root === this) {
1752
1752
  throw fail$1("A state tree is not allowed to contain itself. Cannot assign " + this + " to path '" + newParent.path + "/" + subpath + "'");
1753
1753
  }
1754
- if (!this.parent &&
1755
- !!this.environment &&
1756
- this.environment !== newParent.root.environment) {
1754
+ if (!this.parent && !!this.environment && this.environment !== newParent.root.environment) {
1757
1755
  throw fail$1("A state tree cannot be made part of another state tree as long as their environments are different.");
1758
1756
  }
1759
1757
  }
@@ -1777,9 +1775,7 @@ var ObjectNode = /** @class */ (function (_super) {
1777
1775
  value: function (name) {
1778
1776
  var _this = this;
1779
1777
  this.fireInternalHook(name);
1780
- var fn = this.storedValue &&
1781
- typeof this.storedValue === "object" &&
1782
- this.storedValue[name];
1778
+ var fn = this.storedValue && typeof this.storedValue === "object" && this.storedValue[name];
1783
1779
  if (typeof fn === "function") {
1784
1780
  // we check for it to allow old mobx peer dependencies that don't have the method to work (even when still bugged)
1785
1781
  if (mobx._allowStateChangesInsideComputed) {
@@ -1881,8 +1877,7 @@ var ObjectNode = /** @class */ (function (_super) {
1881
1877
  var actionFullPath = "";
1882
1878
  if (actionContext && actionContext.name != null) {
1883
1879
  // try to use the context, and if it not available use the node one
1884
- var actionPath = (actionContext && actionContext.context && getPath(actionContext.context)) ||
1885
- escapedPath;
1880
+ var actionPath = (actionContext && actionContext.context && getPath(actionContext.context)) || escapedPath;
1886
1881
  actionFullPath = actionPath + "." + actionContext.name + "()";
1887
1882
  }
1888
1883
  return "You are trying to read or write to an object that is no longer part of a state tree. (Object type: '" + this.type.name + "', Path upon death: '" + escapedPath + "', Subpath: '" + subpath + "', Action: '" + actionFullPath + "'). Either detach nodes first, or don't use objects after removing / replacing them in the tree.";
@@ -2517,8 +2512,7 @@ var ComplexType = /** @class */ (function (_super) {
2517
2512
  writable: true,
2518
2513
  value: function (current, snapshot) {
2519
2514
  return (!current.identifierAttribute ||
2520
- current.identifier ===
2521
- normalizeIdentifier(snapshot[current.identifierAttribute]));
2515
+ current.identifier === normalizeIdentifier(snapshot[current.identifierAttribute]));
2522
2516
  }
2523
2517
  });
2524
2518
  Object.defineProperty(ComplexType.prototype, "tryToReconcileNode", {
@@ -2831,11 +2825,11 @@ var RunningAction = /** @class */ (function () {
2831
2825
  * @returns
2832
2826
  */
2833
2827
  function createActionTrackingMiddleware2(middlewareHooks) {
2834
- var runningActions = new WeakMap();
2828
+ var runningActions = new Map();
2835
2829
  return function actionTrackingMiddleware(call, next) {
2836
2830
  // find parentRunningAction
2837
2831
  var parentRunningAction = call.parentActionEvent
2838
- ? runningActions.get(call.parentActionEvent)
2832
+ ? runningActions.get(call.parentActionEvent.id)
2839
2833
  : undefined;
2840
2834
  if (call.type === "action") {
2841
2835
  var newCall = __assign(__assign({}, call), {
@@ -2844,17 +2838,19 @@ function createActionTrackingMiddleware2(middlewareHooks) {
2844
2838
  var passesFilter = !middlewareHooks.filter || middlewareHooks.filter(newCall);
2845
2839
  var hooks = passesFilter ? middlewareHooks : undefined;
2846
2840
  var runningAction = new RunningAction(hooks, newCall);
2847
- runningActions.set(call, runningAction);
2841
+ runningActions.set(call.id, runningAction);
2848
2842
  var res = void 0;
2849
2843
  try {
2850
2844
  res = next(call);
2851
2845
  }
2852
2846
  catch (e) {
2847
+ runningActions.delete(call.id);
2853
2848
  runningAction.finish(e);
2854
2849
  throw e;
2855
2850
  }
2851
+ // sync action finished
2856
2852
  if (!runningAction.hasFlowsPending) {
2857
- // sync action finished
2853
+ runningActions.delete(call.id);
2858
2854
  runningAction.finish();
2859
2855
  }
2860
2856
  return res;
@@ -2880,6 +2876,7 @@ function createActionTrackingMiddleware2(middlewareHooks) {
2880
2876
  finally {
2881
2877
  parentRunningAction.decFlowsPending();
2882
2878
  if (!parentRunningAction.hasFlowsPending) {
2879
+ runningActions.delete(call.parentActionEvent.id);
2883
2880
  parentRunningAction.finish(error);
2884
2881
  }
2885
2882
  }
@@ -2891,6 +2888,7 @@ function createActionTrackingMiddleware2(middlewareHooks) {
2891
2888
  finally {
2892
2889
  parentRunningAction.decFlowsPending();
2893
2890
  if (!parentRunningAction.hasFlowsPending) {
2891
+ runningActions.delete(call.parentActionEvent.id);
2894
2892
  parentRunningAction.finish();
2895
2893
  }
2896
2894
  }
@@ -2912,8 +2910,7 @@ function serializeArgument(node, actionName, index, arg) {
2912
2910
  if (typeof arg === "function")
2913
2911
  return serializeTheUnserializable("[function]");
2914
2912
  if (typeof arg === "object" && !isPlainObject(arg) && !isArray(arg))
2915
- return serializeTheUnserializable("[object " + ((arg && arg.constructor && arg.constructor.name) ||
2916
- "Complex Object") + "]");
2913
+ return serializeTheUnserializable("[object " + ((arg && arg.constructor && arg.constructor.name) || "Complex Object") + "]");
2917
2914
  try {
2918
2915
  // Check if serializable, cycle free etc...
2919
2916
  // MWE: there must be a better way....
@@ -3169,8 +3166,7 @@ function createActionInvoker(target, name, fn) {
3169
3166
  tree: getRoot(target),
3170
3167
  rootId: parentContext ? parentContext.rootId : id,
3171
3168
  parentId: parentContext ? parentContext.id : 0,
3172
- allParentIds: parentContext
3173
- ? __spread(parentContext.allParentIds, [parentContext.id]) : [],
3169
+ allParentIds: parentContext ? __spread(parentContext.allParentIds, [parentContext.id]) : [],
3174
3170
  parentEvent: parentContext,
3175
3171
  parentActionEvent: parentActionContext
3176
3172
  }, fn);
@@ -3764,7 +3760,7 @@ function getRelativePathBetweenNodes(base, target) {
3764
3760
  break;
3765
3761
  }
3766
3762
  // TODO: assert that no targetParts paths are "..", "." or ""!
3767
- return (baseParts.slice(common).map(doubleDot).join("/") + joinJsonPath(targetParts.slice(common)));
3763
+ return baseParts.slice(common).map(doubleDot).join("/") + joinJsonPath(targetParts.slice(common));
3768
3764
  }
3769
3765
  /**
3770
3766
  * @internal
@@ -3781,47 +3777,44 @@ function resolveNodeByPath(base, path, failIfResolveFails) {
3781
3777
  function resolveNodeByPathParts(base, pathParts, failIfResolveFails) {
3782
3778
  if (failIfResolveFails === void 0) { failIfResolveFails = true; }
3783
3779
  var current = base;
3784
- for (var i = 0; i < pathParts.length; i++) {
3785
- var part = pathParts[i];
3786
- if (part === "..") {
3787
- current = current.parent;
3788
- if (current)
3789
- continue; // not everything has a parent
3790
- }
3791
- else if (part === ".") {
3792
- continue;
3793
- }
3794
- else if (current) {
3795
- if (current instanceof ScalarNode) {
3796
- // check if the value of a scalar resolves to a state tree node (e.g. references)
3797
- // then we can continue resolving...
3798
- try {
3780
+ try {
3781
+ for (var i = 0; i < pathParts.length; i++) {
3782
+ var part = pathParts[i];
3783
+ if (part === "..") {
3784
+ current = current.parent;
3785
+ if (current)
3786
+ continue; // not everything has a parent
3787
+ }
3788
+ else if (part === ".") {
3789
+ continue;
3790
+ }
3791
+ else if (current) {
3792
+ if (current instanceof ScalarNode) {
3793
+ // check if the value of a scalar resolves to a state tree node (e.g. references)
3794
+ // then we can continue resolving...
3799
3795
  var value = current.value;
3800
3796
  if (isStateTreeNode(value)) {
3801
3797
  current = getStateTreeNode(value);
3802
3798
  // fall through
3803
3799
  }
3804
3800
  }
3805
- catch (e) {
3806
- if (!failIfResolveFails) {
3807
- return undefined;
3801
+ if (current instanceof ObjectNode) {
3802
+ var subType = current.getChildType(part);
3803
+ if (subType) {
3804
+ current = current.getChildNode(part);
3805
+ if (current)
3806
+ continue;
3808
3807
  }
3809
- throw e;
3810
3808
  }
3811
3809
  }
3812
- if (current instanceof ObjectNode) {
3813
- var subType = current.getChildType(part);
3814
- if (subType) {
3815
- current = current.getChildNode(part);
3816
- if (current)
3817
- continue;
3818
- }
3819
- }
3820
- }
3821
- if (failIfResolveFails)
3822
3810
  throw fail$1("Could not resolve '" + part + "' in path '" + (joinJsonPath(pathParts.slice(0, i)) || "/") + "' while resolving '" + joinJsonPath(pathParts) + "'");
3823
- else
3811
+ }
3812
+ }
3813
+ catch (e) {
3814
+ if (!failIfResolveFails) {
3824
3815
  return undefined;
3816
+ }
3817
+ throw e;
3825
3818
  }
3826
3819
  return current;
3827
3820
  }
@@ -4011,8 +4004,7 @@ function deepFreeze(value) {
4011
4004
  freeze(value);
4012
4005
  if (isPlainObject(value)) {
4013
4006
  Object.keys(value).forEach(function (propKey) {
4014
- if (!isPrimitive(value[propKey]) &&
4015
- !Object.isFrozen(value[propKey])) {
4007
+ if (!isPrimitive(value[propKey]) && !Object.isFrozen(value[propKey])) {
4016
4008
  deepFreeze(value[propKey]);
4017
4009
  }
4018
4010
  });
@@ -6418,9 +6410,7 @@ var ModelType = /** @class */ (function (_super) {
6418
6410
  var _this = this;
6419
6411
  // optimization: cache
6420
6412
  return ("{ " +
6421
- this.propertyNames
6422
- .map(function (key) { return key + ": " + _this.properties[key].describe(); })
6423
- .join("; ") +
6413
+ this.propertyNames.map(function (key) { return key + ": " + _this.properties[key].describe(); }).join("; ") +
6424
6414
  " }");
6425
6415
  }
6426
6416
  });
@@ -6908,9 +6898,7 @@ function refinement() {
6908
6898
  var name = typeof args[0] === "string" ? args.shift() : isType(args[0]) ? args[0].name : null;
6909
6899
  var type = args[0];
6910
6900
  var predicate = args[1];
6911
- var message = args[2]
6912
- ? args[2]
6913
- : function (v) { return "Value does not respect the refinement predicate"; };
6901
+ var message = args[2] ? args[2] : function (v) { return "Value does not respect the refinement predicate"; };
6914
6902
  // ensures all parameters are correct
6915
6903
  assertIsType(type, [1, 2]);
6916
6904
  assertIsString(name, 1);
@@ -7053,9 +7041,7 @@ var Union = /** @class */ (function (_super) {
7053
7041
  if (reconcileCurrentType.is(value)) {
7054
7042
  return reconcileCurrentType;
7055
7043
  }
7056
- return this._types
7057
- .filter(function (t) { return t !== reconcileCurrentType; })
7058
- .find(function (type) { return type.is(value); });
7044
+ return this._types.filter(function (t) { return t !== reconcileCurrentType; }).find(function (type) { return type.is(value); });
7059
7045
  }
7060
7046
  else {
7061
7047
  return this._types.find(function (type) { return type.is(value); });
@@ -7534,8 +7520,7 @@ var Lazy = /** @class */ (function (_super) {
7534
7520
  mobx.when(function () {
7535
7521
  return _this.pendingNodeList.length > 0 &&
7536
7522
  _this.pendingNodeList.some(function (node) {
7537
- return node.isAlive &&
7538
- _this.options.shouldLoadPredicate(node.parent ? node.parent.value : null);
7523
+ return node.isAlive && _this.options.shouldLoadPredicate(node.parent ? node.parent.value : null);
7539
7524
  });
7540
7525
  }, function () {
7541
7526
  _this.options.loadType().then(mobx.action(function (type) {
@@ -8049,9 +8034,7 @@ var IdentifierReferenceType = /** @class */ (function (_super) {
8049
8034
  configurable: true,
8050
8035
  writable: true,
8051
8036
  value: function (parent, subpath, environment, initialValue) {
8052
- var identifier = isStateTreeNode(initialValue)
8053
- ? getIdentifier(initialValue)
8054
- : initialValue;
8037
+ var identifier = isStateTreeNode(initialValue) ? getIdentifier(initialValue) : initialValue;
8055
8038
  var storedRef = new StoredReference(initialValue, this.targetType);
8056
8039
  var storedRefNode = createScalarNode(this, parent, subpath, environment, storedRef);
8057
8040
  storedRef.node = storedRefNode;
@@ -8136,9 +8119,7 @@ var CustomReferenceType = /** @class */ (function (_super) {
8136
8119
  var newIdentifier = isStateTreeNode(newValue)
8137
8120
  ? this.options.set(newValue, current ? current.storedValue : null)
8138
8121
  : newValue;
8139
- if (!current.isDetaching &&
8140
- current.type === this &&
8141
- current.storedValue === newIdentifier) {
8122
+ if (!current.isDetaching && current.type === this && current.storedValue === newIdentifier) {
8142
8123
  current.setParent(parent, subpath);
8143
8124
  return current;
8144
8125
  }