mobx 6.10.2 → 6.11.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 +9 -0
- package/README.md +2 -2
- package/dist/api/action.d.ts +16 -15
- package/dist/api/annotation.d.ts +19 -18
- package/dist/api/autorun.d.ts +25 -25
- package/dist/api/become-observed.d.ts +5 -5
- package/dist/api/computed.d.ts +14 -13
- package/dist/api/configure.d.ts +17 -17
- package/dist/api/decorators.d.ts +20 -17
- package/dist/api/extendobservable.d.ts +2 -2
- package/dist/api/extras.d.ts +10 -10
- package/dist/api/flow.d.ts +19 -18
- package/dist/api/intercept-read.d.ts +8 -8
- package/dist/api/intercept.d.ts +8 -8
- package/dist/api/iscomputed.d.ts +3 -3
- package/dist/api/isobservable.d.ts +2 -2
- package/dist/api/makeObservable.d.ts +6 -6
- package/dist/api/object-api.d.ts +36 -36
- package/dist/api/observable.d.ts +45 -44
- package/dist/api/observe.d.ts +8 -8
- package/dist/api/tojs.d.ts +7 -7
- package/dist/api/trace.d.ts +3 -3
- package/dist/api/transaction.d.ts +8 -8
- package/dist/api/when.d.ts +11 -11
- package/dist/core/action.d.ts +20 -20
- package/dist/core/atom.d.ts +37 -37
- package/dist/core/computedvalue.d.ts +99 -99
- package/dist/core/derivation.d.ts +73 -73
- package/dist/core/globalstate.d.ts +121 -121
- package/dist/core/observable.d.ts +45 -45
- package/dist/core/reaction.d.ts +62 -62
- package/dist/core/spy.d.ts +37 -37
- package/dist/errors.d.ts +42 -42
- package/dist/internal.d.ts +54 -54
- package/dist/mobx.cjs.development.js +419 -252
- package/dist/mobx.cjs.development.js.map +1 -1
- package/dist/mobx.cjs.production.min.js +1 -1
- package/dist/mobx.cjs.production.min.js.map +1 -1
- package/dist/mobx.d.ts +2 -2
- package/dist/mobx.esm.development.js +419 -252
- package/dist/mobx.esm.development.js.map +1 -1
- package/dist/mobx.esm.js +419 -252
- package/dist/mobx.esm.js.map +1 -1
- package/dist/mobx.esm.production.min.js +1 -1
- package/dist/mobx.esm.production.min.js.map +1 -1
- package/dist/mobx.umd.development.js +419 -252
- package/dist/mobx.umd.development.js.map +1 -1
- package/dist/mobx.umd.production.min.js +1 -1
- package/dist/mobx.umd.production.min.js.map +1 -1
- package/dist/types/actionannotation.d.ts +8 -8
- package/dist/types/autoannotation.d.ts +3 -3
- package/dist/types/computedannotation.d.ts +2 -2
- package/dist/types/decorator_fills.d.ts +6 -0
- package/dist/types/dynamicobject.d.ts +2 -2
- package/dist/types/flowannotation.d.ts +2 -2
- package/dist/types/generic-abort-signal.d.ts +6 -6
- package/dist/types/intercept-utils.d.ts +8 -8
- package/dist/types/legacyobservablearray.d.ts +14 -3
- package/dist/types/listen-utils.d.ts +7 -7
- package/dist/types/modifiers.d.ts +7 -7
- package/dist/types/observableannotation.d.ts +2 -2
- package/dist/types/observablearray.d.ts +85 -85
- package/dist/types/observablemap.d.ts +78 -78
- package/dist/types/observableobject.d.ts +98 -98
- package/dist/types/observableset.d.ts +53 -53
- package/dist/types/observablevalue.d.ts +49 -49
- package/dist/types/overrideannotation.d.ts +4 -3
- package/dist/types/type-utils.d.ts +11 -11
- package/dist/utils/comparer.d.ts +14 -14
- package/dist/utils/eq.d.ts +1 -1
- package/dist/utils/global.d.ts +1 -1
- package/dist/utils/iterable.d.ts +1 -1
- package/dist/utils/utils.d.ts +45 -45
- package/package.json +1 -1
- package/src/api/action.ts +21 -4
- package/src/api/annotation.ts +1 -0
- package/src/api/computed.ts +11 -4
- package/src/api/decorators.ts +33 -8
- package/src/api/flow.ts +10 -3
- package/src/api/observable.ts +20 -6
- package/src/types/actionannotation.ts +48 -2
- package/src/types/autoannotation.ts +8 -2
- package/src/types/computedannotation.ts +39 -2
- package/src/types/decorator_fills.ts +33 -0
- package/src/types/flowannotation.ts +28 -2
- package/src/types/legacyobservablearray.ts +1 -1
- package/src/types/observableannotation.ts +75 -2
- package/src/types/overrideannotation.ts +15 -7
package/src/api/computed.ts
CHANGED
|
@@ -10,19 +10,22 @@ import {
|
|
|
10
10
|
die,
|
|
11
11
|
IComputedValue,
|
|
12
12
|
createComputedAnnotation,
|
|
13
|
-
comparer
|
|
13
|
+
comparer,
|
|
14
|
+
is20223Decorator
|
|
14
15
|
} from "../internal"
|
|
15
16
|
|
|
17
|
+
import type { ClassGetterDecorator } from "../types/decorator_fills"
|
|
18
|
+
|
|
16
19
|
export const COMPUTED = "computed"
|
|
17
20
|
export const COMPUTED_STRUCT = "computed.struct"
|
|
18
21
|
|
|
19
|
-
export interface IComputedFactory extends Annotation, PropertyDecorator {
|
|
22
|
+
export interface IComputedFactory extends Annotation, PropertyDecorator, ClassGetterDecorator {
|
|
20
23
|
// @computed(opts)
|
|
21
|
-
<T>(options: IComputedValueOptions<T>): Annotation & PropertyDecorator
|
|
24
|
+
<T>(options: IComputedValueOptions<T>): Annotation & PropertyDecorator & ClassGetterDecorator
|
|
22
25
|
// computed(fn, opts)
|
|
23
26
|
<T>(func: () => T, options?: IComputedValueOptions<T>): IComputedValue<T>
|
|
24
27
|
|
|
25
|
-
struct: Annotation & PropertyDecorator
|
|
28
|
+
struct: Annotation & PropertyDecorator & ClassGetterDecorator
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
const computedAnnotation = createComputedAnnotation(COMPUTED)
|
|
@@ -35,6 +38,10 @@ const computedStructAnnotation = createComputedAnnotation(COMPUTED_STRUCT, {
|
|
|
35
38
|
* For legacy purposes also invokable as ES5 observable created: `computed(() => expr)`;
|
|
36
39
|
*/
|
|
37
40
|
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
|
+
}
|
|
38
45
|
if (isStringish(arg2)) {
|
|
39
46
|
// @computed
|
|
40
47
|
return storeAnnotation(arg1, arg2, computedAnnotation)
|
package/src/api/decorators.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { Annotation, addHiddenProp, AnnotationsMap, hasProp, die, isOverride } from "../internal"
|
|
2
2
|
|
|
3
|
+
import type { Decorator } from "../types/decorator_fills"
|
|
4
|
+
|
|
3
5
|
export const storedAnnotationsSymbol = Symbol("mobx-stored-annotations")
|
|
4
6
|
|
|
5
7
|
/**
|
|
@@ -7,11 +9,17 @@ export const storedAnnotationsSymbol = Symbol("mobx-stored-annotations")
|
|
|
7
9
|
* - decorator
|
|
8
10
|
* - annotation object
|
|
9
11
|
*/
|
|
10
|
-
export function createDecoratorAnnotation
|
|
12
|
+
export function createDecoratorAnnotation<D extends Decorator = Decorator>(
|
|
13
|
+
annotation: Annotation
|
|
14
|
+
): PropertyDecorator & Annotation & D {
|
|
11
15
|
function decorator(target, property) {
|
|
12
|
-
|
|
16
|
+
if (is20223Decorator(property)) {
|
|
17
|
+
return annotation.decorate_20223_(target, property)
|
|
18
|
+
} else {
|
|
19
|
+
storeAnnotation(target, property, annotation)
|
|
20
|
+
}
|
|
13
21
|
}
|
|
14
|
-
return Object.assign(decorator, annotation)
|
|
22
|
+
return Object.assign(decorator, annotation) as any
|
|
15
23
|
}
|
|
16
24
|
|
|
17
25
|
/**
|
|
@@ -61,13 +69,30 @@ function assertNotDecorated(prototype: object, annotation: Annotation, key: Prop
|
|
|
61
69
|
*/
|
|
62
70
|
export function collectStoredAnnotations(target): AnnotationsMap<any, any> {
|
|
63
71
|
if (!hasProp(target, storedAnnotationsSymbol)) {
|
|
64
|
-
if (__DEV__ && !target[storedAnnotationsSymbol]) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
72
|
+
// if (__DEV__ && !target[storedAnnotationsSymbol]) {
|
|
73
|
+
// die(
|
|
74
|
+
// `No annotations were passed to makeObservable, but no decorated members have been found either`
|
|
75
|
+
// )
|
|
76
|
+
// }
|
|
69
77
|
// We need a copy as we will remove annotation from the list once it's applied.
|
|
70
78
|
addHiddenProp(target, storedAnnotationsSymbol, { ...target[storedAnnotationsSymbol] })
|
|
71
79
|
}
|
|
72
80
|
return target[storedAnnotationsSymbol]
|
|
73
81
|
}
|
|
82
|
+
|
|
83
|
+
export function is20223Decorator(context): context is DecoratorContext {
|
|
84
|
+
return typeof context == "object" && typeof context["kind"] == "string"
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function assert20223DecoratorType(
|
|
88
|
+
context: DecoratorContext,
|
|
89
|
+
types: DecoratorContext["kind"][]
|
|
90
|
+
) {
|
|
91
|
+
if (__DEV__ && !types.includes(context.kind)) {
|
|
92
|
+
die(
|
|
93
|
+
`The decorator applied to '${String(context.name)}' cannot be used on a ${
|
|
94
|
+
context.kind
|
|
95
|
+
} element`
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
}
|
package/src/api/flow.ts
CHANGED
|
@@ -7,9 +7,12 @@ import {
|
|
|
7
7
|
isStringish,
|
|
8
8
|
storeAnnotation,
|
|
9
9
|
createFlowAnnotation,
|
|
10
|
-
createDecoratorAnnotation
|
|
10
|
+
createDecoratorAnnotation,
|
|
11
|
+
is20223Decorator
|
|
11
12
|
} from "../internal"
|
|
12
13
|
|
|
14
|
+
import type { ClassMethodDecorator } from "../types/decorator_fills"
|
|
15
|
+
|
|
13
16
|
export const FLOW = "flow"
|
|
14
17
|
|
|
15
18
|
let generatorId = 0
|
|
@@ -25,11 +28,11 @@ export function isFlowCancellationError(error: Error) {
|
|
|
25
28
|
|
|
26
29
|
export type CancellablePromise<T> = Promise<T> & { cancel(): void }
|
|
27
30
|
|
|
28
|
-
interface Flow extends Annotation, PropertyDecorator {
|
|
31
|
+
interface Flow extends Annotation, PropertyDecorator, ClassMethodDecorator {
|
|
29
32
|
<R, Args extends any[]>(
|
|
30
33
|
generator: (...args: Args) => Generator<any, R, any> | AsyncGenerator<any, R, any>
|
|
31
34
|
): (...args: Args) => CancellablePromise<R>
|
|
32
|
-
bound: Annotation & PropertyDecorator
|
|
35
|
+
bound: Annotation & PropertyDecorator & ClassMethodDecorator
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
const flowAnnotation = createFlowAnnotation("flow")
|
|
@@ -37,6 +40,10 @@ const flowBoundAnnotation = createFlowAnnotation("flow.bound", { bound: true })
|
|
|
37
40
|
|
|
38
41
|
export const flow: Flow = Object.assign(
|
|
39
42
|
function flow(arg1, arg2?) {
|
|
43
|
+
// @flow (2022.3 Decorators)
|
|
44
|
+
if (is20223Decorator(arg2)) {
|
|
45
|
+
return flowAnnotation.decorate_20223_(arg1, arg2)
|
|
46
|
+
}
|
|
40
47
|
// @flow
|
|
41
48
|
if (isStringish(arg2)) {
|
|
42
49
|
return storeAnnotation(arg1, arg2, flowAnnotation)
|
package/src/api/observable.ts
CHANGED
|
@@ -30,9 +30,12 @@ import {
|
|
|
30
30
|
isStringish,
|
|
31
31
|
createObservableAnnotation,
|
|
32
32
|
createAutoAnnotation,
|
|
33
|
+
is20223Decorator,
|
|
33
34
|
initObservable
|
|
34
35
|
} from "../internal"
|
|
35
36
|
|
|
37
|
+
import type { ClassAccessorDecorator, ClassFieldDecorator } from "../types/decorator_fills"
|
|
38
|
+
|
|
36
39
|
export const OBSERVABLE = "observable"
|
|
37
40
|
export const OBSERVABLE_REF = "observable.ref"
|
|
38
41
|
export const OBSERVABLE_SHALLOW = "observable.shallow"
|
|
@@ -71,7 +74,8 @@ const observableShallowAnnotation = createObservableAnnotation(OBSERVABLE_SHALLO
|
|
|
71
74
|
const observableStructAnnotation = createObservableAnnotation(OBSERVABLE_STRUCT, {
|
|
72
75
|
enhancer: refStructEnhancer
|
|
73
76
|
})
|
|
74
|
-
const observableDecoratorAnnotation =
|
|
77
|
+
const observableDecoratorAnnotation =
|
|
78
|
+
createDecoratorAnnotation<ClassAccessorDecorator>(observableAnnotation)
|
|
75
79
|
|
|
76
80
|
export function getEnhancerFromOptions(options: CreateObservableOptions): IEnhancer<any> {
|
|
77
81
|
return options.deep === true
|
|
@@ -96,6 +100,11 @@ export function getEnhancerFromAnnotation(annotation?: Annotation): IEnhancer<an
|
|
|
96
100
|
* @param v the value which should become observable.
|
|
97
101
|
*/
|
|
98
102
|
function createObservable(v: any, arg2?: any, arg3?: any) {
|
|
103
|
+
// @observable someProp; (2022.3 Decorators)
|
|
104
|
+
if (is20223Decorator(arg2)) {
|
|
105
|
+
return observableAnnotation.decorate_20223_(v, arg2)
|
|
106
|
+
}
|
|
107
|
+
|
|
99
108
|
// @observable someProp;
|
|
100
109
|
if (isStringish(arg2)) {
|
|
101
110
|
storeAnnotation(v, arg2, observableAnnotation)
|
|
@@ -142,7 +151,12 @@ export interface IObservableValueFactory {
|
|
|
142
151
|
<T>(value?: T, options?: CreateObservableOptions): IObservableValue<T | undefined>
|
|
143
152
|
}
|
|
144
153
|
|
|
145
|
-
export interface IObservableFactory
|
|
154
|
+
export interface IObservableFactory
|
|
155
|
+
extends Annotation,
|
|
156
|
+
PropertyDecorator,
|
|
157
|
+
ClassAccessorDecorator,
|
|
158
|
+
ClassFieldDecorator {
|
|
159
|
+
// TODO: remove ClassFieldDecorator, this is only temporarily support for legacy decorators
|
|
146
160
|
<T = any>(value: T[], options?: CreateObservableOptions): IObservableArray<T>
|
|
147
161
|
<T = any>(value: Set<T>, options?: CreateObservableOptions): ObservableSet<T>
|
|
148
162
|
<K = any, V = any>(value: Map<K, V>, options?: CreateObservableOptions): ObservableMap<K, V>
|
|
@@ -171,13 +185,13 @@ export interface IObservableFactory extends Annotation, PropertyDecorator {
|
|
|
171
185
|
/**
|
|
172
186
|
* Decorator that creates an observable that only observes the references, but doesn't try to turn the assigned value into an observable.ts.
|
|
173
187
|
*/
|
|
174
|
-
ref: Annotation & PropertyDecorator
|
|
188
|
+
ref: Annotation & PropertyDecorator & ClassAccessorDecorator & ClassFieldDecorator
|
|
175
189
|
/**
|
|
176
190
|
* Decorator that creates an observable converts its value (objects, maps or arrays) into a shallow observable structure
|
|
177
191
|
*/
|
|
178
|
-
shallow: Annotation & PropertyDecorator
|
|
179
|
-
deep: Annotation & PropertyDecorator
|
|
180
|
-
struct: Annotation & PropertyDecorator
|
|
192
|
+
shallow: Annotation & PropertyDecorator & ClassAccessorDecorator & ClassFieldDecorator
|
|
193
|
+
deep: Annotation & PropertyDecorator & ClassAccessorDecorator & ClassFieldDecorator
|
|
194
|
+
struct: Annotation & PropertyDecorator & ClassAccessorDecorator & ClassFieldDecorator
|
|
181
195
|
}
|
|
182
196
|
|
|
183
197
|
const observableFactories: IObservableFactory = {
|
|
@@ -7,7 +7,9 @@ import {
|
|
|
7
7
|
isFunction,
|
|
8
8
|
Annotation,
|
|
9
9
|
globalState,
|
|
10
|
-
MakeResult
|
|
10
|
+
MakeResult,
|
|
11
|
+
assert20223DecoratorType,
|
|
12
|
+
storeAnnotation
|
|
11
13
|
} from "../internal"
|
|
12
14
|
|
|
13
15
|
export function createActionAnnotation(name: string, options?: object): Annotation {
|
|
@@ -15,11 +17,13 @@ export function createActionAnnotation(name: string, options?: object): Annotati
|
|
|
15
17
|
annotationType_: name,
|
|
16
18
|
options_: options,
|
|
17
19
|
make_,
|
|
18
|
-
extend_
|
|
20
|
+
extend_,
|
|
21
|
+
decorate_20223_
|
|
19
22
|
}
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
function make_(
|
|
26
|
+
this: Annotation,
|
|
23
27
|
adm: ObservableObjectAdministration,
|
|
24
28
|
key: PropertyKey,
|
|
25
29
|
descriptor: PropertyDescriptor,
|
|
@@ -49,6 +53,7 @@ function make_(
|
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
function extend_(
|
|
56
|
+
this: Annotation,
|
|
52
57
|
adm: ObservableObjectAdministration,
|
|
53
58
|
key: PropertyKey,
|
|
54
59
|
descriptor: PropertyDescriptor,
|
|
@@ -58,6 +63,47 @@ function extend_(
|
|
|
58
63
|
return adm.defineProperty_(key, actionDescriptor, proxyTrap)
|
|
59
64
|
}
|
|
60
65
|
|
|
66
|
+
function decorate_20223_(this: Annotation, mthd, context: DecoratorContext) {
|
|
67
|
+
if (__DEV__) {
|
|
68
|
+
assert20223DecoratorType(context, ["method", "field"])
|
|
69
|
+
}
|
|
70
|
+
const { kind, name, addInitializer } = context
|
|
71
|
+
const ann = this
|
|
72
|
+
|
|
73
|
+
const _createAction = m =>
|
|
74
|
+
createAction(ann.options_?.name ?? name!.toString(), m, ann.options_?.autoAction ?? false)
|
|
75
|
+
|
|
76
|
+
// Backwards/Legacy behavior, expects makeObservable(this)
|
|
77
|
+
if (kind == "field") {
|
|
78
|
+
addInitializer(function () {
|
|
79
|
+
storeAnnotation(this, name, ann)
|
|
80
|
+
})
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (kind == "method") {
|
|
85
|
+
if (!isAction(mthd)) {
|
|
86
|
+
mthd = _createAction(mthd)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (this.options_?.bound) {
|
|
90
|
+
addInitializer(function () {
|
|
91
|
+
const self = this as any
|
|
92
|
+
const bound = self[name].bind(self)
|
|
93
|
+
bound.isMobxAction = true
|
|
94
|
+
self[name] = bound
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return mthd
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
die(
|
|
102
|
+
`Cannot apply '${ann.annotationType_}' to '${String(name)}' (kind: ${kind}):` +
|
|
103
|
+
`\n'${ann.annotationType_}' can only be used on properties with a function value.`
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
61
107
|
function assertActionDescriptor(
|
|
62
108
|
adm: ObservableObjectAdministration,
|
|
63
109
|
{ annotationType_ }: Annotation,
|
|
@@ -9,7 +9,8 @@ import {
|
|
|
9
9
|
computed,
|
|
10
10
|
autoAction,
|
|
11
11
|
isGenerator,
|
|
12
|
-
MakeResult
|
|
12
|
+
MakeResult,
|
|
13
|
+
die
|
|
13
14
|
} from "../internal"
|
|
14
15
|
|
|
15
16
|
const AUTO = "true"
|
|
@@ -21,7 +22,8 @@ export function createAutoAnnotation(options?: object): Annotation {
|
|
|
21
22
|
annotationType_: AUTO,
|
|
22
23
|
options_: options,
|
|
23
24
|
make_,
|
|
24
|
-
extend_
|
|
25
|
+
extend_,
|
|
26
|
+
decorate_20223_
|
|
25
27
|
}
|
|
26
28
|
}
|
|
27
29
|
|
|
@@ -105,3 +107,7 @@ function extend_(
|
|
|
105
107
|
let observableAnnotation = this.options_?.deep === false ? observable.ref : observable
|
|
106
108
|
return observableAnnotation.extend_(adm, key, descriptor, proxyTrap)
|
|
107
109
|
}
|
|
110
|
+
|
|
111
|
+
function decorate_20223_(this: Annotation, desc, context: ClassGetterDecoratorContext) {
|
|
112
|
+
die(`'${this.annotationType_}' cannot be used as a decorator`)
|
|
113
|
+
}
|
|
@@ -1,15 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ObservableObjectAdministration,
|
|
3
|
+
die,
|
|
4
|
+
Annotation,
|
|
5
|
+
MakeResult,
|
|
6
|
+
assert20223DecoratorType,
|
|
7
|
+
$mobx,
|
|
8
|
+
asObservableObject,
|
|
9
|
+
ComputedValue
|
|
10
|
+
} from "../internal"
|
|
2
11
|
|
|
3
12
|
export function createComputedAnnotation(name: string, options?: object): Annotation {
|
|
4
13
|
return {
|
|
5
14
|
annotationType_: name,
|
|
6
15
|
options_: options,
|
|
7
16
|
make_,
|
|
8
|
-
extend_
|
|
17
|
+
extend_,
|
|
18
|
+
decorate_20223_
|
|
9
19
|
}
|
|
10
20
|
}
|
|
11
21
|
|
|
12
22
|
function make_(
|
|
23
|
+
this: Annotation,
|
|
13
24
|
adm: ObservableObjectAdministration,
|
|
14
25
|
key: PropertyKey,
|
|
15
26
|
descriptor: PropertyDescriptor
|
|
@@ -18,6 +29,7 @@ function make_(
|
|
|
18
29
|
}
|
|
19
30
|
|
|
20
31
|
function extend_(
|
|
32
|
+
this: Annotation,
|
|
21
33
|
adm: ObservableObjectAdministration,
|
|
22
34
|
key: PropertyKey,
|
|
23
35
|
descriptor: PropertyDescriptor,
|
|
@@ -35,6 +47,31 @@ function extend_(
|
|
|
35
47
|
)
|
|
36
48
|
}
|
|
37
49
|
|
|
50
|
+
function decorate_20223_(this: Annotation, get, context: ClassGetterDecoratorContext) {
|
|
51
|
+
if (__DEV__) {
|
|
52
|
+
assert20223DecoratorType(context, ["getter"])
|
|
53
|
+
}
|
|
54
|
+
const ann = this
|
|
55
|
+
const { name: key, addInitializer } = context
|
|
56
|
+
|
|
57
|
+
addInitializer(function () {
|
|
58
|
+
const adm: ObservableObjectAdministration = asObservableObject(this)[$mobx]
|
|
59
|
+
const options = {
|
|
60
|
+
...ann.options_,
|
|
61
|
+
get,
|
|
62
|
+
context: this
|
|
63
|
+
}
|
|
64
|
+
options.name ||= __DEV__
|
|
65
|
+
? `${adm.name_}.${key.toString()}`
|
|
66
|
+
: `ObservableObject.${key.toString()}`
|
|
67
|
+
adm.values_.set(key, new ComputedValue(options))
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
return function () {
|
|
71
|
+
return this[$mobx].getObservablePropValue_(key)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
38
75
|
function assertComputedDescriptor(
|
|
39
76
|
adm: ObservableObjectAdministration,
|
|
40
77
|
{ annotationType_ }: Annotation,
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Hopefully these will be main-lined into Typescipt, but at the moment TS only declares the Contexts
|
|
2
|
+
|
|
3
|
+
export type ClassAccessorDecorator<This = any, Value = any> = (
|
|
4
|
+
value: ClassAccessorDecoratorTarget<This, Value>,
|
|
5
|
+
context: ClassAccessorDecoratorContext
|
|
6
|
+
) => ClassAccessorDecoratorResult<This, Value> | void
|
|
7
|
+
|
|
8
|
+
export type ClassGetterDecorator<This = any, Value = any> = (
|
|
9
|
+
value: (this: This) => Value,
|
|
10
|
+
context: ClassGetterDecoratorContext
|
|
11
|
+
) => ((this: This) => Value) | void
|
|
12
|
+
|
|
13
|
+
export type ClassSetterDecorator<This = any, Value = any> = (
|
|
14
|
+
value: (this: This, value: Value) => void,
|
|
15
|
+
context: ClassSetterDecoratorContext
|
|
16
|
+
) => ((this: This, value: Value) => void) | void
|
|
17
|
+
|
|
18
|
+
export type ClassMethodDecorator<This = any, Value extends (...p: any[]) => any = any> = (
|
|
19
|
+
value: Value,
|
|
20
|
+
context: ClassMethodDecoratorContext<This, Value>
|
|
21
|
+
) => Value | void
|
|
22
|
+
|
|
23
|
+
export type ClassFieldDecorator<This = any, Value extends (...p: any[]) => any = any> = (
|
|
24
|
+
value: Value,
|
|
25
|
+
context: ClassFieldDecoratorContext<This, Value>
|
|
26
|
+
) => Value | void
|
|
27
|
+
|
|
28
|
+
export type Decorator =
|
|
29
|
+
| ClassAccessorDecorator
|
|
30
|
+
| ClassGetterDecorator
|
|
31
|
+
| ClassSetterDecorator
|
|
32
|
+
| ClassMethodDecorator
|
|
33
|
+
| ClassFieldDecorator
|
|
@@ -8,7 +8,8 @@ import {
|
|
|
8
8
|
isFunction,
|
|
9
9
|
globalState,
|
|
10
10
|
MakeResult,
|
|
11
|
-
hasProp
|
|
11
|
+
hasProp,
|
|
12
|
+
assert20223DecoratorType
|
|
12
13
|
} from "../internal"
|
|
13
14
|
|
|
14
15
|
export function createFlowAnnotation(name: string, options?: object): Annotation {
|
|
@@ -16,11 +17,13 @@ export function createFlowAnnotation(name: string, options?: object): Annotation
|
|
|
16
17
|
annotationType_: name,
|
|
17
18
|
options_: options,
|
|
18
19
|
make_,
|
|
19
|
-
extend_
|
|
20
|
+
extend_,
|
|
21
|
+
decorate_20223_
|
|
20
22
|
}
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
function make_(
|
|
26
|
+
this: Annotation,
|
|
24
27
|
adm: ObservableObjectAdministration,
|
|
25
28
|
key: PropertyKey,
|
|
26
29
|
descriptor: PropertyDescriptor,
|
|
@@ -50,6 +53,7 @@ function make_(
|
|
|
50
53
|
}
|
|
51
54
|
|
|
52
55
|
function extend_(
|
|
56
|
+
this: Annotation,
|
|
53
57
|
adm: ObservableObjectAdministration,
|
|
54
58
|
key: PropertyKey,
|
|
55
59
|
descriptor: PropertyDescriptor,
|
|
@@ -59,6 +63,28 @@ function extend_(
|
|
|
59
63
|
return adm.defineProperty_(key, flowDescriptor, proxyTrap)
|
|
60
64
|
}
|
|
61
65
|
|
|
66
|
+
function decorate_20223_(this: Annotation, mthd, context: ClassMethodDecoratorContext) {
|
|
67
|
+
if (__DEV__) {
|
|
68
|
+
assert20223DecoratorType(context, ["method"])
|
|
69
|
+
}
|
|
70
|
+
const { name, addInitializer } = context
|
|
71
|
+
|
|
72
|
+
if (!isFlow(mthd)) {
|
|
73
|
+
mthd = flow(mthd)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (this.options_?.bound) {
|
|
77
|
+
addInitializer(function () {
|
|
78
|
+
const self = this as any
|
|
79
|
+
const bound = self[name].bind(self)
|
|
80
|
+
bound.isMobXFlow = true
|
|
81
|
+
self[name] = bound
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return mthd
|
|
86
|
+
}
|
|
87
|
+
|
|
62
88
|
function assertFlowDescriptor(
|
|
63
89
|
adm: ObservableObjectAdministration,
|
|
64
90
|
{ annotationType_ }: Annotation,
|
|
@@ -52,7 +52,7 @@ inherit(StubArray, Array.prototype)
|
|
|
52
52
|
// but it is unclear why the hack is need as MobX never changed the prototype
|
|
53
53
|
// anyway, so removed it in V6
|
|
54
54
|
|
|
55
|
-
class LegacyObservableArray<T> extends StubArray {
|
|
55
|
+
export class LegacyObservableArray<T> extends StubArray {
|
|
56
56
|
constructor(
|
|
57
57
|
initialValues: T[] | undefined,
|
|
58
58
|
enhancer: IEnhancer<T>,
|
|
@@ -3,7 +3,11 @@ import {
|
|
|
3
3
|
deepEnhancer,
|
|
4
4
|
die,
|
|
5
5
|
Annotation,
|
|
6
|
-
MakeResult
|
|
6
|
+
MakeResult,
|
|
7
|
+
assert20223DecoratorType,
|
|
8
|
+
ObservableValue,
|
|
9
|
+
asObservableObject,
|
|
10
|
+
$mobx
|
|
7
11
|
} from "../internal"
|
|
8
12
|
|
|
9
13
|
export function createObservableAnnotation(name: string, options?: object): Annotation {
|
|
@@ -11,11 +15,13 @@ export function createObservableAnnotation(name: string, options?: object): Anno
|
|
|
11
15
|
annotationType_: name,
|
|
12
16
|
options_: options,
|
|
13
17
|
make_,
|
|
14
|
-
extend_
|
|
18
|
+
extend_,
|
|
19
|
+
decorate_20223_
|
|
15
20
|
}
|
|
16
21
|
}
|
|
17
22
|
|
|
18
23
|
function make_(
|
|
24
|
+
this: Annotation,
|
|
19
25
|
adm: ObservableObjectAdministration,
|
|
20
26
|
key: PropertyKey,
|
|
21
27
|
descriptor: PropertyDescriptor
|
|
@@ -24,6 +30,7 @@ function make_(
|
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
function extend_(
|
|
33
|
+
this: Annotation,
|
|
27
34
|
adm: ObservableObjectAdministration,
|
|
28
35
|
key: PropertyKey,
|
|
29
36
|
descriptor: PropertyDescriptor,
|
|
@@ -38,6 +45,72 @@ function extend_(
|
|
|
38
45
|
)
|
|
39
46
|
}
|
|
40
47
|
|
|
48
|
+
function decorate_20223_(
|
|
49
|
+
this: Annotation,
|
|
50
|
+
desc,
|
|
51
|
+
context: ClassAccessorDecoratorContext | ClassFieldDecoratorContext
|
|
52
|
+
) {
|
|
53
|
+
if (__DEV__) {
|
|
54
|
+
if (context.kind === "field") {
|
|
55
|
+
throw die(
|
|
56
|
+
`Please use \`@observable accessor ${String(
|
|
57
|
+
context.name
|
|
58
|
+
)}\` instead of \`@observable ${String(context.name)}\``
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
assert20223DecoratorType(context, ["accessor"])
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const ann = this
|
|
65
|
+
const { kind, name } = context
|
|
66
|
+
|
|
67
|
+
// The laziness here is not ideal... It's a workaround to how 2022.3 Decorators are implemented:
|
|
68
|
+
// `addInitializer` callbacks are executed _before_ any accessors are defined (instead of the ideal-for-us right after each).
|
|
69
|
+
// This means that, if we were to do our stuff in an `addInitializer`, we'd attempt to read a private slot
|
|
70
|
+
// before it has been initialized. The runtime doesn't like that and throws a `Cannot read private member
|
|
71
|
+
// from an object whose class did not declare it` error.
|
|
72
|
+
// TODO: it seems that this will not be required anymore in the final version of the spec
|
|
73
|
+
// See TODO: link
|
|
74
|
+
const initializedObjects = new WeakSet()
|
|
75
|
+
|
|
76
|
+
function initializeObservable(target, value) {
|
|
77
|
+
const adm: ObservableObjectAdministration = asObservableObject(target)[$mobx]
|
|
78
|
+
const observable = new ObservableValue(
|
|
79
|
+
value,
|
|
80
|
+
ann.options_?.enhancer ?? deepEnhancer,
|
|
81
|
+
__DEV__ ? `${adm.name_}.${name.toString()}` : `ObservableObject.${name.toString()}`,
|
|
82
|
+
false
|
|
83
|
+
)
|
|
84
|
+
adm.values_.set(name, observable)
|
|
85
|
+
initializedObjects.add(target)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (kind == "accessor") {
|
|
89
|
+
return {
|
|
90
|
+
get() {
|
|
91
|
+
if (!initializedObjects.has(this)) {
|
|
92
|
+
initializeObservable(this, desc.get.call(this))
|
|
93
|
+
}
|
|
94
|
+
return this[$mobx].getObservablePropValue_(name)
|
|
95
|
+
},
|
|
96
|
+
set(value) {
|
|
97
|
+
if (!initializedObjects.has(this)) {
|
|
98
|
+
initializeObservable(this, value)
|
|
99
|
+
}
|
|
100
|
+
return this[$mobx].setObservablePropValue_(name, value)
|
|
101
|
+
},
|
|
102
|
+
init(value) {
|
|
103
|
+
if (!initializedObjects.has(this)) {
|
|
104
|
+
initializeObservable(this, value)
|
|
105
|
+
}
|
|
106
|
+
return value
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
|
|
41
114
|
function assertObservableDescriptor(
|
|
42
115
|
adm: ObservableObjectAdministration,
|
|
43
116
|
{ annotationType_ }: Annotation,
|
|
@@ -7,19 +7,23 @@ import {
|
|
|
7
7
|
MakeResult
|
|
8
8
|
} from "../internal"
|
|
9
9
|
|
|
10
|
+
import type { ClassMethodDecorator } from "./decorator_fills"
|
|
11
|
+
|
|
10
12
|
const OVERRIDE = "override"
|
|
11
13
|
|
|
12
|
-
export const override: Annotation & PropertyDecorator =
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
export const override: Annotation & PropertyDecorator & ClassMethodDecorator =
|
|
15
|
+
createDecoratorAnnotation({
|
|
16
|
+
annotationType_: OVERRIDE,
|
|
17
|
+
make_,
|
|
18
|
+
extend_,
|
|
19
|
+
decorate_20223_
|
|
20
|
+
})
|
|
17
21
|
|
|
18
22
|
export function isOverride(annotation: Annotation): boolean {
|
|
19
23
|
return annotation.annotationType_ === OVERRIDE
|
|
20
24
|
}
|
|
21
25
|
|
|
22
|
-
function make_(adm: ObservableObjectAdministration, key): MakeResult {
|
|
26
|
+
function make_(this: Annotation, adm: ObservableObjectAdministration, key): MakeResult {
|
|
23
27
|
// Must not be plain object
|
|
24
28
|
if (__DEV__ && adm.isPlainObject_) {
|
|
25
29
|
die(
|
|
@@ -37,6 +41,10 @@ function make_(adm: ObservableObjectAdministration, key): MakeResult {
|
|
|
37
41
|
return MakeResult.Cancel
|
|
38
42
|
}
|
|
39
43
|
|
|
40
|
-
function extend_(adm, key, descriptor, proxyTrap): boolean {
|
|
44
|
+
function extend_(this: Annotation, adm, key, descriptor, proxyTrap): boolean {
|
|
41
45
|
die(`'${this.annotationType_}' can only be used with 'makeObservable'`)
|
|
42
46
|
}
|
|
47
|
+
|
|
48
|
+
function decorate_20223_(this: Annotation, desc, context: DecoratorContext) {
|
|
49
|
+
console.warn(`'${this.annotationType_}' cannot be used with decorators - this is a no-op`)
|
|
50
|
+
}
|