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.
@@ -9,6 +9,18 @@
9
9
  1: function _(annotationType, key) {
10
10
  return "Cannot apply '" + annotationType + "' to '" + key.toString() + "': Field not found.";
11
11
  },
12
+
13
+ /*
14
+ 2(prop) {
15
+ return `invalid decorator for '${prop.toString()}'`
16
+ },
17
+ 3(prop) {
18
+ return `Cannot decorate '${prop.toString()}': action can only be used on properties with a function value.`
19
+ },
20
+ 4(prop) {
21
+ return `Cannot decorate '${prop.toString()}': computed can only be used on getter properties.`
22
+ },
23
+ */
12
24
  5: "'keys()' can only be used on observable objects, arrays, sets and maps",
13
25
  6: "'values()' can only be used on observable objects, arrays, sets and maps",
14
26
  7: "'entries()' can only be used on observable objects, arrays and maps",
@@ -65,7 +77,9 @@
65
77
  36: "isolateGlobalState should be called before MobX is running any reactions",
66
78
  37: function _(method) {
67
79
  return "[mobx] `observableArray." + method + "()` mutates the array in-place, which is not allowed inside a derivation. Use `array.slice()." + method + "()` instead";
68
- }
80
+ },
81
+ 38: "'ownKeys()' can only be used on observable objects",
82
+ 39: "'defineProperty()' can only be used on observable objects"
69
83
  };
70
84
  var errors = niceErrors ;
71
85
  function die(error) {
@@ -502,7 +516,8 @@
502
516
  }
503
517
 
504
518
  function defaultComparer(a, b) {
505
- return Object.is(a, b);
519
+ if (Object.is) return Object.is(a, b);
520
+ return a === b ? a !== 0 || 1 / a === 1 / b : a !== a && b !== b;
506
521
  }
507
522
 
508
523
  var comparer = {
@@ -700,6 +715,8 @@
700
715
  }
701
716
 
702
717
  function make_$2(adm, key, descriptor, source) {
718
+ var _this$options_;
719
+
703
720
  // own
704
721
  if (source === adm.target_) {
705
722
  return this.extend_(adm, key, descriptor, false) === null ? 0
@@ -708,8 +725,15 @@
708
725
  /* Continue */
709
726
  ;
710
727
  } // prototype
728
+ // bound - must annotate protos to support super.flow()
711
729
 
712
730
 
731
+ if (((_this$options_ = this.options_) == null ? void 0 : _this$options_.bound) && !isFlow(adm.target_[key])) {
732
+ if (this.extend_(adm, key, descriptor, false) === null) return 0
733
+ /* Cancel */
734
+ ;
735
+ }
736
+
713
737
  if (isFlow(descriptor.value)) {
714
738
  // A prototype could have been annotated already by other constructor,
715
739
  // rest of the proto chain must be annotated already
@@ -718,7 +742,7 @@
718
742
  ;
719
743
  }
720
744
 
721
- var flowDescriptor = createFlowDescriptor(adm, this, key, descriptor, false);
745
+ var flowDescriptor = createFlowDescriptor(adm, this, key, descriptor, false, false);
722
746
  defineProperty(source, key, flowDescriptor);
723
747
  return 2
724
748
  /* Continue */
@@ -726,7 +750,9 @@
726
750
  }
727
751
 
728
752
  function extend_$2(adm, key, descriptor, proxyTrap) {
729
- var flowDescriptor = createFlowDescriptor(adm, this, key, descriptor);
753
+ var _this$options_2;
754
+
755
+ var flowDescriptor = createFlowDescriptor(adm, this, key, descriptor, (_this$options_2 = this.options_) == null ? void 0 : _this$options_2.bound);
730
756
  return adm.defineProperty_(key, flowDescriptor, proxyTrap);
731
757
  }
732
758
 
@@ -739,15 +765,23 @@
739
765
  }
740
766
  }
741
767
 
742
- function createFlowDescriptor(adm, annotation, key, descriptor, // provides ability to disable safeDescriptors for prototypes
768
+ function createFlowDescriptor(adm, annotation, key, descriptor, bound, // provides ability to disable safeDescriptors for prototypes
743
769
  safeDescriptors) {
744
770
  if (safeDescriptors === void 0) {
745
771
  safeDescriptors = globalState.safeDescriptors;
746
772
  }
747
773
 
748
774
  assertFlowDescriptor(adm, annotation, key, descriptor);
775
+ var value = descriptor.value;
776
+
777
+ if (bound) {
778
+ var _adm$proxy_;
779
+
780
+ value = value.bind((_adm$proxy_ = adm.proxy_) != null ? _adm$proxy_ : adm.target_);
781
+ }
782
+
749
783
  return {
750
- value: flow(descriptor.value),
784
+ value: flow(value),
751
785
  // Non-configurable for classes
752
786
  // prevents accidental field redefinition in subclass
753
787
  configurable: safeDescriptors ? adm.isPlainObject_ : true,
@@ -837,7 +871,7 @@
837
871
  }
838
872
 
839
873
  function make_$5(adm, key, descriptor, source) {
840
- var _this$options_2, _this$options_3;
874
+ var _this$options_3, _this$options_4;
841
875
 
842
876
  // getter -> computed
843
877
  if (descriptor.get) {
@@ -872,22 +906,25 @@
872
906
 
873
907
 
874
908
  if (source !== adm.target_ && typeof descriptor.value === "function") {
875
- var _this$options_;
909
+ var _this$options_2;
876
910
 
877
911
  if (isGenerator(descriptor.value)) {
878
- return flow.make_(adm, key, descriptor, source);
912
+ var _this$options_;
913
+
914
+ var flowAnnotation = ((_this$options_ = this.options_) == null ? void 0 : _this$options_.autoBind) ? flow.bound : flow;
915
+ return flowAnnotation.make_(adm, key, descriptor, source);
879
916
  }
880
917
 
881
- var actionAnnotation = ((_this$options_ = this.options_) == null ? void 0 : _this$options_.autoBind) ? autoAction.bound : autoAction;
918
+ var actionAnnotation = ((_this$options_2 = this.options_) == null ? void 0 : _this$options_2.autoBind) ? autoAction.bound : autoAction;
882
919
  return actionAnnotation.make_(adm, key, descriptor, source);
883
920
  } // other -> observable
884
921
  // Copy props from proto as well, see test:
885
922
  // "decorate should work with Object.create"
886
923
 
887
924
 
888
- var observableAnnotation = ((_this$options_2 = this.options_) == null ? void 0 : _this$options_2.deep) === false ? observable.ref : observable; // if function respect autoBind option
925
+ var observableAnnotation = ((_this$options_3 = this.options_) == null ? void 0 : _this$options_3.deep) === false ? observable.ref : observable; // if function respect autoBind option
889
926
 
890
- if (typeof descriptor.value === "function" && ((_this$options_3 = this.options_) == null ? void 0 : _this$options_3.autoBind)) {
927
+ if (typeof descriptor.value === "function" && ((_this$options_4 = this.options_) == null ? void 0 : _this$options_4.autoBind)) {
891
928
  var _adm$proxy_;
892
929
 
893
930
  descriptor.value = descriptor.value.bind((_adm$proxy_ = adm.proxy_) != null ? _adm$proxy_ : adm.target_);
@@ -897,7 +934,7 @@
897
934
  }
898
935
 
899
936
  function extend_$5(adm, key, descriptor, proxyTrap) {
900
- var _this$options_4, _this$options_5;
937
+ var _this$options_5, _this$options_6;
901
938
 
902
939
  // getter -> computed
903
940
  if (descriptor.get) {
@@ -915,16 +952,20 @@
915
952
  // if function respect autoBind option
916
953
 
917
954
 
918
- if (typeof descriptor.value === "function" && ((_this$options_4 = this.options_) == null ? void 0 : _this$options_4.autoBind)) {
955
+ if (typeof descriptor.value === "function" && ((_this$options_5 = this.options_) == null ? void 0 : _this$options_5.autoBind)) {
919
956
  var _adm$proxy_2;
920
957
 
921
958
  descriptor.value = descriptor.value.bind((_adm$proxy_2 = adm.proxy_) != null ? _adm$proxy_2 : adm.target_);
922
959
  }
923
960
 
924
- var observableAnnotation = ((_this$options_5 = this.options_) == null ? void 0 : _this$options_5.deep) === false ? observable.ref : observable;
961
+ var observableAnnotation = ((_this$options_6 = this.options_) == null ? void 0 : _this$options_6.deep) === false ? observable.ref : observable;
925
962
  return observableAnnotation.extend_(adm, key, descriptor, proxyTrap);
926
963
  }
927
964
 
965
+ var OBSERVABLE = "observable";
966
+ var OBSERVABLE_REF = "observable.ref";
967
+ var OBSERVABLE_SHALLOW = "observable.shallow";
968
+ var OBSERVABLE_STRUCT = "observable.struct"; // Predefined bags of create observable options, to avoid allocating temporarily option objects
928
969
  // in the majority of cases
929
970
 
930
971
  var defaultCreateObservableOptions = {
@@ -937,14 +978,14 @@
937
978
  function asCreateObservableOptions(thing) {
938
979
  return thing || defaultCreateObservableOptions;
939
980
  }
940
- var observableAnnotation = /*#__PURE__*/createObservableAnnotation("observable");
941
- var observableRefAnnotation = /*#__PURE__*/createObservableAnnotation("observable.ref", {
981
+ var observableAnnotation = /*#__PURE__*/createObservableAnnotation(OBSERVABLE);
982
+ var observableRefAnnotation = /*#__PURE__*/createObservableAnnotation(OBSERVABLE_REF, {
942
983
  enhancer: referenceEnhancer
943
984
  });
944
- var observableShallowAnnotation = /*#__PURE__*/createObservableAnnotation("observable.shallow", {
985
+ var observableShallowAnnotation = /*#__PURE__*/createObservableAnnotation(OBSERVABLE_SHALLOW, {
945
986
  enhancer: shallowEnhancer
946
987
  });
947
- var observableStructAnnotation = /*#__PURE__*/createObservableAnnotation("observable.struct", {
988
+ var observableStructAnnotation = /*#__PURE__*/createObservableAnnotation(OBSERVABLE_STRUCT, {
948
989
  enhancer: refStructEnhancer
949
990
  });
950
991
  var observableDecoratorAnnotation = /*#__PURE__*/createDecoratorAnnotation(observableAnnotation);
@@ -1549,6 +1590,10 @@
1549
1590
  if (!this.keepAlive_) {
1550
1591
  clearObserving(this);
1551
1592
  this.value_ = undefined; // don't hold on to computed value!
1593
+
1594
+ if ( this.isTracing_ !== TraceMode.NONE) {
1595
+ console.log("[mobx.trace] Computed value '" + this.name_ + "' was suspended and it will recompute on the next access.");
1596
+ }
1552
1597
  }
1553
1598
  };
1554
1599
 
@@ -1581,16 +1626,12 @@
1581
1626
 
1582
1627
  _proto.warnAboutUntrackedRead_ = function warnAboutUntrackedRead_() {
1583
1628
 
1584
- if (this.requiresReaction_ === true) {
1585
- die("[mobx] Computed value " + this.name_ + " is read outside a reactive context");
1586
- }
1587
-
1588
1629
  if (this.isTracing_ !== TraceMode.NONE) {
1589
- console.log("[mobx.trace] '" + this.name_ + "' is being read outside a reactive context. Doing a full recompute");
1630
+ console.log("[mobx.trace] Computed value '" + this.name_ + "' is being read outside a reactive context. Doing a full recompute.");
1590
1631
  }
1591
1632
 
1592
- if (globalState.computedRequiresReaction) {
1593
- console.warn("[mobx] Computed value " + this.name_ + " is being read outside a reactive context. Doing a full recompute");
1633
+ if (globalState.computedRequiresReaction || this.requiresReaction_) {
1634
+ console.warn("[mobx] Computed value '" + this.name_ + "' is being read outside a reactive context. Doing a full recompute.");
1594
1635
  }
1595
1636
  };
1596
1637
 
@@ -1723,7 +1764,7 @@
1723
1764
  }
1724
1765
  function checkIfStateReadsAreAllowed(observable) {
1725
1766
  if ( !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
  /**
@@ -1767,7 +1808,7 @@
1767
1808
  if (derivation.observing_.length !== 0) return;
1768
1809
 
1769
1810
  if (globalState.reactionRequiresObservable || derivation.requiresObservable_) {
1770
- console.warn("[mobx] Derivation " + derivation.name_ + " is created/updated without reading any observable value");
1811
+ console.warn("[mobx] Derivation '" + derivation.name_ + "' is created/updated without reading any observable value.");
1771
1812
  }
1772
1813
  }
1773
1814
  /**
@@ -2787,6 +2828,9 @@
2787
2828
  return error instanceof FlowCancellationError;
2788
2829
  }
2789
2830
  var flowAnnotation = /*#__PURE__*/createFlowAnnotation("flow");
2831
+ var flowBoundAnnotation = /*#__PURE__*/createFlowAnnotation("flow.bound", {
2832
+ bound: true
2833
+ });
2790
2834
  var flow = /*#__PURE__*/Object.assign(function flow(arg1, arg2) {
2791
2835
  // @flow
2792
2836
  if (isStringish(arg2)) {
@@ -2872,6 +2916,7 @@
2872
2916
  res.isMobXFlow = true;
2873
2917
  return res;
2874
2918
  }, flowAnnotation);
2919
+ flow.bound = /*#__PURE__*/createDecoratorAnnotation(flowBoundAnnotation);
2875
2920
 
2876
2921
  function cancelPromise(promise) {
2877
2922
  if (isFunction(promise.cancel)) promise.cancel();
@@ -3096,6 +3141,20 @@
3096
3141
 
3097
3142
  die(11);
3098
3143
  }
3144
+ function apiDefineProperty(obj, key, descriptor) {
3145
+ if (isObservableObject(obj)) {
3146
+ return obj[$mobx].defineProperty_(key, descriptor);
3147
+ }
3148
+
3149
+ die(39);
3150
+ }
3151
+ function apiOwnKeys(obj) {
3152
+ if (isObservableObject(obj)) {
3153
+ return obj[$mobx].ownKeys_();
3154
+ }
3155
+
3156
+ die(38);
3157
+ }
3099
3158
 
3100
3159
  function observe(thing, propOrCb, cbOrFire, fireImmediately) {
3101
3160
  if (isFunction(cbOrFire)) return observeObservableProperty(thing, propOrCb, cbOrFire, fireImmediately);else return observeObservable(thing, propOrCb, cbOrFire);
@@ -3116,7 +3175,7 @@
3116
3175
 
3117
3176
  function toJSHelper(source, __alreadySeen) {
3118
3177
  if (source == null || typeof source !== "object" || source instanceof Date || !isObservable(source)) return source;
3119
- if (isObservableValue(source)) return toJSHelper(source.get(), __alreadySeen);
3178
+ if (isObservableValue(source) || isComputedValue(source)) return toJSHelper(source.get(), __alreadySeen);
3120
3179
 
3121
3180
  if (__alreadySeen.has(source)) {
3122
3181
  return __alreadySeen.get(source);
@@ -3150,7 +3209,7 @@
3150
3209
  // must be observable object
3151
3210
  var _res3 = cache(__alreadySeen, source, {});
3152
3211
 
3153
- source[$mobx].ownKeys_().forEach(function (key) {
3212
+ apiOwnKeys(source).forEach(function (key) {
3154
3213
  if (objectPrototype.propertyIsEnumerable.call(source, key)) {
3155
3214
  _res3[key] = toJSHelper(source[key], __alreadySeen);
3156
3215
  }
@@ -3233,10 +3292,10 @@
3233
3292
  var timeoutHandle;
3234
3293
 
3235
3294
  if (typeof opts.timeout === "number") {
3295
+ var error = new Error("WHEN_TIMEOUT");
3236
3296
  timeoutHandle = setTimeout(function () {
3237
3297
  if (!disposer[$mobx].isDisposed_) {
3238
3298
  disposer();
3239
- var error = new Error("WHEN_TIMEOUT");
3240
3299
  if (opts.onError) opts.onError(error);else throw error;
3241
3300
  }
3242
3301
  }, opts.timeout);
@@ -3323,7 +3382,7 @@
3323
3382
  return (_getAdm$definePropert = getAdm(target).defineProperty_(name, descriptor)) != null ? _getAdm$definePropert : true;
3324
3383
  },
3325
3384
  ownKeys: function ownKeys(target) {
3326
- if ( globalState.trackingDerivation) warnAboutProxyRequirement("iterate keys to detect added / removed properties. Use `keys` from 'mobx' instead.");
3385
+ if ( globalState.trackingDerivation) warnAboutProxyRequirement("iterate keys to detect added / removed properties. Use 'keys' from 'mobx' instead.");
3327
3386
  return getAdm(target).ownKeys_();
3328
3387
  },
3329
3388
  preventExtensions: function preventExtensions(target) {
@@ -5686,7 +5745,7 @@
5686
5745
  * - utils/ Utility stuff.
5687
5746
  *
5688
5747
  */
5689
- ["Symbol", "Map", "Set", "Symbol"].forEach(function (m) {
5748
+ ["Symbol", "Map", "Set"].forEach(function (m) {
5690
5749
  var g = getGlobal();
5691
5750
 
5692
5751
  if (typeof g[m] === "undefined") {
@@ -5728,6 +5787,7 @@
5728
5787
  exports.computed = computed;
5729
5788
  exports.configure = configure;
5730
5789
  exports.createAtom = createAtom;
5790
+ exports.defineProperty = apiDefineProperty;
5731
5791
  exports.entries = entries;
5732
5792
  exports.extendObservable = extendObservable;
5733
5793
  exports.flow = flow;
@@ -5760,6 +5820,7 @@
5760
5820
  exports.onBecomeUnobserved = onBecomeUnobserved;
5761
5821
  exports.onReactionError = onReactionError;
5762
5822
  exports.override = override;
5823
+ exports.ownKeys = apiOwnKeys;
5763
5824
  exports.reaction = reaction;
5764
5825
  exports.remove = remove;
5765
5826
  exports.runInAction = runInAction;