mobx 6.16.1 → 7.0.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.
Files changed (98) hide show
  1. package/CHANGELOG.md +72 -0
  2. package/README.md +9 -13
  3. package/dist/api/action.d.ts +6 -4
  4. package/dist/api/annotation.d.ts +0 -1
  5. package/dist/api/computed.d.ts +4 -7
  6. package/dist/api/configure.d.ts +0 -1
  7. package/dist/api/decoratorannotation.d.ts +3 -0
  8. package/dist/api/decorators.d.ts +0 -19
  9. package/dist/api/flow.d.ts +4 -5
  10. package/dist/api/makeObservable.d.ts +2 -3
  11. package/dist/api/observable.d.ts +9 -15
  12. package/dist/core/atom.d.ts +0 -3
  13. package/dist/core/computedvalue.d.ts +4 -32
  14. package/dist/core/derivation.d.ts +1 -7
  15. package/dist/core/globalstate.d.ts +0 -2
  16. package/dist/core/reaction.d.ts +1 -9
  17. package/dist/errors.d.ts +6 -4
  18. package/dist/internal.d.ts +1 -4
  19. package/dist/mobx.cjs.development.js +1630 -2447
  20. package/dist/mobx.cjs.development.js.map +1 -1
  21. package/dist/mobx.cjs.production.min.js +1 -1
  22. package/dist/mobx.cjs.production.min.js.map +1 -1
  23. package/dist/mobx.d.ts +2 -2
  24. package/dist/mobx.esm.development.js +1619 -2446
  25. package/dist/mobx.esm.development.js.map +1 -1
  26. package/dist/mobx.esm.js +1567 -2397
  27. package/dist/mobx.esm.js.map +1 -1
  28. package/dist/mobx.esm.production.min.js +1 -1
  29. package/dist/mobx.esm.production.min.js.map +1 -1
  30. package/dist/mobx.mjs +5271 -0
  31. package/dist/mobx.mjs.map +1 -0
  32. package/dist/mobx.umd.development.js +1633 -2450
  33. package/dist/mobx.umd.development.js.map +1 -1
  34. package/dist/mobx.umd.production.min.js +1 -1
  35. package/dist/mobx.umd.production.min.js.map +1 -1
  36. package/dist/types/actionannotation.d.ts +1 -0
  37. package/dist/types/computedannotation.d.ts +1 -0
  38. package/dist/types/decorator_fills.d.ts +2 -0
  39. package/dist/types/flowannotation.d.ts +1 -0
  40. package/dist/types/observableannotation.d.ts +5 -0
  41. package/dist/types/observablearray.d.ts +2 -5
  42. package/dist/types/observablemap.d.ts +1 -8
  43. package/dist/types/observableobject.d.ts +2 -13
  44. package/dist/types/observableset.d.ts +1 -3
  45. package/dist/types/observablevalue.d.ts +4 -6
  46. package/dist/types/overrideannotation.d.ts +1 -2
  47. package/dist/utils/comparer.d.ts +4 -11
  48. package/dist/utils/utils.d.ts +0 -2
  49. package/package.json +25 -15
  50. package/src/api/action.ts +27 -35
  51. package/src/api/annotation.ts +0 -1
  52. package/src/api/autorun.ts +2 -4
  53. package/src/api/computed.ts +21 -28
  54. package/src/api/configure.ts +1 -15
  55. package/src/api/decoratorannotation.ts +20 -0
  56. package/src/api/decorators.ts +1 -85
  57. package/src/api/extras.ts +1 -1
  58. package/src/api/flow.ts +26 -29
  59. package/src/api/intercept.ts +4 -3
  60. package/src/api/makeObservable.ts +45 -17
  61. package/src/api/object-api.ts +1 -1
  62. package/src/api/observable.ts +34 -67
  63. package/src/api/observe.ts +82 -3
  64. package/src/api/when.ts +4 -3
  65. package/src/core/action.ts +12 -5
  66. package/src/core/atom.ts +14 -13
  67. package/src/core/computedvalue.ts +27 -67
  68. package/src/core/derivation.ts +15 -14
  69. package/src/core/globalstate.ts +8 -13
  70. package/src/core/observable.ts +0 -48
  71. package/src/core/reaction.ts +19 -25
  72. package/src/core/spy.ts +3 -3
  73. package/src/errors.ts +23 -12
  74. package/src/internal.ts +1 -4
  75. package/src/mobx.ts +21 -10
  76. package/src/types/actionannotation.ts +5 -9
  77. package/src/types/autoannotation.ts +8 -11
  78. package/src/types/computedannotation.ts +13 -11
  79. package/src/types/decorator_fills.ts +4 -0
  80. package/src/types/dynamicobject.ts +0 -29
  81. package/src/types/flowannotation.ts +7 -4
  82. package/src/types/observableannotation.ts +6 -7
  83. package/src/types/observablearray.ts +8 -61
  84. package/src/types/observablemap.ts +4 -29
  85. package/src/types/observableobject.ts +5 -70
  86. package/src/types/observableset.ts +5 -28
  87. package/src/types/observablevalue.ts +7 -29
  88. package/src/types/overrideannotation.ts +7 -22
  89. package/src/types/type-utils.ts +4 -2
  90. package/src/utils/comparer.ts +4 -17
  91. package/src/utils/iterable.ts +3 -4
  92. package/src/utils/utils.ts +4 -46
  93. package/dist/api/trace.d.ts +0 -3
  94. package/dist/types/legacyobservablearray.d.ts +0 -14
  95. package/dist/utils/global.d.ts +0 -1
  96. package/src/api/trace.ts +0 -35
  97. package/src/types/legacyobservablearray.ts +0 -155
  98. package/src/utils/global.ts +0 -20
@@ -1,5 +1,6 @@
1
1
  import { ObservableObjectAdministration, Annotation } from "../internal";
2
2
  export declare function createActionAnnotation(name: string, options?: object): Annotation;
3
+ export declare function decorateAction20223_(annotation: Annotation, mthd: any, context: DecoratorContext): any;
3
4
  export declare function createActionDescriptor(adm: ObservableObjectAdministration, annotation: Annotation, key: PropertyKey, descriptor: PropertyDescriptor, safeDescriptors?: boolean): {
4
5
  value: Function;
5
6
  configurable: boolean;
@@ -1,2 +1,3 @@
1
1
  import { Annotation } from "../internal";
2
2
  export declare function createComputedAnnotation(name: string, options?: object): Annotation;
3
+ export declare function decorateComputed20223_(annotation: Annotation, get: any, context: ClassGetterDecoratorContext): () => any;
@@ -3,4 +3,6 @@ export type ClassGetterDecorator<This = any, Value = any> = (value: (this: This)
3
3
  export type ClassSetterDecorator<This = any, Value = any> = (value: (this: This, value: Value) => void, context: ClassSetterDecoratorContext) => ((this: This, value: Value) => void) | void;
4
4
  export type ClassMethodDecorator<This = any, Value extends (...p: any[]) => any = any> = (value: Value, context: ClassMethodDecoratorContext<This, Value>) => Value | void;
5
5
  export type ClassFieldDecorator<This = any, Value extends (...p: any[]) => any = any> = (value: Value, context: ClassFieldDecoratorContext<This, Value>) => Value | void;
6
+ export type ClassMethodAndFieldDecorator = ClassMethodDecorator & ClassFieldDecorator;
7
+ export type ClassAccessorAndFieldDecorator = ClassAccessorDecorator & ClassFieldDecorator;
6
8
  export type Decorator = ClassAccessorDecorator | ClassGetterDecorator | ClassSetterDecorator | ClassMethodDecorator | ClassFieldDecorator;
@@ -1,2 +1,3 @@
1
1
  import { Annotation } from "../internal";
2
2
  export declare function createFlowAnnotation(name: string, options?: object): Annotation;
3
+ export declare function decorateFlow20223_(annotation: Annotation, mthd: any, context: ClassMethodDecoratorContext): any;
@@ -1,2 +1,7 @@
1
1
  import { Annotation } from "../internal";
2
2
  export declare function createObservableAnnotation(name: string, options?: object): Annotation;
3
+ export declare function decorateObservable20223_(annotation: Annotation, desc: any, context: ClassAccessorDecoratorContext | ClassFieldDecoratorContext): {
4
+ get(): any;
5
+ set(value: any): boolean | null;
6
+ init(value: any): any;
7
+ } | undefined;
@@ -1,4 +1,4 @@
1
- import { IAtom, IEnhancer, IInterceptable, IInterceptor, IListenable, Lambda } from "../internal";
1
+ import { IAtom, IEnhancer, IInterceptable, IListenable } from "../internal";
2
2
  export declare const UPDATE = "update";
3
3
  export declare const MAX_SPLICE_SIZE = 10000;
4
4
  export interface IObservableArray<T = any> extends Array<T> {
@@ -42,7 +42,6 @@ export interface IArrayWillSplice<T = any> {
42
42
  }
43
43
  export declare class ObservableArrayAdministration implements IInterceptable<IArrayWillChange<any> | IArrayWillSplice<any>>, IListenable {
44
44
  owned_: boolean;
45
- legacyMode_: boolean;
46
45
  atom_: IAtom;
47
46
  readonly values_: any[];
48
47
  interceptors_: any;
@@ -51,11 +50,9 @@ export declare class ObservableArrayAdministration implements IInterceptable<IAr
51
50
  dehancer: any;
52
51
  proxy_: IObservableArray<any>;
53
52
  lastKnownLength_: number;
54
- constructor(name: string | undefined, enhancer: IEnhancer<any>, owned_: boolean, legacyMode_: boolean);
53
+ constructor(name: string | undefined, enhancer: IEnhancer<any>, owned_: boolean);
55
54
  dehanceValue_(value: any): any;
56
55
  dehanceValues_(values: any[]): any[];
57
- intercept_(handler: IInterceptor<IArrayWillChange<any> | IArrayWillSplice<any>>): Lambda;
58
- observe_(listener: (changeData: IArrayDidChange<any>) => void, fireImmediately?: boolean): Lambda;
59
56
  getArrayLength_(): number;
60
57
  setArrayLength_(newLength: number): void;
61
58
  updateArrayLength_(oldLength: number, delta: number): void;
@@ -1,4 +1,4 @@
1
- import { $mobx, IEnhancer, IInterceptable, IInterceptor, IListenable, Lambda, ObservableValue, IAtom } from "../internal";
1
+ import { $mobx, IEnhancer, IInterceptable, IListenable, ObservableValue, IAtom } from "../internal";
2
2
  export interface IKeyValueMap<V = any> {
3
3
  [key: string]: V;
4
4
  }
@@ -69,12 +69,5 @@ export declare class ObservableMap<K = any, V = any> implements Map<K, V>, IInte
69
69
  toString(): string;
70
70
  toJSON(): [K, V][];
71
71
  get [Symbol.toStringTag](): string;
72
- /**
73
- * Observes this object. Triggers for the events 'add', 'update' and 'delete'.
74
- * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
75
- * for callback details
76
- */
77
- observe_(listener: (changes: IMapDidChange<K, V>) => void, fireImmediately?: boolean): Lambda;
78
- intercept_(handler: IInterceptor<IMapWillChange<K, V>>): Lambda;
79
72
  }
80
73
  export declare var isObservableMap: (thing: any) => thing is ObservableMap<any, any>;
@@ -1,4 +1,4 @@
1
- import { CreateObservableOptions, $mobx, Annotation, ComputedValue, IAtom, IComputedValueOptions, IEnhancer, IInterceptable, IListenable, Lambda, ObservableValue } from "../internal";
1
+ import { CreateObservableOptions, $mobx, Annotation, ComputedValue, IAtom, IComputedValueOptions, IEnhancer, IInterceptable, IListenable, ObservableValue } from "../internal";
2
2
  export type IObjectDidChange<T = any> = {
3
3
  observableKind: "object";
4
4
  name: PropertyKey;
@@ -54,11 +54,6 @@ export declare class ObservableObjectAdministration implements IInterceptable<IO
54
54
  */
55
55
  set_(key: PropertyKey, value: any, proxyTrap?: boolean): boolean | null;
56
56
  has_(key: PropertyKey): boolean;
57
- /**
58
- * @param {PropertyKey} key
59
- * @param {Annotation|boolean} annotation true - use default annotation, false - ignore prop
60
- */
61
- make_(key: PropertyKey, annotation: Annotation | boolean): void;
62
57
  /**
63
58
  * @param {PropertyKey} key
64
59
  * @param {PropertyDescriptor} descriptor
@@ -83,13 +78,6 @@ export declare class ObservableObjectAdministration implements IInterceptable<IO
83
78
  * @returns {boolean|null} true on success, false on failure (proxyTrap + non-configurable), null when cancelled by interceptor
84
79
  */
85
80
  delete_(key: PropertyKey, proxyTrap?: boolean): boolean | null;
86
- /**
87
- * Observes this object. Triggers for the events 'add', 'update' and 'delete'.
88
- * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
89
- * for callback details
90
- */
91
- observe_(callback: (changes: IObjectDidChange) => void, fireImmediately?: boolean): Lambda;
92
- intercept_(handler: any): Lambda;
93
81
  notifyPropertyAddition_(key: PropertyKey, value: any): void;
94
82
  ownKeys_(): Array<string | symbol>;
95
83
  keys_(): PropertyKey[];
@@ -100,3 +88,4 @@ export interface IIsObservableObject {
100
88
  export declare function asObservableObject(target: any, options?: CreateObservableOptions): IIsObservableObject;
101
89
  export declare function isObservableObject(thing: any): boolean;
102
90
  export declare function recordAnnotationApplied(adm: ObservableObjectAdministration, annotation: Annotation, key: PropertyKey): void;
91
+ export declare function assertAnnotable(adm: ObservableObjectAdministration, annotation: Annotation, key: PropertyKey): void;
@@ -1,4 +1,4 @@
1
- import { $mobx, IEnhancer, IListenable, Lambda, IInterceptable, IInterceptor, IAtom } from "../internal";
1
+ import { $mobx, IEnhancer, IListenable, IInterceptable, IAtom } from "../internal";
2
2
  export type IObservableSetInitialValues<T> = Set<T> | readonly T[];
3
3
  export type ISetDidChange<T = any> = {
4
4
  object: ObservableSet<T>;
@@ -52,8 +52,6 @@ export declare class ObservableSet<T = any> implements Set<T>, IInterceptable<IS
52
52
  isSupersetOf(otherSet: ReadonlySetLike<unknown>): boolean;
53
53
  isDisjointFrom(otherSet: ReadonlySetLike<unknown> | Set<unknown>): boolean;
54
54
  replace(other: ObservableSet<T> | IObservableSetInitialValues<T>): ObservableSet<T>;
55
- observe_(listener: (changes: ISetDidChange<T>) => void, fireImmediately?: boolean): Lambda;
56
- intercept_(handler: IInterceptor<ISetWillChange<T>>): Lambda;
57
55
  toJSON(): T[];
58
56
  toString(): string;
59
57
  [Symbol.iterator](): SetIterator<T>;
@@ -1,4 +1,4 @@
1
- import { Atom, IEnhancer, IInterceptable, IEqualsComparer, IInterceptor, IListenable, Lambda } from "../internal";
1
+ import { Atom, IEnhancer, IInterceptable, IEqualsComparer, IListenable } from "../internal";
2
2
  export interface IValueWillChange<T> {
3
3
  object: IObservableValue<T>;
4
4
  type: "update";
@@ -24,22 +24,20 @@ export interface IObservableValue<T> {
24
24
  set(value: T): void;
25
25
  }
26
26
  export declare class ObservableValue<T> extends Atom implements IObservableValue<T>, IInterceptable<IValueWillChange<T>>, IListenable {
27
- enhancer: IEnhancer<T>;
27
+ enhancer_: IEnhancer<T>;
28
28
  name_: string;
29
- private equals;
29
+ private equals_;
30
30
  hasUnreportedChange_: boolean;
31
31
  interceptors_: any;
32
32
  changeListeners_: any;
33
33
  value_: any;
34
34
  dehancer: any;
35
- constructor(value: T, enhancer: IEnhancer<T>, name_?: string, notifySpy?: boolean, equals?: IEqualsComparer<any>);
35
+ constructor(value: T, enhancer_: IEnhancer<T>, name_?: string, notifySpy?: boolean, equals_?: IEqualsComparer<any>);
36
36
  private dehanceValue;
37
37
  set(newValue: T): void;
38
38
  private prepareNewValue_;
39
39
  setNewValue_(newValue: T): void;
40
40
  get(): T;
41
- intercept_(handler: IInterceptor<IValueWillChange<T>>): Lambda;
42
- observe_(listener: (change: IValueDidChange<T>) => void, fireImmediately?: boolean): Lambda;
43
41
  raw(): any;
44
42
  toJSON(): T;
45
43
  toString(): string;
@@ -1,4 +1,3 @@
1
1
  import { Annotation } from "../internal";
2
- import type { ClassMethodDecorator } from "./decorator_fills";
3
- export declare const override: Annotation & PropertyDecorator & ClassMethodDecorator;
2
+ export declare const override: Annotation;
4
3
  export declare function isOverride(annotation: Annotation): boolean;
@@ -1,14 +1,7 @@
1
1
  export interface IEqualsComparer<T> {
2
2
  (a: T, b: T): boolean;
3
3
  }
4
- declare function identityComparer(a: any, b: any): boolean;
5
- declare function structuralComparer(a: any, b: any): boolean;
6
- declare function shallowComparer(a: any, b: any): boolean;
7
- declare function defaultComparer(a: any, b: any): boolean;
8
- export declare const comparer: {
9
- identity: typeof identityComparer;
10
- structural: typeof structuralComparer;
11
- default: typeof defaultComparer;
12
- shallow: typeof shallowComparer;
13
- };
14
- export {};
4
+ export declare function compareIdentity(a: any, b: any): boolean;
5
+ export declare function compareStructural(a: any, b: any): boolean;
6
+ export declare function compareShallow(a: any, b: any): boolean;
7
+ export declare const compareDefault: (value1: any, value2: any) => boolean;
@@ -13,8 +13,6 @@ export interface Lambda {
13
13
  (): void;
14
14
  name?: string;
15
15
  }
16
- export declare function assertProxies(): void;
17
- export declare function warnAboutProxyRequirement(msg: string): void;
18
16
  export declare function getNextId(): number;
19
17
  /**
20
18
  * Makes sure that the provided function is invoked at most once.
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "mobx",
3
- "version": "6.16.1",
3
+ "version": "7.0.0",
4
4
  "description": "Simple, scalable state management.",
5
5
  "source": "src/mobx.ts",
6
+ "type": "commonjs",
6
7
  "main": "dist/index.js",
7
8
  "umd:main": "dist/mobx.umd.production.min.js",
8
9
  "unpkg": "dist/mobx.umd.production.min.js",
@@ -12,6 +13,25 @@
12
13
  "react-native": "dist/mobx.esm.js",
13
14
  "types": "dist/mobx.d.ts",
14
15
  "typings": "dist/mobx.d.ts",
16
+ "exports": {
17
+ "./package.json": "./package.json",
18
+ ".": {
19
+ "react-native": {
20
+ "types": "./dist/mobx.d.ts",
21
+ "default": "./dist/mobx.esm.js"
22
+ },
23
+ "import": {
24
+ "types": "./dist/mobx.d.ts",
25
+ "default": "./dist/mobx.mjs"
26
+ },
27
+ "default": {
28
+ "types": "./dist/mobx.d.ts",
29
+ "default": "./dist/index.js"
30
+ }
31
+ },
32
+ "./dist/*": "./dist/*",
33
+ "./src/*": "./src/*"
34
+ },
15
35
  "files": [
16
36
  "src",
17
37
  "dist",
@@ -36,13 +56,6 @@
36
56
  "homepage": "https://mobx.js.org/",
37
57
  "dependencies": {},
38
58
  "devDependencies": {
39
- "@babel/core": "^7.9.0",
40
- "@babel/plugin-proposal-class-properties": "^7.8.3",
41
- "@babel/plugin-proposal-decorators": "^7.8.3",
42
- "@babel/plugin-transform-runtime": "^7.9.0",
43
- "@babel/preset-env": "^7.9.5",
44
- "@babel/preset-typescript": "^7.9.0",
45
- "@babel/runtime": "^7.9.2",
46
59
  "conditional-type-checks": "^1.0.5"
47
60
  },
48
61
  "keywords": [
@@ -62,18 +75,15 @@
62
75
  "scripts": {
63
76
  "test": "jest --config jest.projects.js",
64
77
  "lint": "eslint src/**/*",
65
- "build": "node ../../scripts/build.js mobx",
66
- "build:test": "npm run build -- --target test",
78
+ "build": "rollup --config rollup.config.mjs",
79
+ "build:test": "npm run build -- --environment TARGET:test",
67
80
  "perf": "scripts/perf.sh",
68
- "perf-legacy": "node --expose-gc ./__tests__/perf/index.js legacy",
69
- "perf-proxy": "node --expose-gc ./__tests__/perf/index.js proxy",
70
81
  "perf-decorator": "tsc -p ./__tests__/perf/tsconfig.decorator.json && node --expose-gc ./__tests__/perf/compiled/lazy-computed-decorator.js && node --expose-gc ./__tests__/perf/compiled/lazy-observable-decorator.js",
71
- "test:performance": "npm run perf -- proxy && npm run perf -- legacy && npm run perf-decorator",
82
+ "test:performance": "npm run perf && npm run perf-decorator",
72
83
  "test:mixed-versions": "npm test -- --testRegex mixed-versions",
73
84
  "test:types": "tsc --noEmit",
74
85
  "test:coverage": "npm test -- -i --coverage",
75
- "test:size": "import-size --report . observable computed autorun action",
76
86
  "test:check": "npm run test:types",
77
- "prepublishOnly": "node ./scripts/prepublish.js && npm run build -- --target publish"
87
+ "prepublishOnly": "node ./scripts/prepublish.js && npm run build -- --environment TARGET:publish"
78
88
  }
79
89
  }
package/src/api/action.ts CHANGED
@@ -2,16 +2,15 @@ import {
2
2
  createAction,
3
3
  executeAction,
4
4
  Annotation,
5
- storeAnnotation,
6
5
  die,
7
6
  isFunction,
8
7
  isStringish,
9
- createDecoratorAnnotation,
10
8
  createActionAnnotation,
11
- is20223Decorator
9
+ decorateAction20223_,
10
+ assign
12
11
  } from "../internal"
13
-
14
- import type { ClassFieldDecorator, ClassMethodDecorator } from "../types/decorator_fills"
12
+ import { createDecoratorAnnotation, type DecoratorAnnotation } from "./decoratorannotation"
13
+ import type { ClassMethodAndFieldDecorator } from "../types/decorator_fills"
15
14
 
16
15
  export const ACTION = "action"
17
16
  export const ACTION_BOUND = "action.bound"
@@ -32,28 +31,32 @@ const autoActionBoundAnnotation = createActionAnnotation(AUTOACTION_BOUND, {
32
31
  bound: true
33
32
  })
34
33
 
35
- export interface IActionFactory
36
- extends Annotation,
37
- PropertyDecorator,
38
- ClassMethodDecorator,
39
- ClassFieldDecorator {
34
+ function createActionDecoratorAnnotation(
35
+ annotation: Annotation
36
+ ): DecoratorAnnotation<ClassMethodAndFieldDecorator> {
37
+ return createDecoratorAnnotation(annotation, decorateAction20223_)
38
+ }
39
+
40
+ export interface IActionFactory extends Annotation, ClassMethodAndFieldDecorator {
40
41
  // nameless actions
41
42
  <T extends Function | undefined | null>(fn: T): T
42
43
  // named actions
43
44
  <T extends Function | undefined | null>(name: string, fn: T): T
44
45
 
45
- // named decorator
46
- (customName: string): PropertyDecorator &
47
- Annotation &
48
- ClassMethodDecorator &
49
- ClassFieldDecorator
50
-
51
- // decorator (name no longer supported)
52
- bound: Annotation & PropertyDecorator & ClassMethodDecorator & ClassFieldDecorator
46
+ // named annotation
47
+ (customName: string): DecoratorAnnotation<ClassMethodAndFieldDecorator>
53
48
  }
54
49
 
55
50
  function createActionFactory(autoAction: boolean): IActionFactory {
56
51
  const res: IActionFactory = function action(arg1, arg2?): any {
52
+ if (arg2 && typeof arg2.kind === "string") {
53
+ return decorateAction20223_(
54
+ autoAction ? autoActionAnnotation : actionAnnotation,
55
+ arg1,
56
+ arg2
57
+ )
58
+ }
59
+
57
60
  // action(fn() {})
58
61
  if (isFunction(arg1)) {
59
62
  return createAction(arg1.name || DEFAULT_ACTION_NAME, arg1, autoAction)
@@ -62,20 +65,9 @@ function createActionFactory(autoAction: boolean): IActionFactory {
62
65
  if (isFunction(arg2)) {
63
66
  return createAction(arg1, arg2, autoAction)
64
67
  }
65
- // @action (2022.3 Decorators)
66
- if (is20223Decorator(arg2)) {
67
- return (autoAction ? autoActionAnnotation : actionAnnotation).decorate_20223_(
68
- arg1,
69
- arg2
70
- )
71
- }
72
- // @action
73
- if (isStringish(arg2)) {
74
- return storeAnnotation(arg1, arg2, autoAction ? autoActionAnnotation : actionAnnotation)
75
- }
76
- // action("name") & @action("name")
68
+ // action("name") annotation
77
69
  if (isStringish(arg1)) {
78
- return createDecoratorAnnotation(
70
+ return createActionDecoratorAnnotation(
79
71
  createActionAnnotation(autoAction ? AUTOACTION : ACTION, {
80
72
  name: arg1,
81
73
  autoAction
@@ -91,12 +83,12 @@ function createActionFactory(autoAction: boolean): IActionFactory {
91
83
  }
92
84
 
93
85
  export const action: IActionFactory = createActionFactory(false)
94
- Object.assign(action, actionAnnotation)
86
+ assign(action, actionAnnotation)
95
87
  export const autoAction: IActionFactory = createActionFactory(true)
96
- Object.assign(autoAction, autoActionAnnotation)
88
+ assign(autoAction, autoActionAnnotation)
97
89
 
98
- action.bound = createDecoratorAnnotation(actionBoundAnnotation)
99
- autoAction.bound = createDecoratorAnnotation(autoActionBoundAnnotation)
90
+ export const actionBound = createActionDecoratorAnnotation(actionBoundAnnotation)
91
+ export const autoActionBound = createActionDecoratorAnnotation(autoActionBoundAnnotation)
100
92
 
101
93
  export function runInAction<T>(fn: () => T): T {
102
94
  return executeAction(fn.name || DEFAULT_ACTION_NAME, false, fn, this, undefined)
@@ -20,7 +20,6 @@ export type Annotation = {
20
20
  descriptor: PropertyDescriptor,
21
21
  proxyTrap: boolean
22
22
  ): boolean | null
23
- decorate_20223_(value: any, context: DecoratorContext)
24
23
  options_?: any
25
24
  }
26
25
 
@@ -6,7 +6,7 @@ import {
6
6
  Lambda,
7
7
  Reaction,
8
8
  action,
9
- comparer,
9
+ compareDefault,
10
10
  getNextId,
11
11
  isAction,
12
12
  isFunction,
@@ -140,9 +140,7 @@ export function reaction<T, FireImmediately extends boolean = false>(
140
140
  let isScheduled = false
141
141
  let value: T
142
142
 
143
- const equals: IEqualsComparer<T> = (opts as any).compareStructural
144
- ? comparer.structural
145
- : opts.equals || comparer.default
143
+ const equals: IEqualsComparer<T> = opts.equals || compareDefault
146
144
 
147
145
  const r = new Reaction(
148
146
  name,
@@ -2,53 +2,48 @@ import {
2
2
  ComputedValue,
3
3
  IComputedValueOptions,
4
4
  Annotation,
5
- storeAnnotation,
6
- createDecoratorAnnotation,
7
- isStringish,
8
5
  isPlainObject,
9
6
  isFunction,
10
7
  die,
11
8
  IComputedValue,
12
9
  createComputedAnnotation,
13
- comparer,
14
- is20223Decorator
10
+ compareStructural,
11
+ decorateComputed20223_,
12
+ assign
15
13
  } from "../internal"
16
-
14
+ import { createDecoratorAnnotation, type DecoratorAnnotation } from "./decoratorannotation"
17
15
  import type { ClassGetterDecorator } from "../types/decorator_fills"
18
16
 
19
17
  export const COMPUTED = "computed"
20
18
  export const COMPUTED_STRUCT = "computed.struct"
21
19
 
22
- export interface IComputedFactory extends Annotation, PropertyDecorator, ClassGetterDecorator {
23
- // @computed(opts)
24
- <T>(options: IComputedValueOptions<T>): Annotation & PropertyDecorator & ClassGetterDecorator
20
+ function createComputedDecoratorAnnotation(
21
+ annotation: Annotation
22
+ ): DecoratorAnnotation<ClassGetterDecorator> {
23
+ return createDecoratorAnnotation(annotation, decorateComputed20223_)
24
+ }
25
+
26
+ export interface IComputedFactory extends Annotation, ClassGetterDecorator {
27
+ // computed annotation with options
28
+ <T>(options: IComputedValueOptions<T>): DecoratorAnnotation<ClassGetterDecorator>
25
29
  // computed(fn, opts)
26
30
  <T>(func: () => T, options?: IComputedValueOptions<T>): IComputedValue<T>
27
-
28
- struct: Annotation & PropertyDecorator & ClassGetterDecorator
29
31
  }
30
32
 
31
33
  const computedAnnotation = createComputedAnnotation(COMPUTED)
32
34
  const computedStructAnnotation = createComputedAnnotation(COMPUTED_STRUCT, {
33
- equals: comparer.structural
35
+ equals: compareStructural
34
36
  })
37
+ export const computedStruct = createComputedDecoratorAnnotation(computedStructAnnotation)
35
38
 
36
- /**
37
- * Decorator for class properties: @computed get value() { return expr; }.
38
- * For legacy purposes also invokable as ES5 observable created: `computed(() => expr)`;
39
- */
40
39
  export const computed: IComputedFactory = function computed(arg1, arg2) {
41
- if (is20223Decorator(arg2)) {
42
- // @computed (2022.3 Decorators)
43
- return computedAnnotation.decorate_20223_(arg1, arg2)
44
- }
45
- if (isStringish(arg2)) {
46
- // @computed
47
- return storeAnnotation(arg1, arg2, computedAnnotation)
40
+ if (arg2 && typeof arg2.kind === "string") {
41
+ return decorateComputed20223_(computedAnnotation, arg1, arg2)
48
42
  }
43
+
49
44
  if (isPlainObject(arg1)) {
50
- // @computed({ options })
51
- return createDecoratorAnnotation(createComputedAnnotation(COMPUTED, arg1))
45
+ // computed annotation with options
46
+ return createComputedDecoratorAnnotation(createComputedAnnotation(COMPUTED, arg1))
52
47
  }
53
48
 
54
49
  // computed(expr, options?)
@@ -69,6 +64,4 @@ export const computed: IComputedFactory = function computed(arg1, arg2) {
69
64
  return new ComputedValue(opts)
70
65
  } as any
71
66
 
72
- Object.assign(computed, computedAnnotation)
73
-
74
- computed.struct = createDecoratorAnnotation(computedStructAnnotation)
67
+ assign(computed, computedAnnotation)
@@ -1,9 +1,7 @@
1
1
  import { globalState, isolateGlobalState, setReactionScheduler } from "../internal"
2
2
 
3
- const NEVER = "never"
4
3
  const ALWAYS = "always"
5
4
  const OBSERVED = "observed"
6
- // const IF_AVAILABLE = "ifavailable"
7
5
 
8
6
  export function configure(options: {
9
7
  enforceActions?: "never" | "always" | "observed"
@@ -20,23 +18,11 @@ export function configure(options: {
20
18
  disableErrorBoundaries?: boolean
21
19
  safeDescriptors?: boolean
22
20
  reactionScheduler?: (f: () => void) => void
23
- useProxies?: "always" | "never" | "ifavailable"
24
21
  }): void {
25
22
  if (options.isolateGlobalState === true) {
26
23
  isolateGlobalState()
27
24
  }
28
- const { useProxies, enforceActions } = options
29
- if (useProxies !== undefined) {
30
- globalState.useProxies =
31
- useProxies === ALWAYS
32
- ? true
33
- : useProxies === NEVER
34
- ? false
35
- : typeof Proxy !== "undefined"
36
- }
37
- if (useProxies === "ifavailable") {
38
- globalState.verifyProxies = true
39
- }
25
+ const { enforceActions } = options
40
26
  if (enforceActions !== undefined) {
41
27
  const ea = enforceActions === ALWAYS ? ALWAYS : enforceActions === OBSERVED
42
28
  globalState.enforceActions = ea
@@ -0,0 +1,20 @@
1
+ import { die } from "../errors"
2
+ import { assign } from "../utils/utils"
3
+ import type { Annotation } from "./annotation"
4
+
5
+ export type DecoratorAnnotation<Decorator extends (...args: any[]) => any> = Annotation & Decorator
6
+
7
+ export function createDecoratorAnnotation<Decorator extends (...args: any[]) => any>(
8
+ annotation: Annotation,
9
+ decorate: (annotation: Annotation, value: any, context: any) => any
10
+ ): DecoratorAnnotation<Decorator> {
11
+ return assign(function decoratorAnnotation(value, context) {
12
+ if (context && typeof context.kind === "string") {
13
+ return decorate(annotation, value, context)
14
+ }
15
+ if (__DEV__) {
16
+ die(`Invalid arguments for \`${annotation.annotationType_}\``)
17
+ }
18
+ return undefined
19
+ }, annotation) as any
20
+ }
@@ -1,88 +1,4 @@
1
- import { Annotation, addHiddenProp, AnnotationsMap, hasProp, die, isOverride } from "../internal"
2
-
3
- import type { Decorator } from "../types/decorator_fills"
4
-
5
- export const storedAnnotationsSymbol = Symbol("mobx-stored-annotations")
6
-
7
- /**
8
- * Creates a function that acts as
9
- * - decorator
10
- * - annotation object
11
- */
12
- export function createDecoratorAnnotation<D extends Decorator = Decorator>(
13
- annotation: Annotation
14
- ): PropertyDecorator & Annotation & D {
15
- function decorator(target, property) {
16
- if (is20223Decorator(property)) {
17
- return annotation.decorate_20223_(target, property)
18
- } else {
19
- storeAnnotation(target, property, annotation)
20
- }
21
- }
22
- return Object.assign(decorator, annotation) as any
23
- }
24
-
25
- /**
26
- * Stores annotation to prototype,
27
- * so it can be inspected later by `makeObservable` called from constructor
28
- */
29
- export function storeAnnotation(prototype: any, key: PropertyKey, annotation: Annotation) {
30
- if (!hasProp(prototype, storedAnnotationsSymbol)) {
31
- addHiddenProp(prototype, storedAnnotationsSymbol, {
32
- // Inherit annotations
33
- ...prototype[storedAnnotationsSymbol]
34
- })
35
- }
36
- // @override must override something
37
- if (__DEV__ && isOverride(annotation) && !hasProp(prototype[storedAnnotationsSymbol], key)) {
38
- const fieldName = `${prototype.constructor.name}.prototype.${key.toString()}`
39
- die(
40
- `'${fieldName}' is decorated with 'override', ` +
41
- `but no such decorated member was found on prototype.`
42
- )
43
- }
44
- // Cannot re-decorate
45
- assertNotDecorated(prototype, annotation, key)
46
-
47
- // Ignore override
48
- if (!isOverride(annotation)) {
49
- prototype[storedAnnotationsSymbol][key] = annotation
50
- }
51
- }
52
-
53
- function assertNotDecorated(prototype: object, annotation: Annotation, key: PropertyKey) {
54
- if (__DEV__ && !isOverride(annotation) && hasProp(prototype[storedAnnotationsSymbol], key)) {
55
- const fieldName = `${prototype.constructor.name}.prototype.${key.toString()}`
56
- const currentAnnotationType = prototype[storedAnnotationsSymbol][key].annotationType_
57
- const requestedAnnotationType = annotation.annotationType_
58
- die(
59
- `Cannot apply '@${requestedAnnotationType}' to '${fieldName}':` +
60
- `\nThe field is already decorated with '@${currentAnnotationType}'.` +
61
- `\nRe-decorating fields is not allowed.` +
62
- `\nUse '@override' decorator for methods overridden by subclass.`
63
- )
64
- }
65
- }
66
-
67
- /**
68
- * Collects annotations from prototypes and stores them on target (instance)
69
- */
70
- export function collectStoredAnnotations(target): AnnotationsMap<any, any> {
71
- if (!hasProp(target, storedAnnotationsSymbol)) {
72
- // if (__DEV__ && !target[storedAnnotationsSymbol]) {
73
- // die(
74
- // `No annotations were passed to makeObservable, but no decorated members have been found either`
75
- // )
76
- // }
77
- // We need a copy as we will remove annotation from the list once it's applied.
78
- addHiddenProp(target, storedAnnotationsSymbol, { ...target[storedAnnotationsSymbol] })
79
- }
80
- return target[storedAnnotationsSymbol]
81
- }
82
-
83
- export function is20223Decorator(context): context is DecoratorContext {
84
- return typeof context == "object" && typeof context["kind"] == "string"
85
- }
1
+ import { die } from "../internal"
86
2
 
87
3
  export function assert20223DecoratorType(
88
4
  context: DecoratorContext,
package/src/api/extras.ts CHANGED
@@ -33,7 +33,7 @@ function nodeToObserverTree(node: IDepTreeNode): IObserverTree {
33
33
  name: node.name_
34
34
  }
35
35
  if (hasObservers(node as any)) {
36
- result.observers = Array.from(<any>getObservers(node as any)).map(<any>nodeToObserverTree)
36
+ result.observers = Array.from(<any>getObservers(node as any), <any>nodeToObserverTree)
37
37
  }
38
38
  return result
39
39
  }