mobx-state-tree 7.0.0-pre.2 → 7.0.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.
@@ -6125,11 +6125,16 @@ var ModelType = /** @class */ (function (_super) {
6125
6125
  configurable: true,
6126
6126
  writable: true,
6127
6127
  value: function (self, actions) {
6128
+ var _this = this;
6128
6129
  // check if return is correct
6129
- if (!isPlainObject(actions))
6130
+ if (!isPlainObject(actions)) {
6130
6131
  throw new MstError("actions initializer should return a plain object containing actions");
6132
+ }
6131
6133
  // bind actions to the object created
6132
- Object.keys(actions).forEach(function (name) {
6134
+ Object.getOwnPropertyNames(actions).forEach(function (name) {
6135
+ if (name in _this.properties) {
6136
+ throw new MstError("'".concat(name, "' is a property and cannot be declared as an action"));
6137
+ }
6133
6138
  // warn if preprocessor was given
6134
6139
  if (name === PRE_PROCESS_SNAPSHOT)
6135
6140
  throw new MstError("Cannot define action '".concat(PRE_PROCESS_SNAPSHOT, "', it should be defined using 'type.preProcessSnapshot(fn)' instead"));
@@ -6179,10 +6184,17 @@ var ModelType = /** @class */ (function (_super) {
6179
6184
  configurable: true,
6180
6185
  writable: true,
6181
6186
  value: function (self, state) {
6187
+ var _this = this;
6182
6188
  // check views return
6183
- if (!isPlainObject(state))
6189
+ if (!isPlainObject(state)) {
6184
6190
  throw new MstError("volatile state initializer should return a plain object containing state");
6185
- set(self, state);
6191
+ }
6192
+ Object.getOwnPropertyNames(state).forEach(function (name) {
6193
+ if (name in _this.properties) {
6194
+ throw new MstError("'".concat(name, "' is a property and cannot be declared as volatile state"));
6195
+ }
6196
+ set(self, name, state[name]);
6197
+ });
6186
6198
  }
6187
6199
  });
6188
6200
  Object.defineProperty(ModelType.prototype, "extend", {
@@ -6224,19 +6236,24 @@ var ModelType = /** @class */ (function (_super) {
6224
6236
  configurable: true,
6225
6237
  writable: true,
6226
6238
  value: function (self, views) {
6239
+ var _this = this;
6227
6240
  // check views return
6228
- if (!isPlainObject(views))
6241
+ if (!isPlainObject(views)) {
6229
6242
  throw new MstError("views initializer should return a plain object containing views");
6230
- Object.getOwnPropertyNames(views).forEach(function (key) {
6243
+ }
6244
+ Object.getOwnPropertyNames(views).forEach(function (name) {
6231
6245
  var _a;
6246
+ if (name in _this.properties) {
6247
+ throw new MstError("'".concat(name, "' is a property and cannot be declared as a view"));
6248
+ }
6232
6249
  // is this a computed property?
6233
- var descriptor = Object.getOwnPropertyDescriptor(views, key);
6250
+ var descriptor = Object.getOwnPropertyDescriptor(views, name);
6234
6251
  if ("get" in descriptor) {
6235
- defineProperty$1(self, key, descriptor);
6236
- makeObservable(self, (_a = {}, _a[key] = computed, _a));
6252
+ defineProperty$1(self, name, descriptor);
6253
+ makeObservable(self, (_a = {}, _a[name] = computed, _a));
6237
6254
  }
6238
6255
  else if (typeof descriptor.value === "function") {
6239
- (!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(self, key, descriptor.value);
6256
+ (!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(self, name, descriptor.value);
6240
6257
  }
6241
6258
  else {
6242
6259
  throw new MstError("A view member should either be a function or getter based property");
@@ -6372,14 +6389,9 @@ var ModelType = /** @class */ (function (_super) {
6372
6389
  if (applyPostProcess === void 0) { applyPostProcess = true; }
6373
6390
  var res = {};
6374
6391
  this.forAllProps(function (name, type) {
6375
- try {
6376
- // TODO: FIXME, make sure the observable ref is used!
6377
- var atom = getAtom(node.storedValue, name);
6378
- atom.reportObserved();
6379
- }
6380
- catch (e) {
6381
- throw new MstError("".concat(name, " property is declared twice"));
6382
- }
6392
+ // TODO: FIXME, make sure the observable ref is used!
6393
+ var atom = getAtom(node.storedValue, name);
6394
+ atom.reportObserved();
6383
6395
  res[name] = _this.getChildNode(node, name).snapshot;
6384
6396
  });
6385
6397
  if (applyPostProcess) {
@@ -7173,7 +7185,7 @@ function union(optionsOrType) {
7173
7185
  otherTypes[_i - 1] = arguments[_i];
7174
7186
  }
7175
7187
  var options = isType(optionsOrType) ? undefined : optionsOrType;
7176
- var types = isType(optionsOrType) ? __spreadArray([optionsOrType], __read(otherTypes), false) : otherTypes;
7188
+ var types = (isType(optionsOrType) ? __spreadArray([optionsOrType], __read(otherTypes), false) : otherTypes);
7177
7189
  var name = "(" + types.map(function (type) { return type.name; }).join(" | ") + ")";
7178
7190
  // check all options
7179
7191
  if (devMode()) {
@@ -6107,11 +6107,16 @@
6107
6107
  configurable: true,
6108
6108
  writable: true,
6109
6109
  value: function (self, actions) {
6110
+ var _this = this;
6110
6111
  // check if return is correct
6111
- if (!isPlainObject(actions))
6112
+ if (!isPlainObject(actions)) {
6112
6113
  throw new MstError("actions initializer should return a plain object containing actions");
6114
+ }
6113
6115
  // bind actions to the object created
6114
- Object.keys(actions).forEach(function (name) {
6116
+ Object.getOwnPropertyNames(actions).forEach(function (name) {
6117
+ if (name in _this.properties) {
6118
+ throw new MstError("'".concat(name, "' is a property and cannot be declared as an action"));
6119
+ }
6115
6120
  // warn if preprocessor was given
6116
6121
  if (name === PRE_PROCESS_SNAPSHOT)
6117
6122
  throw new MstError("Cannot define action '".concat(PRE_PROCESS_SNAPSHOT, "', it should be defined using 'type.preProcessSnapshot(fn)' instead"));
@@ -6161,10 +6166,17 @@
6161
6166
  configurable: true,
6162
6167
  writable: true,
6163
6168
  value: function (self, state) {
6169
+ var _this = this;
6164
6170
  // check views return
6165
- if (!isPlainObject(state))
6171
+ if (!isPlainObject(state)) {
6166
6172
  throw new MstError("volatile state initializer should return a plain object containing state");
6167
- mobx.set(self, state);
6173
+ }
6174
+ Object.getOwnPropertyNames(state).forEach(function (name) {
6175
+ if (name in _this.properties) {
6176
+ throw new MstError("'".concat(name, "' is a property and cannot be declared as volatile state"));
6177
+ }
6178
+ mobx.set(self, name, state[name]);
6179
+ });
6168
6180
  }
6169
6181
  });
6170
6182
  Object.defineProperty(ModelType.prototype, "extend", {
@@ -6206,19 +6218,24 @@
6206
6218
  configurable: true,
6207
6219
  writable: true,
6208
6220
  value: function (self, views) {
6221
+ var _this = this;
6209
6222
  // check views return
6210
- if (!isPlainObject(views))
6223
+ if (!isPlainObject(views)) {
6211
6224
  throw new MstError("views initializer should return a plain object containing views");
6212
- Object.getOwnPropertyNames(views).forEach(function (key) {
6225
+ }
6226
+ Object.getOwnPropertyNames(views).forEach(function (name) {
6213
6227
  var _a;
6228
+ if (name in _this.properties) {
6229
+ throw new MstError("'".concat(name, "' is a property and cannot be declared as a view"));
6230
+ }
6214
6231
  // is this a computed property?
6215
- var descriptor = Object.getOwnPropertyDescriptor(views, key);
6232
+ var descriptor = Object.getOwnPropertyDescriptor(views, name);
6216
6233
  if ("get" in descriptor) {
6217
- mobx.defineProperty(self, key, descriptor);
6218
- mobx.makeObservable(self, (_a = {}, _a[key] = mobx.computed, _a));
6234
+ mobx.defineProperty(self, name, descriptor);
6235
+ mobx.makeObservable(self, (_a = {}, _a[name] = mobx.computed, _a));
6219
6236
  }
6220
6237
  else if (typeof descriptor.value === "function") {
6221
- (addHiddenWritableProp)(self, key, descriptor.value);
6238
+ (addHiddenWritableProp)(self, name, descriptor.value);
6222
6239
  }
6223
6240
  else {
6224
6241
  throw new MstError("A view member should either be a function or getter based property");
@@ -6354,14 +6371,9 @@
6354
6371
  if (applyPostProcess === void 0) { applyPostProcess = true; }
6355
6372
  var res = {};
6356
6373
  this.forAllProps(function (name, type) {
6357
- try {
6358
- // TODO: FIXME, make sure the observable ref is used!
6359
- var atom = mobx.getAtom(node.storedValue, name);
6360
- atom.reportObserved();
6361
- }
6362
- catch (e) {
6363
- throw new MstError("".concat(name, " property is declared twice"));
6364
- }
6374
+ // TODO: FIXME, make sure the observable ref is used!
6375
+ var atom = mobx.getAtom(node.storedValue, name);
6376
+ atom.reportObserved();
6365
6377
  res[name] = _this.getChildNode(node, name).snapshot;
6366
6378
  });
6367
6379
  if (applyPostProcess) {
@@ -7155,7 +7167,7 @@
7155
7167
  otherTypes[_i - 1] = arguments[_i];
7156
7168
  }
7157
7169
  var options = isType(optionsOrType) ? undefined : optionsOrType;
7158
- var types = isType(optionsOrType) ? __spreadArray([optionsOrType], __read(otherTypes), false) : otherTypes;
7170
+ var types = (isType(optionsOrType) ? __spreadArray([optionsOrType], __read(otherTypes), false) : otherTypes);
7159
7171
  var name = "(" + types.map(function (type) { return type.name; }).join(" | ") + ")";
7160
7172
  // check all options
7161
7173
  {