mobx 7.0.0 → 7.0.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/dist/mobx.mjs CHANGED
@@ -1,3 +1,5 @@
1
+ const __MOBX_DEV__ = process.env.NODE_ENV !== "production";
2
+
1
3
  const niceErrors = {
2
4
  0: `Invalid value for configuration 'enforceActions', expected 'never', 'always' or 'observed'`,
3
5
  1(annotationType, key) {
@@ -84,9 +86,9 @@ const niceErrors = {
84
86
  return `'${annotationType}' can only be used with 'makeObservable'`;
85
87
  }
86
88
  };
87
- const errors = process.env.NODE_ENV !== "production" ? niceErrors : {};
89
+ const errors = __MOBX_DEV__ ? niceErrors : {};
88
90
  function die(error, ...args) {
89
- if (process.env.NODE_ENV !== "production") {
91
+ if (__MOBX_DEV__) {
90
92
  let e = typeof error === "string" ? error : errors[error];
91
93
  if (typeof e === "function") e = e.apply(null, args);
92
94
  throw new Error(`[MobX] ${e}`);
@@ -246,7 +248,7 @@ function setFlag(flags, mask, newValue) {
246
248
  }
247
249
 
248
250
  function assert20223DecoratorType(context, types) {
249
- if (process.env.NODE_ENV !== "production" && !types.includes(context.kind)) {
251
+ if (__MOBX_DEV__ && !types.includes(context.kind)) {
250
252
  die(`The decorator applied to '${String(context.name)}' cannot be used on a ${context.kind} element`);
251
253
  }
252
254
  }
@@ -257,10 +259,11 @@ class Atom {
257
259
  * Create a new atom. For debugging purposes it is recommended to give it a name.
258
260
  * The onBecomeObserved and onBecomeUnobserved callbacks can be used for resource management.
259
261
  */
260
- constructor(name_ = process.env.NODE_ENV !== "production" ? "Atom@" + getNextId() : "Atom") {
262
+ constructor(name_ = __MOBX_DEV__ ? "Atom@" + getNextId() : "Atom") {
261
263
  this.name_ = void 0;
262
264
  this.flags_ = 0b000;
263
- this.observers_ = new Set();
265
+ // Allocated lazily on first observer to save memory.
266
+ this.observers_ = null;
264
267
  this.lastAccessedBy_ = 0;
265
268
  this.lowestObserverState_ = -1 /* IDerivationState_.NOT_TRACKING_ */;
266
269
  // onBecomeObservedListeners
@@ -342,6 +345,10 @@ function compareShallow(a, b) {
342
345
  const compareDefault = Object.is;
343
346
 
344
347
  function deepEnhancer(v, _, name) {
348
+ // primitives can never be made observable; skip the type checks below
349
+ if (v === null || typeof v !== "object" && typeof v !== "function") {
350
+ return v;
351
+ }
345
352
  // it is an observable already, done
346
353
  if (isObservable(v)) {
347
354
  return v;
@@ -407,7 +414,7 @@ function shallowEnhancer(v, _, name) {
407
414
  deep: false
408
415
  });
409
416
  }
410
- if (process.env.NODE_ENV !== "production") {
417
+ if (__MOBX_DEV__) {
411
418
  die("The shallow modifier / decorator can only used in combination with arrays, objects, maps and sets");
412
419
  }
413
420
  }
@@ -416,7 +423,7 @@ function referenceEnhancer(newValue) {
416
423
  return newValue;
417
424
  }
418
425
  function refStructEnhancer(v, oldValue) {
419
- if (process.env.NODE_ENV !== "production" && isObservable(v)) {
426
+ if (__MOBX_DEV__ && isObservable(v)) {
420
427
  die(`observable.struct should not be used with observable values`);
421
428
  }
422
429
  if (deepEqual(v, oldValue)) {
@@ -436,11 +443,11 @@ function isOverride(annotation) {
436
443
  }
437
444
  function make_$6(adm, key) {
438
445
  // Must not be plain object
439
- if (process.env.NODE_ENV !== "production" && adm.isPlainObject_) {
446
+ if (__MOBX_DEV__ && adm.isPlainObject_) {
440
447
  die(`Cannot apply '${this.annotationType_}' to '${adm.name_}.${key.toString()}':` + `\n'${this.annotationType_}' cannot be used on plain objects.`);
441
448
  }
442
449
  // Must override something
443
- if (process.env.NODE_ENV !== "production" && !hasProp(adm.appliedAnnotations_, key)) {
450
+ if (__MOBX_DEV__ && !hasProp(adm.appliedAnnotations_, key)) {
444
451
  die(`'${adm.name_}.${key.toString()}' is annotated with '${this.annotationType_}', ` + `but no such annotated member was found on prototype.`);
445
452
  }
446
453
  return 0 /* MakeResult.Cancel */;
@@ -482,7 +489,7 @@ function extend_$4(adm, key, descriptor, proxyTrap) {
482
489
  return adm.defineProperty_(key, actionDescriptor, proxyTrap);
483
490
  }
484
491
  function decorateAction20223_(annotation, mthd, context) {
485
- if (process.env.NODE_ENV !== "production") {
492
+ if (__MOBX_DEV__) {
486
493
  assert20223DecoratorType(context, ["method", "field"]);
487
494
  }
488
495
  const {
@@ -531,7 +538,7 @@ function assertActionDescriptor(adm, {
531
538
  }, key, {
532
539
  value
533
540
  }) {
534
- if (process.env.NODE_ENV !== "production" && !isFunction(value)) {
541
+ if (__MOBX_DEV__ && !isFunction(value)) {
535
542
  die(`Cannot apply '${annotationType_}' to '${adm.name_}.${key.toString()}':` + `\n'${annotationType_}' can only be used on properties with a function value.`);
536
543
  }
537
544
  }
@@ -599,7 +606,7 @@ function extend_$3(adm, key, descriptor, proxyTrap) {
599
606
  }
600
607
  function decorateFlow20223_(annotation, mthd, context) {
601
608
  var _annotation$options_;
602
- if (process.env.NODE_ENV !== "production") {
609
+ if (__MOBX_DEV__) {
603
610
  assert20223DecoratorType(context, ["method"]);
604
611
  }
605
612
  const {
@@ -624,7 +631,7 @@ function assertFlowDescriptor(adm, {
624
631
  }, key, {
625
632
  value
626
633
  }) {
627
- if (process.env.NODE_ENV !== "production" && !isFunction(value)) {
634
+ if (__MOBX_DEV__ && !isFunction(value)) {
628
635
  die(`Cannot apply '${annotationType_}' to '${adm.name_}.${key.toString()}':` + `\n'${annotationType_}' can only be used on properties with a generator function value.`);
629
636
  }
630
637
  }
@@ -678,7 +685,7 @@ function extend_$2(adm, key, descriptor, proxyTrap) {
678
685
  }), proxyTrap);
679
686
  }
680
687
  function decorateComputed20223_(annotation, get, context) {
681
- if (process.env.NODE_ENV !== "production") {
688
+ if (__MOBX_DEV__) {
682
689
  assert20223DecoratorType(context, ["getter"]);
683
690
  }
684
691
  const ann = annotation;
@@ -695,7 +702,7 @@ function decorateComputed20223_(annotation, get, context) {
695
702
  get,
696
703
  context: target
697
704
  });
698
- options.name || (options.name = process.env.NODE_ENV !== "production" ? `${adm.name_}.${key.toString()}` : `ObservableObject.${key.toString()}`);
705
+ options.name || (options.name = __MOBX_DEV__ ? `${adm.name_}.${key.toString()}` : `ObservableObject.${key.toString()}`);
699
706
  return new ComputedValue(options);
700
707
  }
701
708
  addInitializer(function () {
@@ -729,7 +736,7 @@ function assertComputedDescriptor(adm, {
729
736
  }, key, {
730
737
  get
731
738
  }) {
732
- if (process.env.NODE_ENV !== "production" && !get) {
739
+ if (__MOBX_DEV__ && !get) {
733
740
  die(`Cannot apply '${annotationType_}' to '${adm.name_}.${key.toString()}':` + `\n'${annotationType_}' can only be used on getter(+setter) properties.`);
734
741
  }
735
742
  }
@@ -751,7 +758,7 @@ function extend_$1(adm, key, descriptor, proxyTrap) {
751
758
  return adm.defineObservableProperty_(key, descriptor.value, (_this$options_$enhanc = (_this$options_ = this.options_) == null ? void 0 : _this$options_.enhancer_) != null ? _this$options_$enhanc : deepEnhancer, proxyTrap);
752
759
  }
753
760
  function decorateObservable20223_(annotation, desc, context) {
754
- if (process.env.NODE_ENV !== "production") {
761
+ if (__MOBX_DEV__) {
755
762
  if (context.kind === "field") {
756
763
  throw die(`Please use \`@observable accessor ${String(context.name)}\` instead of \`@observable ${String(context.name)}\``);
757
764
  }
@@ -773,7 +780,7 @@ function decorateObservable20223_(annotation, desc, context) {
773
780
  const adm = asObservableObject(target)[$mobx];
774
781
  ((_adm$lazyObservableKe = adm.lazyObservableKeys_) != null ? _adm$lazyObservableKe : adm.lazyObservableKeys_ = new Map()).set(name, () => {
775
782
  var _ann$options_$enhance, _ann$options_;
776
- return new ObservableValue(value, (_ann$options_$enhance = (_ann$options_ = ann.options_) == null ? void 0 : _ann$options_.enhancer_) != null ? _ann$options_$enhance : deepEnhancer, process.env.NODE_ENV !== "production" ? `${adm.name_}.${name.toString()}` : `ObservableObject.${name.toString()}`, false);
783
+ return new ObservableValue(value, (_ann$options_$enhance = (_ann$options_ = ann.options_) == null ? void 0 : _ann$options_.enhancer_) != null ? _ann$options_$enhance : deepEnhancer, __MOBX_DEV__ ? `${adm.name_}.${name.toString()}` : `ObservableObject.${name.toString()}`, false);
777
784
  });
778
785
  return adm;
779
786
  }
@@ -797,7 +804,7 @@ function decorateObservable20223_(annotation, desc, context) {
797
804
  function assertObservableDescriptor(adm, {
798
805
  annotationType_
799
806
  }, key, descriptor) {
800
- if (process.env.NODE_ENV !== "production" && !("value" in descriptor)) {
807
+ if (__MOBX_DEV__ && !("value" in descriptor)) {
801
808
  die(`Cannot apply '${annotationType_}' to '${adm.name_}.${key.toString()}':` + `\n'${annotationType_}' cannot be used on getter/setter properties`);
802
809
  }
803
810
  }
@@ -888,7 +895,7 @@ function createDecoratorAnnotation(annotation, decorate) {
888
895
  if (context && typeof context.kind === "string") {
889
896
  return decorate(annotation, value, context);
890
897
  }
891
- if (process.env.NODE_ENV !== "production") {
898
+ if (__MOBX_DEV__) {
892
899
  die(`Invalid arguments for \`${annotation.annotationType_}\``);
893
900
  }
894
901
  return undefined;
@@ -1016,7 +1023,7 @@ const computed = function computed(arg1, arg2) {
1016
1023
  return createComputedDecoratorAnnotation(createComputedAnnotation(COMPUTED, arg1));
1017
1024
  }
1018
1025
  // computed(expr, options?)
1019
- if (process.env.NODE_ENV !== "production") {
1026
+ if (__MOBX_DEV__) {
1020
1027
  if (!isFunction(arg1)) {
1021
1028
  die("First argument to `computed` should be an expression.");
1022
1029
  }
@@ -1045,7 +1052,7 @@ const tmpNameDescriptor = {
1045
1052
  enumerable: false
1046
1053
  };
1047
1054
  function createAction(actionName, fn, autoAction = false, ref) {
1048
- if (process.env.NODE_ENV !== "production") {
1055
+ if (__MOBX_DEV__) {
1049
1056
  if (!isFunction(fn)) {
1050
1057
  die("`action` can only be invoked on functions");
1051
1058
  }
@@ -1078,7 +1085,7 @@ function executeAction(actionName, canRunAsDerivation, fn, scope, args) {
1078
1085
  function _startAction(actionName, canRunAsDerivation,
1079
1086
  // true for autoAction
1080
1087
  scope, args) {
1081
- const notifySpy_ = process.env.NODE_ENV !== "production" && isSpyEnabled() && !!actionName;
1088
+ const notifySpy_ = __MOBX_DEV__ && isSpyEnabled() && !!actionName;
1082
1089
  let startTime_ = 0;
1083
1090
  if (notifySpy_) {
1084
1091
  startTime_ = Date.now();
@@ -1096,12 +1103,12 @@ scope, args) {
1096
1103
  let prevAllowStateChanges_ = globalState.allowStateChanges; // by default preserve previous allow
1097
1104
  if (runAsAction) {
1098
1105
  untrackedStart();
1099
- if (process.env.NODE_ENV !== "production") {
1106
+ if (__MOBX_DEV__) {
1100
1107
  prevAllowStateChanges_ = allowStateChangesStart(true);
1101
1108
  }
1102
1109
  }
1103
1110
  const prevAllowStateReads_ = globalState.allowStateReads;
1104
- if (process.env.NODE_ENV !== "production") {
1111
+ if (__MOBX_DEV__) {
1105
1112
  allowStateReadsStart(true);
1106
1113
  }
1107
1114
  const runInfo = {
@@ -1125,7 +1132,7 @@ function _endAction(runInfo) {
1125
1132
  if (runInfo.error_ !== undefined) {
1126
1133
  globalState.suppressReactionErrors = true;
1127
1134
  }
1128
- if (process.env.NODE_ENV !== "production") {
1135
+ if (__MOBX_DEV__) {
1129
1136
  allowStateChangesEnd(runInfo.prevAllowStateChanges_);
1130
1137
  allowStateReadsEnd(runInfo.prevAllowStateReads_);
1131
1138
  }
@@ -1133,7 +1140,7 @@ function _endAction(runInfo) {
1133
1140
  if (runInfo.runAsAction_) {
1134
1141
  untrackedEnd(runInfo.prevDerivation_);
1135
1142
  }
1136
- if (process.env.NODE_ENV !== "production" && runInfo.notifySpy_) {
1143
+ if (__MOBX_DEV__ && runInfo.notifySpy_) {
1137
1144
  spyReportEnd({
1138
1145
  time: Date.now() - runInfo.startTime_
1139
1146
  });
@@ -1159,7 +1166,7 @@ function allowStateChangesEnd(prev) {
1159
1166
 
1160
1167
  const CREATE = "create";
1161
1168
  class ObservableValue extends Atom {
1162
- constructor(value, enhancer_, name_ = process.env.NODE_ENV !== "production" ? "ObservableValue@" + getNextId() : "ObservableValue", notifySpy = true, equals_ = compareDefault) {
1169
+ constructor(value, enhancer_, name_ = __MOBX_DEV__ ? "ObservableValue@" + getNextId() : "ObservableValue", notifySpy = true, equals_ = compareDefault) {
1163
1170
  super(name_);
1164
1171
  this.enhancer_ = void 0;
1165
1172
  this.name_ = void 0;
@@ -1173,7 +1180,7 @@ class ObservableValue extends Atom {
1173
1180
  this.name_ = name_;
1174
1181
  this.equals_ = equals_;
1175
1182
  this.value_ = enhancer_(value, undefined, name_);
1176
- if (process.env.NODE_ENV !== "production" && notifySpy && isSpyEnabled()) {
1183
+ if (__MOBX_DEV__ && notifySpy && isSpyEnabled()) {
1177
1184
  var _this$value_;
1178
1185
  // only notify spy if this is a stand-alone observable
1179
1186
  spyReport({
@@ -1195,8 +1202,8 @@ class ObservableValue extends Atom {
1195
1202
  const oldValue = this.value_;
1196
1203
  newValue = this.prepareNewValue_(newValue);
1197
1204
  if (newValue !== globalState.UNCHANGED) {
1198
- const notifySpy = process.env.NODE_ENV !== "production" && isSpyEnabled();
1199
- if (process.env.NODE_ENV !== "production" && notifySpy) {
1205
+ const notifySpy = __MOBX_DEV__ && isSpyEnabled();
1206
+ if (__MOBX_DEV__ && notifySpy) {
1200
1207
  spyReportStart({
1201
1208
  type: UPDATE,
1202
1209
  object: this,
@@ -1207,7 +1214,7 @@ class ObservableValue extends Atom {
1207
1214
  });
1208
1215
  }
1209
1216
  this.setNewValue_(newValue);
1210
- if (process.env.NODE_ENV !== "production" && notifySpy) {
1217
+ if (__MOBX_DEV__ && notifySpy) {
1211
1218
  spyReportEnd();
1212
1219
  }
1213
1220
  }
@@ -1282,7 +1289,8 @@ class ComputedValue {
1282
1289
  // nodes we are looking at. Our value depends on these nodes
1283
1290
  this.newObserving_ = null;
1284
1291
  // during tracking it's an array with new observed observers
1285
- this.observers_ = new Set();
1292
+ // Lazily allocated on first observer - see Atom.observers_.
1293
+ this.observers_ = null;
1286
1294
  this.runId_ = 0;
1287
1295
  this.lastAccessedBy_ = 0;
1288
1296
  this.lowestObserverState_ = 0 /* IDerivationState_.UP_TO_DATE_ */;
@@ -1304,9 +1312,9 @@ class ComputedValue {
1304
1312
  die(31);
1305
1313
  }
1306
1314
  this.derivation = options.get;
1307
- this.name_ = options.name || (process.env.NODE_ENV !== "production" ? "ComputedValue@" + getNextId() : "ComputedValue");
1315
+ this.name_ = options.name || (__MOBX_DEV__ ? "ComputedValue@" + getNextId() : "ComputedValue");
1308
1316
  if (options.set) {
1309
- this.setter_ = createAction(process.env.NODE_ENV !== "production" ? this.name_ + "-setter" : "ComputedValue-setter", options.set);
1317
+ this.setter_ = createAction(__MOBX_DEV__ ? this.name_ + "-setter" : "ComputedValue-setter", options.set);
1310
1318
  }
1311
1319
  this.equals_ = options.equals || compareDefault;
1312
1320
  this.scope_ = options.context;
@@ -1365,9 +1373,9 @@ class ComputedValue {
1365
1373
  if (this.isComputing) {
1366
1374
  die(32, this.name_, this.derivation);
1367
1375
  }
1368
- if (globalState.inBatch === 0 &&
1376
+ if (globalState.inBatch === 0 && (
1369
1377
  // !globalState.trackingDerivatpion &&
1370
- this.observers_.size === 0 && !this.keepAlive_) {
1378
+ !this.observers_ || this.observers_.size === 0) && !this.keepAlive_) {
1371
1379
  if (shouldCompute(this)) {
1372
1380
  this.warnAboutUntrackedRead_();
1373
1381
  startBatch(); // See perf test 'computed memoization'
@@ -1375,6 +1383,7 @@ class ComputedValue {
1375
1383
  endBatch();
1376
1384
  }
1377
1385
  } else {
1386
+ const wasBeingObserved = this.isBeingObserved;
1378
1387
  reportObserved(this);
1379
1388
  if (shouldCompute(this)) {
1380
1389
  let prevTrackingContext = globalState.trackingContext;
@@ -1385,6 +1394,10 @@ class ComputedValue {
1385
1394
  propagateChangeConfirmed(this);
1386
1395
  }
1387
1396
  globalState.trackingContext = prevTrackingContext;
1397
+ } else if (!wasBeingObserved && this.isBeingObserved) {
1398
+ // We just became observed while serving a cached value, so the getter
1399
+ // won't run and won't re-report our dependencies. Cascade to them. #4547
1400
+ this.observing_.forEach(markObserved);
1388
1401
  }
1389
1402
  }
1390
1403
  const result = this.value_;
@@ -1416,7 +1429,7 @@ class ComputedValue {
1416
1429
  const changed = wasSuspended || isCaughtException(oldValue) || isCaughtException(newValue) || !this.equals_(oldValue, newValue);
1417
1430
  if (changed) {
1418
1431
  this.value_ = newValue;
1419
- if (process.env.NODE_ENV !== "production" && isSpyEnabled()) {
1432
+ if (__MOBX_DEV__ && isSpyEnabled()) {
1420
1433
  spyReport({
1421
1434
  observableKind: "computed",
1422
1435
  debugObjectName: this.name_,
@@ -1432,7 +1445,7 @@ class ComputedValue {
1432
1445
  computeValue_(track) {
1433
1446
  this.isComputing = true;
1434
1447
  // don't allow state changes during computation
1435
- const prev = process.env.NODE_ENV !== "production" ? allowStateChangesStart(false) : false;
1448
+ const prev = __MOBX_DEV__ ? allowStateChangesStart(false) : false;
1436
1449
  let res;
1437
1450
  if (track) {
1438
1451
  res = trackDerivedFunction(this, this.derivation, this.scope_);
@@ -1447,7 +1460,7 @@ class ComputedValue {
1447
1460
  }
1448
1461
  }
1449
1462
  }
1450
- if (process.env.NODE_ENV !== "production") {
1463
+ if (__MOBX_DEV__) {
1451
1464
  allowStateChangesEnd(prev);
1452
1465
  }
1453
1466
  this.isComputing = false;
@@ -1460,7 +1473,7 @@ class ComputedValue {
1460
1473
  }
1461
1474
  }
1462
1475
  warnAboutUntrackedRead_() {
1463
- if (!(process.env.NODE_ENV !== "production")) {
1476
+ if (!__MOBX_DEV__) {
1464
1477
  return;
1465
1478
  }
1466
1479
  if (typeof this.requiresReaction_ === "boolean" ? this.requiresReaction_ : globalState.computedRequiresReaction) {
@@ -1510,7 +1523,7 @@ function shouldCompute(derivation) {
1510
1523
  case 1 /* IDerivationState_.POSSIBLY_STALE_ */:
1511
1524
  {
1512
1525
  // state propagation can occur outside of action/reactive context #2195
1513
- const prevAllowStateReads = process.env.NODE_ENV !== "production" ? allowStateReadsStart(true) : true;
1526
+ const prevAllowStateReads = __MOBX_DEV__ ? allowStateReadsStart(true) : true;
1514
1527
  const prevUntracked = untrackedStart(); // no need for those computeds to be reported, they will be picked up in trackDerivedFunction.
1515
1528
  const obs = derivation.observing_,
1516
1529
  l = obs.length;
@@ -1525,7 +1538,7 @@ function shouldCompute(derivation) {
1525
1538
  } catch (e) {
1526
1539
  // we are not interested in the value *or* exception at this moment, but if there is one, notify all
1527
1540
  untrackedEnd(prevUntracked);
1528
- if (process.env.NODE_ENV !== "production") {
1541
+ if (__MOBX_DEV__) {
1529
1542
  allowStateReadsEnd(prevAllowStateReads);
1530
1543
  }
1531
1544
  return true;
@@ -1536,7 +1549,7 @@ function shouldCompute(derivation) {
1536
1549
  // invariantShouldCompute(derivation)
1537
1550
  if (derivation.dependenciesState_ === 2 /* IDerivationState_.STALE_ */) {
1538
1551
  untrackedEnd(prevUntracked);
1539
- if (process.env.NODE_ENV !== "production") {
1552
+ if (__MOBX_DEV__) {
1540
1553
  allowStateReadsEnd(prevAllowStateReads);
1541
1554
  }
1542
1555
  return true;
@@ -1545,7 +1558,7 @@ function shouldCompute(derivation) {
1545
1558
  }
1546
1559
  changeDependenciesStateTo0(derivation);
1547
1560
  untrackedEnd(prevUntracked);
1548
- if (process.env.NODE_ENV !== "production") {
1561
+ if (__MOBX_DEV__) {
1549
1562
  allowStateReadsEnd(prevAllowStateReads);
1550
1563
  }
1551
1564
  return false;
@@ -1556,17 +1569,17 @@ function isComputingDerivation() {
1556
1569
  return globalState.trackingDerivation !== null; // filter out actions inside computations
1557
1570
  }
1558
1571
  function checkIfStateModificationsAreAllowed(atom) {
1559
- if (!(process.env.NODE_ENV !== "production")) {
1572
+ if (!__MOBX_DEV__) {
1560
1573
  return;
1561
1574
  }
1562
- const hasObservers = atom.observers_.size > 0;
1575
+ const hasObservers = !!atom.observers_ && atom.observers_.size > 0;
1563
1576
  // Should not be possible to change observed state outside strict mode, except during initialization, see #563
1564
1577
  if (!globalState.allowStateChanges && (hasObservers || globalState.enforceActions === "always")) {
1565
1578
  console.warn("[MobX] " + (globalState.enforceActions ? "Since strict-mode is enabled, changing (observed) observable values without using an action is not allowed. Tried to modify: " : "Side effects like changing state are not allowed at this point. Are you trying to modify state from, for example, a computed value or the render function of a React component? You can wrap side effects in 'runInAction' (or decorate functions with 'action') if needed. Tried to modify: ") + atom.name_);
1566
1579
  }
1567
1580
  }
1568
1581
  function checkIfStateReadsAreAllowed(observable) {
1569
- if (process.env.NODE_ENV !== "production" && !globalState.allowStateReads && globalState.observableRequiresReaction) {
1582
+ if (__MOBX_DEV__ && !globalState.allowStateReads && globalState.observableRequiresReaction) {
1570
1583
  console.warn(`[mobx] Observable '${observable.name_}' being read outside a reactive context.`);
1571
1584
  }
1572
1585
  }
@@ -1576,7 +1589,7 @@ function checkIfStateReadsAreAllowed(observable) {
1576
1589
  * as observer of any of the accessed observables.
1577
1590
  */
1578
1591
  function trackDerivedFunction(derivation, f, context) {
1579
- const prevAllowStateReads = process.env.NODE_ENV !== "production" ? allowStateReadsStart(true) : true;
1592
+ const prevAllowStateReads = __MOBX_DEV__ ? allowStateReadsStart(true) : true;
1580
1593
  changeDependenciesStateTo0(derivation);
1581
1594
  // Preallocate array; will be trimmed by bindDependencies.
1582
1595
  derivation.newObserving_ = new Array(
@@ -1602,13 +1615,13 @@ function trackDerivedFunction(derivation, f, context) {
1602
1615
  globalState.trackingDerivation = prevTracking;
1603
1616
  bindDependencies(derivation);
1604
1617
  warnAboutDerivationWithoutDependencies(derivation);
1605
- if (process.env.NODE_ENV !== "production") {
1618
+ if (__MOBX_DEV__) {
1606
1619
  allowStateReadsEnd(prevAllowStateReads);
1607
1620
  }
1608
1621
  return result;
1609
1622
  }
1610
1623
  function warnAboutDerivationWithoutDependencies(derivation) {
1611
- if (!(process.env.NODE_ENV !== "production")) {
1624
+ if (!__MOBX_DEV__) {
1612
1625
  return;
1613
1626
  }
1614
1627
  if (derivation.observing_.length !== 0) {
@@ -1785,6 +1798,13 @@ class MobXGlobals {
1785
1798
  * Are we currently processing reactions?
1786
1799
  */
1787
1800
  this.isRunningReactions = false;
1801
+ /**
1802
+ * Are we currently draining pendingUnobservations in endBatch?
1803
+ * An onBecomeUnobserved handler can dispose a Reaction, which calls
1804
+ * startBatch/endBatch again; this guards against re-entering the same
1805
+ * drain loop recursively (see endBatch in observable.ts).
1806
+ */
1807
+ this.isRunningUnobservations = false;
1788
1808
  /**
1789
1809
  * Is it allowed to change observables at this point?
1790
1810
  * In general, MobX doesn't allow that when running computations and React.render.
@@ -1901,10 +1921,11 @@ function resetGlobalState() {
1901
1921
  }
1902
1922
 
1903
1923
  function hasObservers(observable) {
1904
- return observable.observers_ && observable.observers_.size > 0;
1924
+ return !!observable.observers_ && observable.observers_.size > 0;
1905
1925
  }
1906
1926
  function getObservers(observable) {
1907
- return observable.observers_;
1927
+ var _observable$observers;
1928
+ return (_observable$observers = observable.observers_) != null ? _observable$observers : new Set();
1908
1929
  }
1909
1930
  // function invariantObservers(observable: IObservable) {
1910
1931
  // const list = observable.observers
@@ -1924,10 +1945,8 @@ function getObservers(observable) {
1924
1945
  // )
1925
1946
  // }
1926
1947
  function addObserver(observable, node) {
1927
- // invariant(node.dependenciesState !== -1, "INTERNAL ERROR, can add only dependenciesState !== -1");
1928
- // invariant(observable._observers.indexOf(node) === -1, "INTERNAL ERROR add already added node");
1929
- // invariantObservers(observable);
1930
- observable.observers_.add(node);
1948
+ var _observable$observers2;
1949
+ ((_observable$observers2 = observable.observers_) != null ? _observable$observers2 : observable.observers_ = new Set()).add(node);
1931
1950
  if (observable.lowestObserverState_ > node.dependenciesState_) {
1932
1951
  observable.lowestObserverState_ = node.dependenciesState_;
1933
1952
  }
@@ -1938,8 +1957,12 @@ function removeObserver(observable, node) {
1938
1957
  // invariant(globalState.inBatch > 0, "INTERNAL ERROR, remove should be called only inside batch");
1939
1958
  // invariant(observable._observers.indexOf(node) !== -1, "INTERNAL ERROR remove already removed node");
1940
1959
  // invariantObservers(observable);
1941
- observable.observers_.delete(node);
1942
- if (observable.observers_.size === 0) {
1960
+ const observers = observable.observers_;
1961
+ if (!observers) {
1962
+ return;
1963
+ }
1964
+ observers.delete(node);
1965
+ if (observers.size === 0) {
1943
1966
  // deleting last observer
1944
1967
  queueForUnobservation(observable);
1945
1968
  }
@@ -1965,26 +1988,59 @@ function endBatch() {
1965
1988
  if (--globalState.inBatch === 0) {
1966
1989
  runReactions();
1967
1990
  // the batch is actually about to finish, all unobserving should happen here.
1968
- const list = globalState.pendingUnobservations;
1969
- for (let i = 0; i < list.length; i++) {
1970
- const observable = list[i];
1971
- observable.isPendingUnobservation = false;
1972
- if (observable.observers_.size === 0) {
1973
- if (observable.isBeingObserved) {
1974
- // if this observable had reactive observers, trigger the hooks
1975
- observable.isBeingObserved = false;
1976
- observable.onBUO();
1977
- }
1978
- if (observable instanceof ComputedValue) {
1979
- // computed values are automatically teared down when the last observer leaves
1980
- // this process happens recursively, this computed might be the last observabe of another, etc..
1981
- observable.suspend_();
1991
+ // Guard against re-entering this loop: an onBUO handler can dispose a Reaction,
1992
+ // which calls startBatch/endBatch again while we're still iterating. Bail out of
1993
+ // the nested call instead of recursing; the outer loop re-reads list.length on
1994
+ // every iteration, so it picks up anything the nested dispose() pushes onto the
1995
+ // same pendingUnobservations array.
1996
+ if (!globalState.isRunningUnobservations) {
1997
+ globalState.isRunningUnobservations = true;
1998
+ try {
1999
+ const list = globalState.pendingUnobservations;
2000
+ for (let i = 0; i < list.length; i++) {
2001
+ const observable = list[i];
2002
+ observable.isPendingUnobservation = false;
2003
+ if (!observable.observers_ || observable.observers_.size === 0) {
2004
+ if (observable.isBeingObserved) {
2005
+ // if this observable had reactive observers, trigger the hooks
2006
+ observable.isBeingObserved = false;
2007
+ observable.onBUO();
2008
+ }
2009
+ if (observable instanceof ComputedValue) {
2010
+ // computed values are automatically teared down when the last observer leaves
2011
+ // this process happens recursively, this computed might be the last observabe of another, etc..
2012
+ observable.suspend_();
2013
+ }
2014
+ }
1982
2015
  }
2016
+ globalState.pendingUnobservations = [];
2017
+ } finally {
2018
+ // Always release the guard, even if an onBUO handler (user code) threw,
2019
+ // otherwise every future endBatch() would see isRunningUnobservations
2020
+ // stuck true and silently stop draining pendingUnobservations forever.
2021
+ globalState.isRunningUnobservations = false;
1983
2022
  }
1984
2023
  }
1985
- globalState.pendingUnobservations = [];
1986
2024
  }
1987
2025
  }
2026
+ /**
2027
+ * Marks an observable as observed, cascading into the dependencies of a ComputedValue.
2028
+ * Unobservation already cascades (`suspend_` -> `clearObserving`), observation normally
2029
+ * only does so by accident: a newly observed computed usually recomputes and re-reports
2030
+ * its dependencies. When it serves a cached value instead nothing re-reports them, so the
2031
+ * transition has to be propagated by hand. See #4547.
2032
+ */
2033
+ function markObserved(observable) {
2034
+ var _observable$observing;
2035
+ if (observable.isBeingObserved) {
2036
+ return;
2037
+ }
2038
+ observable.isBeingObserved = true;
2039
+ observable.onBO();
2040
+ // No queueForUnobservation here: the observer links already exist, so the regular
2041
+ // suspend_ -> clearObserving -> removeObserver teardown still delivers the onBUO.
2042
+ (_observable$observing = observable.observing_) == null || _observable$observing.forEach(markObserved);
2043
+ }
1988
2044
  function reportObserved(observable) {
1989
2045
  checkIfStateReadsAreAllowed(observable);
1990
2046
  const derivation = globalState.trackingDerivation;
@@ -2004,7 +2060,7 @@ function reportObserved(observable) {
2004
2060
  }
2005
2061
  }
2006
2062
  return observable.isBeingObserved;
2007
- } else if (observable.observers_.size === 0 && globalState.inBatch > 0) {
2063
+ } else if ((!observable.observers_ || observable.observers_.size === 0) && globalState.inBatch > 0) {
2008
2064
  queueForUnobservation(observable);
2009
2065
  }
2010
2066
  return false;
@@ -2031,13 +2087,14 @@ function reportObserved(observable) {
2031
2087
  */
2032
2088
  // Called by Atom when its value changes
2033
2089
  function propagateChanged(observable) {
2090
+ var _observable$observers3;
2034
2091
  // invariantLOS(observable, "changed start");
2035
2092
  if (observable.lowestObserverState_ === 2 /* IDerivationState_.STALE_ */) {
2036
2093
  return;
2037
2094
  }
2038
2095
  observable.lowestObserverState_ = 2 /* IDerivationState_.STALE_ */;
2039
2096
  // Ideally we use for..of here, but the downcompiled version is really slow...
2040
- observable.observers_.forEach(d => {
2097
+ (_observable$observers3 = observable.observers_) == null || _observable$observers3.forEach(d => {
2041
2098
  if (d.dependenciesState_ === 0 /* IDerivationState_.UP_TO_DATE_ */) {
2042
2099
  d.onBecomeStale_();
2043
2100
  }
@@ -2047,12 +2104,13 @@ function propagateChanged(observable) {
2047
2104
  }
2048
2105
  // Called by ComputedValue when it recalculate and its value changed
2049
2106
  function propagateChangeConfirmed(observable) {
2107
+ var _observable$observers4;
2050
2108
  // invariantLOS(observable, "confirmed start");
2051
2109
  if (observable.lowestObserverState_ === 2 /* IDerivationState_.STALE_ */) {
2052
2110
  return;
2053
2111
  }
2054
2112
  observable.lowestObserverState_ = 2 /* IDerivationState_.STALE_ */;
2055
- observable.observers_.forEach(d => {
2113
+ (_observable$observers4 = observable.observers_) == null || _observable$observers4.forEach(d => {
2056
2114
  if (d.dependenciesState_ === 1 /* IDerivationState_.POSSIBLY_STALE_ */) {
2057
2115
  d.dependenciesState_ = 2 /* IDerivationState_.STALE_ */;
2058
2116
  } else if (d.dependenciesState_ === 0 /* IDerivationState_.UP_TO_DATE_ */ // this happens during computing of `d`, just keep lowestObserverState up to date.
@@ -2064,12 +2122,13 @@ function propagateChangeConfirmed(observable) {
2064
2122
  }
2065
2123
  // Used by computed when its dependency changed, but we don't wan't to immediately recompute.
2066
2124
  function propagateMaybeChanged(observable) {
2125
+ var _observable$observers5;
2067
2126
  // invariantLOS(observable, "maybe start");
2068
2127
  if (observable.lowestObserverState_ !== 0 /* IDerivationState_.UP_TO_DATE_ */) {
2069
2128
  return;
2070
2129
  }
2071
2130
  observable.lowestObserverState_ = 1 /* IDerivationState_.POSSIBLY_STALE_ */;
2072
- observable.observers_.forEach(d => {
2131
+ (_observable$observers5 = observable.observers_) == null || _observable$observers5.forEach(d => {
2073
2132
  if (d.dependenciesState_ === 0 /* IDerivationState_.UP_TO_DATE_ */) {
2074
2133
  d.dependenciesState_ = 1 /* IDerivationState_.POSSIBLY_STALE_ */;
2075
2134
  d.onBecomeStale_();
@@ -2079,7 +2138,7 @@ function propagateMaybeChanged(observable) {
2079
2138
  }
2080
2139
 
2081
2140
  class Reaction {
2082
- constructor(name_ = process.env.NODE_ENV !== "production" ? "Reaction@" + getNextId() : "Reaction", onInvalidate_, errorHandler_, requiresObservable_) {
2141
+ constructor(name_ = __MOBX_DEV__ ? "Reaction@" + getNextId() : "Reaction", onInvalidate_, errorHandler_, requiresObservable_) {
2083
2142
  this.name_ = void 0;
2084
2143
  this.onInvalidate_ = void 0;
2085
2144
  this.errorHandler_ = void 0;
@@ -2149,7 +2208,7 @@ class Reaction {
2149
2208
  this.isTrackPending = true;
2150
2209
  try {
2151
2210
  this.onInvalidate_();
2152
- if (process.env.NODE_ENV !== "production" && this.isTrackPending && isSpyEnabled()) {
2211
+ if (__MOBX_DEV__ && this.isTrackPending && isSpyEnabled()) {
2153
2212
  // onInvalidate didn't trigger track right away..
2154
2213
  spyReport({
2155
2214
  name: this.name_,
@@ -2170,9 +2229,9 @@ class Reaction {
2170
2229
  // console.warn("Reaction already disposed") // Note: Not a warning / error in mobx 4 either
2171
2230
  }
2172
2231
  startBatch();
2173
- const notify = process.env.NODE_ENV !== "production" && isSpyEnabled();
2232
+ const notify = __MOBX_DEV__ && isSpyEnabled();
2174
2233
  let startTime;
2175
- if (process.env.NODE_ENV !== "production" && notify) {
2234
+ if (__MOBX_DEV__ && notify) {
2176
2235
  startTime = Date.now();
2177
2236
  spyReportStart({
2178
2237
  name: this.name_,
@@ -2193,7 +2252,7 @@ class Reaction {
2193
2252
  if (isCaughtException(result)) {
2194
2253
  this.reportExceptionInDerivation_(result.cause);
2195
2254
  }
2196
- if (process.env.NODE_ENV !== "production" && notify) {
2255
+ if (__MOBX_DEV__ && notify) {
2197
2256
  spyReportEnd({
2198
2257
  time: Date.now() - startTime
2199
2258
  });
@@ -2208,14 +2267,14 @@ class Reaction {
2208
2267
  if (globalState.disableErrorBoundaries) {
2209
2268
  throw error;
2210
2269
  }
2211
- const message = process.env.NODE_ENV !== "production" ? `[mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: '${this}'` : `[mobx] uncaught error in '${this}'`;
2270
+ const message = __MOBX_DEV__ ? `[mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: '${this}'` : `[mobx] uncaught error in '${this}'`;
2212
2271
  if (!globalState.suppressReactionErrors) {
2213
2272
  console.error(message, error);
2214
2273
  /** If debugging brought you here, please, read the above message :-). Tnx! */
2215
- } else if (process.env.NODE_ENV !== "production") {
2274
+ } else if (__MOBX_DEV__) {
2216
2275
  console.warn(`[mobx] (error in reaction '${this.name_}' suppressed, fix error of causing action below)`);
2217
2276
  } // prettier-ignore
2218
- if (process.env.NODE_ENV !== "production" && isSpyEnabled()) {
2277
+ if (__MOBX_DEV__ && isSpyEnabled()) {
2219
2278
  spyReport({
2220
2279
  type: "error",
2221
2280
  name: this.name_,
@@ -2284,7 +2343,7 @@ function runReactionsHelper() {
2284
2343
  // we converge to no remaining reactions after a while.
2285
2344
  while (allReactions.length > 0) {
2286
2345
  if (++iterations === MAX_REACTION_ITERATIONS) {
2287
- console.error(process.env.NODE_ENV !== "production" ? `Reaction doesn't converge to a stable state after ${MAX_REACTION_ITERATIONS} iterations.` + ` Probably there is a cycle in the reactive function: ${allReactions[0]}` : `[mobx] cycle in reaction: ${allReactions[0]}`);
2346
+ console.error(__MOBX_DEV__ ? `Reaction doesn't converge to a stable state after ${MAX_REACTION_ITERATIONS} iterations.` + ` Probably there is a cycle in the reactive function: ${allReactions[0]}` : `[mobx] cycle in reaction: ${allReactions[0]}`);
2288
2347
  allReactions.splice(0); // clear reactions
2289
2348
  }
2290
2349
  let remainingReactions = allReactions.splice(0);
@@ -2301,10 +2360,10 @@ function setReactionScheduler(fn) {
2301
2360
  }
2302
2361
 
2303
2362
  function isSpyEnabled() {
2304
- return process.env.NODE_ENV !== "production" && !!globalState.spyListeners.length;
2363
+ return __MOBX_DEV__ && !!globalState.spyListeners.length;
2305
2364
  }
2306
2365
  function spyReport(event) {
2307
- if (!(process.env.NODE_ENV !== "production")) {
2366
+ if (!__MOBX_DEV__) {
2308
2367
  return;
2309
2368
  } // dead code elimination can do the rest
2310
2369
  if (!globalState.spyListeners.length) {
@@ -2316,7 +2375,7 @@ function spyReport(event) {
2316
2375
  }
2317
2376
  }
2318
2377
  function spyReportStart(event) {
2319
- if (!(process.env.NODE_ENV !== "production")) {
2378
+ if (!__MOBX_DEV__) {
2320
2379
  return;
2321
2380
  }
2322
2381
  const change = assign({}, event, {
@@ -2329,7 +2388,7 @@ const END_EVENT = {
2329
2388
  spyReportEnd: true
2330
2389
  };
2331
2390
  function spyReportEnd(change) {
2332
- if (!(process.env.NODE_ENV !== "production")) {
2391
+ if (!__MOBX_DEV__) {
2333
2392
  return;
2334
2393
  }
2335
2394
  if (change) {
@@ -2342,7 +2401,7 @@ function spyReportEnd(change) {
2342
2401
  }
2343
2402
  }
2344
2403
  function spy(listener) {
2345
- if (!(process.env.NODE_ENV !== "production")) {
2404
+ if (!__MOBX_DEV__) {
2346
2405
  console.warn(`[mobx.spy] Is a no-op in production builds`);
2347
2406
  return function () {};
2348
2407
  } else {
@@ -2392,7 +2451,7 @@ function createActionFactory(autoAction) {
2392
2451
  autoAction
2393
2452
  }));
2394
2453
  }
2395
- if (process.env.NODE_ENV !== "production") {
2454
+ if (__MOBX_DEV__) {
2396
2455
  die("Invalid arguments for `action`");
2397
2456
  }
2398
2457
  };
@@ -2419,7 +2478,7 @@ function isAction(thing) {
2419
2478
  */
2420
2479
  function autorun(view, opts = EMPTY_OBJECT) {
2421
2480
  var _opts$name, _opts$signal;
2422
- if (process.env.NODE_ENV !== "production") {
2481
+ if (__MOBX_DEV__) {
2423
2482
  if (!isFunction(view)) {
2424
2483
  die("Autorun expects a function as first argument");
2425
2484
  }
@@ -2427,7 +2486,7 @@ function autorun(view, opts = EMPTY_OBJECT) {
2427
2486
  die("Autorun does not accept actions since actions are untrackable");
2428
2487
  }
2429
2488
  }
2430
- const name = (_opts$name = opts == null ? void 0 : opts.name) != null ? _opts$name : process.env.NODE_ENV !== "production" ? view.name || "Autorun@" + getNextId() : "Autorun";
2489
+ const name = (_opts$name = opts == null ? void 0 : opts.name) != null ? _opts$name : __MOBX_DEV__ ? view.name || "Autorun@" + getNextId() : "Autorun";
2431
2490
  const runSync = !opts.scheduler && !opts.delay;
2432
2491
  let reaction;
2433
2492
  if (runSync) {
@@ -2465,7 +2524,7 @@ function createSchedulerFromOptions(opts) {
2465
2524
  }
2466
2525
  function reaction(expression, effect, opts = EMPTY_OBJECT) {
2467
2526
  var _opts$name2, _opts$signal2;
2468
- if (process.env.NODE_ENV !== "production") {
2527
+ if (__MOBX_DEV__) {
2469
2528
  if (!isFunction(expression) || !isFunction(effect)) {
2470
2529
  die("First and second argument to reaction should be functions");
2471
2530
  }
@@ -2473,7 +2532,7 @@ function reaction(expression, effect, opts = EMPTY_OBJECT) {
2473
2532
  die("Third argument of reactions should be an object");
2474
2533
  }
2475
2534
  }
2476
- const name = (_opts$name2 = opts.name) != null ? _opts$name2 : process.env.NODE_ENV !== "production" ? "Reaction@" + getNextId() : "Reaction";
2535
+ const name = (_opts$name2 = opts.name) != null ? _opts$name2 : __MOBX_DEV__ ? "Reaction@" + getNextId() : "Reaction";
2477
2536
  const effectAction = action(name, opts.onError ? wrapErrorHandler(opts.onError, effect) : effect);
2478
2537
  const runSync = !opts.scheduler && !opts.delay;
2479
2538
  const scheduler = createSchedulerFromOptions(opts);
@@ -2571,7 +2630,7 @@ function configure(options) {
2571
2630
  }
2572
2631
  });
2573
2632
  globalState.allowStateReads = !globalState.observableRequiresReaction;
2574
- if (process.env.NODE_ENV !== "production" && globalState.disableErrorBoundaries === true) {
2633
+ if (__MOBX_DEV__ && globalState.disableErrorBoundaries === true) {
2575
2634
  console.warn("WARNING: Debug feature only. MobX will NOT recover from errors when `disableErrorBoundaries` is enabled.");
2576
2635
  }
2577
2636
  if (options.reactionScheduler) {
@@ -2580,7 +2639,7 @@ function configure(options) {
2580
2639
  }
2581
2640
 
2582
2641
  function extendObservable(target, properties, annotations, options) {
2583
- if (process.env.NODE_ENV !== "production") {
2642
+ if (__MOBX_DEV__) {
2584
2643
  if (arguments.length > 4) {
2585
2644
  die("'extendObservable' expected 2-4 arguments");
2586
2645
  }
@@ -2664,17 +2723,17 @@ const flow = /*#__PURE__*/assign(function flow(arg1, arg2) {
2664
2723
  return decorateFlow20223_(flowAnnotation, arg1, arg2);
2665
2724
  }
2666
2725
  // flow(fn)
2667
- if (process.env.NODE_ENV !== "production" && arguments.length !== 1) {
2726
+ if (__MOBX_DEV__ && arguments.length !== 1) {
2668
2727
  die(`Flow expects single argument with generator function`);
2669
2728
  }
2670
2729
  const generator = arg1;
2671
- const name = generator.name || (process.env.NODE_ENV !== "production" ? "<unnamed flow>" : "flow");
2730
+ const name = generator.name || (__MOBX_DEV__ ? "<unnamed flow>" : "flow");
2672
2731
  // Implementation based on https://github.com/tj/co/blob/master/index.js
2673
2732
  const res = function res() {
2674
2733
  const ctx = this;
2675
2734
  const args = arguments;
2676
- const runId = process.env.NODE_ENV !== "production" ? ++generatorId : 0;
2677
- const gen = action(process.env.NODE_ENV !== "production" ? `${name} - runid: ${runId} - init` : name, generator).apply(ctx, args);
2735
+ const runId = __MOBX_DEV__ ? ++generatorId : 0;
2736
+ const gen = action(__MOBX_DEV__ ? `${name} - runid: ${runId} - init` : name, generator).apply(ctx, args);
2678
2737
  let rejector;
2679
2738
  let pendingPromise = undefined;
2680
2739
  const promise = new Promise(function (resolve, reject) {
@@ -2684,7 +2743,7 @@ const flow = /*#__PURE__*/assign(function flow(arg1, arg2) {
2684
2743
  pendingPromise = undefined;
2685
2744
  let ret;
2686
2745
  try {
2687
- ret = action(process.env.NODE_ENV !== "production" ? `${name} - runid: ${runId} - yield ${stepId++}` : name, gen.next).call(gen, res);
2746
+ ret = action(__MOBX_DEV__ ? `${name} - runid: ${runId} - yield ${stepId++}` : name, gen.next).call(gen, res);
2688
2747
  } catch (e) {
2689
2748
  return reject(e);
2690
2749
  }
@@ -2694,7 +2753,7 @@ const flow = /*#__PURE__*/assign(function flow(arg1, arg2) {
2694
2753
  pendingPromise = undefined;
2695
2754
  let ret;
2696
2755
  try {
2697
- ret = action(process.env.NODE_ENV !== "production" ? `${name} - runid: ${runId} - yield ${stepId++}` : name, gen.throw).call(gen, err);
2756
+ ret = action(__MOBX_DEV__ ? `${name} - runid: ${runId} - yield ${stepId++}` : name, gen.throw).call(gen, err);
2698
2757
  } catch (e) {
2699
2758
  return reject(e);
2700
2759
  }
@@ -2714,7 +2773,7 @@ const flow = /*#__PURE__*/assign(function flow(arg1, arg2) {
2714
2773
  }
2715
2774
  onFulfilled(undefined); // kick off the process
2716
2775
  });
2717
- const cancelActionName = process.env.NODE_ENV !== "production" ? `${name} - runid: ${runId} - cancel` : name;
2776
+ const cancelActionName = __MOBX_DEV__ ? `${name} - runid: ${runId} - cancel` : name;
2718
2777
  promise.cancel = action(cancelActionName, function () {
2719
2778
  try {
2720
2779
  if (pendingPromise) {
@@ -2755,14 +2814,14 @@ function interceptReads(thing, propOrHandler, handler) {
2755
2814
  if (isObservableMap(thing) || isObservableArray(thing) || isObservableValue(thing)) {
2756
2815
  target = getAdministration(thing);
2757
2816
  } else if (isObservableObject(thing)) {
2758
- if (process.env.NODE_ENV !== "production" && !isStringish(propOrHandler)) {
2817
+ if (__MOBX_DEV__ && !isStringish(propOrHandler)) {
2759
2818
  return die(`InterceptReads can only be used with a specific property, not with an object in general`);
2760
2819
  }
2761
2820
  target = getAdministration(thing, propOrHandler);
2762
- } else if (process.env.NODE_ENV !== "production") {
2821
+ } else if (__MOBX_DEV__) {
2763
2822
  return die(`Expected observable map, object or array as first array`);
2764
2823
  }
2765
- if (process.env.NODE_ENV !== "production" && target.dehancer !== undefined) {
2824
+ if (__MOBX_DEV__ && target.dehancer !== undefined) {
2766
2825
  return die(`An intercept reader was already established`);
2767
2826
  }
2768
2827
  target.dehancer = typeof propOrHandler === "function" ? propOrHandler : handler;
@@ -2804,13 +2863,13 @@ function _isComputed(value, property) {
2804
2863
  return isComputedValue(atom);
2805
2864
  }
2806
2865
  function isComputed(value) {
2807
- if (process.env.NODE_ENV !== "production" && arguments.length > 1) {
2866
+ if (__MOBX_DEV__ && arguments.length > 1) {
2808
2867
  return die(`isComputed expects only 1 argument. Use isComputedProp to inspect the observability of a property`);
2809
2868
  }
2810
2869
  return _isComputed(value);
2811
2870
  }
2812
2871
  function isComputedProp(value, propName) {
2813
- if (process.env.NODE_ENV !== "production" && !isStringish(propName)) {
2872
+ if (__MOBX_DEV__ && !isStringish(propName)) {
2814
2873
  return die(`isComputed expected a property name as second argument`);
2815
2874
  }
2816
2875
  return _isComputed(value, propName);
@@ -2821,7 +2880,7 @@ function _isObservable(value, property) {
2821
2880
  return false;
2822
2881
  }
2823
2882
  if (property !== undefined) {
2824
- if (process.env.NODE_ENV !== "production" && (isObservableMap(value) || isObservableArray(value))) {
2883
+ if (__MOBX_DEV__ && (isObservableMap(value) || isObservableArray(value))) {
2825
2884
  return die("isObservable(object, propertyName) is not supported for arrays and maps. Use map.has or array.length instead.");
2826
2885
  }
2827
2886
  if (isObservableObject(value)) {
@@ -2835,13 +2894,13 @@ function _isObservable(value, property) {
2835
2894
  return isObservableObject(value) || !!value[$mobx] || isAtom(value) || isReaction(value) || isComputedValue(value);
2836
2895
  }
2837
2896
  function isObservable(value) {
2838
- if (process.env.NODE_ENV !== "production" && arguments.length !== 1) {
2897
+ if (__MOBX_DEV__ && arguments.length !== 1) {
2839
2898
  die(`isObservable expects only 1 argument. Use isObservableProp to inspect the observability of a property`);
2840
2899
  }
2841
2900
  return _isObservable(value);
2842
2901
  }
2843
2902
  function isObservableProp(value, propName) {
2844
- if (process.env.NODE_ENV !== "production" && !isStringish(propName)) {
2903
+ if (__MOBX_DEV__ && !isStringish(propName)) {
2845
2904
  return die(`expected a property name as second argument`);
2846
2905
  }
2847
2906
  return _isObservable(value, propName);
@@ -3003,15 +3062,15 @@ function observeObservable(thing, listener, fireImmediately) {
3003
3062
  });
3004
3063
  }
3005
3064
  } else if (isObservableMap(thing)) {
3006
- if (process.env.NODE_ENV !== "production" && fireImmediately === true) {
3065
+ if (__MOBX_DEV__ && fireImmediately === true) {
3007
3066
  die("`observe` doesn't support fireImmediately=true in combination with maps.");
3008
3067
  }
3009
3068
  } else if (isObservableSet(thing)) {
3010
- if (process.env.NODE_ENV !== "production" && fireImmediately === true) {
3069
+ if (__MOBX_DEV__ && fireImmediately === true) {
3011
3070
  die("`observe` doesn't support fireImmediately=true in combination with sets.");
3012
3071
  }
3013
3072
  } else if (isObservableObject(thing)) {
3014
- if (process.env.NODE_ENV !== "production" && fireImmediately === true) {
3073
+ if (__MOBX_DEV__ && fireImmediately === true) {
3015
3074
  die("`observe` doesn't support the fire immediately property for observable objects.");
3016
3075
  }
3017
3076
  } else {
@@ -3109,7 +3168,7 @@ function toJSHelper(source, __alreadySeen) {
3109
3168
  * Complex scenarios require custom solution, eg implementing `toJSON` or using `serializr` lib.
3110
3169
  */
3111
3170
  function toJS(source, options) {
3112
- if (process.env.NODE_ENV !== "production" && options) {
3171
+ if (__MOBX_DEV__ && options) {
3113
3172
  die("toJS no longer supports options");
3114
3173
  }
3115
3174
  return toJSHelper(source, new Map());
@@ -3152,8 +3211,8 @@ function _when(predicate, effect, opts) {
3152
3211
  }
3153
3212
  }, opts.timeout);
3154
3213
  }
3155
- opts.name = process.env.NODE_ENV !== "production" ? opts.name || "When@" + getNextId() : "When";
3156
- const effectAction = createAction(process.env.NODE_ENV !== "production" ? opts.name + "-effect" : "When-effect", effect);
3214
+ opts.name = __MOBX_DEV__ ? opts.name || "When@" + getNextId() : "When";
3215
+ const effectAction = createAction(__MOBX_DEV__ ? opts.name + "-effect" : "When-effect", effect);
3157
3216
  // eslint-disable-next-line
3158
3217
  var disposer = autorun(r => {
3159
3218
  // predicate should not change state
@@ -3170,7 +3229,7 @@ function _when(predicate, effect, opts) {
3170
3229
  }
3171
3230
  function whenPromise(predicate, opts) {
3172
3231
  var _opts$signal;
3173
- if (process.env.NODE_ENV !== "production" && opts && opts.onError) {
3232
+ if (__MOBX_DEV__ && opts && opts.onError) {
3174
3233
  return die(`the options 'onError' and 'promise' cannot be combined`);
3175
3234
  }
3176
3235
  if (opts != null && (_opts$signal = opts.signal) != null && _opts$signal.aborted) {
@@ -3318,7 +3377,7 @@ function makeObservable(target, annotations, options) {
3318
3377
  // proto[keysSymbol] = new Set<PropertyKey>()
3319
3378
  const keysSymbol = /*#__PURE__*/Symbol("mobx-keys");
3320
3379
  function makeAutoObservable(target, overrides, options) {
3321
- if (process.env.NODE_ENV !== "production") {
3380
+ if (__MOBX_DEV__) {
3322
3381
  if (!isPlainObject(target) && !isPlainObject(Object.getPrototypeOf(target))) {
3323
3382
  die(`'makeAutoObservable' can only be used for classes that don't have a superclass`);
3324
3383
  }
@@ -3414,7 +3473,7 @@ const arrayTraps = {
3414
3473
  }
3415
3474
  };
3416
3475
  class ObservableArrayAdministration {
3417
- constructor(name = process.env.NODE_ENV !== "production" ? "ObservableArray@" + getNextId() : "ObservableArray", enhancer, owned_) {
3476
+ constructor(name = __MOBX_DEV__ ? "ObservableArray@" + getNextId() : "ObservableArray", enhancer, owned_) {
3418
3477
  this.owned_ = void 0;
3419
3478
  this.atom_ = void 0;
3420
3479
  this.values_ = [];
@@ -3427,7 +3486,7 @@ class ObservableArrayAdministration {
3427
3486
  this.lastKnownLength_ = 0;
3428
3487
  this.owned_ = owned_;
3429
3488
  this.atom_ = new Atom(name);
3430
- this.enhancer_ = (newV, oldV) => enhancer(newV, oldV, process.env.NODE_ENV !== "production" ? name + "[..]" : "ObservableArray[..]");
3489
+ this.enhancer_ = (newV, oldV) => enhancer(newV, oldV, __MOBX_DEV__ ? name + "[..]" : "ObservableArray[..]");
3431
3490
  }
3432
3491
  dehanceValue_(value) {
3433
3492
  if (this.dehancer !== undefined) {
@@ -3502,7 +3561,7 @@ class ObservableArrayAdministration {
3502
3561
  newItems = change.added;
3503
3562
  }
3504
3563
  newItems = newItems.length === 0 ? newItems : newItems.map(v => this.enhancer_(v, undefined));
3505
- if (process.env.NODE_ENV !== "production") {
3564
+ if (__MOBX_DEV__) {
3506
3565
  const lengthDelta = newItems.length - deleteCount;
3507
3566
  this.updateArrayLength_(length, lengthDelta); // checks if internal array wasn't modified
3508
3567
  }
@@ -3532,7 +3591,7 @@ class ObservableArrayAdministration {
3532
3591
  }
3533
3592
  }
3534
3593
  notifyArrayChildUpdate_(index, newValue, oldValue) {
3535
- const notifySpy = process.env.NODE_ENV !== "production" && !this.owned_ && isSpyEnabled();
3594
+ const notifySpy = __MOBX_DEV__ && !this.owned_ && isSpyEnabled();
3536
3595
  const notify = hasListeners(this);
3537
3596
  const change = notify || notifySpy ? {
3538
3597
  observableKind: "array",
@@ -3545,19 +3604,19 @@ class ObservableArrayAdministration {
3545
3604
  } : null;
3546
3605
  // 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
3547
3606
  // cause any runtime overhead in development mode without NODE_ENV set, unless spying is enabled
3548
- if (process.env.NODE_ENV !== "production" && notifySpy) {
3607
+ if (__MOBX_DEV__ && notifySpy) {
3549
3608
  spyReportStart(change);
3550
3609
  }
3551
3610
  this.atom_.reportChanged();
3552
3611
  if (notify) {
3553
3612
  notifyListeners(this, change);
3554
3613
  }
3555
- if (process.env.NODE_ENV !== "production" && notifySpy) {
3614
+ if (__MOBX_DEV__ && notifySpy) {
3556
3615
  spyReportEnd();
3557
3616
  }
3558
3617
  }
3559
3618
  notifyArraySplice_(index, added, removed) {
3560
- const notifySpy = process.env.NODE_ENV !== "production" && !this.owned_ && isSpyEnabled();
3619
+ const notifySpy = __MOBX_DEV__ && !this.owned_ && isSpyEnabled();
3561
3620
  const notify = hasListeners(this);
3562
3621
  const change = notify || notifySpy ? {
3563
3622
  observableKind: "array",
@@ -3570,7 +3629,7 @@ class ObservableArrayAdministration {
3570
3629
  removedCount: removed.length,
3571
3630
  addedCount: added.length
3572
3631
  } : null;
3573
- if (process.env.NODE_ENV !== "production" && notifySpy) {
3632
+ if (__MOBX_DEV__ && notifySpy) {
3574
3633
  spyReportStart(change);
3575
3634
  }
3576
3635
  this.atom_.reportChanged();
@@ -3578,7 +3637,7 @@ class ObservableArrayAdministration {
3578
3637
  if (notify) {
3579
3638
  notifyListeners(this, change);
3580
3639
  }
3581
- if (process.env.NODE_ENV !== "production" && notifySpy) {
3640
+ if (__MOBX_DEV__ && notifySpy) {
3582
3641
  spyReportEnd();
3583
3642
  }
3584
3643
  }
@@ -3623,7 +3682,7 @@ class ObservableArrayAdministration {
3623
3682
  }
3624
3683
  }
3625
3684
  }
3626
- function createObservableArray(initialValues, enhancer, name = process.env.NODE_ENV !== "production" ? "ObservableArray@" + getNextId() : "ObservableArray", owned = false) {
3685
+ function createObservableArray(initialValues, enhancer, name = __MOBX_DEV__ ? "ObservableArray@" + getNextId() : "ObservableArray", owned = false) {
3627
3686
  return initObservable(() => {
3628
3687
  const adm = new ObservableArrayAdministration(name, enhancer, owned);
3629
3688
  addHiddenFinalProp(adm.values_, $mobx, adm);
@@ -3798,7 +3857,7 @@ const DELETE = "delete";
3798
3857
  // just extend Map? See also https://gist.github.com/nestharus/13b4d74f2ef4a2f4357dbd3fc23c1e54
3799
3858
  // But: https://github.com/mobxjs/mobx/issues/1556
3800
3859
  class ObservableMap {
3801
- constructor(initialData, enhancer_ = deepEnhancer, name_ = process.env.NODE_ENV !== "production" ? "ObservableMap@" + getNextId() : "ObservableMap") {
3860
+ constructor(initialData, enhancer_ = deepEnhancer, name_ = __MOBX_DEV__ ? "ObservableMap@" + getNextId() : "ObservableMap") {
3802
3861
  this.enhancer_ = void 0;
3803
3862
  this.name_ = void 0;
3804
3863
  this[$mobx] = ObservableMapMarker;
@@ -3812,7 +3871,7 @@ class ObservableMap {
3812
3871
  this.enhancer_ = enhancer_;
3813
3872
  this.name_ = name_;
3814
3873
  initObservable(() => {
3815
- this.keysAtom_ = createAtom(process.env.NODE_ENV !== "production" ? `${this.name_}.keys()` : "ObservableMap.keys()");
3874
+ this.keysAtom_ = createAtom(__MOBX_DEV__ ? `${this.name_}.keys()` : "ObservableMap.keys()");
3816
3875
  this.data_ = new Map();
3817
3876
  this.hasMap_ = new Map();
3818
3877
  if (initialData) {
@@ -3829,7 +3888,7 @@ class ObservableMap {
3829
3888
  }
3830
3889
  let entry = this.hasMap_.get(key);
3831
3890
  if (!entry) {
3832
- const newEntry = entry = new ObservableValue(this.has_(key), referenceEnhancer, process.env.NODE_ENV !== "production" ? `${this.name_}.${stringifyKey(key)}?` : "ObservableMap.key?", false);
3891
+ const newEntry = entry = new ObservableValue(this.has_(key), referenceEnhancer, __MOBX_DEV__ ? `${this.name_}.${stringifyKey(key)}?` : "ObservableMap.key?", false);
3833
3892
  this.hasMap_.set(key, newEntry);
3834
3893
  newEntry.onBUOL = new Set([() => this.hasMap_.delete(key)]);
3835
3894
  }
@@ -3869,7 +3928,7 @@ class ObservableMap {
3869
3928
  }
3870
3929
  }
3871
3930
  if (this.has_(key)) {
3872
- const notifySpy = process.env.NODE_ENV !== "production" && isSpyEnabled();
3931
+ const notifySpy = __MOBX_DEV__ && isSpyEnabled();
3873
3932
  const notify = hasListeners(this);
3874
3933
  const change = notify || notifySpy ? {
3875
3934
  observableKind: "map",
@@ -3879,7 +3938,7 @@ class ObservableMap {
3879
3938
  oldValue: this.data_.get(key).value_,
3880
3939
  name: key
3881
3940
  } : null;
3882
- if (process.env.NODE_ENV !== "production" && notifySpy) {
3941
+ if (__MOBX_DEV__ && notifySpy) {
3883
3942
  spyReportStart(change);
3884
3943
  } // TODO fix type
3885
3944
  transaction(() => {
@@ -3893,7 +3952,7 @@ class ObservableMap {
3893
3952
  if (notify) {
3894
3953
  notifyListeners(this, change);
3895
3954
  }
3896
- if (process.env.NODE_ENV !== "production" && notifySpy) {
3955
+ if (__MOBX_DEV__ && notifySpy) {
3897
3956
  spyReportEnd();
3898
3957
  }
3899
3958
  return true;
@@ -3904,7 +3963,7 @@ class ObservableMap {
3904
3963
  const observable = this.data_.get(key);
3905
3964
  newValue = observable.prepareNewValue_(newValue);
3906
3965
  if (newValue !== globalState.UNCHANGED) {
3907
- const notifySpy = process.env.NODE_ENV !== "production" && isSpyEnabled();
3966
+ const notifySpy = __MOBX_DEV__ && isSpyEnabled();
3908
3967
  const notify = hasListeners(this);
3909
3968
  const change = notify || notifySpy ? {
3910
3969
  observableKind: "map",
@@ -3915,14 +3974,14 @@ class ObservableMap {
3915
3974
  name: key,
3916
3975
  newValue
3917
3976
  } : null;
3918
- if (process.env.NODE_ENV !== "production" && notifySpy) {
3977
+ if (__MOBX_DEV__ && notifySpy) {
3919
3978
  spyReportStart(change);
3920
3979
  } // TODO fix type
3921
3980
  observable.setNewValue_(newValue);
3922
3981
  if (notify) {
3923
3982
  notifyListeners(this, change);
3924
3983
  }
3925
- if (process.env.NODE_ENV !== "production" && notifySpy) {
3984
+ if (__MOBX_DEV__ && notifySpy) {
3926
3985
  spyReportEnd();
3927
3986
  }
3928
3987
  }
@@ -3931,13 +3990,13 @@ class ObservableMap {
3931
3990
  checkIfStateModificationsAreAllowed(this.keysAtom_);
3932
3991
  transaction(() => {
3933
3992
  var _this$hasMap_$get2;
3934
- const observable = new ObservableValue(newValue, this.enhancer_, process.env.NODE_ENV !== "production" ? `${this.name_}.${stringifyKey(key)}` : "ObservableMap.key", false);
3993
+ const observable = new ObservableValue(newValue, this.enhancer_, __MOBX_DEV__ ? `${this.name_}.${stringifyKey(key)}` : "ObservableMap.key", false);
3935
3994
  this.data_.set(key, observable);
3936
3995
  newValue = observable.value_; // value might have been changed
3937
3996
  (_this$hasMap_$get2 = this.hasMap_.get(key)) == null || _this$hasMap_$get2.setNewValue_(true);
3938
3997
  this.keysAtom_.reportChanged();
3939
3998
  });
3940
- const notifySpy = process.env.NODE_ENV !== "production" && isSpyEnabled();
3999
+ const notifySpy = __MOBX_DEV__ && isSpyEnabled();
3941
4000
  const notify = hasListeners(this);
3942
4001
  const change = notify || notifySpy ? {
3943
4002
  observableKind: "map",
@@ -3947,13 +4006,13 @@ class ObservableMap {
3947
4006
  name: key,
3948
4007
  newValue
3949
4008
  } : null;
3950
- if (process.env.NODE_ENV !== "production" && notifySpy) {
4009
+ if (__MOBX_DEV__ && notifySpy) {
3951
4010
  spyReportStart(change);
3952
4011
  } // TODO fix type
3953
4012
  if (notify) {
3954
4013
  notifyListeners(this, change);
3955
4014
  }
3956
- if (process.env.NODE_ENV !== "production" && notifySpy) {
4015
+ if (__MOBX_DEV__ && notifySpy) {
3957
4016
  spyReportEnd();
3958
4017
  }
3959
4018
  }
@@ -4170,7 +4229,7 @@ function convertToMap(dataStructure) {
4170
4229
 
4171
4230
  const ObservableSetMarker = {};
4172
4231
  class ObservableSet {
4173
- constructor(initialData, enhancer = deepEnhancer, name_ = process.env.NODE_ENV !== "production" ? "ObservableSet@" + getNextId() : "ObservableSet") {
4232
+ constructor(initialData, enhancer = deepEnhancer, name_ = __MOBX_DEV__ ? "ObservableSet@" + getNextId() : "ObservableSet") {
4174
4233
  this.name_ = void 0;
4175
4234
  this[$mobx] = ObservableSetMarker;
4176
4235
  this.data_ = new Set();
@@ -4231,7 +4290,7 @@ class ObservableSet {
4231
4290
  this.data_.add(this.enhancer_(value, undefined));
4232
4291
  this.atom_.reportChanged();
4233
4292
  });
4234
- const notifySpy = process.env.NODE_ENV !== "production" && isSpyEnabled();
4293
+ const notifySpy = __MOBX_DEV__ && isSpyEnabled();
4235
4294
  const notify = hasListeners(this);
4236
4295
  const change = notify || notifySpy ? {
4237
4296
  observableKind: "set",
@@ -4240,13 +4299,13 @@ class ObservableSet {
4240
4299
  object: this,
4241
4300
  newValue: value
4242
4301
  } : null;
4243
- if (notifySpy && process.env.NODE_ENV !== "production") {
4302
+ if (notifySpy && __MOBX_DEV__) {
4244
4303
  spyReportStart(change);
4245
4304
  }
4246
4305
  if (notify) {
4247
4306
  notifyListeners(this, change);
4248
4307
  }
4249
- if (notifySpy && process.env.NODE_ENV !== "production") {
4308
+ if (notifySpy && __MOBX_DEV__) {
4250
4309
  spyReportEnd();
4251
4310
  }
4252
4311
  }
@@ -4264,7 +4323,7 @@ class ObservableSet {
4264
4323
  }
4265
4324
  }
4266
4325
  if (this.has(value)) {
4267
- const notifySpy = process.env.NODE_ENV !== "production" && isSpyEnabled();
4326
+ const notifySpy = __MOBX_DEV__ && isSpyEnabled();
4268
4327
  const notify = hasListeners(this);
4269
4328
  const change = notify || notifySpy ? {
4270
4329
  observableKind: "set",
@@ -4273,7 +4332,7 @@ class ObservableSet {
4273
4332
  object: this,
4274
4333
  oldValue: value
4275
4334
  } : null;
4276
- if (notifySpy && process.env.NODE_ENV !== "production") {
4335
+ if (notifySpy && __MOBX_DEV__) {
4277
4336
  spyReportStart(change);
4278
4337
  }
4279
4338
  transaction(() => {
@@ -4283,7 +4342,7 @@ class ObservableSet {
4283
4342
  if (notify) {
4284
4343
  notifyListeners(this, change);
4285
4344
  }
4286
- if (notifySpy && process.env.NODE_ENV !== "production") {
4345
+ if (notifySpy && __MOBX_DEV__) {
4287
4346
  spyReportEnd();
4288
4347
  }
4289
4348
  return true;
@@ -4336,31 +4395,16 @@ class ObservableSet {
4336
4395
  });
4337
4396
  }
4338
4397
  intersection(otherSet) {
4339
- if (isES6Set(otherSet) && !isObservableSet(otherSet)) {
4340
- return otherSet.intersection(this);
4341
- } else {
4342
- const dehancedSet = new Set(this);
4343
- return dehancedSet.intersection(otherSet);
4344
- }
4398
+ return new Set(this).intersection(otherSet);
4345
4399
  }
4346
4400
  union(otherSet) {
4347
- if (isES6Set(otherSet) && !isObservableSet(otherSet)) {
4348
- return otherSet.union(this);
4349
- } else {
4350
- const dehancedSet = new Set(this);
4351
- return dehancedSet.union(otherSet);
4352
- }
4401
+ return new Set(this).union(otherSet);
4353
4402
  }
4354
4403
  difference(otherSet) {
4355
4404
  return new Set(this).difference(otherSet);
4356
4405
  }
4357
4406
  symmetricDifference(otherSet) {
4358
- if (isES6Set(otherSet) && !isObservableSet(otherSet)) {
4359
- return otherSet.symmetricDifference(this);
4360
- } else {
4361
- const dehancedSet = new Set(this);
4362
- return dehancedSet.symmetricDifference(otherSet);
4363
- }
4407
+ return new Set(this).symmetricDifference(otherSet);
4364
4408
  }
4365
4409
  isSubsetOf(otherSet) {
4366
4410
  return new Set(this).isSubsetOf(otherSet);
@@ -4369,28 +4413,46 @@ class ObservableSet {
4369
4413
  return new Set(this).isSupersetOf(otherSet);
4370
4414
  }
4371
4415
  isDisjointFrom(otherSet) {
4372
- if (isES6Set(otherSet) && !isObservableSet(otherSet)) {
4373
- return otherSet.isDisjointFrom(this);
4374
- } else {
4375
- const dehancedSet = new Set(this);
4376
- return dehancedSet.isDisjointFrom(otherSet);
4377
- }
4416
+ return new Set(this).isDisjointFrom(otherSet);
4378
4417
  }
4379
4418
  replace(other) {
4380
4419
  if (isObservableSet(other)) {
4381
4420
  other = new Set(other);
4382
4421
  }
4383
- transaction(() => {
4384
- if (Array.isArray(other)) {
4385
- this.clear();
4386
- other.forEach(value => this.add(value));
4387
- } else if (isES6Set(other)) {
4388
- this.clear();
4389
- other.forEach(value => this.add(value));
4390
- } else if (other !== null && other !== undefined) {
4391
- die(41, other);
4392
- }
4393
- });
4422
+ if (Array.isArray(other) || isES6Set(other)) {
4423
+ // Only emit `delete`/`add` events (and `reportChanged`) for values that
4424
+ // actually change, instead of clearing and re-adding everything. `add` and
4425
+ // `delete` are already no-ops for values that are respectively already
4426
+ // present or already absent, so we just need to avoid deleting values that
4427
+ // are part of the replacement. See #3761.
4428
+ transaction(() => {
4429
+ // Collect the desired values for quick lookup. `other` is already a Set
4430
+ // here when it was passed (or snapshotted from an observable set) as one,
4431
+ // so reuse it rather than allocating another; arrays are wrapped (which
4432
+ // also dedupes them).
4433
+ const replacementValues = isES6Set(other) ? other : new Set(other);
4434
+ // Short-circuit the trivial cases: an empty replacement is just a clear,
4435
+ // and replacing into an empty set only needs the adds.
4436
+ if (replacementValues.size === 0) {
4437
+ this.clear();
4438
+ return;
4439
+ }
4440
+ if (this.data_.size === 0) {
4441
+ replacementValues.forEach(value => this.add(value));
4442
+ return;
4443
+ }
4444
+ // Delete values that are not part of the replacement.
4445
+ for (const value of this.data_.values()) {
4446
+ if (!replacementValues.has(this.dehanceValue_(value))) {
4447
+ this.delete(value);
4448
+ }
4449
+ }
4450
+ // Add new values; values that are already present are a no-op.
4451
+ replacementValues.forEach(value => this.add(value));
4452
+ });
4453
+ } else if (other !== null && other !== undefined) {
4454
+ die(41, other);
4455
+ }
4394
4456
  return this;
4395
4457
  }
4396
4458
  toJSON() {
@@ -4436,13 +4498,13 @@ class ObservableObjectAdministration {
4436
4498
  this.values_ = values_;
4437
4499
  this.name_ = name_;
4438
4500
  this.defaultAnnotation_ = defaultAnnotation_;
4439
- this.keysAtom_ = new Atom(process.env.NODE_ENV !== "production" ? `${this.name_}.keys` : "ObservableObject.keys");
4501
+ this.keysAtom_ = new Atom(__MOBX_DEV__ ? `${this.name_}.keys` : "ObservableObject.keys");
4440
4502
  // Optimization: we use this frequently
4441
4503
  this.isPlainObject_ = isPlainObject(this.target_);
4442
- if (process.env.NODE_ENV !== "production" && !isAnnotation(this.defaultAnnotation_)) {
4504
+ if (__MOBX_DEV__ && !isAnnotation(this.defaultAnnotation_)) {
4443
4505
  die(`defaultAnnotation must be valid annotation`);
4444
4506
  }
4445
- if (process.env.NODE_ENV !== "production") {
4507
+ if (__MOBX_DEV__) {
4446
4508
  // Prepare structure for tracking which fields were already annotated
4447
4509
  this.appliedAnnotations_ = {};
4448
4510
  }
@@ -4505,7 +4567,7 @@ class ObservableObjectAdministration {
4505
4567
  // notify spy & observers
4506
4568
  if (newValue !== globalState.UNCHANGED) {
4507
4569
  const notify = hasListeners(this);
4508
- const notifySpy = process.env.NODE_ENV !== "production" && isSpyEnabled();
4570
+ const notifySpy = __MOBX_DEV__ && isSpyEnabled();
4509
4571
  const change = notify || notifySpy ? {
4510
4572
  type: UPDATE,
4511
4573
  observableKind: "object",
@@ -4515,14 +4577,14 @@ class ObservableObjectAdministration {
4515
4577
  name: key,
4516
4578
  newValue
4517
4579
  } : null;
4518
- if (process.env.NODE_ENV !== "production" && notifySpy) {
4580
+ if (__MOBX_DEV__ && notifySpy) {
4519
4581
  spyReportStart(change);
4520
4582
  }
4521
4583
  observable.setNewValue_(newValue);
4522
4584
  if (notify) {
4523
4585
  notifyListeners(this, change);
4524
4586
  }
4525
- if (process.env.NODE_ENV !== "production" && notifySpy) {
4587
+ if (__MOBX_DEV__ && notifySpy) {
4526
4588
  spyReportEnd();
4527
4589
  }
4528
4590
  }
@@ -4576,7 +4638,7 @@ class ObservableObjectAdministration {
4576
4638
  this.pendingKeys_ || (this.pendingKeys_ = new Map());
4577
4639
  let entry = this.pendingKeys_.get(key);
4578
4640
  if (!entry) {
4579
- entry = new ObservableValue(key in this.target_, referenceEnhancer, process.env.NODE_ENV !== "production" ? `${this.name_}.${stringifyKey(key)}?` : "ObservableObject.key?", false);
4641
+ entry = new ObservableValue(key in this.target_, referenceEnhancer, __MOBX_DEV__ ? `${this.name_}.${stringifyKey(key)}?` : "ObservableObject.key?", false);
4580
4642
  this.pendingKeys_.set(key, entry);
4581
4643
  }
4582
4644
  return entry.get();
@@ -4692,7 +4754,7 @@ class ObservableObjectAdministration {
4692
4754
  } else {
4693
4755
  defineProperty(this.target_, key, descriptor);
4694
4756
  }
4695
- const observable = new ObservableValue(value, enhancer, process.env.NODE_ENV !== "production" ? `${this.name_}.${key.toString()}` : "ObservableObject.key", false);
4757
+ const observable = new ObservableValue(value, enhancer, __MOBX_DEV__ ? `${this.name_}.${key.toString()}` : "ObservableObject.key", false);
4696
4758
  this.values_.set(key, observable);
4697
4759
  // Notify (value possibly changed by ObservableValue)
4698
4760
  this.notifyPropertyAddition_(key, observable.value_);
@@ -4724,7 +4786,7 @@ class ObservableObjectAdministration {
4724
4786
  return null;
4725
4787
  }
4726
4788
  }
4727
- options.name || (options.name = process.env.NODE_ENV !== "production" ? `${this.name_}.${key.toString()}` : "ObservableObject.key");
4789
+ options.name || (options.name = __MOBX_DEV__ ? `${this.name_}.${key.toString()}` : "ObservableObject.key");
4728
4790
  options.context = this.proxy_ || this.target_;
4729
4791
  const cachedDescriptor = getCachedObservablePropDescriptor(key);
4730
4792
  const descriptor = {
@@ -4778,7 +4840,7 @@ class ObservableObjectAdministration {
4778
4840
  var _this$pendingKeys_;
4779
4841
  startBatch();
4780
4842
  const notify = hasListeners(this);
4781
- const notifySpy = process.env.NODE_ENV !== "production" && isSpyEnabled();
4843
+ const notifySpy = __MOBX_DEV__ && isSpyEnabled();
4782
4844
  const observable = this.values_.get(key);
4783
4845
  // Value needed for spies/listeners
4784
4846
  let value = undefined;
@@ -4796,7 +4858,7 @@ class ObservableObjectAdministration {
4796
4858
  delete this.target_[key];
4797
4859
  }
4798
4860
  // Allow re-annotating this field
4799
- if (process.env.NODE_ENV !== "production") {
4861
+ if (__MOBX_DEV__) {
4800
4862
  delete this.appliedAnnotations_[key];
4801
4863
  }
4802
4864
  // Clear observable
@@ -4824,13 +4886,13 @@ class ObservableObjectAdministration {
4824
4886
  oldValue: value,
4825
4887
  name: key
4826
4888
  };
4827
- if (process.env.NODE_ENV !== "production" && notifySpy) {
4889
+ if (__MOBX_DEV__ && notifySpy) {
4828
4890
  spyReportStart(change);
4829
4891
  }
4830
4892
  if (notify) {
4831
4893
  notifyListeners(this, change);
4832
4894
  }
4833
- if (process.env.NODE_ENV !== "production" && notifySpy) {
4895
+ if (__MOBX_DEV__ && notifySpy) {
4834
4896
  spyReportEnd();
4835
4897
  }
4836
4898
  }
@@ -4842,7 +4904,7 @@ class ObservableObjectAdministration {
4842
4904
  notifyPropertyAddition_(key, value) {
4843
4905
  var _this$pendingKeys_2;
4844
4906
  const notify = hasListeners(this);
4845
- const notifySpy = process.env.NODE_ENV !== "production" && isSpyEnabled();
4907
+ const notifySpy = __MOBX_DEV__ && isSpyEnabled();
4846
4908
  if (notify || notifySpy) {
4847
4909
  const change = notify || notifySpy ? {
4848
4910
  type: ADD,
@@ -4852,13 +4914,13 @@ class ObservableObjectAdministration {
4852
4914
  name: key,
4853
4915
  newValue: value
4854
4916
  } : null;
4855
- if (process.env.NODE_ENV !== "production" && notifySpy) {
4917
+ if (__MOBX_DEV__ && notifySpy) {
4856
4918
  spyReportStart(change);
4857
4919
  }
4858
4920
  if (notify) {
4859
4921
  notifyListeners(this, change);
4860
4922
  }
4861
- if (process.env.NODE_ENV !== "production" && notifySpy) {
4923
+ if (__MOBX_DEV__ && notifySpy) {
4862
4924
  spyReportEnd();
4863
4925
  }
4864
4926
  }
@@ -4883,19 +4945,19 @@ class ObservableObjectAdministration {
4883
4945
  }
4884
4946
  function asObservableObject(target, options) {
4885
4947
  var _options$name;
4886
- if (process.env.NODE_ENV !== "production" && options && isObservableObject(target)) {
4948
+ if (__MOBX_DEV__ && options && isObservableObject(target)) {
4887
4949
  die(`Options can't be provided for already observable objects.`);
4888
4950
  }
4889
4951
  if (hasProp(target, $mobx)) {
4890
- if (process.env.NODE_ENV !== "production" && !(getAdministration(target) instanceof ObservableObjectAdministration)) {
4952
+ if (__MOBX_DEV__ && !(getAdministration(target) instanceof ObservableObjectAdministration)) {
4891
4953
  die(`Cannot convert '${getDebugName(target)}' into observable object:` + `\nThe target is already observable of different type.` + `\nExtending builtins is not supported.`);
4892
4954
  }
4893
4955
  return target;
4894
4956
  }
4895
- if (process.env.NODE_ENV !== "production" && !Object.isExtensible(target)) {
4957
+ if (__MOBX_DEV__ && !Object.isExtensible(target)) {
4896
4958
  die("Cannot make the designated object observable; it is not extensible");
4897
4959
  }
4898
- const name = (_options$name = options == null ? void 0 : options.name) != null ? _options$name : process.env.NODE_ENV !== "production" ? `${isPlainObject(target) ? "ObservableObject" : target.constructor.name}@${getNextId()}` : "ObservableObject";
4960
+ const name = (_options$name = options == null ? void 0 : options.name) != null ? _options$name : __MOBX_DEV__ ? `${isPlainObject(target) ? "ObservableObject" : target.constructor.name}@${getNextId()}` : "ObservableObject";
4899
4961
  const adm = new ObservableObjectAdministration(target, new Map(), String(name), getAnnotationFromOptions(options));
4900
4962
  addHiddenProp(target, $mobx, adm);
4901
4963
  return target;
@@ -4918,20 +4980,20 @@ function isObservableObject(thing) {
4918
4980
  return false;
4919
4981
  }
4920
4982
  function recordAnnotationApplied(adm, annotation, key) {
4921
- if (process.env.NODE_ENV !== "production") {
4983
+ if (__MOBX_DEV__) {
4922
4984
  adm.appliedAnnotations_[key] = annotation;
4923
4985
  }
4924
4986
  }
4925
4987
  function assertAnnotable(adm, annotation, key) {
4926
4988
  // Valid annotation
4927
- if (process.env.NODE_ENV !== "production" && !isAnnotation(annotation)) {
4989
+ if (__MOBX_DEV__ && !isAnnotation(annotation)) {
4928
4990
  die(`Cannot annotate '${adm.name_}.${key.toString()}': Invalid annotation.`);
4929
4991
  }
4930
4992
  /*
4931
4993
  // Configurable, not sealed, not frozen
4932
4994
  // Possibly not needed, just a little better error then the one thrown by engine.
4933
4995
  // Cases where this would be useful the most (subclass field initializer) are not interceptable by this.
4934
- if (__DEV__) {
4996
+ if (__MOBX_DEV__) {
4935
4997
  const configurable = getDescriptor(adm.target_, key)?.configurable
4936
4998
  const frozen = Object.isFrozen(adm.target_)
4937
4999
  const sealed = Object.isSealed(adm.target_)
@@ -4958,7 +5020,7 @@ function assertAnnotable(adm, annotation, key) {
4958
5020
  }
4959
5021
  */
4960
5022
  // Not annotated
4961
- if (process.env.NODE_ENV !== "production" && !isOverride(annotation) && hasProp(adm.appliedAnnotations_, key)) {
5023
+ if (__MOBX_DEV__ && !isOverride(annotation) && hasProp(adm.appliedAnnotations_, key)) {
4962
5024
  const fieldName = `${adm.name_}.${key.toString()}`;
4963
5025
  const currentAnnotationType = adm.appliedAnnotations_[key].annotationType_;
4964
5026
  const requestedAnnotationType = annotation.annotationType_;
@@ -5050,13 +5112,13 @@ function getDebugName(thing, property) {
5050
5112
  */
5051
5113
  function initObservable(cb) {
5052
5114
  const derivation = untrackedStart();
5053
- const allowStateChanges = process.env.NODE_ENV !== "production" ? allowStateChangesStart(true) : true;
5115
+ const allowStateChanges = __MOBX_DEV__ ? allowStateChangesStart(true) : true;
5054
5116
  startBatch();
5055
5117
  try {
5056
5118
  return cb();
5057
5119
  } finally {
5058
5120
  endBatch();
5059
- if (process.env.NODE_ENV !== "production") {
5121
+ if (__MOBX_DEV__) {
5060
5122
  allowStateChangesEnd(allowStateChanges);
5061
5123
  }
5062
5124
  untrackedEnd(derivation);
@@ -5248,7 +5310,7 @@ function isAnnotation(thing) {
5248
5310
  * - utils/ Utility stuff.
5249
5311
  *
5250
5312
  */
5251
- if (process.env.NODE_ENV !== "production") {
5313
+ if (__MOBX_DEV__) {
5252
5314
  const g = globalThis;
5253
5315
  ["Symbol", "Map", "Set", "Proxy"].forEach(m => {
5254
5316
  if (typeof g[m] === "undefined") {
@@ -5256,7 +5318,7 @@ if (process.env.NODE_ENV !== "production") {
5256
5318
  }
5257
5319
  });
5258
5320
  }
5259
- if (process.env.NODE_ENV !== "production" && typeof __MOBX_DEVTOOLS_GLOBAL_HOOK__ === "object") {
5321
+ if (__MOBX_DEV__ && typeof __MOBX_DEVTOOLS_GLOBAL_HOOK__ === "object") {
5260
5322
  // See: https://github.com/andykog/mobx-devtools/
5261
5323
  __MOBX_DEVTOOLS_GLOBAL_HOOK__.injectMobx({
5262
5324
  spy,