-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
observable.ts
246 lines (222 loc) · 8.02 KB
/
observable.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import {
IEnhancer,
IEqualsComparer,
IObservableArray,
IObservableMapInitialValues,
IObservableSetInitialValues,
IObservableValue,
ObservableMap,
ObservableSet,
ObservableValue,
asDynamicObservableObject,
createObservableArray,
deepEnhancer,
extendObservable,
isES6Map,
isES6Set,
isObservable,
isPlainObject,
referenceEnhancer,
Annotation,
shallowEnhancer,
refStructEnhancer,
AnnotationsMap,
asObservableObject,
storeAnnotation,
createDecoratorAnnotation,
createLegacyArray,
globalState,
assign,
isStringish,
createObservableAnnotation,
createAutoAnnotation,
is20223Decorator,
initObservable
} from "../internal"
import type { ClassAccessorDecorator, ClassFieldDecorator } from "../types/decorator_fills"
export const OBSERVABLE = "observable"
export const OBSERVABLE_REF = "observable.ref"
export const OBSERVABLE_SHALLOW = "observable.shallow"
export const OBSERVABLE_STRUCT = "observable.struct"
export type CreateObservableOptions = {
name?: string
equals?: IEqualsComparer<any>
deep?: boolean
defaultDecorator?: Annotation
proxy?: boolean
autoBind?: boolean
}
// Predefined bags of create observable options, to avoid allocating temporarily option objects
// in the majority of cases
export const defaultCreateObservableOptions: CreateObservableOptions = {
deep: true,
name: undefined,
defaultDecorator: undefined,
proxy: true
}
Object.freeze(defaultCreateObservableOptions)
export function asCreateObservableOptions(thing: any): CreateObservableOptions {
return thing || defaultCreateObservableOptions
}
const observableAnnotation = createObservableAnnotation(OBSERVABLE)
const observableRefAnnotation = createObservableAnnotation(OBSERVABLE_REF, {
enhancer: referenceEnhancer
})
const observableShallowAnnotation = createObservableAnnotation(OBSERVABLE_SHALLOW, {
enhancer: shallowEnhancer
})
const observableStructAnnotation = createObservableAnnotation(OBSERVABLE_STRUCT, {
enhancer: refStructEnhancer
})
const observableDecoratorAnnotation =
createDecoratorAnnotation<ClassAccessorDecorator>(observableAnnotation)
export function getEnhancerFromOptions(options: CreateObservableOptions): IEnhancer<any> {
return options.deep === true
? deepEnhancer
: options.deep === false
? referenceEnhancer
: getEnhancerFromAnnotation(options.defaultDecorator)
}
export function getAnnotationFromOptions(
options?: CreateObservableOptions
): Annotation | undefined {
return options ? options.defaultDecorator ?? createAutoAnnotation(options) : undefined
}
export function getEnhancerFromAnnotation(annotation?: Annotation): IEnhancer<any> {
return !annotation ? deepEnhancer : annotation.options_?.enhancer ?? deepEnhancer
}
/**
* Turns an object, array or function into a reactive structure.
* @param v the value which should become observable.
*/
function createObservable(v: any, arg2?: any, arg3?: any) {
// @observable someProp; (2022.3 Decorators)
if (is20223Decorator(arg2)) {
return observableAnnotation.decorate_20223_(v, arg2)
}
// @observable someProp;
if (isStringish(arg2)) {
storeAnnotation(v, arg2, observableAnnotation)
return
}
// already observable - ignore
if (isObservable(v)) {
return v
}
// plain object
if (isPlainObject(v)) {
return observable.object(v, arg2, arg3)
}
// Array
if (Array.isArray(v)) {
return observable.array(v, arg2)
}
// Map
if (isES6Map(v)) {
return observable.map(v, arg2)
}
// Set
if (isES6Set(v)) {
return observable.set(v, arg2)
}
// other object - ignore
if (typeof v === "object" && v !== null) {
return v
}
// anything else
return observable.box(v, arg2)
}
assign(createObservable, observableDecoratorAnnotation)
export interface IObservableValueFactory {
<T>(value: T, options?: CreateObservableOptions): IObservableValue<T>
<T>(value?: T, options?: CreateObservableOptions): IObservableValue<T | undefined>
}
export interface IObservableFactory
extends Annotation,
PropertyDecorator,
ClassAccessorDecorator,
ClassFieldDecorator {
// TODO: remove ClassFieldDecorator, this is only temporarily support for legacy decorators
<T = any>(value: T[], options?: CreateObservableOptions): IObservableArray<T>
<T = any>(value: Set<T>, options?: CreateObservableOptions): ObservableSet<T>
<K = any, V = any>(value: Map<K, V>, options?: CreateObservableOptions): ObservableMap<K, V>
<T extends object>(
value: T,
decorators?: AnnotationsMap<T, never>,
options?: CreateObservableOptions
): T
box: IObservableValueFactory
array: <T = any>(initialValues?: T[], options?: CreateObservableOptions) => IObservableArray<T>
set: <T = any>(
initialValues?: IObservableSetInitialValues<T>,
options?: CreateObservableOptions
) => ObservableSet<T>
map: <K = any, V = any>(
initialValues?: IObservableMapInitialValues<K, V>,
options?: CreateObservableOptions
) => ObservableMap<K, V>
object: <T = any>(
props: T,
decorators?: AnnotationsMap<T, never>,
options?: CreateObservableOptions
) => T
/**
* Decorator that creates an observable that only observes the references, but doesn't try to turn the assigned value into an observable.ts.
*/
ref: Annotation & PropertyDecorator & ClassAccessorDecorator & ClassFieldDecorator
/**
* Decorator that creates an observable converts its value (objects, maps or arrays) into a shallow observable structure
*/
shallow: Annotation & PropertyDecorator & ClassAccessorDecorator & ClassFieldDecorator
deep: Annotation & PropertyDecorator & ClassAccessorDecorator & ClassFieldDecorator
struct: Annotation & PropertyDecorator & ClassAccessorDecorator & ClassFieldDecorator
}
const observableFactories: IObservableFactory = {
box<T = any>(value: T, options?: CreateObservableOptions): IObservableValue<T> {
const o = asCreateObservableOptions(options)
return new ObservableValue(value, getEnhancerFromOptions(o), o.name, true, o.equals)
},
array<T = any>(initialValues?: T[], options?: CreateObservableOptions): IObservableArray<T> {
const o = asCreateObservableOptions(options)
return (
globalState.useProxies === false || o.proxy === false
? createLegacyArray
: createObservableArray
)(initialValues, getEnhancerFromOptions(o), o.name)
},
map<K = any, V = any>(
initialValues?: IObservableMapInitialValues<K, V>,
options?: CreateObservableOptions
): ObservableMap<K, V> {
const o = asCreateObservableOptions(options)
return new ObservableMap<K, V>(initialValues, getEnhancerFromOptions(o), o.name)
},
set<T = any>(
initialValues?: IObservableSetInitialValues<T>,
options?: CreateObservableOptions
): ObservableSet<T> {
const o = asCreateObservableOptions(options)
return new ObservableSet<T>(initialValues, getEnhancerFromOptions(o), o.name)
},
object<T extends object = any>(
props: T,
decorators?: AnnotationsMap<T, never>,
options?: CreateObservableOptions
): T {
return initObservable(() =>
extendObservable(
globalState.useProxies === false || options?.proxy === false
? asObservableObject({}, options)
: asDynamicObservableObject({}, options),
props,
decorators
)
)
},
ref: createDecoratorAnnotation(observableRefAnnotation),
shallow: createDecoratorAnnotation(observableShallowAnnotation),
deep: observableDecoratorAnnotation,
struct: createDecoratorAnnotation(observableStructAnnotation)
} as any
// eslint-disable-next-line
export var observable: IObservableFactory = assign(createObservable, observableFactories)