mobx-state-tree 5.0.3 → 5.1.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/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  [![npm version](https://badge.fury.io/js/mobx-state-tree.svg)](https://badge.fury.io/js/mobx-state-tree)
6
6
  [![CircleCI](https://circleci.com/gh/mobxjs/mobx-state-tree.svg?style=svg)](https://circleci.com/gh/mobxjs/mobx-state-tree)
7
- [![Coverage Status](https://coveralls.io/repos/github/mobxjs/mobx-state-tree/badge.svg?branch=master)](https://coveralls.io/github/mobxjs/mobx-state-tree?branch=master)
7
+ [![Codecov](https://codecov.io/gh/mobxjs/mobx-state-tree/branch/master/graph/badge.svg?token=BFkuCB6CtL)](https://codecov.io/gh/mobxjs/mobx-state-tree)
8
8
  [![Have a question? Ask on GitHub Discussions!](https://img.shields.io/badge/Have%20a%20question%3F-Ask%20on%20GitHub%20Discussions!-blue)](https://github.com/mobxjs/mobx-state-tree/discussions)
9
9
 
10
10
  ## What is mobx-state-tree?
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * @hidden
3
3
  */
4
- export declare type FlowReturn<R> = R extends Promise<infer T> ? T : R;
4
+ export declare type FlowReturn<R> = R extends PromiseLike<infer T> ? T : R;
5
5
  /**
6
6
  * See [asynchronous actions](concepts/async-actions.md).
7
7
  *
8
8
  * @returns The flow as a promise.
9
9
  */
10
- export declare function flow<R, Args extends any[]>(generator: (...args: Args) => Generator<Promise<any>, R, any>): (...args: Args) => Promise<FlowReturn<R>>;
10
+ export declare function flow<R, Args extends any[]>(generator: (...args: Args) => Generator<PromiseLike<any>, R, any>): (...args: Args) => PromiseLike<FlowReturn<R>>;
11
11
  /**
12
12
  * @deprecated Not needed since TS3.6.
13
13
  * Used for TypeScript to make flows that return a promise return the actual promise result.
@@ -38,7 +38,7 @@ export declare function castFlowReturn<T>(val: T): T;
38
38
  * }))
39
39
  * ```
40
40
  */
41
- export declare function toGeneratorFunction<R, Args extends any[]>(p: (...args: Args) => Promise<R>): (...args: Args) => Generator<Promise<R>, R, R>;
41
+ export declare function toGeneratorFunction<R, Args extends any[]>(p: (...args: Args) => PromiseLike<R>): (...args: Args) => Generator<PromiseLike<R>, R, R>;
42
42
  /**
43
43
  * @experimental
44
44
  * experimental api - might change on minor/patch releases
@@ -580,7 +580,7 @@ function resolveIdentifier(type, target, identifier) {
580
580
  assertIsStateTreeNode(target, 2);
581
581
  assertIsValidIdentifier(identifier, 3);
582
582
  var node = getStateTreeNode(target).root.identifierCache.resolve(type, normalizeIdentifier(identifier));
583
- return node ? node.value : undefined;
583
+ return node === null || node === void 0 ? void 0 : node.value;
584
584
  }
585
585
  /**
586
586
  * Returns the identifier of the target node.
@@ -1130,6 +1130,14 @@ var BaseNode = /** @class */ (function () {
1130
1130
  enumerable: false,
1131
1131
  configurable: true
1132
1132
  });
1133
+ Object.defineProperty(BaseNode.prototype, "getReconciliationType", {
1134
+ enumerable: false,
1135
+ configurable: true,
1136
+ writable: true,
1137
+ value: function () {
1138
+ return this.type;
1139
+ }
1140
+ });
1133
1141
  Object.defineProperty(BaseNode.prototype, "baseSetParent", {
1134
1142
  enumerable: false,
1135
1143
  configurable: true,
@@ -3922,14 +3930,12 @@ function isMutable(value) {
3922
3930
  */
3923
3931
  function isPrimitive(value, includeDate) {
3924
3932
  if (includeDate === void 0) { includeDate = true; }
3925
- if (value === null || value === undefined)
3926
- return true;
3927
- if (typeof value === "string" ||
3933
+ return (value === null ||
3934
+ value === undefined ||
3935
+ typeof value === "string" ||
3928
3936
  typeof value === "number" ||
3929
3937
  typeof value === "boolean" ||
3930
- (includeDate && value instanceof Date))
3931
- return true;
3932
- return false;
3938
+ (includeDate && value instanceof Date));
3933
3939
  }
3934
3940
  /**
3935
3941
  * @internal
@@ -4586,6 +4592,8 @@ function splitJsonPath(path) {
4586
4592
  return parts;
4587
4593
  }
4588
4594
 
4595
+ /** @hidden */
4596
+ var $preProcessorFailed = Symbol("$preProcessorFailed");
4589
4597
  var SnapshotProcessor = /** @class */ (function (_super) {
4590
4598
  __extends(SnapshotProcessor, _super);
4591
4599
  function SnapshotProcessor(_subtype, _processors, name) {
@@ -4630,6 +4638,19 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4630
4638
  return sn;
4631
4639
  }
4632
4640
  });
4641
+ Object.defineProperty(SnapshotProcessor.prototype, "preProcessSnapshotSafe", {
4642
+ enumerable: false,
4643
+ configurable: true,
4644
+ writable: true,
4645
+ value: function (sn) {
4646
+ try {
4647
+ return this.preProcessSnapshot(sn);
4648
+ }
4649
+ catch (e) {
4650
+ return $preProcessorFailed;
4651
+ }
4652
+ }
4653
+ });
4633
4654
  Object.defineProperty(SnapshotProcessor.prototype, "postProcessSnapshot", {
4634
4655
  enumerable: false,
4635
4656
  configurable: true,
@@ -4648,11 +4669,14 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4648
4669
  value: function (node) {
4649
4670
  var _this = this;
4650
4671
  // the node has to use these methods rather than the original type ones
4651
- proxyNodeTypeMethods(node.type, this, "create", "is", "isMatchingSnapshotId");
4672
+ proxyNodeTypeMethods(node.type, this, "create");
4652
4673
  var oldGetSnapshot = node.getSnapshot;
4653
4674
  node.getSnapshot = function () {
4654
4675
  return _this.postProcessSnapshot(oldGetSnapshot.call(node));
4655
4676
  };
4677
+ node.getReconciliationType = function () {
4678
+ return _this;
4679
+ };
4656
4680
  }
4657
4681
  });
4658
4682
  Object.defineProperty(SnapshotProcessor.prototype, "instantiate", {
@@ -4695,7 +4719,10 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4695
4719
  configurable: true,
4696
4720
  writable: true,
4697
4721
  value: function (value, context) {
4698
- var processedSn = this.preProcessSnapshot(value);
4722
+ var processedSn = this.preProcessSnapshotSafe(value);
4723
+ if (processedSn === $preProcessorFailed) {
4724
+ return typeCheckFailure(context, value, "Failed to preprocess value");
4725
+ }
4699
4726
  return this._subtype.validate(processedSn, context);
4700
4727
  }
4701
4728
  });
@@ -4716,7 +4743,10 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4716
4743
  ? this._subtype
4717
4744
  : isStateTreeNode(thing)
4718
4745
  ? getSnapshot(thing, false)
4719
- : this.preProcessSnapshot(thing);
4746
+ : this.preProcessSnapshotSafe(thing);
4747
+ if (value === $preProcessorFailed) {
4748
+ return false;
4749
+ }
4720
4750
  return this._subtype.validate(value, [{ path: "", type: this._subtype }]).length === 0;
4721
4751
  }
4722
4752
  });
@@ -4737,7 +4767,7 @@ var SnapshotProcessor = /** @class */ (function (_super) {
4737
4767
  return false;
4738
4768
  }
4739
4769
  var processedSn = this.preProcessSnapshot(snapshot);
4740
- return ComplexType.prototype.isMatchingSnapshotId.call(this._subtype, current, processedSn);
4770
+ return this._subtype.isMatchingSnapshotId(current, processedSn);
4741
4771
  }
4742
4772
  });
4743
4773
  return SnapshotProcessor;
@@ -5001,18 +5031,17 @@ var MapType = /** @class */ (function (_super) {
5001
5031
  }
5002
5032
  var modelTypes = [];
5003
5033
  if (tryCollectModelTypes(this._subType, modelTypes)) {
5004
- var identifierAttribute_1 = undefined;
5005
- modelTypes.forEach(function (type) {
5006
- if (type.identifierAttribute) {
5007
- if (identifierAttribute_1 && identifierAttribute_1 !== type.identifierAttribute) {
5008
- throw fail$1("The objects in a map should all have the same identifier attribute, expected '" + identifierAttribute_1 + "', but child of type '" + type.name + "' declared attribute '" + type.identifierAttribute + "' as identifier");
5009
- }
5010
- identifierAttribute_1 = type.identifierAttribute;
5034
+ var identifierAttribute = modelTypes.reduce(function (current, type) {
5035
+ if (!type.identifierAttribute)
5036
+ return current;
5037
+ if (current && current !== type.identifierAttribute) {
5038
+ throw fail$1("The objects in a map should all have the same identifier attribute, expected '" + current + "', but child of type '" + type.name + "' declared attribute '" + type.identifierAttribute + "' as identifier");
5011
5039
  }
5012
- });
5013
- if (identifierAttribute_1) {
5040
+ return type.identifierAttribute;
5041
+ }, undefined);
5042
+ if (identifierAttribute) {
5014
5043
  this.identifierMode = MapIdentifierMode.YES;
5015
- this.mapIdentifierAttribute = identifierAttribute_1;
5044
+ this.mapIdentifierAttribute = identifierAttribute;
5016
5045
  }
5017
5046
  else {
5018
5047
  this.identifierMode = MapIdentifierMode.NO;
@@ -5731,13 +5760,17 @@ function areSame(oldNode, newValue) {
5731
5760
  if (oldNode.snapshot === newValue) {
5732
5761
  return true;
5733
5762
  }
5763
+ // Non object nodes don't get reconciled
5764
+ if (!(oldNode instanceof ObjectNode)) {
5765
+ return false;
5766
+ }
5767
+ var oldNodeType = oldNode.getReconciliationType();
5734
5768
  // new value is a snapshot with the correct identifier
5735
- return (oldNode instanceof ObjectNode &&
5736
- oldNode.identifier !== null &&
5769
+ return (oldNode.identifier !== null &&
5737
5770
  oldNode.identifierAttribute &&
5738
5771
  isPlainObject(newValue) &&
5739
- oldNode.type.isMatchingSnapshotId(oldNode, newValue) &&
5740
- oldNode.type.is(newValue));
5772
+ oldNodeType.is(newValue) &&
5773
+ oldNodeType.isMatchingSnapshotId(oldNode, newValue));
5741
5774
  }
5742
5775
  /**
5743
5776
  * Returns if a given value represents an array type.
@@ -6891,7 +6924,7 @@ var Union = /** @class */ (function (_super) {
6891
6924
  configurable: true,
6892
6925
  writable: true,
6893
6926
  value: function (current, newValue, parent, subpath) {
6894
- var type = this.determineType(newValue, current.type);
6927
+ var type = this.determineType(newValue, current.getReconciliationType());
6895
6928
  if (!type)
6896
6929
  throw fail$1("No matching type for union " + this.describe()); // can happen in prod builds
6897
6930
  return type.reconcile(current, newValue, parent, subpath);