mobx-state-tree 5.2.0-alpha.1 → 5.2.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.
@@ -196,7 +196,7 @@ function getChildType(object, propertyName) {
196
196
  /**
197
197
  * Registers a function that will be invoked for each mutation that is applied to the provided model instance, or to any of its children.
198
198
  * See [patches](https://github.com/mobxjs/mobx-state-tree#patches) for more details. onPatch events are emitted immediately and will not await the end of a transaction.
199
- * Patches can be used to deep observe a model tree.
199
+ * Patches can be used to deeply observe a model tree.
200
200
  *
201
201
  * @param target the model instance from which to receive patches
202
202
  * @param callback the callback that is invoked for each patch. The reversePatch is a patch that would actually undo the emitted patch
@@ -241,7 +241,7 @@ function applyPatch(target, patch) {
241
241
  }
242
242
  /**
243
243
  * Small abstraction around `onPatch` and `applyPatch`, attaches a patch listener to a tree and records all the patches.
244
- * Returns an recorder object with the following signature:
244
+ * Returns a recorder object with the following signature:
245
245
  *
246
246
  * Example:
247
247
  * ```ts
@@ -591,7 +591,7 @@ function getIdentifier(target) {
591
591
  return getStateTreeNode(target).identifier;
592
592
  }
593
593
  /**
594
- * Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if it the check passes,
594
+ * Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if the check passes,
595
595
  * else it returns undefined.
596
596
  *
597
597
  * @param getter Function to access the reference.
@@ -691,9 +691,9 @@ function getRelativePath(base, target) {
691
691
  }
692
692
  /**
693
693
  * Returns a deep copy of the given state tree node as new tree.
694
- * Short hand for `snapshot(x) = getType(x).create(getSnapshot(x))`
694
+ * Shorthand for `snapshot(x) = getType(x).create(getSnapshot(x))`
695
695
  *
696
- * _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
696
+ * _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc. during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
697
697
  *
698
698
  * @param source
699
699
  * @param keepEnvironment indicates whether the clone should inherit the same environment (`true`, the default), or not have an environment (`false`). If an object is passed in as second argument, that will act as the environment for the cloned tree.
@@ -903,13 +903,13 @@ function getMembers(target) {
903
903
  * ```
904
904
  *
905
905
  * @param snapshotOrInstance Snapshot or instance
906
- * @returns The same object casted as an instance
906
+ * @returns The same object cast as an instance
907
907
  */
908
908
  function cast(snapshotOrInstance) {
909
909
  return snapshotOrInstance;
910
910
  }
911
911
  /**
912
- * Casts a node instance type to an snapshot type so it can be assigned to a type snapshot (e.g. to be used inside a create call).
912
+ * Casts a node instance type to a snapshot type so it can be assigned to a type snapshot (e.g. to be used inside a create call).
913
913
  * Note that this is just a cast for the type system, this is, it won't actually convert an instance to a snapshot,
914
914
  * but just fool typescript into thinking so.
915
915
  *
@@ -933,14 +933,14 @@ function cast(snapshotOrInstance) {
933
933
  * ```
934
934
  *
935
935
  * @param snapshotOrInstance Snapshot or instance
936
- * @returns The same object casted as an input (creation) snapshot
936
+ * @returns The same object cast as an input (creation) snapshot
937
937
  */
938
938
  function castToSnapshot(snapshotOrInstance) {
939
939
  return snapshotOrInstance;
940
940
  }
941
941
  /**
942
- * Casts a node instance type to a reference snapshot type so it can be assigned to a refernence snapshot (e.g. to be used inside a create call).
943
- * Note that this is just a cast for the type system, this is, it won't actually convert an instance to a refererence snapshot,
942
+ * Casts a node instance type to a reference snapshot type so it can be assigned to a reference snapshot (e.g. to be used inside a create call).
943
+ * Note that this is just a cast for the type system, this is, it won't actually convert an instance to a reference snapshot,
944
944
  * but just fool typescript into thinking so.
945
945
  *
946
946
  * Example:
@@ -964,7 +964,7 @@ function castToSnapshot(snapshotOrInstance) {
964
964
  * ```
965
965
  *
966
966
  * @param instance Instance
967
- * @returns The same object casted as an reference snapshot (string or number)
967
+ * @returns The same object cast as a reference snapshot (string or number)
968
968
  */
969
969
  function castToReferenceSnapshot(instance) {
970
970
  return instance;
@@ -5123,7 +5123,7 @@ var MapType = /** @class */ (function (_super) {
5123
5123
  configurable: true,
5124
5124
  writable: true,
5125
5125
  value: function (childNodes) {
5126
- return new MSTMap(childNodes, this.describe());
5126
+ return new MSTMap(childNodes, this.name);
5127
5127
  }
5128
5128
  });
5129
5129
  Object.defineProperty(MapType.prototype, "finalizeNewInstance", {
@@ -5150,7 +5150,7 @@ var MapType = /** @class */ (function (_super) {
5150
5150
  configurable: true,
5151
5151
  writable: true,
5152
5152
  value: function () {
5153
- return "Map<string, " + this._subType.describe() + ">";
5153
+ return this.name;
5154
5154
  }
5155
5155
  });
5156
5156
  Object.defineProperty(MapType.prototype, "getChildren", {
@@ -5386,7 +5386,7 @@ MapType.prototype.applySnapshot = action(MapType.prototype.applySnapshot);
5386
5386
  * @returns
5387
5387
  */
5388
5388
  function map(subtype) {
5389
- return new MapType("map<string, " + subtype.name + ">", subtype);
5389
+ return new MapType("Map<string, " + subtype.name + ">", subtype);
5390
5390
  }
5391
5391
  /**
5392
5392
  * Returns if a given value represents a map type.
@@ -5465,7 +5465,7 @@ var ArrayType = /** @class */ (function (_super) {
5465
5465
  configurable: true,
5466
5466
  writable: true,
5467
5467
  value: function (childNodes) {
5468
- var options = __assign(__assign({}, mobxShallow), { name: this.describe() });
5468
+ var options = __assign(__assign({}, mobxShallow), { name: this.name });
5469
5469
  return observable.array(convertChildNodesToArray(childNodes), options);
5470
5470
  }
5471
5471
  });
@@ -5493,7 +5493,7 @@ var ArrayType = /** @class */ (function (_super) {
5493
5493
  configurable: true,
5494
5494
  writable: true,
5495
5495
  value: function () {
5496
- return this._subType.describe() + "[]";
5496
+ return this.name;
5497
5497
  }
5498
5498
  });
5499
5499
  Object.defineProperty(ArrayType.prototype, "getChildren", {
@@ -5849,8 +5849,18 @@ var defaultObjectOptions = {
5849
5849
  initializers: EMPTY_ARRAY
5850
5850
  };
5851
5851
  function toPropertiesObject(declaredProps) {
5852
+ var keysList = Object.keys(declaredProps);
5853
+ var alreadySeenKeys = {};
5854
+ keysList.forEach(function (key) {
5855
+ if (alreadySeenKeys[key]) {
5856
+ throw fail$1(key + " is declared twice in the model. Model should not contain same keys");
5857
+ }
5858
+ else {
5859
+ alreadySeenKeys[key] = true;
5860
+ }
5861
+ });
5852
5862
  // loop through properties and ensures that all items are types
5853
- return Object.keys(declaredProps).reduce(function (props, key) {
5863
+ return keysList.reduce(function (props, key) {
5854
5864
  var _a, _b, _c;
5855
5865
  // warn if user intended a HOOK
5856
5866
  if (key in Hook)
@@ -6196,7 +6206,7 @@ var ModelType = /** @class */ (function (_super) {
6196
6206
  configurable: true,
6197
6207
  writable: true,
6198
6208
  value: function (childNodes) {
6199
- var options = __assign(__assign({}, mobxShallow), { name: this.describe() });
6209
+ var options = __assign(__assign({}, mobxShallow), { name: this.name });
6200
6210
  return observable.object(childNodes, EMPTY_OBJECT, options);
6201
6211
  }
6202
6212
  });
@@ -6273,10 +6283,11 @@ var ModelType = /** @class */ (function (_super) {
6273
6283
  configurable: true,
6274
6284
  writable: true,
6275
6285
  value: function (node, key) {
6286
+ var _a;
6276
6287
  if (!(key in this.properties))
6277
6288
  throw fail$1("Not a value property: " + key);
6278
6289
  var adm = _getAdministration(node.storedValue, key);
6279
- var childNode = adm.raw();
6290
+ var childNode = (_a = adm.raw) === null || _a === void 0 ? void 0 : _a.call(adm);
6280
6291
  if (!childNode)
6281
6292
  throw fail$1("Node not available for property " + key);
6282
6293
  return childNode;
@@ -6291,7 +6302,14 @@ var ModelType = /** @class */ (function (_super) {
6291
6302
  if (applyPostProcess === void 0) { applyPostProcess = true; }
6292
6303
  var res = {};
6293
6304
  this.forAllProps(function (name, type) {
6294
- getAtom(node.storedValue, name).reportObserved();
6305
+ try {
6306
+ // TODO: FIXME, make sure the observable ref is used!
6307
+ var atom = getAtom(node.storedValue, name);
6308
+ atom.reportObserved();
6309
+ }
6310
+ catch (e) {
6311
+ throw fail$1(name + " property is declared twice");
6312
+ }
6295
6313
  res[name] = _this.getChildNode(node, name).snapshot;
6296
6314
  });
6297
6315
  if (applyPostProcess) {
@@ -7163,7 +7181,7 @@ var OptionalValue = /** @class */ (function (_super) {
7163
7181
  writable: true,
7164
7182
  value: function (parent, subpath, environment, initialValue) {
7165
7183
  if (this.optionalValues.indexOf(initialValue) >= 0) {
7166
- var defaultInstanceOrSnapshot = this.getDefaultInstanceOrSnapshot(parent);
7184
+ var defaultInstanceOrSnapshot = this.getDefaultInstanceOrSnapshot();
7167
7185
  return this._subtype.instantiate(parent, subpath, environment, defaultInstanceOrSnapshot);
7168
7186
  }
7169
7187
  return this._subtype.instantiate(parent, subpath, environment, initialValue);
@@ -7176,16 +7194,16 @@ var OptionalValue = /** @class */ (function (_super) {
7176
7194
  value: function (current, newValue, parent, subpath) {
7177
7195
  return this._subtype.reconcile(current, this.optionalValues.indexOf(newValue) < 0 && this._subtype.is(newValue)
7178
7196
  ? newValue
7179
- : this.getDefaultInstanceOrSnapshot(parent), parent, subpath);
7197
+ : this.getDefaultInstanceOrSnapshot(), parent, subpath);
7180
7198
  }
7181
7199
  });
7182
7200
  Object.defineProperty(OptionalValue.prototype, "getDefaultInstanceOrSnapshot", {
7183
7201
  enumerable: false,
7184
7202
  configurable: true,
7185
7203
  writable: true,
7186
- value: function (parent) {
7204
+ value: function () {
7187
7205
  var defaultInstanceOrSnapshot = typeof this._defaultValue === "function"
7188
- ? this._defaultValue(parent)
7206
+ ? this._defaultValue()
7189
7207
  : this._defaultValue;
7190
7208
  // while static values are already snapshots and checked on types.optional
7191
7209
  // generator functions must always be rechecked just in case
@@ -200,7 +200,7 @@
200
200
  /**
201
201
  * Registers a function that will be invoked for each mutation that is applied to the provided model instance, or to any of its children.
202
202
  * See [patches](https://github.com/mobxjs/mobx-state-tree#patches) for more details. onPatch events are emitted immediately and will not await the end of a transaction.
203
- * Patches can be used to deep observe a model tree.
203
+ * Patches can be used to deeply observe a model tree.
204
204
  *
205
205
  * @param target the model instance from which to receive patches
206
206
  * @param callback the callback that is invoked for each patch. The reversePatch is a patch that would actually undo the emitted patch
@@ -245,7 +245,7 @@
245
245
  }
246
246
  /**
247
247
  * Small abstraction around `onPatch` and `applyPatch`, attaches a patch listener to a tree and records all the patches.
248
- * Returns an recorder object with the following signature:
248
+ * Returns a recorder object with the following signature:
249
249
  *
250
250
  * Example:
251
251
  * ```ts
@@ -595,7 +595,7 @@
595
595
  return getStateTreeNode(target).identifier;
596
596
  }
597
597
  /**
598
- * Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if it the check passes,
598
+ * Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if the check passes,
599
599
  * else it returns undefined.
600
600
  *
601
601
  * @param getter Function to access the reference.
@@ -695,9 +695,9 @@
695
695
  }
696
696
  /**
697
697
  * Returns a deep copy of the given state tree node as new tree.
698
- * Short hand for `snapshot(x) = getType(x).create(getSnapshot(x))`
698
+ * Shorthand for `snapshot(x) = getType(x).create(getSnapshot(x))`
699
699
  *
700
- * _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
700
+ * _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc. during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
701
701
  *
702
702
  * @param source
703
703
  * @param keepEnvironment indicates whether the clone should inherit the same environment (`true`, the default), or not have an environment (`false`). If an object is passed in as second argument, that will act as the environment for the cloned tree.
@@ -907,13 +907,13 @@
907
907
  * ```
908
908
  *
909
909
  * @param snapshotOrInstance Snapshot or instance
910
- * @returns The same object casted as an instance
910
+ * @returns The same object cast as an instance
911
911
  */
912
912
  function cast(snapshotOrInstance) {
913
913
  return snapshotOrInstance;
914
914
  }
915
915
  /**
916
- * Casts a node instance type to an snapshot type so it can be assigned to a type snapshot (e.g. to be used inside a create call).
916
+ * Casts a node instance type to a snapshot type so it can be assigned to a type snapshot (e.g. to be used inside a create call).
917
917
  * Note that this is just a cast for the type system, this is, it won't actually convert an instance to a snapshot,
918
918
  * but just fool typescript into thinking so.
919
919
  *
@@ -937,14 +937,14 @@
937
937
  * ```
938
938
  *
939
939
  * @param snapshotOrInstance Snapshot or instance
940
- * @returns The same object casted as an input (creation) snapshot
940
+ * @returns The same object cast as an input (creation) snapshot
941
941
  */
942
942
  function castToSnapshot(snapshotOrInstance) {
943
943
  return snapshotOrInstance;
944
944
  }
945
945
  /**
946
- * Casts a node instance type to a reference snapshot type so it can be assigned to a refernence snapshot (e.g. to be used inside a create call).
947
- * Note that this is just a cast for the type system, this is, it won't actually convert an instance to a refererence snapshot,
946
+ * Casts a node instance type to a reference snapshot type so it can be assigned to a reference snapshot (e.g. to be used inside a create call).
947
+ * Note that this is just a cast for the type system, this is, it won't actually convert an instance to a reference snapshot,
948
948
  * but just fool typescript into thinking so.
949
949
  *
950
950
  * Example:
@@ -968,7 +968,7 @@
968
968
  * ```
969
969
  *
970
970
  * @param instance Instance
971
- * @returns The same object casted as an reference snapshot (string or number)
971
+ * @returns The same object cast as a reference snapshot (string or number)
972
972
  */
973
973
  function castToReferenceSnapshot(instance) {
974
974
  return instance;
@@ -5105,7 +5105,7 @@
5105
5105
  configurable: true,
5106
5106
  writable: true,
5107
5107
  value: function (childNodes) {
5108
- return new MSTMap(childNodes, this.describe());
5108
+ return new MSTMap(childNodes, this.name);
5109
5109
  }
5110
5110
  });
5111
5111
  Object.defineProperty(MapType.prototype, "finalizeNewInstance", {
@@ -5132,7 +5132,7 @@
5132
5132
  configurable: true,
5133
5133
  writable: true,
5134
5134
  value: function () {
5135
- return "Map<string, " + this._subType.describe() + ">";
5135
+ return this.name;
5136
5136
  }
5137
5137
  });
5138
5138
  Object.defineProperty(MapType.prototype, "getChildren", {
@@ -5368,7 +5368,7 @@
5368
5368
  * @returns
5369
5369
  */
5370
5370
  function map(subtype) {
5371
- return new MapType("map<string, " + subtype.name + ">", subtype);
5371
+ return new MapType("Map<string, " + subtype.name + ">", subtype);
5372
5372
  }
5373
5373
  /**
5374
5374
  * Returns if a given value represents a map type.
@@ -5447,7 +5447,7 @@
5447
5447
  configurable: true,
5448
5448
  writable: true,
5449
5449
  value: function (childNodes) {
5450
- var options = __assign(__assign({}, mobxShallow), { name: this.describe() });
5450
+ var options = __assign(__assign({}, mobxShallow), { name: this.name });
5451
5451
  return mobx.observable.array(convertChildNodesToArray(childNodes), options);
5452
5452
  }
5453
5453
  });
@@ -5475,7 +5475,7 @@
5475
5475
  configurable: true,
5476
5476
  writable: true,
5477
5477
  value: function () {
5478
- return this._subType.describe() + "[]";
5478
+ return this.name;
5479
5479
  }
5480
5480
  });
5481
5481
  Object.defineProperty(ArrayType.prototype, "getChildren", {
@@ -5831,8 +5831,18 @@
5831
5831
  initializers: EMPTY_ARRAY
5832
5832
  };
5833
5833
  function toPropertiesObject(declaredProps) {
5834
+ var keysList = Object.keys(declaredProps);
5835
+ var alreadySeenKeys = {};
5836
+ keysList.forEach(function (key) {
5837
+ if (alreadySeenKeys[key]) {
5838
+ throw fail$1(key + " is declared twice in the model. Model should not contain same keys");
5839
+ }
5840
+ else {
5841
+ alreadySeenKeys[key] = true;
5842
+ }
5843
+ });
5834
5844
  // loop through properties and ensures that all items are types
5835
- return Object.keys(declaredProps).reduce(function (props, key) {
5845
+ return keysList.reduce(function (props, key) {
5836
5846
  var _a, _b, _c;
5837
5847
  // warn if user intended a HOOK
5838
5848
  if (key in Hook)
@@ -6178,7 +6188,7 @@
6178
6188
  configurable: true,
6179
6189
  writable: true,
6180
6190
  value: function (childNodes) {
6181
- var options = __assign(__assign({}, mobxShallow), { name: this.describe() });
6191
+ var options = __assign(__assign({}, mobxShallow), { name: this.name });
6182
6192
  return mobx.observable.object(childNodes, EMPTY_OBJECT, options);
6183
6193
  }
6184
6194
  });
@@ -6255,10 +6265,11 @@
6255
6265
  configurable: true,
6256
6266
  writable: true,
6257
6267
  value: function (node, key) {
6268
+ var _a;
6258
6269
  if (!(key in this.properties))
6259
6270
  throw fail$1("Not a value property: " + key);
6260
6271
  var adm = mobx._getAdministration(node.storedValue, key);
6261
- var childNode = adm.raw();
6272
+ var childNode = (_a = adm.raw) === null || _a === void 0 ? void 0 : _a.call(adm);
6262
6273
  if (!childNode)
6263
6274
  throw fail$1("Node not available for property " + key);
6264
6275
  return childNode;
@@ -6273,7 +6284,14 @@
6273
6284
  if (applyPostProcess === void 0) { applyPostProcess = true; }
6274
6285
  var res = {};
6275
6286
  this.forAllProps(function (name, type) {
6276
- mobx.getAtom(node.storedValue, name).reportObserved();
6287
+ try {
6288
+ // TODO: FIXME, make sure the observable ref is used!
6289
+ var atom = mobx.getAtom(node.storedValue, name);
6290
+ atom.reportObserved();
6291
+ }
6292
+ catch (e) {
6293
+ throw fail$1(name + " property is declared twice");
6294
+ }
6277
6295
  res[name] = _this.getChildNode(node, name).snapshot;
6278
6296
  });
6279
6297
  if (applyPostProcess) {
@@ -7145,7 +7163,7 @@
7145
7163
  writable: true,
7146
7164
  value: function (parent, subpath, environment, initialValue) {
7147
7165
  if (this.optionalValues.indexOf(initialValue) >= 0) {
7148
- var defaultInstanceOrSnapshot = this.getDefaultInstanceOrSnapshot(parent);
7166
+ var defaultInstanceOrSnapshot = this.getDefaultInstanceOrSnapshot();
7149
7167
  return this._subtype.instantiate(parent, subpath, environment, defaultInstanceOrSnapshot);
7150
7168
  }
7151
7169
  return this._subtype.instantiate(parent, subpath, environment, initialValue);
@@ -7158,16 +7176,16 @@
7158
7176
  value: function (current, newValue, parent, subpath) {
7159
7177
  return this._subtype.reconcile(current, this.optionalValues.indexOf(newValue) < 0 && this._subtype.is(newValue)
7160
7178
  ? newValue
7161
- : this.getDefaultInstanceOrSnapshot(parent), parent, subpath);
7179
+ : this.getDefaultInstanceOrSnapshot(), parent, subpath);
7162
7180
  }
7163
7181
  });
7164
7182
  Object.defineProperty(OptionalValue.prototype, "getDefaultInstanceOrSnapshot", {
7165
7183
  enumerable: false,
7166
7184
  configurable: true,
7167
7185
  writable: true,
7168
- value: function (parent) {
7186
+ value: function () {
7169
7187
  var defaultInstanceOrSnapshot = typeof this._defaultValue === "function"
7170
- ? this._defaultValue(parent)
7188
+ ? this._defaultValue()
7171
7189
  : this._defaultValue;
7172
7190
  // while static values are already snapshots and checked on types.optional
7173
7191
  // generator functions must always be rechecked just in case