mobx 5.14.1 → 5.15.2

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/lib/mobx.umd.js CHANGED
@@ -79,7 +79,7 @@
79
79
  return ar;
80
80
  }
81
81
 
82
- var OBFUSCATED_ERROR = "An invariant failed, however the error is obfuscated because this is an production build.";
82
+ var OBFUSCATED_ERROR = "An invariant failed, however the error is obfuscated because this is a production build.";
83
83
  var EMPTY_ARRAY = [];
84
84
  Object.freeze(EMPTY_ARRAY);
85
85
  var EMPTY_OBJECT = {};
@@ -323,14 +323,27 @@
323
323
  }));
324
324
  }
325
325
  function initializeInstance(target) {
326
+ var e_1, _a;
326
327
  if (target[mobxDidRunLazyInitializersSymbol] === true)
327
328
  return;
328
329
  var decorators = target[mobxPendingDecorators];
329
330
  if (decorators) {
330
331
  addHiddenProp(target, mobxDidRunLazyInitializersSymbol, true);
331
- for (var key in decorators) {
332
- var d = decorators[key];
333
- d.propertyCreator(target, d.prop, d.descriptor, d.decoratorTarget, d.decoratorArguments);
332
+ // Build property key array from both strings and symbols
333
+ var keys = __spread(Object.getOwnPropertySymbols(decorators), Object.keys(decorators));
334
+ try {
335
+ for (var keys_1 = __values(keys), keys_1_1 = keys_1.next(); !keys_1_1.done; keys_1_1 = keys_1.next()) {
336
+ var key = keys_1_1.value;
337
+ var d = decorators[key];
338
+ d.propertyCreator(target, d.prop, d.descriptor, d.decoratorTarget, d.decoratorArguments);
339
+ }
340
+ }
341
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
342
+ finally {
343
+ try {
344
+ if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1);
345
+ }
346
+ finally { if (e_1) throw e_1.error; }
334
347
  }
335
348
  }
336
349
  }
@@ -373,7 +386,8 @@
373
386
  };
374
387
  }
375
388
  function quacksLikeADecorator(args) {
376
- return (((args.length === 2 || args.length === 3) && typeof args[1] === "string") ||
389
+ return (((args.length === 2 || args.length === 3) &&
390
+ (typeof args[1] === "string" || typeof args[1] === "symbol")) ||
377
391
  (args.length === 4 && args[3] === true));
378
392
  }
379
393
 
@@ -490,7 +504,7 @@
490
504
  */
491
505
  function createObservable(v, arg2, arg3) {
492
506
  // @observable someProp;
493
- if (typeof arguments[1] === "string") {
507
+ if (typeof arguments[1] === "string" || typeof arguments[1] === "symbol") {
494
508
  return deepDecorator.apply(null, arguments);
495
509
  }
496
510
  // it is an observable already, done
@@ -657,6 +671,8 @@
657
671
  case exports.IDerivationState.STALE:
658
672
  return true;
659
673
  case exports.IDerivationState.POSSIBLY_STALE: {
674
+ // state propagation can occur outside of action/reactive context #2195
675
+ var prevAllowStateReads = allowStateReadsStart(true);
660
676
  var prevUntracked = untrackedStart(); // no need for those computeds to be reported, they will be picked up in trackDerivedFunction.
661
677
  var obs = derivation.observing, l = obs.length;
662
678
  for (var i = 0; i < l; i++) {
@@ -672,6 +688,7 @@
672
688
  catch (e) {
673
689
  // we are not interested in the value *or* exception at this moment, but if there is one, notify all
674
690
  untrackedEnd(prevUntracked);
691
+ allowStateReadsEnd(prevAllowStateReads);
675
692
  return true;
676
693
  }
677
694
  }
@@ -680,12 +697,14 @@
680
697
  // invariantShouldCompute(derivation)
681
698
  if (derivation.dependenciesState === exports.IDerivationState.STALE) {
682
699
  untrackedEnd(prevUntracked);
700
+ allowStateReadsEnd(prevAllowStateReads);
683
701
  return true;
684
702
  }
685
703
  }
686
704
  }
687
705
  changeDependenciesStateTo0(derivation);
688
706
  untrackedEnd(prevUntracked);
707
+ allowStateReadsEnd(prevAllowStateReads);
689
708
  return false;
690
709
  }
691
710
  }
@@ -1134,8 +1153,7 @@
1134
1153
  this.isComputing = false; // to check for cycles
1135
1154
  this.isRunningSetter = false;
1136
1155
  this.isTracing = TraceMode.NONE;
1137
- if (process.env.NODE_ENV !== "production" && !options.get)
1138
- throw "[mobx] missing option for computed: get";
1156
+ invariant(options.get, "missing option for computed: get");
1139
1157
  this.derivation = options.get;
1140
1158
  this.name = options.name || "ComputedValue@" + getNextId();
1141
1159
  if (options.set)
@@ -1429,6 +1447,19 @@
1429
1447
  }
1430
1448
  return MobXGlobals;
1431
1449
  }());
1450
+ var mockGlobal = {};
1451
+ function getGlobal() {
1452
+ if (typeof window !== "undefined") {
1453
+ return window;
1454
+ }
1455
+ if (typeof global !== "undefined") {
1456
+ return global;
1457
+ }
1458
+ if (typeof self !== "undefined") {
1459
+ return self;
1460
+ }
1461
+ return mockGlobal;
1462
+ }
1432
1463
  var canMergeGlobalState = true;
1433
1464
  var isolateCalled = false;
1434
1465
  var globalState = (function () {
@@ -1482,16 +1513,6 @@
1482
1513
  globalState[key] = defaultGlobals[key];
1483
1514
  globalState.allowStateChanges = !globalState.enforceActions;
1484
1515
  }
1485
- var mockGlobal = {};
1486
- function getGlobal() {
1487
- if (typeof window !== "undefined") {
1488
- return window;
1489
- }
1490
- if (typeof global !== "undefined") {
1491
- return global;
1492
- }
1493
- return mockGlobal;
1494
- }
1495
1516
 
1496
1517
  function hasObservers(observable) {
1497
1518
  return observable.observers && observable.observers.size > 0;
@@ -1897,7 +1918,7 @@
1897
1918
  function spyReportStart(event) {
1898
1919
  if (process.env.NODE_ENV === "production")
1899
1920
  return;
1900
- var change = __assign({}, event, { spyReportStart: true });
1921
+ var change = __assign(__assign({}, event), { spyReportStart: true });
1901
1922
  spyReport(change);
1902
1923
  }
1903
1924
  var END_EVENT = { spyReportEnd: true };
@@ -1905,7 +1926,7 @@
1905
1926
  if (process.env.NODE_ENV === "production")
1906
1927
  return;
1907
1928
  if (change)
1908
- spyReport(__assign({}, change, { spyReportEnd: true }));
1929
+ spyReport(__assign(__assign({}, change), { spyReportEnd: true }));
1909
1930
  else
1910
1931
  spyReport(END_EVENT);
1911
1932
  }
@@ -2300,8 +2321,6 @@
2300
2321
  if (process.env.NODE_ENV !== "production") {
2301
2322
  if (!isPlainObject(properties))
2302
2323
  fail("'extendObservabe' only accepts plain objects as second argument");
2303
- if (Object.getOwnPropertyDescriptor(target, key))
2304
- fail("'extendObservable' can only be used to introduce new properties. Use 'set' or 'decorate' instead. The property '" + stringifyKey(key) + "' already exists on '" + target + "'");
2305
2324
  if (isComputed(descriptor.value))
2306
2325
  fail("Passing a 'computed' as initial property value is no longer supported by extendObservable. Use a getter or decorator instead");
2307
2326
  }
@@ -2355,9 +2374,16 @@
2355
2374
  }
2356
2375
 
2357
2376
  var generatorId = 0;
2377
+ function FlowCancellationError() {
2378
+ this.message = "FLOW_CANCELLED";
2379
+ }
2380
+ FlowCancellationError.prototype = Object.create(Error.prototype);
2381
+ function isFlowCancellationError(error) {
2382
+ return error instanceof FlowCancellationError;
2383
+ }
2358
2384
  function flow(generator) {
2359
2385
  if (arguments.length !== 1)
2360
- fail(!!process.env.NODE_ENV && "Flow expects one 1 argument and cannot be used as decorator");
2386
+ fail(!!process.env.NODE_ENV && "Flow expects 1 argument and cannot be used as decorator");
2361
2387
  var name = generator.name || "<unnamed flow>";
2362
2388
  // Implementation based on https://github.com/tj/co/blob/master/index.js
2363
2389
  return function () {
@@ -2410,13 +2436,13 @@
2410
2436
  if (pendingPromise)
2411
2437
  cancelPromise(pendingPromise);
2412
2438
  // Finally block can return (or yield) stuff..
2413
- var res = gen.return();
2439
+ var res = gen.return(undefined);
2414
2440
  // eat anything that promise would do, it's cancelled!
2415
2441
  var yieldedPromise = Promise.resolve(res.value);
2416
2442
  yieldedPromise.then(noop, noop);
2417
2443
  cancelPromise(yieldedPromise); // maybe it can be cancelled :)
2418
2444
  // reject our original promise
2419
- rejector(new Error("FLOW_CANCELLED"));
2445
+ rejector(new FlowCancellationError());
2420
2446
  }
2421
2447
  catch (e) {
2422
2448
  rejector(e); // there could be a throwing finally block
@@ -2863,7 +2889,7 @@
2863
2889
  return fail("the options 'onError' and 'promise' cannot be combined");
2864
2890
  var cancel;
2865
2891
  var res = new Promise(function (resolve, reject) {
2866
- var disposer = _when(predicate, resolve, __assign({}, opts, { onError: reject }));
2892
+ var disposer = _when(predicate, resolve, __assign(__assign({}, opts), { onError: reject }));
2867
2893
  cancel = function () {
2868
2894
  disposer();
2869
2895
  reject("WHEN_CANCELLED");
@@ -3180,7 +3206,7 @@
3180
3206
  // The reason why this is on right hand side here (and not above), is this way the uglifier will drop it, but it won't
3181
3207
  // cause any runtime overhead in development mode without NODE_ENV set, unless spying is enabled
3182
3208
  if (notifySpy && process.env.NODE_ENV !== "production")
3183
- spyReportStart(__assign({}, change, { name: this.atom.name }));
3209
+ spyReportStart(__assign(__assign({}, change), { name: this.atom.name }));
3184
3210
  this.atom.reportChanged();
3185
3211
  if (notify)
3186
3212
  notifyListeners(this, change);
@@ -3202,7 +3228,7 @@
3202
3228
  }
3203
3229
  : null;
3204
3230
  if (notifySpy && process.env.NODE_ENV !== "production")
3205
- spyReportStart(__assign({}, change, { name: this.atom.name }));
3231
+ spyReportStart(__assign(__assign({}, change), { name: this.atom.name }));
3206
3232
  this.atom.reportChanged();
3207
3233
  // conform: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/observe
3208
3234
  if (notify)
@@ -3471,7 +3497,7 @@
3471
3497
  }
3472
3498
  : null;
3473
3499
  if (notifySpy && process.env.NODE_ENV !== "production")
3474
- spyReportStart(__assign({}, change, { name: this.name, key: key }));
3500
+ spyReportStart(__assign(__assign({}, change), { name: this.name, key: key }));
3475
3501
  transaction(function () {
3476
3502
  _this._keysAtom.reportChanged();
3477
3503
  _this._updateHasMapEntry(key, false);
@@ -3509,7 +3535,7 @@
3509
3535
  }
3510
3536
  : null;
3511
3537
  if (notifySpy && process.env.NODE_ENV !== "production")
3512
- spyReportStart(__assign({}, change, { name: this.name, key: key }));
3538
+ spyReportStart(__assign(__assign({}, change), { name: this.name, key: key }));
3513
3539
  observable.setNewValue(newValue);
3514
3540
  if (notify)
3515
3541
  notifyListeners(this, change);
@@ -3538,7 +3564,7 @@
3538
3564
  }
3539
3565
  : null;
3540
3566
  if (notifySpy && process.env.NODE_ENV !== "production")
3541
- spyReportStart(__assign({}, change, { name: this.name, key: key }));
3567
+ spyReportStart(__assign(__assign({}, change), { name: this.name, key: key }));
3542
3568
  if (notify)
3543
3569
  notifyListeners(this, change);
3544
3570
  if (notifySpy && process.env.NODE_ENV !== "production")
@@ -3592,17 +3618,17 @@
3592
3618
  return this.entries();
3593
3619
  };
3594
3620
  ObservableMap.prototype.forEach = function (callback, thisArg) {
3595
- var e_1, _a;
3621
+ var e_1, _b;
3596
3622
  try {
3597
- for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) {
3598
- var _d = __read(_c.value, 2), key = _d[0], value = _d[1];
3623
+ for (var _c = __values(this), _d = _c.next(); !_d.done; _d = _c.next()) {
3624
+ var _e = __read(_d.value, 2), key = _e[0], value = _e[1];
3599
3625
  callback.call(thisArg, value, key, this);
3600
3626
  }
3601
3627
  }
3602
3628
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
3603
3629
  finally {
3604
3630
  try {
3605
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
3631
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
3606
3632
  }
3607
3633
  finally { if (e_1) throw e_1.error; }
3608
3634
  }
@@ -3617,8 +3643,8 @@
3617
3643
  if (isPlainObject(other))
3618
3644
  getPlainObjectKeys(other).forEach(function (key) { return _this.set(key, other[key]); });
3619
3645
  else if (Array.isArray(other))
3620
- other.forEach(function (_a) {
3621
- var _b = __read(_a, 2), key = _b[0], value = _b[1];
3646
+ other.forEach(function (_b) {
3647
+ var _c = __read(_b, 2), key = _c[0], value = _c[1];
3622
3648
  return _this.set(key, value);
3623
3649
  });
3624
3650
  else if (isES6Map(other)) {
@@ -3635,17 +3661,17 @@
3635
3661
  var _this = this;
3636
3662
  transaction(function () {
3637
3663
  untracked(function () {
3638
- var e_2, _a;
3664
+ var e_2, _b;
3639
3665
  try {
3640
- for (var _b = __values(_this.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
3641
- var key = _c.value;
3666
+ for (var _c = __values(_this.keys()), _d = _c.next(); !_d.done; _d = _c.next()) {
3667
+ var key = _d.value;
3642
3668
  _this.delete(key);
3643
3669
  }
3644
3670
  }
3645
3671
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
3646
3672
  finally {
3647
3673
  try {
3648
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
3674
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
3649
3675
  }
3650
3676
  finally { if (e_2) throw e_2.error; }
3651
3677
  }
@@ -3680,11 +3706,11 @@
3680
3706
  * If there are duplicating keys after converting them to strings, behaviour is undetermined.
3681
3707
  */
3682
3708
  ObservableMap.prototype.toPOJO = function () {
3683
- var e_3, _a;
3709
+ var e_3, _b;
3684
3710
  var res = {};
3685
3711
  try {
3686
- for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) {
3687
- var _d = __read(_c.value, 2), key = _d[0], value = _d[1];
3712
+ for (var _c = __values(this), _d = _c.next(); !_d.done; _d = _c.next()) {
3713
+ var _e = __read(_d.value, 2), key = _e[0], value = _e[1];
3688
3714
  // We lie about symbol key types due to https://github.com/Microsoft/TypeScript/issues/1863
3689
3715
  res[typeof key === "symbol" ? key : stringifyKey(key)] = value;
3690
3716
  }
@@ -3692,7 +3718,7 @@
3692
3718
  catch (e_3_1) { e_3 = { error: e_3_1 }; }
3693
3719
  finally {
3694
3720
  try {
3695
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
3721
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
3696
3722
  }
3697
3723
  finally { if (e_3) throw e_3.error; }
3698
3724
  }
@@ -3765,17 +3791,17 @@
3765
3791
  var _this = this;
3766
3792
  transaction(function () {
3767
3793
  untracked(function () {
3768
- var e_1, _a;
3794
+ var e_1, _b;
3769
3795
  try {
3770
- for (var _b = __values(_this._data.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
3771
- var value = _c.value;
3796
+ for (var _c = __values(_this._data.values()), _d = _c.next(); !_d.done; _d = _c.next()) {
3797
+ var value = _d.value;
3772
3798
  _this.delete(value);
3773
3799
  }
3774
3800
  }
3775
3801
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
3776
3802
  finally {
3777
3803
  try {
3778
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
3804
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
3779
3805
  }
3780
3806
  finally { if (e_1) throw e_1.error; }
3781
3807
  }
@@ -3783,17 +3809,17 @@
3783
3809
  });
3784
3810
  };
3785
3811
  ObservableSet.prototype.forEach = function (callbackFn, thisArg) {
3786
- var e_2, _a;
3812
+ var e_2, _b;
3787
3813
  try {
3788
- for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) {
3789
- var value = _c.value;
3814
+ for (var _c = __values(this), _d = _c.next(); !_d.done; _d = _c.next()) {
3815
+ var value = _d.value;
3790
3816
  callbackFn.call(thisArg, value, value, this);
3791
3817
  }
3792
3818
  }
3793
3819
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
3794
3820
  finally {
3795
3821
  try {
3796
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
3822
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
3797
3823
  }
3798
3824
  finally { if (e_2) throw e_2.error; }
3799
3825
  }
@@ -3865,7 +3891,7 @@
3865
3891
  }
3866
3892
  : null;
3867
3893
  if (notifySpy && process.env.NODE_ENV !== "production")
3868
- spyReportStart(__assign({}, change, { name: this.name }));
3894
+ spyReportStart(__assign(__assign({}, change), { name: this.name }));
3869
3895
  transaction(function () {
3870
3896
  _this._atom.reportChanged();
3871
3897
  _this._data.delete(value);
@@ -4000,7 +4026,7 @@
4000
4026
  }
4001
4027
  : null;
4002
4028
  if (notifySpy && process.env.NODE_ENV !== "production")
4003
- spyReportStart(__assign({}, change, { name: this.name, key: key }));
4029
+ spyReportStart(__assign(__assign({}, change), { name: this.name, key: key }));
4004
4030
  observable.setNewValue(newValue);
4005
4031
  if (notify)
4006
4032
  notifyListeners(this, change);
@@ -4090,7 +4116,7 @@
4090
4116
  }
4091
4117
  : null;
4092
4118
  if (notifySpy && process.env.NODE_ENV !== "production")
4093
- spyReportStart(__assign({}, change, { name: this.name, key: key }));
4119
+ spyReportStart(__assign(__assign({}, change), { name: this.name, key: key }));
4094
4120
  if (notify)
4095
4121
  notifyListeners(this, change);
4096
4122
  if (notifySpy && process.env.NODE_ENV !== "production")
@@ -4147,7 +4173,7 @@
4147
4173
  }
4148
4174
  : null;
4149
4175
  if (notifySpy && process.env.NODE_ENV !== "production")
4150
- spyReportStart(__assign({}, change, { name: this.name, key: key }));
4176
+ spyReportStart(__assign(__assign({}, change), { name: this.name, key: key }));
4151
4177
  if (notify)
4152
4178
  notifyListeners(this, change);
4153
4179
  if (notifySpy && process.env.NODE_ENV !== "production")
@@ -4339,9 +4365,6 @@
4339
4365
  var type = typeof a;
4340
4366
  if (type !== "function" && type !== "object" && typeof b != "object")
4341
4367
  return false;
4342
- // Unwrap any wrapped objects.
4343
- a = unwrap(a);
4344
- b = unwrap(b);
4345
4368
  // Compare `[[Class]]` names.
4346
4369
  var className = toString.call(a);
4347
4370
  if (className !== toString.call(b))
@@ -4369,7 +4392,18 @@
4369
4392
  return +a === +b;
4370
4393
  case "[object Symbol]":
4371
4394
  return (typeof Symbol !== "undefined" && Symbol.valueOf.call(a) === Symbol.valueOf.call(b));
4395
+ case "[object Map]":
4396
+ case "[object Set]":
4397
+ // Maps and Sets are unwrapped to arrays of entry-pairs, adding an incidental level.
4398
+ // Hide this extra level by increasing the depth.
4399
+ if (depth >= 0) {
4400
+ depth++;
4401
+ }
4402
+ break;
4372
4403
  }
4404
+ // Unwrap any wrapped objects.
4405
+ a = unwrap(a);
4406
+ b = unwrap(b);
4373
4407
  var areArrays = className === "[object Array]";
4374
4408
  if (!areArrays) {
4375
4409
  if (typeof a != "object" || typeof b != "object")
@@ -4523,11 +4557,14 @@
4523
4557
  }
4524
4558
 
4525
4559
  exports.$mobx = $mobx;
4560
+ exports.FlowCancellationError = FlowCancellationError;
4526
4561
  exports.ObservableMap = ObservableMap;
4527
4562
  exports.ObservableSet = ObservableSet;
4528
4563
  exports.Reaction = Reaction;
4529
4564
  exports._allowStateChanges = allowStateChanges;
4530
4565
  exports._allowStateChangesInsideComputed = allowStateChangesInsideComputed;
4566
+ exports._allowStateReadsEnd = allowStateReadsEnd;
4567
+ exports._allowStateReadsStart = allowStateReadsStart;
4531
4568
  exports._endAction = _endAction;
4532
4569
  exports._getAdministration = getAdministration;
4533
4570
  exports._getGlobalState = getGlobalState;
@@ -4557,6 +4594,7 @@
4557
4594
  exports.isBoxedObservable = isObservableValue;
4558
4595
  exports.isComputed = isComputed;
4559
4596
  exports.isComputedProp = isComputedProp;
4597
+ exports.isFlowCancellationError = isFlowCancellationError;
4560
4598
  exports.isObservable = isObservable;
4561
4599
  exports.isObservableArray = isObservableArray;
4562
4600
  exports.isObservableMap = isObservableMap;