mobx 6.6.2 → 6.8.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.
@@ -1,4 +1,4 @@
1
- import { $mobx, IEnhancer, IListenable, Lambda, IInterceptable, IInterceptor } from "../internal";
1
+ import { $mobx, IEnhancer, IListenable, Lambda, IInterceptable, IInterceptor, IAtom } from "../internal";
2
2
  export declare type IObservableSetInitialValues<T> = Set<T> | readonly T[];
3
3
  export declare type ISetDidChange<T = any> = {
4
4
  object: ObservableSet<T>;
@@ -26,7 +26,7 @@ export declare class ObservableSet<T = any> implements Set<T>, IInterceptable<IS
26
26
  name_: string;
27
27
  [$mobx]: {};
28
28
  private data_;
29
- private atom_;
29
+ atom_: IAtom;
30
30
  changeListeners_: any;
31
31
  interceptors_: any;
32
32
  dehancer: any;
@@ -9,21 +9,19 @@ export declare type IValueDidChange<T = any> = {
9
9
  observableKind: "value";
10
10
  object: IObservableValue<T>;
11
11
  debugObjectName: string;
12
- newValue: unknown;
13
- oldValue: unknown;
12
+ newValue: T;
13
+ oldValue: T | undefined;
14
14
  };
15
15
  export declare type IBoxDidChange<T = any> = {
16
16
  type: "create";
17
17
  observableKind: "value";
18
18
  object: IObservableValue<T>;
19
19
  debugObjectName: string;
20
- newValue: unknown;
20
+ newValue: T;
21
21
  } | IValueDidChange<T>;
22
22
  export interface IObservableValue<T> {
23
23
  get(): T;
24
24
  set(value: T): void;
25
- intercept_(handler: IInterceptor<IValueWillChange<T>>): Lambda;
26
- observe_(listener: (change: IValueDidChange<T>) => void, fireImmediately?: boolean): Lambda;
27
25
  }
28
26
  export declare class ObservableValue<T> extends Atom implements IObservableValue<T>, IInterceptable<IValueWillChange<T>>, IListenable {
29
27
  enhancer: IEnhancer<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobx",
3
- "version": "6.6.2",
3
+ "version": "6.8.0",
4
4
  "description": "Simple, scalable state management.",
5
5
  "source": "src/mobx.ts",
6
6
  "main": "dist/index.js",
@@ -134,7 +134,7 @@ function createObservable(v: any, arg2?: any, arg3?: any) {
134
134
  // anything else
135
135
  return observable.box(v, arg2)
136
136
  }
137
- Object.assign(createObservable, observableDecoratorAnnotation)
137
+ assign(createObservable, observableDecoratorAnnotation)
138
138
 
139
139
  export interface IObservableValueFactory {
140
140
  <T>(value: T, options?: CreateObservableOptions): IObservableValue<T>
package/src/api/when.ts CHANGED
@@ -9,10 +9,19 @@ import {
9
9
  allowStateChanges
10
10
  } from "../internal"
11
11
 
12
+ // https://github.com/mobxjs/mobx/issues/3582
13
+ interface GenericAbortSignal {
14
+ readonly aborted: boolean
15
+ onabort?: ((...args: any) => any) | null
16
+ addEventListener?: (...args: any) => any
17
+ removeEventListener?: (...args: any) => any
18
+ }
19
+
12
20
  export interface IWhenOptions {
13
21
  name?: string
14
22
  timeout?: number
15
23
  onError?: (error: any) => void
24
+ signal?: GenericAbortSignal
16
25
  }
17
26
 
18
27
  export function when(
@@ -74,14 +83,23 @@ function whenPromise(
74
83
  if (__DEV__ && opts && opts.onError) {
75
84
  return die(`the options 'onError' and 'promise' cannot be combined`)
76
85
  }
86
+ if (opts?.signal?.aborted) {
87
+ return Object.assign(Promise.reject(new Error("WHEN_ABORTED")), { cancel: () => null })
88
+ }
77
89
  let cancel
90
+ let abort
78
91
  const res = new Promise((resolve, reject) => {
79
92
  let disposer = _when(predicate, resolve as Lambda, { ...opts, onError: reject })
80
93
  cancel = () => {
81
94
  disposer()
82
95
  reject(new Error("WHEN_CANCELLED"))
83
96
  }
84
- })
97
+ abort = () => {
98
+ disposer()
99
+ reject(new Error("WHEN_ABORTED"))
100
+ }
101
+ opts?.signal?.addEventListener?.("abort", abort)
102
+ }).finally(() => opts?.signal?.removeEventListener?.("abort", abort))
85
103
  ;(res as any).cancel = cancel
86
104
  return res as any
87
105
  }
@@ -14,7 +14,8 @@ import {
14
14
  ACTION,
15
15
  EMPTY_ARRAY,
16
16
  die,
17
- getDescriptor
17
+ getDescriptor,
18
+ defineProperty
18
19
  } from "../internal"
19
20
 
20
21
  // we don't use globalState for these in order to avoid possible issues with multiple
@@ -51,7 +52,7 @@ export function createAction(
51
52
  res.isMobxAction = true
52
53
  if (isFunctionNameConfigurable) {
53
54
  tmpNameDescriptor.value = actionName
54
- Object.defineProperty(res, "name", tmpNameDescriptor)
55
+ defineProperty(res, "name", tmpNameDescriptor)
55
56
  }
56
57
  return res
57
58
  }
@@ -35,7 +35,6 @@ import {
35
35
  export interface IComputedValue<T> {
36
36
  get(): T
37
37
  set(value: T): void
38
- observe_(listener: (change: IComputedDidChange<T>) => void, fireImmediately?: boolean): Lambda
39
38
  }
40
39
 
41
40
  export interface IComputedValueOptions<T> {
@@ -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
  }
@@ -65,7 +65,7 @@ export type ISetWillChange<T = any> =
65
65
  export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillChange>, IListenable {
66
66
  [$mobx] = ObservableSetMarker
67
67
  private data_: Set<any> = new Set()
68
- private atom_: IAtom
68
+ atom_: IAtom
69
69
  changeListeners_
70
70
  interceptors_
71
71
  dehancer: any
@@ -37,8 +37,8 @@ export type IValueDidChange<T = any> = {
37
37
  observableKind: "value"
38
38
  object: IObservableValue<T>
39
39
  debugObjectName: string
40
- newValue: unknown
41
- oldValue: unknown
40
+ newValue: T
41
+ oldValue: T | undefined
42
42
  }
43
43
  export type IBoxDidChange<T = any> =
44
44
  | {
@@ -46,15 +46,13 @@ export type IBoxDidChange<T = any> =
46
46
  observableKind: "value"
47
47
  object: IObservableValue<T>
48
48
  debugObjectName: string
49
- newValue: unknown
49
+ newValue: T
50
50
  }
51
51
  | IValueDidChange<T>
52
52
 
53
53
  export interface IObservableValue<T> {
54
54
  get(): T
55
55
  set(value: T): void
56
- intercept_(handler: IInterceptor<IValueWillChange<T>>): Lambda
57
- observe_(listener: (change: IValueDidChange<T>) => void, fireImmediately?: boolean): Lambda
58
56
  }
59
57
 
60
58
  const CREATE = "create"
@@ -22,7 +22,7 @@ export function getAtom(thing: any, property?: PropertyKey): IDepTreeNode {
22
22
  return (thing as any)[$mobx].atom_
23
23
  }
24
24
  if (isObservableSet(thing)) {
25
- return (thing as any)[$mobx]
25
+ return thing.atom_
26
26
  }
27
27
  if (isObservableMap(thing)) {
28
28
  if (property === undefined) {