forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobx.js
500 lines (420 loc) · 14.6 KB
/
mobx.js
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// @flow
export type IObservableMapInitialValues<K, V> = IMapEntries<K, V> | KeyValueMap<V> | IMap<K, V>
export interface IMobxConfigurationOptions {
+enforceActions?: boolean | "strict",
computedRequiresReaction?: boolean,
isolateGlobalState?: boolean,
disableErrorBoundaries?: boolean,
arrayBuffer?: number,
reactionScheduler?: (f: () => void) => void
}
declare export function configure(options: IMobxConfigurationOptions): void
export interface IAutorunOptions {
delay?: number,
name?: string,
scheduler?: (callback: () => void) => any,
onError?: (error: any) => void
}
export interface IReactionOptions extends IAutorunOptions {
fireImmediately?: boolean,
equals?: IEqualsComparer<any>
}
export interface IInterceptable<T> {
interceptors: IInterceptor<T>[] | any,
intercept(handler: IInterceptor<T>): Lambda
}
export type IEqualsComparer<T> = (a: T, b: T) => boolean
export type IInterceptor<T> = (change: T) => T
export type IMapEntry<K, V> = [K, V]
export type IMapEntries<K, V> = IMapEntry<K, V>[]
export interface IMap<K, V> {
clear(): void,
delete(key: K): boolean,
forEach(callbackfn: (value: V, index: K, map: IMap<K, V>) => void, thisArg?: any): void,
get(key: K): V | any,
has(key: K): boolean,
set(key: K, value?: V): any,
size: number
}
declare export function isObservableMap(x: any): boolean
export interface IComputedValueOptions<T> {
get?: () => T,
set?: (value: T) => void,
name?: string,
equals?: IEqualsComparer<T>,
context?: any
}
declare type PropertyDescriptor = any
export interface IComputed {
<T>(func: () => T, setter?: (value: T) => void): IComputedValue<T>,
<T>(func: () => T, options: IComputedValueOptions<T>): IComputedValue<T>,
(target: Object, key: string, baseDescriptor?: PropertyDescriptor): void,
struct(target: Object, key: string, baseDescriptor?: PropertyDescriptor): void
}
export interface IDependencyTree {
name: string,
dependencies?: IDependencyTree[]
}
export interface IObserverTree {
name: string,
observers?: IObserverTree[]
}
export interface IAtom {
reportObserved: () => void,
reportChanged: () => void
}
export interface IComputedValue<T> {
get(): T,
set(value: T): void,
observe(listener: (newValue: T, oldValue: T) => void, fireImmediately?: boolean): Lambda
}
export interface IObservable {}
export interface IDepTreeNode {
name: string,
observing?: IObservable[]
}
export interface IDerivation {
name: string
}
export interface IReactionPublic {
dispose: () => void,
trace: (enterBreakPoint?: boolean) => void
}
declare export class IListenable {
observe(handler: (change: any, oldValue?: any) => void, fireImmediately?: boolean): Lambda
}
export interface IObservableArray<T> extends Array<T> {
spliceWithArray(index: number, deleteCount?: number, newItems?: T[]): T[],
observe(
listener: (changeData: IArrayChange<T> | IArraySplice<T>) => void,
fireImmediately?: boolean
): Lambda,
intercept(handler: IInterceptor<IArrayWillChange<T> | IArrayWillSplice<T>>): Lambda,
intercept(handler: IInterceptor<IArrayChange<T> | IArraySplice<T>>): Lambda,
intercept<T>(handler: IInterceptor<IArrayChange<T> | IArraySplice<T>>): Lambda,
clear(): T[],
replace(newItems: T[]): T[],
find(
predicate: (item: T, index: number, array: Array<T>) => boolean,
thisArg?: any,
fromIndex?: number
): T | any,
findIndex(
predicate: (item: T, index: number, array: Array<T>) => boolean,
thisArg?: any,
fromIndex?: number
): number,
remove(value: T): boolean
}
export interface IArrayChange<T> {
type: "update",
object: IObservableArray<T>,
index: number,
newValue: T,
oldValue: T
}
export interface IArraySplice<T> {
type: "splice",
object: IObservableArray<T>,
index: number,
added: T[],
addedCount: number,
removed: T[],
removedCount: number
}
export interface IArrayWillChange<T> {
type: "update",
object: IObservableArray<T>,
index: number,
newValue: T
}
export interface IArrayWillSplice<T> {
type: "splice",
object: IObservableArray<T>,
index: number,
added: T[],
removedCount: number
}
export type KeyValueMap<V> = {
[key: string]: V
}
export interface IMapChange<K, T> {
object: ObservableMap<K, T>,
type: "update" | "add" | "delete",
name: K,
newValue?: any,
oldValue?: any
}
export interface IMapWillChange<K, T> {
object: ObservableMap<K, T>,
type: "update" | "add" | "delete",
name: K,
newValue?: any
}
export interface IObservableObject {}
export interface IObjectChange {
name: string,
object: any,
type: "update" | "add" | "remove",
oldValue?: any,
newValue: any
}
export interface IObjectWillChange {
object: any,
type: "update" | "add" | "remove",
name: string,
newValue: any
}
export interface IValueWillChange<T> {
object: any,
type: "update",
newValue: T
}
export interface IValueDidChange<T> extends IValueWillChange<T> {
oldValue: ?T
}
export interface IObservableValue<T> {
get(): T,
set(value: T): void,
intercept(handler: IInterceptor<IValueWillChange<T>>): Lambda,
observe(listener: (change: IValueDidChange<T>) => void, fireImmediately?: boolean): Lambda
}
export interface IEnhancer<T> {
(newValue: T, oldValue: T | void, name: string): T
}
export interface IObservableFactory {
// observable overloads
(target: Object, key: string, baseDescriptor?: PropertyDescriptor): any,
<T>(value: Array<T>): IObservableArray<T>,
<T>(value: null | void): IObservableValue<T>,
(value: null | void): IObservableValue<any>,
<T>(value: IMap<string | number | boolean, T>): ObservableMap<T>,
<T: Object>(value: T): T
}
export type IObservableDecorator = {
(target: Object, property: string, descriptor?: PropertyDescriptor): void,
enhancer: IEnhancer<any>
}
export type CreateObservableOptions = {
name?: string,
deep?: boolean,
defaultDecorator?: IObservableDecorator
}
declare export class IObservableFactories {
box<T>(value?: T, options?: CreateObservableOptions): IObservableValue<T>,
array<T>(initialValues?: T[], options?: CreateObservableOptions): IObservableArray<T>,
map<K, V>(
initialValues?: IObservableMapInitialValues<K, V>,
options?: CreateObservableOptions
): ObservableMap<K, V>,
object<T>(props: T, options?: CreateObservableOptions): T & IObservableObject,
ref(target: Object, property?: string, descriptor?: PropertyDescriptor): IObservableDecorator,
shallow(
target: Object,
property?: string,
descriptor?: PropertyDescriptor
): IObservableDecorator,
deep(target: Object, property?: string, descriptor?: PropertyDescriptor): IObservableDecorator
}
export interface Iterator<T> {
next(): {
done: boolean,
value?: T
}
}
export interface Lambda {
(): void,
name?: string
}
export interface IActionFactory {
(a1: any, a2?: any, a3?: any, a4?: any, a6?: any): any,
bound(target: Object, propertyKey: string, descriptor?: PropertyDescriptor): void
}
declare export class ObservableMap<K, V> {
constructor(initialData?: IMapEntries<K, V> | KeyValueMap<V>, valueModeFunc?: Function): this,
has(key: K): boolean,
set(key: K, value: V): void,
delete(key: K): boolean,
get(key: K): V,
keys(): Iterator<K>,
values(): Iterator<V>,
entries(): IMapEntries<K, V> & Iterator<IMapEntry<K, V>>,
forEach(callback: (value: V, key: K, object: KeyValueMap<K, V>) => void, thisArg?: any): void,
merge(other: ObservableMap<K, V> | KeyValueMap<K, V>): ObservableMap<K, V>,
clear(): void,
replace(other: ObservableMap<K, V> | KeyValueMap<K, V>): ObservableMap<K, V>,
size: number,
toJS(): Map<K, V>,
toPOJO(): KeyValueMap<V>,
toJSON(): KeyValueMap<V>,
toString(): string,
observe(listener: (changes: IMapChange<K, V>) => void, fireImmediately?: boolean): Lambda,
intercept(handler: IInterceptor<IMapWillChange<K, V>>): Lambda
}
declare export function action(
targetOrName: any,
propertyKeyOrFuc?: any,
descriptor?: PropertyDescriptor
): any
declare export function action<T>(name: string, func: T): T
declare export function action<T>(func: T): T
declare export function runInAction<T>(name: string, block: () => T): T
declare export function runInAction<T>(block: () => T): T
declare export function isAction(thing: any): boolean
declare export function autorun(
nameOrFunction: string | ((r: IReactionPublic) => any),
options?: IAutorunOptions
): any
declare export function reaction<T>(
expression: (r: IReactionPublic) => T,
effect: (arg: T, r: IReactionPublic) => void,
opts?: IReactionOptions
): any
export interface IWhenOptions {
name?: string,
timeout?: number,
onError?: (error: any) => void
}
declare export function when(cond: () => boolean, effect: Lambda, options?: IWhenOptions): any
declare export function when(cond: () => boolean, options?: IWhenOptions): Promise<any>
declare export function computed<T>(
target: any,
key?: string,
baseDescriptor?: PropertyDescriptor
): any
declare export function extendObservable<A, B>(
target: A,
properties: B,
decorators?: any,
options?: any
): A & B
declare export function intercept(
object: Object,
property: string,
handler: IInterceptor<any>
): Lambda
declare export function isComputed(value: any): boolean
declare export function isComputedProp(value: any, property: string): boolean
declare export function isObservable(value: any): boolean
declare export function isObservableProp(value: any, property: string): boolean
declare export var observable: IObservableFactory &
IObservableFactories & {
deep: {
struct<T>(initialValue?: T): T
},
ref: {
struct<T>(initialValue?: T): T
}
}
declare export function observe<T>(
value: IObservableValue<T> | IComputedValue<T>,
listener: (change: IValueDidChange<T>) => void,
fireImmediately?: boolean
): Lambda
declare export function observe<T>(
observableArray: IObservableArray<T>,
listener: (change: IArrayChange<T> | IArraySplice<T>) => void,
fireImmediately?: boolean
): Lambda
declare export function observe<K, T>(
observableMap: ObservableMap<K, T>,
listener: (change: IMapChange<K, T>) => void,
fireImmediately?: boolean
): Lambda
declare export function observe<K, T>(
observableMap: ObservableMap<K, T>,
property: string,
listener: (change: IValueDidChange<K, T>) => void,
fireImmediately?: boolean
): Lambda
declare export function observe(
object: any,
listener: (change: IObjectChange) => void,
fireImmediately?: boolean
): Lambda
declare export function observe(
object: any,
property: string,
listener: (change: IValueDidChange<any>) => void,
fireImmediately?: boolean
): Lambda
export interface ToJSOptions {
detectCycles?: boolean,
exportMapsAsObjects?: boolean
}
declare export function toJS<T>(source: T, options?: ToJSOptions): T
declare export function untracked<T>(action: () => T): T
declare export function spy(listener: (change: any) => void): Lambda
declare export function transaction<T>(action: () => T, thisArg?: any, report?: boolean): T
declare export function isObservableArray(thing: any): boolean
declare export function isObservableObject<T>(thing: T): boolean
declare export function isArrayLike(x: any): boolean
declare export class Reaction {
name: string,
isDisposed: boolean,
constructor(name: string, onInvalidate: () => void): this,
schedule(): void,
isScheduled(): boolean,
track(fn: () => void): void,
dispose(): void,
getDisposer(): Lambda & {
$mosbservable: Reaction
},
toString(): string,
trace(enterBreakPoint?: boolean): void
}
declare export function createAtom(
name: string,
onBecomeObservedHandler?: () => void,
onBecomeUnobservedHandler?: () => void
): IAtom
declare export function decorate<T>(target: T, decorators: any): T
declare export function flow<T>(fn: (...args: any[]) => T): (...args: any[]) => Promise<T>
declare export function flow<T>(
name: string,
fn: (...args: any[]) => T
): (...args: any[]) => Promise<T>
declare export function keys<K>(map: ObservableMap<K, any>): K[]
declare export function keys(obj: any): string[]
declare export function values<K, T>(map: ObservableMap<K, T>): T[]
declare export function values<T>(ar: IObservableArray<T>): T[]
declare export function values(obj: any): any[]
declare export function set<V>(obj: ObservableMap<string, V>, values: { [key: string]: V }): void
declare export function set<K, V>(obj: ObservableMap<K, V>, key: K, value: V): void
declare export function set<T>(obj: IObservableArray<T>, index: number, value: T): void
declare export function set(obj: any, values: { [key: string]: any }): void
declare export function set(obj: any, key: string, value: any): void
declare export function remove<K, V>(obj: ObservableMap<K, V>, key: K): void
declare export function remove<T>(obj: IObservableArray<T>, index: number): void
declare export function remove(obj: any, key: string): void
declare export function has<K>(obj: ObservableMap<K, any>, key: K): boolean
declare export function has<T>(obj: IObservableArray<T>, index: number): boolean
declare export function has(obj: any, key: string): boolean
declare export function get<K, V>(obj: ObservableMap<K, V>, key: K): V | void
declare export function get<T>(obj: IObservableArray<T>, index: number): T | void
declare export function get(obj: any, key: string): any
declare export function onReactionError(
handler: (error: any, derivation: IDerivation) => void
): () => void
declare export function onBecomeObserved(
value: IObservable | IComputedValue<any> | IObservableArray<any> | ObservableMap<any, any>,
listener: Lambda
): Lambda
declare export function onBecomeObserved<K>(
value: ObservableMap<K, any> | Object,
property: K,
listener: Lambda
): Lambda
declare export function onBecomeUnobserved(
value: IObservable | IComputedValue<any> | IObservableArray<any> | ObservableMap<any, any>,
listener: Lambda
): Lambda
declare export function onBecomeUnobserved<K>(
value: ObservableMap<K, any> | Object,
property: K,
listener: Lambda
): Lambda
declare export function getAtom(thing: any, property?: string): IDepTreeNode
declare export function getDebugName(thing: any, property?: string): string
declare export function getDependencyTree(thing: any, property?: string): IDependencyTree
declare export function getObserverTree(thing: any, property?: string): IObserverTree