mobx 6.7.0 → 6.9.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.7.0",
3
+ "version": "6.9.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,11 +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
16
- signal?: AbortSignal
24
+ signal?: GenericAbortSignal
17
25
  }
18
26
 
19
27
  export function when(
@@ -90,8 +98,8 @@ function whenPromise(
90
98
  disposer()
91
99
  reject(new Error("WHEN_ABORTED"))
92
100
  }
93
- opts?.signal?.addEventListener("abort", abort)
94
- }).finally(() => opts?.signal?.removeEventListener("abort", abort))
101
+ opts?.signal?.addEventListener?.("abort", abort)
102
+ }).finally(() => opts?.signal?.removeEventListener?.("abort", abort))
95
103
  ;(res as any).cancel = cancel
96
104
  return res as any
97
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
  }
package/src/core/atom.ts CHANGED
@@ -11,7 +11,8 @@ import {
11
11
  propagateChanged,
12
12
  reportObserved,
13
13
  startBatch,
14
- Lambda
14
+ Lambda,
15
+ globalState
15
16
  } from "../internal"
16
17
 
17
18
  export const $mobx = Symbol("mobx administration")
@@ -66,6 +67,12 @@ export class Atom implements IAtom {
66
67
  public reportChanged() {
67
68
  startBatch()
68
69
  propagateChanged(this)
70
+ // We could update state version only at the end of batch,
71
+ // but we would still have to switch some global flag here to signal a change.
72
+ globalState.stateVersion =
73
+ globalState.stateVersion < Number.MAX_SAFE_INTEGER
74
+ ? globalState.stateVersion + 1
75
+ : Number.MIN_SAFE_INTEGER
69
76
  endBatch()
70
77
  }
71
78
 
@@ -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> {
@@ -150,6 +150,11 @@ export class MobXGlobals {
150
150
  * configurable: true
151
151
  */
152
152
  safeDescriptors = true
153
+
154
+ /**
155
+ * Changes with each state update, used by useSyncExternalStore
156
+ */
157
+ stateVersion = Number.MIN_SAFE_INTEGER
153
158
  }
154
159
 
155
160
  let canMergeGlobalState = true
@@ -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) {