mobx 6.1.4 → 6.1.8

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.
Files changed (47) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/dist/api/annotation.d.ts +0 -1
  3. package/dist/api/iscomputed.d.ts +2 -2
  4. package/dist/core/computedvalue.d.ts +0 -1
  5. package/dist/core/derivation.d.ts +0 -1
  6. package/dist/core/reaction.d.ts +0 -1
  7. package/dist/mobx.cjs.development.js +92 -46
  8. package/dist/mobx.cjs.development.js.map +1 -1
  9. package/dist/mobx.cjs.production.min.js +1 -1
  10. package/dist/mobx.cjs.production.min.js.map +1 -1
  11. package/dist/mobx.esm.development.js +92 -46
  12. package/dist/mobx.esm.development.js.map +1 -1
  13. package/dist/mobx.esm.js +92 -46
  14. package/dist/mobx.esm.js.map +1 -1
  15. package/dist/mobx.esm.production.min.js +1 -1
  16. package/dist/mobx.esm.production.min.js.map +1 -1
  17. package/dist/mobx.umd.development.js +92 -46
  18. package/dist/mobx.umd.development.js.map +1 -1
  19. package/dist/mobx.umd.production.min.js +1 -1
  20. package/dist/mobx.umd.production.min.js.map +1 -1
  21. package/dist/types/observablearray.d.ts +1 -1
  22. package/dist/types/type-utils.d.ts +1 -1
  23. package/package.json +1 -1
  24. package/src/api/annotation.ts +0 -1
  25. package/src/api/autorun.ts +3 -2
  26. package/src/api/computed.ts +1 -1
  27. package/src/api/decorators.ts +1 -4
  28. package/src/api/iscomputed.ts +2 -2
  29. package/src/api/makeObservable.ts +10 -2
  30. package/src/api/observable.ts +1 -1
  31. package/src/api/when.ts +5 -2
  32. package/src/core/atom.ts +1 -1
  33. package/src/core/computedvalue.ts +7 -3
  34. package/src/core/derivation.ts +0 -1
  35. package/src/core/reaction.ts +1 -2
  36. package/src/types/actionannotation.ts +3 -2
  37. package/src/types/computedannotation.ts +3 -2
  38. package/src/types/flowannotation.ts +3 -2
  39. package/src/types/legacyobservablearray.ts +1 -1
  40. package/src/types/observableannotation.ts +3 -2
  41. package/src/types/observablearray.ts +5 -4
  42. package/src/types/observablemap.ts +4 -4
  43. package/src/types/observableobject.ts +27 -14
  44. package/src/types/observableset.ts +1 -1
  45. package/src/types/observablevalue.ts +1 -1
  46. package/src/types/type-utils.ts +11 -4
  47. package/src/utils/global.ts +3 -0
@@ -82,6 +82,10 @@
82
82
 
83
83
  var mockGlobal = {};
84
84
  function getGlobal() {
85
+ if (typeof globalThis !== "undefined") {
86
+ return globalThis;
87
+ }
88
+
85
89
  if (typeof window !== "undefined") {
86
90
  return window;
87
91
  }
@@ -363,9 +367,7 @@
363
367
  assertNotDecorated(prototype, annotation, key); // Ignore override
364
368
 
365
369
  if (!isOverride(annotation)) {
366
- prototype[storedAnnotationsSymbol][key] = _extends({}, annotation, {
367
- isDecorator_: true
368
- });
370
+ prototype[storedAnnotationsSymbol][key] = annotation;
369
371
  }
370
372
  }
371
373
 
@@ -405,7 +407,7 @@
405
407
  */
406
408
  function Atom(name_) {
407
409
  if (name_ === void 0) {
408
- name_ = "Atom@" + getNextId();
410
+ name_ = "Atom@" + getNextId() ;
409
411
  }
410
412
 
411
413
  this.name_ = void 0;
@@ -595,7 +597,7 @@
595
597
  }
596
598
 
597
599
  function make_$1(adm, key) {
598
- var _this$options_$bound, _this$options_;
600
+ var _this$options_$bound, _this$options_, _adm$target_$storedAn;
599
601
 
600
602
  var annotated = false;
601
603
  var source = adm.target_;
@@ -645,7 +647,7 @@
645
647
 
646
648
  if (annotated) {
647
649
  recordAnnotationApplied(adm, this, key);
648
- } else if (!this.isDecorator_) {
650
+ } else if (!((_adm$target_$storedAn = adm.target_[storedAnnotationsSymbol]) == null ? void 0 : _adm$target_$storedAn[key])) {
649
651
  // Throw on missing key, except for decorators:
650
652
  // Decorator annotations are collected from whole prototype chain.
651
653
  // When called from super() some props may not exist yet.
@@ -709,6 +711,8 @@
709
711
  }
710
712
 
711
713
  function make_$2(adm, key) {
714
+ var _adm$target_$storedAn;
715
+
712
716
  var annotated = false;
713
717
  var source = adm.target_;
714
718
 
@@ -746,7 +750,7 @@
746
750
 
747
751
  if (annotated) {
748
752
  recordAnnotationApplied(adm, this, key);
749
- } else if (!this.isDecorator_) {
753
+ } else if (!((_adm$target_$storedAn = adm.target_[storedAnnotationsSymbol]) == null ? void 0 : _adm$target_$storedAn[key])) {
750
754
  // Throw on missing key, except for decorators:
751
755
  // Decorator annotations are collected from whole prototype chain.
752
756
  // When called from super() some props may not exist yet.
@@ -800,6 +804,8 @@
800
804
  }
801
805
 
802
806
  function make_$3(adm, key) {
807
+ var _adm$target_$storedAn;
808
+
803
809
  var source = adm.target_;
804
810
 
805
811
  while (source && source !== objectPrototype) {
@@ -824,7 +830,7 @@
824
830
  source = Object.getPrototypeOf(source);
825
831
  }
826
832
 
827
- if (!this.isDecorator_) {
833
+ if (!((_adm$target_$storedAn = adm.target_[storedAnnotationsSymbol]) == null ? void 0 : _adm$target_$storedAn[key])) {
828
834
  // Throw on missing key, except for decorators:
829
835
  // Decorator annotations are collected from whole prototype chain.
830
836
  // When called from super() some props may not exist yet.
@@ -861,6 +867,8 @@
861
867
  }
862
868
 
863
869
  function make_$4(adm, key) {
870
+ var _adm$target_$storedAn;
871
+
864
872
  var source = adm.target_; // Copy props from proto as well, see test:
865
873
  // "decorate should work with Object.create"
866
874
 
@@ -885,7 +893,7 @@
885
893
  source = Object.getPrototypeOf(source);
886
894
  }
887
895
 
888
- if (!this.isDecorator_) {
896
+ if (!((_adm$target_$storedAn = adm.target_[storedAnnotationsSymbol]) == null ? void 0 : _adm$target_$storedAn[key])) {
889
897
  // Throw on missing key, except for decorators:
890
898
  // Decorator annotations are collected from whole prototype chain.
891
899
  // When called from super() some props may not exist yet.
@@ -969,7 +977,7 @@
969
977
 
970
978
  if (typeof v === "object" && v !== null) return v; // anything else
971
979
 
972
- return observable.box(v);
980
+ return observable.box(v, arg2);
973
981
  }
974
982
 
975
983
  Object.assign(createObservable, observableDecoratorAnnotation);
@@ -1031,7 +1039,7 @@
1031
1039
 
1032
1040
  var opts = isPlainObject(arg2) ? arg2 : {};
1033
1041
  opts.get = arg1;
1034
- opts.name = opts.name || arg1.name || "";
1042
+ opts.name || (opts.name = arg1.name || "");
1035
1043
  /* for generated name */
1036
1044
 
1037
1045
  return new ComputedValue(opts);
@@ -1179,7 +1187,7 @@
1179
1187
  var _this;
1180
1188
 
1181
1189
  if (name_ === void 0) {
1182
- name_ = "ObservableValue@" + getNextId();
1190
+ name_ = "ObservableValue@" + getNextId() ;
1183
1191
  }
1184
1192
 
1185
1193
  if (notifySpy === void 0) {
@@ -1379,7 +1387,6 @@
1379
1387
  this.lastAccessedBy_ = 0;
1380
1388
  this.lowestObserverState_ = IDerivationState_.UP_TO_DATE_;
1381
1389
  this.unboundDepsCount_ = 0;
1382
- this.mapid_ = "#" + getNextId();
1383
1390
  this.value_ = new CaughtException(null);
1384
1391
  this.name_ = void 0;
1385
1392
  this.triggeredBy_ = void 0;
@@ -1396,8 +1403,12 @@
1396
1403
  this.onBUOL = void 0;
1397
1404
  if (!options.get) die(31);
1398
1405
  this.derivation = options.get;
1399
- this.name_ = options.name || "ComputedValue@" + getNextId();
1400
- if (options.set) this.setter_ = createAction(this.name_ + "-setter", options.set);
1406
+ this.name_ = options.name || ( "ComputedValue@" + getNextId() );
1407
+
1408
+ if (options.set) {
1409
+ this.setter_ = createAction( this.name_ + "-setter" , options.set);
1410
+ }
1411
+
1401
1412
  this.equals_ = options.equals || (options.compareStructural || options.struct ? comparer.structural : comparer["default"]);
1402
1413
  this.scope_ = options.context;
1403
1414
  this.requiresReaction_ = !!options.requiresReaction;
@@ -2168,7 +2179,7 @@
2168
2179
  // nodes we are looking at. Our value depends on these nodes
2169
2180
  function Reaction(name_, onInvalidate_, errorHandler_, requiresObservable_) {
2170
2181
  if (name_ === void 0) {
2171
- name_ = "Reaction@" + getNextId();
2182
+ name_ = "Reaction@" + getNextId() ;
2172
2183
  }
2173
2184
 
2174
2185
  if (requiresObservable_ === void 0) {
@@ -2185,7 +2196,6 @@
2185
2196
  this.diffValue_ = 0;
2186
2197
  this.runId_ = 0;
2187
2198
  this.unboundDepsCount_ = 0;
2188
- this.mapid_ = "#" + getNextId();
2189
2199
  this.isDisposed_ = false;
2190
2200
  this.isScheduled_ = false;
2191
2201
  this.isTrackPending_ = false;
@@ -2518,6 +2528,8 @@
2518
2528
  */
2519
2529
 
2520
2530
  function autorun(view, opts) {
2531
+ var _opts$name, _opts;
2532
+
2521
2533
  if (opts === void 0) {
2522
2534
  opts = EMPTY_OBJECT;
2523
2535
  }
@@ -2527,7 +2539,7 @@
2527
2539
  if (isAction(view)) die("Autorun does not accept actions since actions are untrackable");
2528
2540
  }
2529
2541
 
2530
- var name = opts && opts.name || view.name || "Autorun@" + getNextId();
2542
+ var name = (_opts$name = (_opts = opts) == null ? void 0 : _opts.name) != null ? _opts$name : view.name || "Autorun@" + getNextId() ;
2531
2543
  var runSync = !opts.scheduler && !opts.delay;
2532
2544
  var reaction;
2533
2545
 
@@ -2570,6 +2582,8 @@
2570
2582
  }
2571
2583
 
2572
2584
  function reaction(expression, effect, opts) {
2585
+ var _opts$name2;
2586
+
2573
2587
  if (opts === void 0) {
2574
2588
  opts = EMPTY_OBJECT;
2575
2589
  }
@@ -2579,7 +2593,7 @@
2579
2593
  if (!isPlainObject(opts)) die("Third argument of reactions should be an object");
2580
2594
  }
2581
2595
 
2582
- var name = opts.name || "Reaction@" + getNextId();
2596
+ var name = (_opts$name2 = opts.name) != null ? _opts$name2 : "Reaction@" + getNextId() ;
2583
2597
  var effectAction = action(name, opts.onError ? wrapErrorHandler(opts.onError, effect) : effect);
2584
2598
  var runSync = !opts.scheduler && !opts.delay;
2585
2599
  var scheduler = createSchedulerFromOptions(opts);
@@ -3216,8 +3230,8 @@
3216
3230
  }, opts.timeout);
3217
3231
  }
3218
3232
 
3219
- opts.name = opts.name || "When@" + getNextId();
3220
- var effectAction = createAction(opts.name + "-effect", effect); // eslint-disable-next-line
3233
+ opts.name = opts.name || "When@" + getNextId() ;
3234
+ var effectAction = createAction( opts.name + "-effect" , effect); // eslint-disable-next-line
3221
3235
 
3222
3236
  var disposer = autorun(function (r) {
3223
3237
  // predicate should not change state
@@ -3401,9 +3415,20 @@
3401
3415
  startBatch();
3402
3416
 
3403
3417
  try {
3418
+ // Use cached inferred annotations if available (only in classes)
3404
3419
  if (target[inferredAnnotationsSymbol]) {
3405
- for (var key in target[inferredAnnotationsSymbol]) {
3406
- adm.make_(key, target[inferredAnnotationsSymbol][key]);
3420
+ target[inferredAnnotationsSymbol].forEach(function (value, key) {
3421
+ return adm.make_(key, value);
3422
+ }); // Overrides are not cached, unless `true`, see #2832
3423
+
3424
+ if (overrides) {
3425
+ ownKeys(overrides).forEach(function (key) {
3426
+ var annotation = overrides[key];
3427
+
3428
+ if (annotation !== true) {
3429
+ adm.make_(key, annotation);
3430
+ }
3431
+ });
3407
3432
  }
3408
3433
  } else {
3409
3434
  var _ignoreKeys;
@@ -3474,6 +3499,10 @@
3474
3499
  var ObservableArrayAdministration = /*#__PURE__*/function () {
3475
3500
  // this is the prop that gets proxied, so can't replace it!
3476
3501
  function ObservableArrayAdministration(name, enhancer, owned_, legacyMode_) {
3502
+ if (name === void 0) {
3503
+ name = "ObservableArray@" + getNextId() ;
3504
+ }
3505
+
3477
3506
  this.owned_ = void 0;
3478
3507
  this.legacyMode_ = void 0;
3479
3508
  this.atom_ = void 0;
@@ -3486,10 +3515,10 @@
3486
3515
  this.lastKnownLength_ = 0;
3487
3516
  this.owned_ = owned_;
3488
3517
  this.legacyMode_ = legacyMode_;
3489
- this.atom_ = new Atom(name || "ObservableArray@" + getNextId());
3518
+ this.atom_ = new Atom(name);
3490
3519
 
3491
3520
  this.enhancer_ = function (newV, oldV) {
3492
- return enhancer(newV, oldV, name + "[..]");
3521
+ return enhancer(newV, oldV, name + "[..]" );
3493
3522
  };
3494
3523
  }
3495
3524
 
@@ -3704,7 +3733,7 @@
3704
3733
  }();
3705
3734
  function createObservableArray(initialValues, enhancer, name, owned) {
3706
3735
  if (name === void 0) {
3707
- name = "ObservableArray@" + getNextId();
3736
+ name = "ObservableArray@" + getNextId() ;
3708
3737
  }
3709
3738
 
3710
3739
  if (owned === void 0) {
@@ -3926,7 +3955,7 @@
3926
3955
  }
3927
3956
 
3928
3957
  if (name_ === void 0) {
3929
- name_ = "ObservableMap@" + getNextId();
3958
+ name_ = "ObservableMap@" + getNextId() ;
3930
3959
  }
3931
3960
 
3932
3961
  this.enhancer_ = void 0;
@@ -3945,7 +3974,7 @@
3945
3974
  die(18);
3946
3975
  }
3947
3976
 
3948
- this.keysAtom_ = createAtom(this.name_ + ".keys()");
3977
+ this.keysAtom_ = createAtom( this.name_ + ".keys()" );
3949
3978
  this.data_ = new Map();
3950
3979
  this.hasMap_ = new Map();
3951
3980
  this.merge(initialData);
@@ -3964,7 +3993,7 @@
3964
3993
  var entry = this.hasMap_.get(key);
3965
3994
 
3966
3995
  if (!entry) {
3967
- var newEntry = entry = new ObservableValue(this.has_(key), referenceEnhancer, this.name_ + "." + stringifyKey(key) + "?", false);
3996
+ var newEntry = entry = new ObservableValue(this.has_(key), referenceEnhancer, this.name_ + "." + stringifyKey(key) + "?" , false);
3968
3997
  this.hasMap_.set(key, newEntry);
3969
3998
  onBecomeUnobserved(newEntry, function () {
3970
3999
  return _this.hasMap_["delete"](key);
@@ -4080,7 +4109,7 @@
4080
4109
 
4081
4110
  checkIfStateModificationsAreAllowed(this.keysAtom_);
4082
4111
  transaction(function () {
4083
- var observable = new ObservableValue(newValue, _this3.enhancer_, _this3.name_ + "." + stringifyKey(key), false);
4112
+ var observable = new ObservableValue(newValue, _this3.enhancer_, _this3.name_ + "." + stringifyKey(key) , false);
4084
4113
 
4085
4114
  _this3.data_.set(key, observable);
4086
4115
 
@@ -4377,7 +4406,7 @@
4377
4406
  }
4378
4407
 
4379
4408
  if (name_ === void 0) {
4380
- name_ = "ObservableSet@" + getNextId();
4409
+ name_ = "ObservableSet@" + getNextId() ;
4381
4410
  }
4382
4411
 
4383
4412
  this.name_ = void 0;
@@ -4661,7 +4690,7 @@
4661
4690
  this.name_ = name_;
4662
4691
  this.defaultAnnotation_ = defaultAnnotation_;
4663
4692
  this.autoBind_ = autoBind_;
4664
- this.keysAtom_ = new Atom(name_ + ".keys"); // Optimization: we use this frequently
4693
+ this.keysAtom_ = new Atom( this.name_ + ".keys" ); // Optimization: we use this frequently
4665
4694
 
4666
4695
  this.isPlainObject_ = isPlainObject(this.target_);
4667
4696
 
@@ -4788,7 +4817,7 @@
4788
4817
  var entry = this.pendingKeys_.get(key);
4789
4818
 
4790
4819
  if (!entry) {
4791
- entry = new ObservableValue(key in this.target_, referenceEnhancer, this.name_ + "." + stringifyKey(key) + "?", false);
4820
+ entry = new ObservableValue(key in this.target_, referenceEnhancer, this.name_ + "." + stringifyKey(key) + "?" , false);
4792
4821
  this.pendingKeys_.set(key, entry);
4793
4822
  }
4794
4823
 
@@ -4848,7 +4877,7 @@
4848
4877
  var _this$target_$inferre;
4849
4878
 
4850
4879
  // Inherited is fine - annotation cannot differ in subclass
4851
- var annotation = (_this$target_$inferre = this.target_[inferredAnnotationsSymbol]) == null ? void 0 : _this$target_$inferre[key];
4880
+ var annotation = (_this$target_$inferre = this.target_[inferredAnnotationsSymbol]) == null ? void 0 : _this$target_$inferre.get(key);
4852
4881
  if (annotation) return annotation;
4853
4882
  var current = this.target_;
4854
4883
 
@@ -4875,10 +4904,10 @@
4875
4904
  var closestProto = Object.getPrototypeOf(this.target_);
4876
4905
 
4877
4906
  if (!hasProp(closestProto, inferredAnnotationsSymbol)) {
4878
- addHiddenProp(closestProto, inferredAnnotationsSymbol, {});
4907
+ addHiddenProp(closestProto, inferredAnnotationsSymbol, new Map());
4879
4908
  }
4880
4909
 
4881
- closestProto[inferredAnnotationsSymbol][key] = annotation;
4910
+ closestProto[inferredAnnotationsSymbol].set(key, annotation);
4882
4911
  }
4883
4912
 
4884
4913
  return annotation;
@@ -4986,7 +5015,7 @@
4986
5015
  defineProperty(this.target_, key, descriptor);
4987
5016
  }
4988
5017
 
4989
- var _observable = new ObservableValue(value, enhancer, this.name_ + "." + stringifyKey(key), false);
5018
+ var _observable = new ObservableValue(value, enhancer, "development" !== "production" ? this.name_ + "." + key.toString() : "ObservableObject.key", false);
4990
5019
 
4991
5020
  this.values_.set(key, _observable); // Notify (value possibly changed by ObservableValue)
4992
5021
 
@@ -5025,7 +5054,7 @@
5025
5054
  if (!change) return null;
5026
5055
  }
5027
5056
 
5028
- options.name || (options.name = this.name_ + "." + stringifyKey(key));
5057
+ options.name || (options.name = "development" !== "production" ? this.name_ + "." + key.toString() : "ObservableObject.key");
5029
5058
  options.context = this.proxy_ || this.target_;
5030
5059
  var cachedDescriptor = getCachedObservablePropDescriptor(key);
5031
5060
  var descriptor = {
@@ -5217,10 +5246,17 @@
5217
5246
  die("Options can't be provided for already observable objects.");
5218
5247
  }
5219
5248
 
5220
- if (hasProp(target, $mobx)) return target;
5249
+ if (hasProp(target, $mobx)) {
5250
+ if ( !(getAdministration(target) instanceof ObservableObjectAdministration)) {
5251
+ die("Cannot convert '" + getDebugName(target) + "' into observable object:" + "\nThe target is already observable of different type." + "\nExtending builtins is not supported.");
5252
+ }
5253
+
5254
+ return target;
5255
+ }
5256
+
5221
5257
  if ( !Object.isExtensible(target)) die("Cannot make the designated object observable; it is not extensible");
5222
- var name = (_options$name = options == null ? void 0 : options.name) != null ? _options$name : (isPlainObject(target) ? "ObservableObject" : target.constructor.name) + "@" + getNextId();
5223
- var adm = new ObservableObjectAdministration(target, new Map(), stringifyKey(name), getAnnotationFromOptions(options), options == null ? void 0 : options.autoBind);
5258
+ var name = (_options$name = options == null ? void 0 : options.name) != null ? _options$name : (isPlainObject(target) ? "ObservableObject" : target.constructor.name) + "@" + getNextId() ;
5259
+ var adm = new ObservableObjectAdministration(target, new Map(), String(name), getAnnotationFromOptions(options), options == null ? void 0 : options.autoBind);
5224
5260
  addHiddenProp(target, $mobx, adm);
5225
5261
  return target;
5226
5262
  }
@@ -5245,14 +5281,14 @@
5245
5281
  return false;
5246
5282
  }
5247
5283
  function recordAnnotationApplied(adm, annotation, key) {
5284
+ var _adm$target_$storedAn;
5285
+
5248
5286
  {
5249
5287
  adm.appliedAnnotations_[key] = annotation;
5250
5288
  } // Remove applied decorator annotation so we don't try to apply it again in subclass constructor
5251
5289
 
5252
5290
 
5253
- if (annotation.isDecorator_) {
5254
- delete adm.target_[storedAnnotationsSymbol][key];
5255
- }
5291
+ (_adm$target_$storedAn = adm.target_[storedAnnotationsSymbol]) == null ? true : delete _adm$target_$storedAn[key];
5256
5292
  }
5257
5293
 
5258
5294
  function assertAnnotable(adm, annotation, key) {
@@ -5332,7 +5368,7 @@
5332
5368
  var _this;
5333
5369
 
5334
5370
  if (name === void 0) {
5335
- name = "ObservableArray@" + getNextId();
5371
+ name = "ObservableArray@" + getNextId() ;
5336
5372
  }
5337
5373
 
5338
5374
  if (owned === void 0) {
@@ -5491,7 +5527,17 @@
5491
5527
  }
5492
5528
  function getDebugName(thing, property) {
5493
5529
  var named;
5494
- if (property !== undefined) named = getAtom(thing, property);else if (isObservableObject(thing) || isObservableMap(thing) || isObservableSet(thing)) named = getAdministration(thing);else named = getAtom(thing); // valid for arrays as well
5530
+
5531
+ if (property !== undefined) {
5532
+ named = getAtom(thing, property);
5533
+ } else if (isAction(thing)) {
5534
+ return thing.name;
5535
+ } else if (isObservableObject(thing) || isObservableMap(thing) || isObservableSet(thing)) {
5536
+ named = getAdministration(thing);
5537
+ } else {
5538
+ // valid for arrays as well
5539
+ named = getAtom(thing);
5540
+ }
5495
5541
 
5496
5542
  return named.name_;
5497
5543
  }