mobx 6.6.2 → 6.7.0

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,17 @@
1
1
  # mobx
2
2
 
3
+ ## 6.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`8cf4784f`](https://github.com/mobxjs/mobx/commit/8cf4784f53857cc977aed641bd778f2c14a080f5) [#3559](https://github.com/mobxjs/mobx/pull/3559) Thanks [@urugator](https://github.com/urugator)! - Proxied observable arrays can now safely read/write out of bound indices. See https://github.com/mobxjs/mobx/discussions/3537
8
+
9
+ * [`223e3688`](https://github.com/mobxjs/mobx/commit/223e3688631528a327c79d39e2f497c6e1506165) [#3551](https://github.com/mobxjs/mobx/pull/3551) Thanks [@deadbeef84](https://github.com/deadbeef84)! - Added new option `signal` to `when()`, to support abortion using an AbortSignal / AbortController.
10
+
11
+ ### Patch Changes
12
+
13
+ - [`fe25cfed`](https://github.com/mobxjs/mobx/commit/fe25cfede0aee3bddd7fa434a14ed4b40a57ee26) [#3566](https://github.com/mobxjs/mobx/pull/3566) Thanks [@upsuper](https://github.com/upsuper)! - Make return value of reportObserved match invoke of onBecomeObserved
14
+
3
15
  ## 6.6.2
4
16
 
5
17
  ### Patch Changes
@@ -1,8 +1,10 @@
1
+ /// <reference types="node" />
1
2
  import { IReactionDisposer, Lambda } from "../internal";
2
3
  export interface IWhenOptions {
3
4
  name?: string;
4
5
  timeout?: number;
5
6
  onError?: (error: any) => void;
7
+ signal?: AbortSignal;
6
8
  }
7
9
  export declare function when(predicate: () => boolean, opts?: IWhenOptions): Promise<void> & {
8
10
  cancel(): void;
@@ -2318,7 +2318,7 @@ function reportObserved(observable) {
2318
2318
  }
2319
2319
  }
2320
2320
 
2321
- return true;
2321
+ return observable.isBeingObserved_;
2322
2322
  } else if (observable.observers_.size === 0 && globalState.inBatch > 0) {
2323
2323
  queueForUnobservation(observable);
2324
2324
  }
@@ -3685,12 +3685,25 @@ function _when(predicate, effect, opts) {
3685
3685
  }
3686
3686
 
3687
3687
  function whenPromise(predicate, opts) {
3688
+ var _opts$signal;
3689
+
3688
3690
  if ( opts && opts.onError) {
3689
3691
  return die("the options 'onError' and 'promise' cannot be combined");
3690
3692
  }
3691
3693
 
3694
+ if (opts != null && (_opts$signal = opts.signal) != null && _opts$signal.aborted) {
3695
+ return Object.assign(Promise.reject(new Error("WHEN_ABORTED")), {
3696
+ cancel: function cancel() {
3697
+ return null;
3698
+ }
3699
+ });
3700
+ }
3701
+
3692
3702
  var cancel;
3703
+ var abort;
3693
3704
  var res = new Promise(function (resolve, reject) {
3705
+ var _opts$signal2;
3706
+
3694
3707
  var disposer = _when(predicate, resolve, _extends({}, opts, {
3695
3708
  onError: reject
3696
3709
  }));
@@ -3699,6 +3712,17 @@ function whenPromise(predicate, opts) {
3699
3712
  disposer();
3700
3713
  reject(new Error("WHEN_CANCELLED"));
3701
3714
  };
3715
+
3716
+ abort = function abort() {
3717
+ disposer();
3718
+ reject(new Error("WHEN_ABORTED"));
3719
+ };
3720
+
3721
+ opts == null ? void 0 : (_opts$signal2 = opts.signal) == null ? void 0 : _opts$signal2.addEventListener("abort", abort);
3722
+ })["finally"](function () {
3723
+ var _opts$signal3;
3724
+
3725
+ return opts == null ? void 0 : (_opts$signal3 = opts.signal) == null ? void 0 : _opts$signal3.removeEventListener("abort", abort);
3702
3726
  });
3703
3727
  res.cancel = cancel;
3704
3728
  return res;
@@ -4217,17 +4241,23 @@ var ObservableArrayAdministration = /*#__PURE__*/function () {
4217
4241
  };
4218
4242
 
4219
4243
  _proto.get_ = function get_(index) {
4220
- if (index < this.values_.length) {
4221
- this.atom_.reportObserved();
4222
- return this.dehanceValue_(this.values_[index]);
4244
+ if (this.legacyMode_ && index >= this.values_.length) {
4245
+ console.warn( "[mobx.array] Attempt to read an array index (" + index + ") that is out of bounds (" + this.values_.length + "). Please check length first. Out of bound indices will not be tracked by MobX" );
4246
+ return undefined;
4223
4247
  }
4224
4248
 
4225
- console.warn( "[mobx] Out of bounds read: " + index );
4249
+ this.atom_.reportObserved();
4250
+ return this.dehanceValue_(this.values_[index]);
4226
4251
  };
4227
4252
 
4228
4253
  _proto.set_ = function set_(index, newValue) {
4229
4254
  var values = this.values_;
4230
4255
 
4256
+ if (this.legacyMode_ && index > values.length) {
4257
+ // out of bounds
4258
+ die(17, index, values.length);
4259
+ }
4260
+
4231
4261
  if (index < values.length) {
4232
4262
  // update at index in range
4233
4263
  checkIfStateModificationsAreAllowed(this.atom_);
@@ -4255,12 +4285,19 @@ var ObservableArrayAdministration = /*#__PURE__*/function () {
4255
4285
  values[index] = newValue;
4256
4286
  this.notifyArrayChildUpdate_(index, newValue, oldValue);
4257
4287
  }
4258
- } else if (index === values.length) {
4259
- // add a new item
4260
- this.spliceWithArray_(index, 0, [newValue]);
4261
4288
  } else {
4262
- // out of bounds
4263
- die(17, index, values.length);
4289
+ // For out of bound index, we don't create an actual sparse array,
4290
+ // but rather fill the holes with undefined (same as setArrayLength_).
4291
+ // This could be considered a bug.
4292
+ var newItems = new Array(index + 1 - values.length);
4293
+
4294
+ for (var i = 0; i < newItems.length - 1; i++) {
4295
+ newItems[i] = undefined;
4296
+ } // No Array.fill everywhere...
4297
+
4298
+
4299
+ newItems[newItems.length - 1] = newValue;
4300
+ this.spliceWithArray_(values.length, 0, newItems);
4264
4301
  }
4265
4302
  };
4266
4303