-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathobservabledecorator.ts
95 lines (92 loc) · 3.19 KB
/
observabledecorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {
BabelDescriptor,
IEnhancer,
asObservableObject,
createPropDecorator,
fail,
invariant,
quacksLikeAStage2Decorator,
Stage2Decorator,
generateObservablePropConfig
} from "../internal"
export type IObservableDecorator = {
(target: Object, property: string | symbol, descriptor?: PropertyDescriptor): void
enhancer: IEnhancer<any>
}
export function createDecoratorForEnhancer(enhancer: IEnhancer<any>): IObservableDecorator {
invariant(enhancer)
const legacyDecorator = createPropDecorator(
true,
(
target: any,
propertyName: string,
descriptor: BabelDescriptor | undefined,
_decoratorTarget,
decoratorArgs: any[]
) => {
if (process.env.NODE_ENV !== "production") {
invariant(
!descriptor || !descriptor.get,
`@observable cannot be used on getter (property "${propertyName}"), use @computed instead.`
)
}
const initialValue = descriptor
? descriptor.initializer
? descriptor.initializer.call(target)
: descriptor.value
: undefined
asObservableObject(target).addObservableProp(propertyName, initialValue, enhancer)
}
)
const res: any = function observableDecorator() {
if (quacksLikeAStage2Decorator(arguments)) {
return stage2ObservableDecorator(enhancer, arguments[0])
}
if (process.env.NODE_ENV !== "production" && arguments.length < 2)
return fail(
"Incorrect decorator invocation. @observable decorator doesn't expect any arguments"
)
return legacyDecorator.apply(null, arguments)
}
res.enhancer = enhancer
return res
}
function stage2ObservableDecorator(
enhancer: IEnhancer<any>,
elementDescriptor: Stage2Decorator
): Stage2Decorator {
const { key, initializer } = elementDescriptor
return {
key,
kind: "method",
placement: "own",
initializer: undefined,
descriptor: generateObservablePropConfig(key),
extras: [
// introduce an additional property that is always undefined,
// just to be able to rn initialization code upon instance creation
// This property is basically an ugly hack
// To run some code upon initialization,
// see: https://github.com/tc39/proposal-decorators/issues/153
{
key: "__mobx-initializer-" + key,
kind: "field",
placement: "own",
descriptor: {
enumerable: false,
configurable: true,
writable: true
},
initializer() {
asObservableObject(this).initializeObservableProp(
key,
initializer && initializer.call(this),
enhancer
)
// rather, we would want to delete this property...
return undefined
}
}
]
}
}