mobx 6.13.0 → 6.13.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # mobx
2
2
 
3
+ ## 6.13.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [`5e711e0b4737fd6b5b3c6f9b32afd4f195bc5fc3`](https://github.com/mobxjs/mobx/commit/5e711e0b4737fd6b5b3c6f9b32afd4f195bc5fc3) [#3901](https://github.com/mobxjs/mobx/pull/3901) Thanks [@peterm-canva](https://github.com/peterm-canva)! - Shrink Atom and Reaction using a bitfield
8
+
3
9
  ## 6.13.0
4
10
 
5
11
  ### Minor Changes
@@ -6,10 +6,11 @@ export interface IAtom extends IObservable {
6
6
  }
7
7
  export declare class Atom implements IAtom {
8
8
  name_: string;
9
- isPendingUnobservation: boolean;
10
- isBeingObserved: boolean;
9
+ private static readonly isBeingObservedMask_;
10
+ private static readonly isPendingUnobservationMask_;
11
+ private static readonly diffValueMask_;
12
+ private flags_;
11
13
  observers_: Set<IDerivation>;
12
- diffValue_: number;
13
14
  lastAccessedBy_: number;
14
15
  lowestObserverState_: IDerivationState_;
15
16
  /**
@@ -17,6 +18,12 @@ export declare class Atom implements IAtom {
17
18
  * The onBecomeObserved and onBecomeUnobserved callbacks can be used for resource management.
18
19
  */
19
20
  constructor(name_?: string);
21
+ get isBeingObserved(): boolean;
22
+ set isBeingObserved(newValue: boolean);
23
+ get isPendingUnobservation(): boolean;
24
+ set isPendingUnobservation(newValue: boolean);
25
+ get diffValue(): 0 | 1;
26
+ set diffValue(newValue: 0 | 1);
20
27
  onBOL: Set<Lambda> | undefined;
21
28
  onBUOL: Set<Lambda> | undefined;
22
29
  onBO(): void;
@@ -44,7 +44,6 @@ export declare class ComputedValue<T> implements IObservable, IComputedValue<T>,
44
44
  observing_: IObservable[];
45
45
  newObserving_: null;
46
46
  observers_: Set<IDerivation>;
47
- diffValue_: number;
48
47
  runId_: number;
49
48
  lastAccessedBy_: number;
50
49
  lowestObserverState_: IDerivationState_;
@@ -56,6 +55,7 @@ export declare class ComputedValue<T> implements IObservable, IComputedValue<T>,
56
55
  private static readonly isRunningSetterMask_;
57
56
  private static readonly isBeingObservedMask_;
58
57
  private static readonly isPendingUnobservationMask_;
58
+ private static readonly diffValueMask_;
59
59
  private flags_;
60
60
  derivation: () => T;
61
61
  setter_?: (value: T) => void;
@@ -90,6 +90,8 @@ export declare class ComputedValue<T> implements IObservable, IComputedValue<T>,
90
90
  set isBeingObserved(newValue: boolean);
91
91
  get isPendingUnobservation(): boolean;
92
92
  set isPendingUnobservation(newValue: boolean);
93
+ get diffValue(): 0 | 1;
94
+ set diffValue(newValue: 0 | 1);
93
95
  /**
94
96
  * Returns the current value of this computed value.
95
97
  * Will evaluate its computation first if needed.
@@ -4,7 +4,7 @@ export interface IDepTreeNode {
4
4
  observing_?: IObservable[];
5
5
  }
6
6
  export interface IObservable extends IDepTreeNode {
7
- diffValue_: number;
7
+ diffValue: number;
8
8
  /**
9
9
  * Id of the derivation *run* that last accessed this observable.
10
10
  * If this id equals the *run* id of the current derivation,
@@ -33,18 +33,28 @@ export declare class Reaction implements IDerivation, IReactionPublic {
33
33
  observing_: IObservable[];
34
34
  newObserving_: IObservable[];
35
35
  dependenciesState_: IDerivationState_;
36
- diffValue_: number;
37
36
  runId_: number;
38
37
  unboundDepsCount_: number;
39
- isDisposed_: boolean;
40
- isScheduled_: boolean;
41
- isTrackPending_: boolean;
42
- isRunning_: boolean;
38
+ private static readonly isDisposedMask_;
39
+ private static readonly isScheduledMask_;
40
+ private static readonly isTrackPendingMask_;
41
+ private static readonly isRunningMask_;
42
+ private static readonly diffValueMask_;
43
+ private flags_;
43
44
  isTracing_: TraceMode;
44
45
  constructor(name_: string, onInvalidate_: () => void, errorHandler_?: ((error: any, derivation: IDerivation) => void) | undefined, requiresObservable_?: any);
46
+ get isDisposed(): boolean;
47
+ set isDisposed(newValue: boolean);
48
+ get isScheduled(): boolean;
49
+ set isScheduled(newValue: boolean);
50
+ get isTrackPending(): boolean;
51
+ set isTrackPending(newValue: boolean);
52
+ get isRunning(): boolean;
53
+ set isRunning(newValue: boolean);
54
+ get diffValue(): 0 | 1;
55
+ set diffValue(newValue: 0 | 1);
45
56
  onBecomeStale_(): void;
46
57
  schedule_(): void;
47
- isScheduled(): boolean;
48
58
  /**
49
59
  * internal, use schedule() if you intend to kick off a reaction
50
60
  */
@@ -266,6 +266,17 @@ var getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors || function get
266
266
  });
267
267
  return res;
268
268
  };
269
+ function getFlag(flags, mask) {
270
+ return !!(flags & mask);
271
+ }
272
+ function setFlag(flags, mask, newValue) {
273
+ if (newValue) {
274
+ flags |= mask;
275
+ } else {
276
+ flags &= ~mask;
277
+ }
278
+ return flags;
279
+ }
269
280
 
270
281
  function _arrayLikeToArray(r, a) {
271
282
  (null == a || a > r.length) && (a = r.length);
@@ -418,11 +429,8 @@ var Atom = /*#__PURE__*/function () {
418
429
  name_ = "Atom@" + getNextId() ;
419
430
  }
420
431
  this.name_ = void 0;
421
- this.isPendingUnobservation = false;
422
- // for effective unobserving. BaseAtom has true, for extra optimization, so its onBecomeUnobserved never gets called, because it's not needed
423
- this.isBeingObserved = false;
432
+ this.flags_ = 0;
424
433
  this.observers_ = new Set();
425
- this.diffValue_ = 0;
426
434
  this.lastAccessedBy_ = 0;
427
435
  this.lowestObserverState_ = IDerivationState_.NOT_TRACKING_;
428
436
  // onBecomeObservedListeners
@@ -431,6 +439,7 @@ var Atom = /*#__PURE__*/function () {
431
439
  this.onBUOL = void 0;
432
440
  this.name_ = name_;
433
441
  }
442
+ // for effective unobserving. BaseAtom has true, for extra optimization, so its onBecomeUnobserved never gets called, because it's not needed
434
443
  var _proto = Atom.prototype;
435
444
  _proto.onBO = function onBO() {
436
445
  if (this.onBOL) {
@@ -464,8 +473,35 @@ var Atom = /*#__PURE__*/function () {
464
473
  _proto.toString = function toString() {
465
474
  return this.name_;
466
475
  };
467
- return Atom;
476
+ return _createClass(Atom, [{
477
+ key: "isBeingObserved",
478
+ get: function get() {
479
+ return getFlag(this.flags_, Atom.isBeingObservedMask_);
480
+ },
481
+ set: function set(newValue) {
482
+ this.flags_ = setFlag(this.flags_, Atom.isBeingObservedMask_, newValue);
483
+ }
484
+ }, {
485
+ key: "isPendingUnobservation",
486
+ get: function get() {
487
+ return getFlag(this.flags_, Atom.isPendingUnobservationMask_);
488
+ },
489
+ set: function set(newValue) {
490
+ this.flags_ = setFlag(this.flags_, Atom.isPendingUnobservationMask_, newValue);
491
+ }
492
+ }, {
493
+ key: "diffValue",
494
+ get: function get() {
495
+ return getFlag(this.flags_, Atom.diffValueMask_) ? 1 : 0;
496
+ },
497
+ set: function set(newValue) {
498
+ this.flags_ = setFlag(this.flags_, Atom.diffValueMask_, newValue === 1 ? true : false);
499
+ }
500
+ }]);
468
501
  }();
502
+ Atom.isBeingObservedMask_ = 1;
503
+ Atom.isPendingUnobservationMask_ = 2;
504
+ Atom.diffValueMask_ = 4;
469
505
  var isAtom = /*#__PURE__*/createInstanceofPredicate("Atom", Atom);
470
506
  function createAtom(name, onBecomeObservedHandler, onBecomeUnobservedHandler) {
471
507
  if (onBecomeObservedHandler === void 0) {
@@ -1433,17 +1469,6 @@ var ObservableValue = /*#__PURE__*/function (_Atom) {
1433
1469
  }(Atom);
1434
1470
  var isObservableValue = /*#__PURE__*/createInstanceofPredicate("ObservableValue", ObservableValue);
1435
1471
 
1436
- function getFlag(flags, mask) {
1437
- return !!(flags & mask);
1438
- }
1439
- function setFlag(flags, mask, newValue) {
1440
- if (newValue) {
1441
- flags |= mask;
1442
- } else {
1443
- flags &= ~mask;
1444
- }
1445
- return flags;
1446
- }
1447
1472
  /**
1448
1473
  * A node in the state dependency root that observes other nodes, and can be observed itself.
1449
1474
  *
@@ -1483,7 +1508,6 @@ var ComputedValue = /*#__PURE__*/function () {
1483
1508
  this.newObserving_ = null;
1484
1509
  // during tracking it's an array with new observed observers
1485
1510
  this.observers_ = new Set();
1486
- this.diffValue_ = 0;
1487
1511
  this.runId_ = 0;
1488
1512
  this.lastAccessedBy_ = 0;
1489
1513
  this.lowestObserverState_ = IDerivationState_.UP_TO_DATE_;
@@ -1710,12 +1734,21 @@ var ComputedValue = /*#__PURE__*/function () {
1710
1734
  set: function set(newValue) {
1711
1735
  this.flags_ = setFlag(this.flags_, ComputedValue.isPendingUnobservationMask_, newValue);
1712
1736
  }
1737
+ }, {
1738
+ key: "diffValue",
1739
+ get: function get() {
1740
+ return getFlag(this.flags_, ComputedValue.diffValueMask_) ? 1 : 0;
1741
+ },
1742
+ set: function set(newValue) {
1743
+ this.flags_ = setFlag(this.flags_, ComputedValue.diffValueMask_, newValue === 1 ? true : false);
1744
+ }
1713
1745
  }]);
1714
1746
  }();
1715
1747
  ComputedValue.isComputingMask_ = 1;
1716
1748
  ComputedValue.isRunningSetterMask_ = 2;
1717
1749
  ComputedValue.isBeingObservedMask_ = 4;
1718
1750
  ComputedValue.isPendingUnobservationMask_ = 8;
1751
+ ComputedValue.diffValueMask_ = 16;
1719
1752
  var isComputedValue = /*#__PURE__*/createInstanceofPredicate("ComputedValue", ComputedValue);
1720
1753
 
1721
1754
  var IDerivationState_;
@@ -1884,8 +1917,8 @@ function bindDependencies(derivation) {
1884
1917
  l = derivation.unboundDepsCount_;
1885
1918
  for (var i = 0; i < l; i++) {
1886
1919
  var dep = observing[i];
1887
- if (dep.diffValue_ === 0) {
1888
- dep.diffValue_ = 1;
1920
+ if (dep.diffValue === 0) {
1921
+ dep.diffValue = 1;
1889
1922
  if (i0 !== i) {
1890
1923
  observing[i0] = dep;
1891
1924
  }
@@ -1905,18 +1938,18 @@ function bindDependencies(derivation) {
1905
1938
  l = prevObserving.length;
1906
1939
  while (l--) {
1907
1940
  var _dep = prevObserving[l];
1908
- if (_dep.diffValue_ === 0) {
1941
+ if (_dep.diffValue === 0) {
1909
1942
  removeObserver(_dep, derivation);
1910
1943
  }
1911
- _dep.diffValue_ = 0;
1944
+ _dep.diffValue = 0;
1912
1945
  }
1913
1946
  // Go through all new observables and check diffValue: (now it should be unique)
1914
1947
  // 0: it was set to 0 in last loop. don't need to do anything.
1915
1948
  // 1: it wasn't observed, let's observe it. set back to 0
1916
1949
  while (i0--) {
1917
1950
  var _dep2 = observing[i0];
1918
- if (_dep2.diffValue_ === 1) {
1919
- _dep2.diffValue_ = 0;
1951
+ if (_dep2.diffValue === 1) {
1952
+ _dep2.diffValue = 0;
1920
1953
  addObserver(_dep2, derivation);
1921
1954
  }
1922
1955
  }
@@ -2369,13 +2402,9 @@ var Reaction = /*#__PURE__*/function () {
2369
2402
  // nodes we are looking at. Our value depends on these nodes
2370
2403
  this.newObserving_ = [];
2371
2404
  this.dependenciesState_ = IDerivationState_.NOT_TRACKING_;
2372
- this.diffValue_ = 0;
2373
2405
  this.runId_ = 0;
2374
2406
  this.unboundDepsCount_ = 0;
2375
- this.isDisposed_ = false;
2376
- this.isScheduled_ = false;
2377
- this.isTrackPending_ = false;
2378
- this.isRunning_ = false;
2407
+ this.flags_ = 0;
2379
2408
  this.isTracing_ = TraceMode.NONE;
2380
2409
  this.name_ = name_;
2381
2410
  this.onInvalidate_ = onInvalidate_;
@@ -2387,29 +2416,26 @@ var Reaction = /*#__PURE__*/function () {
2387
2416
  this.schedule_();
2388
2417
  };
2389
2418
  _proto.schedule_ = function schedule_() {
2390
- if (!this.isScheduled_) {
2391
- this.isScheduled_ = true;
2419
+ if (!this.isScheduled) {
2420
+ this.isScheduled = true;
2392
2421
  globalState.pendingReactions.push(this);
2393
2422
  runReactions();
2394
2423
  }
2395
- };
2396
- _proto.isScheduled = function isScheduled() {
2397
- return this.isScheduled_;
2398
2424
  }
2399
2425
  /**
2400
2426
  * internal, use schedule() if you intend to kick off a reaction
2401
2427
  */;
2402
2428
  _proto.runReaction_ = function runReaction_() {
2403
- if (!this.isDisposed_) {
2429
+ if (!this.isDisposed) {
2404
2430
  startBatch();
2405
- this.isScheduled_ = false;
2431
+ this.isScheduled = false;
2406
2432
  var prev = globalState.trackingContext;
2407
2433
  globalState.trackingContext = this;
2408
2434
  if (shouldCompute(this)) {
2409
- this.isTrackPending_ = true;
2435
+ this.isTrackPending = true;
2410
2436
  try {
2411
2437
  this.onInvalidate_();
2412
- if ("development" !== "production" && this.isTrackPending_ && isSpyEnabled()) {
2438
+ if ("development" !== "production" && this.isTrackPending && isSpyEnabled()) {
2413
2439
  // onInvalidate didn't trigger track right away..
2414
2440
  spyReport({
2415
2441
  name: this.name_,
@@ -2425,7 +2451,7 @@ var Reaction = /*#__PURE__*/function () {
2425
2451
  }
2426
2452
  };
2427
2453
  _proto.track = function track(fn) {
2428
- if (this.isDisposed_) {
2454
+ if (this.isDisposed) {
2429
2455
  return;
2430
2456
  // console.warn("Reaction already disposed") // Note: Not a warning / error in mobx 4 either
2431
2457
  }
@@ -2439,14 +2465,14 @@ var Reaction = /*#__PURE__*/function () {
2439
2465
  type: "reaction"
2440
2466
  });
2441
2467
  }
2442
- this.isRunning_ = true;
2468
+ this.isRunning = true;
2443
2469
  var prevReaction = globalState.trackingContext; // reactions could create reactions...
2444
2470
  globalState.trackingContext = this;
2445
2471
  var result = trackDerivedFunction(this, fn, undefined);
2446
2472
  globalState.trackingContext = prevReaction;
2447
- this.isRunning_ = false;
2448
- this.isTrackPending_ = false;
2449
- if (this.isDisposed_) {
2473
+ this.isRunning = false;
2474
+ this.isTrackPending = false;
2475
+ if (this.isDisposed) {
2450
2476
  // disposed during last run. Clean up everything that was bound after the dispose call.
2451
2477
  clearObserving(this);
2452
2478
  }
@@ -2489,9 +2515,9 @@ var Reaction = /*#__PURE__*/function () {
2489
2515
  });
2490
2516
  };
2491
2517
  _proto.dispose = function dispose() {
2492
- if (!this.isDisposed_) {
2493
- this.isDisposed_ = true;
2494
- if (!this.isRunning_) {
2518
+ if (!this.isDisposed) {
2519
+ this.isDisposed = true;
2520
+ if (!this.isRunning) {
2495
2521
  // if disposed while running, clean up later. Maybe not optimal, but rare case
2496
2522
  startBatch();
2497
2523
  clearObserving(this);
@@ -2518,8 +2544,53 @@ var Reaction = /*#__PURE__*/function () {
2518
2544
  }
2519
2545
  trace(this, enterBreakPoint);
2520
2546
  };
2521
- return Reaction;
2547
+ return _createClass(Reaction, [{
2548
+ key: "isDisposed",
2549
+ get: function get() {
2550
+ return getFlag(this.flags_, Reaction.isDisposedMask_);
2551
+ },
2552
+ set: function set(newValue) {
2553
+ this.flags_ = setFlag(this.flags_, Reaction.isDisposedMask_, newValue);
2554
+ }
2555
+ }, {
2556
+ key: "isScheduled",
2557
+ get: function get() {
2558
+ return getFlag(this.flags_, Reaction.isScheduledMask_);
2559
+ },
2560
+ set: function set(newValue) {
2561
+ this.flags_ = setFlag(this.flags_, Reaction.isScheduledMask_, newValue);
2562
+ }
2563
+ }, {
2564
+ key: "isTrackPending",
2565
+ get: function get() {
2566
+ return getFlag(this.flags_, Reaction.isTrackPendingMask_);
2567
+ },
2568
+ set: function set(newValue) {
2569
+ this.flags_ = setFlag(this.flags_, Reaction.isTrackPendingMask_, newValue);
2570
+ }
2571
+ }, {
2572
+ key: "isRunning",
2573
+ get: function get() {
2574
+ return getFlag(this.flags_, Reaction.isRunningMask_);
2575
+ },
2576
+ set: function set(newValue) {
2577
+ this.flags_ = setFlag(this.flags_, Reaction.isRunningMask_, newValue);
2578
+ }
2579
+ }, {
2580
+ key: "diffValue",
2581
+ get: function get() {
2582
+ return getFlag(this.flags_, Reaction.diffValueMask_) ? 1 : 0;
2583
+ },
2584
+ set: function set(newValue) {
2585
+ this.flags_ = setFlag(this.flags_, Reaction.diffValueMask_, newValue === 1 ? true : false);
2586
+ }
2587
+ }]);
2522
2588
  }();
2589
+ Reaction.isDisposedMask_ = 1;
2590
+ Reaction.isScheduledMask_ = 2;
2591
+ Reaction.isTrackPendingMask_ = 4;
2592
+ Reaction.isRunningMask_ = 8;
2593
+ Reaction.diffValueMask_ = 16;
2523
2594
  function onReactionError(handler) {
2524
2595
  globalState.globalReactionErrorHandlers.push(handler);
2525
2596
  return function () {
@@ -2713,7 +2784,7 @@ function autorun(view, opts) {
2713
2784
  isScheduled = true;
2714
2785
  scheduler(function () {
2715
2786
  isScheduled = false;
2716
- if (!reaction.isDisposed_) {
2787
+ if (!reaction.isDisposed) {
2717
2788
  reaction.track(reactionRunner);
2718
2789
  }
2719
2790
  });
@@ -2767,7 +2838,7 @@ function reaction(expression, effect, opts) {
2767
2838
  }, opts.onError, opts.requiresObservable);
2768
2839
  function reactionRunner() {
2769
2840
  isScheduled = false;
2770
- if (r.isDisposed_) {
2841
+ if (r.isDisposed) {
2771
2842
  return;
2772
2843
  }
2773
2844
  var changed = false;
@@ -3394,7 +3465,7 @@ function _when(predicate, effect, opts) {
3394
3465
  if (typeof opts.timeout === "number") {
3395
3466
  var error = new Error("WHEN_TIMEOUT");
3396
3467
  timeoutHandle = setTimeout(function () {
3397
- if (!disposer[$mobx].isDisposed_) {
3468
+ if (!disposer[$mobx].isDisposed) {
3398
3469
  disposer();
3399
3470
  if (opts.onError) {
3400
3471
  opts.onError(error);