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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobx",
3
- "version": "6.6.2",
3
+ "version": "6.7.0",
4
4
  "description": "Simple, scalable state management.",
5
5
  "source": "src/mobx.ts",
6
6
  "main": "dist/index.js",
package/src/api/when.ts CHANGED
@@ -13,6 +13,7 @@ export interface IWhenOptions {
13
13
  name?: string
14
14
  timeout?: number
15
15
  onError?: (error: any) => void
16
+ signal?: AbortSignal
16
17
  }
17
18
 
18
19
  export function when(
@@ -74,14 +75,23 @@ function whenPromise(
74
75
  if (__DEV__ && opts && opts.onError) {
75
76
  return die(`the options 'onError' and 'promise' cannot be combined`)
76
77
  }
78
+ if (opts?.signal?.aborted) {
79
+ return Object.assign(Promise.reject(new Error("WHEN_ABORTED")), { cancel: () => null })
80
+ }
77
81
  let cancel
82
+ let abort
78
83
  const res = new Promise((resolve, reject) => {
79
84
  let disposer = _when(predicate, resolve as Lambda, { ...opts, onError: reject })
80
85
  cancel = () => {
81
86
  disposer()
82
87
  reject(new Error("WHEN_CANCELLED"))
83
88
  }
84
- })
89
+ abort = () => {
90
+ disposer()
91
+ reject(new Error("WHEN_ABORTED"))
92
+ }
93
+ opts?.signal?.addEventListener("abort", abort)
94
+ }).finally(() => opts?.signal?.removeEventListener("abort", abort))
85
95
  ;(res as any).cancel = cancel
86
96
  return res as any
87
97
  }
@@ -151,7 +151,7 @@ export function reportObserved(observable: IObservable): boolean {
151
151
  observable.onBO()
152
152
  }
153
153
  }
154
- return true
154
+ return observable.isBeingObserved_
155
155
  } else if (observable.observers_.size === 0 && globalState.inBatch > 0) {
156
156
  queueForUnobservation(observable)
157
157
  }
@@ -345,19 +345,24 @@ export class ObservableArrayAdministration
345
345
  }
346
346
 
347
347
  get_(index: number): any | undefined {
348
- if (index < this.values_.length) {
349
- this.atom_.reportObserved()
350
- return this.dehanceValue_(this.values_[index])
351
- }
352
- console.warn(
353
- __DEV__
354
- ? `[mobx] Out of bounds read: ${index}`
355
- : `[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`
356
- )
348
+ if (this.legacyMode_ && index >= this.values_.length) {
349
+ console.warn(
350
+ __DEV__
351
+ ? `[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`
352
+ : `[mobx] Out of bounds read: ${index}`
353
+ )
354
+ return undefined
355
+ }
356
+ this.atom_.reportObserved()
357
+ return this.dehanceValue_(this.values_[index])
357
358
  }
358
359
 
359
360
  set_(index: number, newValue: any) {
360
361
  const values = this.values_
362
+ if (this.legacyMode_ && index > values.length) {
363
+ // out of bounds
364
+ die(17, index, values.length)
365
+ }
361
366
  if (index < values.length) {
362
367
  // update at index in range
363
368
  checkIfStateModificationsAreAllowed(this.atom_)
@@ -380,12 +385,16 @@ export class ObservableArrayAdministration
380
385
  values[index] = newValue
381
386
  this.notifyArrayChildUpdate_(index, newValue, oldValue)
382
387
  }
383
- } else if (index === values.length) {
384
- // add a new item
385
- this.spliceWithArray_(index, 0, [newValue])
386
388
  } else {
387
- // out of bounds
388
- die(17, index, values.length)
389
+ // For out of bound index, we don't create an actual sparse array,
390
+ // but rather fill the holes with undefined (same as setArrayLength_).
391
+ // This could be considered a bug.
392
+ const newItems = new Array(index + 1 - values.length)
393
+ for (let i = 0; i < newItems.length - 1; i++) {
394
+ newItems[i] = undefined
395
+ } // No Array.fill everywhere...
396
+ newItems[newItems.length - 1] = newValue
397
+ this.spliceWithArray_(values.length, 0, newItems)
389
398
  }
390
399
  }
391
400
  }