mobx 6.2.0 → 6.3.3

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.esm.js CHANGED
@@ -3,6 +3,18 @@ var niceErrors = {
3
3
  1: function _(annotationType, key) {
4
4
  return "Cannot apply '" + annotationType + "' to '" + key.toString() + "': Field not found.";
5
5
  },
6
+
7
+ /*
8
+ 2(prop) {
9
+ return `invalid decorator for '${prop.toString()}'`
10
+ },
11
+ 3(prop) {
12
+ return `Cannot decorate '${prop.toString()}': action can only be used on properties with a function value.`
13
+ },
14
+ 4(prop) {
15
+ return `Cannot decorate '${prop.toString()}': computed can only be used on getter properties.`
16
+ },
17
+ */
6
18
  5: "'keys()' can only be used on observable objects, arrays, sets and maps",
7
19
  6: "'values()' can only be used on observable objects, arrays, sets and maps",
8
20
  7: "'entries()' can only be used on observable objects, arrays and maps",
@@ -59,7 +71,9 @@ var niceErrors = {
59
71
  36: "isolateGlobalState should be called before MobX is running any reactions",
60
72
  37: function _(method) {
61
73
  return "[mobx] `observableArray." + method + "()` mutates the array in-place, which is not allowed inside a derivation. Use `array.slice()." + method + "()` instead";
62
- }
74
+ },
75
+ 38: "'ownKeys()' can only be used on observable objects",
76
+ 39: "'defineProperty()' can only be used on observable objects"
63
77
  };
64
78
  var errors = process.env.NODE_ENV !== "production" ? niceErrors : {};
65
79
  function die(error) {
@@ -498,7 +512,8 @@ function shallowComparer(a, b) {
498
512
  }
499
513
 
500
514
  function defaultComparer(a, b) {
501
- return Object.is(a, b);
515
+ if (Object.is) return Object.is(a, b);
516
+ return a === b ? a !== 0 || 1 / a === 1 / b : a !== a && b !== b;
502
517
  }
503
518
 
504
519
  var comparer = {
@@ -696,6 +711,8 @@ function createFlowAnnotation(name, options) {
696
711
  }
697
712
 
698
713
  function make_$2(adm, key, descriptor, source) {
714
+ var _this$options_;
715
+
699
716
  // own
700
717
  if (source === adm.target_) {
701
718
  return this.extend_(adm, key, descriptor, false) === null ? 0
@@ -704,8 +721,15 @@ function make_$2(adm, key, descriptor, source) {
704
721
  /* Continue */
705
722
  ;
706
723
  } // prototype
724
+ // bound - must annotate protos to support super.flow()
707
725
 
708
726
 
727
+ if (((_this$options_ = this.options_) == null ? void 0 : _this$options_.bound) && !isFlow(adm.target_[key])) {
728
+ if (this.extend_(adm, key, descriptor, false) === null) return 0
729
+ /* Cancel */
730
+ ;
731
+ }
732
+
709
733
  if (isFlow(descriptor.value)) {
710
734
  // A prototype could have been annotated already by other constructor,
711
735
  // rest of the proto chain must be annotated already
@@ -714,7 +738,7 @@ function make_$2(adm, key, descriptor, source) {
714
738
  ;
715
739
  }
716
740
 
717
- var flowDescriptor = createFlowDescriptor(adm, this, key, descriptor, false);
741
+ var flowDescriptor = createFlowDescriptor(adm, this, key, descriptor, false, false);
718
742
  defineProperty(source, key, flowDescriptor);
719
743
  return 2
720
744
  /* Continue */
@@ -722,7 +746,9 @@ function make_$2(adm, key, descriptor, source) {
722
746
  }
723
747
 
724
748
  function extend_$2(adm, key, descriptor, proxyTrap) {
725
- var flowDescriptor = createFlowDescriptor(adm, this, key, descriptor);
749
+ var _this$options_2;
750
+
751
+ var flowDescriptor = createFlowDescriptor(adm, this, key, descriptor, (_this$options_2 = this.options_) == null ? void 0 : _this$options_2.bound);
726
752
  return adm.defineProperty_(key, flowDescriptor, proxyTrap);
727
753
  }
728
754
 
@@ -735,15 +761,23 @@ function assertFlowDescriptor(adm, _ref, key, _ref2) {
735
761
  }
736
762
  }
737
763
 
738
- function createFlowDescriptor(adm, annotation, key, descriptor, // provides ability to disable safeDescriptors for prototypes
764
+ function createFlowDescriptor(adm, annotation, key, descriptor, bound, // provides ability to disable safeDescriptors for prototypes
739
765
  safeDescriptors) {
740
766
  if (safeDescriptors === void 0) {
741
767
  safeDescriptors = globalState.safeDescriptors;
742
768
  }
743
769
 
744
770
  assertFlowDescriptor(adm, annotation, key, descriptor);
771
+ var value = descriptor.value;
772
+
773
+ if (bound) {
774
+ var _adm$proxy_;
775
+
776
+ value = value.bind((_adm$proxy_ = adm.proxy_) != null ? _adm$proxy_ : adm.target_);
777
+ }
778
+
745
779
  return {
746
- value: flow(descriptor.value),
780
+ value: flow(value),
747
781
  // Non-configurable for classes
748
782
  // prevents accidental field redefinition in subclass
749
783
  configurable: safeDescriptors ? adm.isPlainObject_ : true,
@@ -833,7 +867,7 @@ function createAutoAnnotation(options) {
833
867
  }
834
868
 
835
869
  function make_$5(adm, key, descriptor, source) {
836
- var _this$options_2, _this$options_3;
870
+ var _this$options_3, _this$options_4;
837
871
 
838
872
  // getter -> computed
839
873
  if (descriptor.get) {
@@ -868,22 +902,25 @@ function make_$5(adm, key, descriptor, source) {
868
902
 
869
903
 
870
904
  if (source !== adm.target_ && typeof descriptor.value === "function") {
871
- var _this$options_;
905
+ var _this$options_2;
872
906
 
873
907
  if (isGenerator(descriptor.value)) {
874
- return flow.make_(adm, key, descriptor, source);
908
+ var _this$options_;
909
+
910
+ var flowAnnotation = ((_this$options_ = this.options_) == null ? void 0 : _this$options_.autoBind) ? flow.bound : flow;
911
+ return flowAnnotation.make_(adm, key, descriptor, source);
875
912
  }
876
913
 
877
- var actionAnnotation = ((_this$options_ = this.options_) == null ? void 0 : _this$options_.autoBind) ? autoAction.bound : autoAction;
914
+ var actionAnnotation = ((_this$options_2 = this.options_) == null ? void 0 : _this$options_2.autoBind) ? autoAction.bound : autoAction;
878
915
  return actionAnnotation.make_(adm, key, descriptor, source);
879
916
  } // other -> observable
880
917
  // Copy props from proto as well, see test:
881
918
  // "decorate should work with Object.create"
882
919
 
883
920
 
884
- var observableAnnotation = ((_this$options_2 = this.options_) == null ? void 0 : _this$options_2.deep) === false ? observable.ref : observable; // if function respect autoBind option
921
+ var observableAnnotation = ((_this$options_3 = this.options_) == null ? void 0 : _this$options_3.deep) === false ? observable.ref : observable; // if function respect autoBind option
885
922
 
886
- if (typeof descriptor.value === "function" && ((_this$options_3 = this.options_) == null ? void 0 : _this$options_3.autoBind)) {
923
+ if (typeof descriptor.value === "function" && ((_this$options_4 = this.options_) == null ? void 0 : _this$options_4.autoBind)) {
887
924
  var _adm$proxy_;
888
925
 
889
926
  descriptor.value = descriptor.value.bind((_adm$proxy_ = adm.proxy_) != null ? _adm$proxy_ : adm.target_);
@@ -893,7 +930,7 @@ function make_$5(adm, key, descriptor, source) {
893
930
  }
894
931
 
895
932
  function extend_$5(adm, key, descriptor, proxyTrap) {
896
- var _this$options_4, _this$options_5;
933
+ var _this$options_5, _this$options_6;
897
934
 
898
935
  // getter -> computed
899
936
  if (descriptor.get) {
@@ -911,16 +948,20 @@ function extend_$5(adm, key, descriptor, proxyTrap) {
911
948
  // if function respect autoBind option
912
949
 
913
950
 
914
- if (typeof descriptor.value === "function" && ((_this$options_4 = this.options_) == null ? void 0 : _this$options_4.autoBind)) {
951
+ if (typeof descriptor.value === "function" && ((_this$options_5 = this.options_) == null ? void 0 : _this$options_5.autoBind)) {
915
952
  var _adm$proxy_2;
916
953
 
917
954
  descriptor.value = descriptor.value.bind((_adm$proxy_2 = adm.proxy_) != null ? _adm$proxy_2 : adm.target_);
918
955
  }
919
956
 
920
- var observableAnnotation = ((_this$options_5 = this.options_) == null ? void 0 : _this$options_5.deep) === false ? observable.ref : observable;
957
+ var observableAnnotation = ((_this$options_6 = this.options_) == null ? void 0 : _this$options_6.deep) === false ? observable.ref : observable;
921
958
  return observableAnnotation.extend_(adm, key, descriptor, proxyTrap);
922
959
  }
923
960
 
961
+ var OBSERVABLE = "observable";
962
+ var OBSERVABLE_REF = "observable.ref";
963
+ var OBSERVABLE_SHALLOW = "observable.shallow";
964
+ var OBSERVABLE_STRUCT = "observable.struct"; // Predefined bags of create observable options, to avoid allocating temporarily option objects
924
965
  // in the majority of cases
925
966
 
926
967
  var defaultCreateObservableOptions = {
@@ -933,14 +974,14 @@ Object.freeze(defaultCreateObservableOptions);
933
974
  function asCreateObservableOptions(thing) {
934
975
  return thing || defaultCreateObservableOptions;
935
976
  }
936
- var observableAnnotation = /*#__PURE__*/createObservableAnnotation("observable");
937
- var observableRefAnnotation = /*#__PURE__*/createObservableAnnotation("observable.ref", {
977
+ var observableAnnotation = /*#__PURE__*/createObservableAnnotation(OBSERVABLE);
978
+ var observableRefAnnotation = /*#__PURE__*/createObservableAnnotation(OBSERVABLE_REF, {
938
979
  enhancer: referenceEnhancer
939
980
  });
940
- var observableShallowAnnotation = /*#__PURE__*/createObservableAnnotation("observable.shallow", {
981
+ var observableShallowAnnotation = /*#__PURE__*/createObservableAnnotation(OBSERVABLE_SHALLOW, {
941
982
  enhancer: shallowEnhancer
942
983
  });
943
- var observableStructAnnotation = /*#__PURE__*/createObservableAnnotation("observable.struct", {
984
+ var observableStructAnnotation = /*#__PURE__*/createObservableAnnotation(OBSERVABLE_STRUCT, {
944
985
  enhancer: refStructEnhancer
945
986
  });
946
987
  var observableDecoratorAnnotation = /*#__PURE__*/createDecoratorAnnotation(observableAnnotation);
@@ -1545,6 +1586,10 @@ var ComputedValue = /*#__PURE__*/function () {
1545
1586
  if (!this.keepAlive_) {
1546
1587
  clearObserving(this);
1547
1588
  this.value_ = undefined; // don't hold on to computed value!
1589
+
1590
+ if (process.env.NODE_ENV !== "production" && this.isTracing_ !== TraceMode.NONE) {
1591
+ console.log("[mobx.trace] Computed value '" + this.name_ + "' was suspended and it will recompute on the next access.");
1592
+ }
1548
1593
  }
1549
1594
  };
1550
1595
 
@@ -1578,16 +1623,12 @@ var ComputedValue = /*#__PURE__*/function () {
1578
1623
  _proto.warnAboutUntrackedRead_ = function warnAboutUntrackedRead_() {
1579
1624
  if (!(process.env.NODE_ENV !== "production")) return;
1580
1625
 
1581
- if (this.requiresReaction_ === true) {
1582
- die("[mobx] Computed value " + this.name_ + " is read outside a reactive context");
1583
- }
1584
-
1585
1626
  if (this.isTracing_ !== TraceMode.NONE) {
1586
- console.log("[mobx.trace] '" + this.name_ + "' is being read outside a reactive context. Doing a full recompute");
1627
+ console.log("[mobx.trace] Computed value '" + this.name_ + "' is being read outside a reactive context. Doing a full recompute.");
1587
1628
  }
1588
1629
 
1589
- if (globalState.computedRequiresReaction) {
1590
- console.warn("[mobx] Computed value " + this.name_ + " is being read outside a reactive context. Doing a full recompute");
1630
+ if (globalState.computedRequiresReaction || this.requiresReaction_) {
1631
+ console.warn("[mobx] Computed value '" + this.name_ + "' is being read outside a reactive context. Doing a full recompute.");
1591
1632
  }
1592
1633
  };
1593
1634
 
@@ -1723,7 +1764,7 @@ function checkIfStateModificationsAreAllowed(atom) {
1723
1764
  }
1724
1765
  function checkIfStateReadsAreAllowed(observable) {
1725
1766
  if (process.env.NODE_ENV !== "production" && !globalState.allowStateReads && globalState.observableRequiresReaction) {
1726
- console.warn("[mobx] Observable " + observable.name_ + " being read outside a reactive context");
1767
+ console.warn("[mobx] Observable '" + observable.name_ + "' being read outside a reactive context.");
1727
1768
  }
1728
1769
  }
1729
1770
  /**
@@ -1768,7 +1809,7 @@ function warnAboutDerivationWithoutDependencies(derivation) {
1768
1809
  if (derivation.observing_.length !== 0) return;
1769
1810
 
1770
1811
  if (globalState.reactionRequiresObservable || derivation.requiresObservable_) {
1771
- console.warn("[mobx] Derivation " + derivation.name_ + " is created/updated without reading any observable value");
1812
+ console.warn("[mobx] Derivation '" + derivation.name_ + "' is created/updated without reading any observable value.");
1772
1813
  }
1773
1814
  }
1774
1815
  /**
@@ -2794,6 +2835,9 @@ function isFlowCancellationError(error) {
2794
2835
  return error instanceof FlowCancellationError;
2795
2836
  }
2796
2837
  var flowAnnotation = /*#__PURE__*/createFlowAnnotation("flow");
2838
+ var flowBoundAnnotation = /*#__PURE__*/createFlowAnnotation("flow.bound", {
2839
+ bound: true
2840
+ });
2797
2841
  var flow = /*#__PURE__*/Object.assign(function flow(arg1, arg2) {
2798
2842
  // @flow
2799
2843
  if (isStringish(arg2)) {
@@ -2879,6 +2923,7 @@ var flow = /*#__PURE__*/Object.assign(function flow(arg1, arg2) {
2879
2923
  res.isMobXFlow = true;
2880
2924
  return res;
2881
2925
  }, flowAnnotation);
2926
+ flow.bound = /*#__PURE__*/createDecoratorAnnotation(flowBoundAnnotation);
2882
2927
 
2883
2928
  function cancelPromise(promise) {
2884
2929
  if (isFunction(promise.cancel)) promise.cancel();
@@ -3103,6 +3148,20 @@ function get(obj, key) {
3103
3148
 
3104
3149
  die(11);
3105
3150
  }
3151
+ function apiDefineProperty(obj, key, descriptor) {
3152
+ if (isObservableObject(obj)) {
3153
+ return obj[$mobx].defineProperty_(key, descriptor);
3154
+ }
3155
+
3156
+ die(39);
3157
+ }
3158
+ function apiOwnKeys(obj) {
3159
+ if (isObservableObject(obj)) {
3160
+ return obj[$mobx].ownKeys_();
3161
+ }
3162
+
3163
+ die(38);
3164
+ }
3106
3165
 
3107
3166
  function observe(thing, propOrCb, cbOrFire, fireImmediately) {
3108
3167
  if (isFunction(cbOrFire)) return observeObservableProperty(thing, propOrCb, cbOrFire, fireImmediately);else return observeObservable(thing, propOrCb, cbOrFire);
@@ -3123,7 +3182,7 @@ function cache(map, key, value) {
3123
3182
 
3124
3183
  function toJSHelper(source, __alreadySeen) {
3125
3184
  if (source == null || typeof source !== "object" || source instanceof Date || !isObservable(source)) return source;
3126
- if (isObservableValue(source)) return toJSHelper(source.get(), __alreadySeen);
3185
+ if (isObservableValue(source) || isComputedValue(source)) return toJSHelper(source.get(), __alreadySeen);
3127
3186
 
3128
3187
  if (__alreadySeen.has(source)) {
3129
3188
  return __alreadySeen.get(source);
@@ -3157,7 +3216,7 @@ function toJSHelper(source, __alreadySeen) {
3157
3216
  // must be observable object
3158
3217
  var _res3 = cache(__alreadySeen, source, {});
3159
3218
 
3160
- source[$mobx].ownKeys_().forEach(function (key) {
3219
+ apiOwnKeys(source).forEach(function (key) {
3161
3220
  if (objectPrototype.propertyIsEnumerable.call(source, key)) {
3162
3221
  _res3[key] = toJSHelper(source[key], __alreadySeen);
3163
3222
  }
@@ -3241,10 +3300,10 @@ function _when(predicate, effect, opts) {
3241
3300
  var timeoutHandle;
3242
3301
 
3243
3302
  if (typeof opts.timeout === "number") {
3303
+ var error = new Error("WHEN_TIMEOUT");
3244
3304
  timeoutHandle = setTimeout(function () {
3245
3305
  if (!disposer[$mobx].isDisposed_) {
3246
3306
  disposer();
3247
- var error = new Error("WHEN_TIMEOUT");
3248
3307
  if (opts.onError) opts.onError(error);else throw error;
3249
3308
  }
3250
3309
  }, opts.timeout);
@@ -3331,7 +3390,7 @@ var objectProxyTraps = {
3331
3390
  return (_getAdm$definePropert = getAdm(target).defineProperty_(name, descriptor)) != null ? _getAdm$definePropert : true;
3332
3391
  },
3333
3392
  ownKeys: function ownKeys(target) {
3334
- if (process.env.NODE_ENV !== "production" && globalState.trackingDerivation) warnAboutProxyRequirement("iterate keys to detect added / removed properties. Use `keys` from 'mobx' instead.");
3393
+ if (process.env.NODE_ENV !== "production" && globalState.trackingDerivation) warnAboutProxyRequirement("iterate keys to detect added / removed properties. Use 'keys' from 'mobx' instead.");
3335
3394
  return getAdm(target).ownKeys_();
3336
3395
  },
3337
3396
  preventExtensions: function preventExtensions(target) {
@@ -5694,7 +5753,7 @@ function isAnnotation(thing) {
5694
5753
  * - utils/ Utility stuff.
5695
5754
  *
5696
5755
  */
5697
- ["Symbol", "Map", "Set", "Symbol"].forEach(function (m) {
5756
+ ["Symbol", "Map", "Set"].forEach(function (m) {
5698
5757
  var g = getGlobal();
5699
5758
 
5700
5759
  if (typeof g[m] === "undefined") {
@@ -5713,5 +5772,5 @@ if (typeof __MOBX_DEVTOOLS_GLOBAL_HOOK__ === "object") {
5713
5772
  });
5714
5773
  }
5715
5774
 
5716
- export { $mobx, FlowCancellationError, ObservableMap, ObservableSet, Reaction, allowStateChanges as _allowStateChanges, runInAction as _allowStateChangesInsideComputed, allowStateReadsEnd as _allowStateReadsEnd, allowStateReadsStart as _allowStateReadsStart, autoAction as _autoAction, _endAction, getAdministration as _getAdministration, getGlobalState as _getGlobalState, interceptReads as _interceptReads, isComputingDerivation as _isComputingDerivation, resetGlobalState as _resetGlobalState, _startAction, action, autorun, comparer, computed, configure, createAtom, entries, extendObservable, flow, flowResult, get, getAtom, getDebugName, getDependencyTree, getObserverTree, has, intercept, isAction, isObservableValue as isBoxedObservable, isComputed, isComputedProp, isFlow, isFlowCancellationError, isObservable, isObservableArray, isObservableMap, isObservableObject, isObservableProp, isObservableSet, keys, makeAutoObservable, makeObservable, observable, observe, onBecomeObserved, onBecomeUnobserved, onReactionError, override, reaction, remove, runInAction, set, spy, toJS, trace, transaction, untracked, values, when };
5775
+ export { $mobx, FlowCancellationError, ObservableMap, ObservableSet, Reaction, allowStateChanges as _allowStateChanges, runInAction as _allowStateChangesInsideComputed, allowStateReadsEnd as _allowStateReadsEnd, allowStateReadsStart as _allowStateReadsStart, autoAction as _autoAction, _endAction, getAdministration as _getAdministration, getGlobalState as _getGlobalState, interceptReads as _interceptReads, isComputingDerivation as _isComputingDerivation, resetGlobalState as _resetGlobalState, _startAction, action, autorun, comparer, computed, configure, createAtom, apiDefineProperty as defineProperty, entries, extendObservable, flow, flowResult, get, getAtom, getDebugName, getDependencyTree, getObserverTree, has, intercept, isAction, isObservableValue as isBoxedObservable, isComputed, isComputedProp, isFlow, isFlowCancellationError, isObservable, isObservableArray, isObservableMap, isObservableObject, isObservableProp, isObservableSet, keys, makeAutoObservable, makeObservable, observable, observe, onBecomeObserved, onBecomeUnobserved, onReactionError, override, apiOwnKeys as ownKeys, reaction, remove, runInAction, set, spy, toJS, trace, transaction, untracked, values, when };
5717
5776
  //# sourceMappingURL=mobx.esm.js.map