-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
mobx.js
415 lines (352 loc) · 12.5 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
declare module 'mobx' {
declare type extras = {
allowStateChanges: <T>(allowStateChanges: boolean, func: () => T) => T,
getAtom: (thing: any, property?: string) => IDepTreeNode,
getDebugName: (thing: any, property?: string) => string,
getDependencyTree: (thing: any, property?: string) => IDependencyTree,
getGlobalState: () => any;
getObserverTree: (thing: any, property?: string) => IObserverTree,
isComputingDerivation: () => boolean,
isSpyEnabled: () => boolean,
resetGlobalState: () => void,
shareGlobalState: () => void;
spyReport: (event: any) => boolean,
spyReportEnd: (change?: any) => void,
spyReportStart: (event: any) => void,
setReactionScheduler: (fn: (f: () => void) => void) => void
}
declare type IObservableMapInitialValues<V> = IMapEntries<V> | IKeyValueMap<V> | IMap<string, V>;
declare interface IReactionOptions {
context?: any;
fireImmediately?: boolean;
delay?: number;
compareStructural?: boolean;
name?: string;
}
declare interface IInterceptable<T> {
interceptors: IInterceptor<T>[] | any;
intercept(handler: IInterceptor<T>): Lambda;
}
declare type _ = {
getAdministration: (thing: any, property?: string) => any;
resetGlobalState: () => void;
};
declare type ITransformer<A, B> = (object: A) => B;
declare type IInterceptor<T> = (change: T) => T;
declare type IMapEntry<V> = [string, V];
declare type IMapEntries<V> = IMapEntry<V>[];
declare 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 type isObservableMap = (x: any) => boolean;
declare type ISimpleEventListener = {
(): void;
};
declare interface IComputedValueOptions<T> {
compareStructural?: boolean;
name?: string;
setter?: (value: T) => void;
context?: any;
}
declare type IDerivationState = 'NOT_TRACKING' | 'UP_TO_DATE' | 'POSSIBLY_STALE' | 'STALE'
declare type PropertyDescriptor = any
declare 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;
}
declare interface IDependencyTree {
name: string;
dependencies?: IDependencyTree[];
}
declare interface IObserverTree {
name: string;
observers?: IObserverTree[];
}
declare interface IAtom {
}
declare interface IComputedValue<T> {
get(): T;
set(value: T): void;
observe(
listener: (newValue: T, oldValue: T) => void, fireImmediately?: boolean
): Lambda;
}
declare interface IObservable {
diffValue: number;
lastAccessedBy: number;
lowestObserverState: IDerivationState;
isPendingUnobservation: boolean;
observers: IDerivation[];
observersIndexes: {};
onBecomeUnobserved(): any;
}
declare interface IDepTreeNode {
name: string;
observing?: IObservable[];
}
declare interface IDerivation {
name: string;
observing: IObservable[];
newObserving: ?IObservable[];
dependenciesState: IDerivationState;
runId: number;
unboundDepsCount: number;
___mapid: string;
onBecomeStale(): any;
recoverFromError(): any;
}
declare interface IReactionPublic {
dispose: () => void;
}
declare interface IListenable {
changeListeners: any;
observe(
handler: (change: any, oldValue?: any) => void, fireImmediately?: boolean
): Lambda;
}
declare 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<T>(handler: IInterceptor<IArrayChange<T> | IArraySplice<T>>): Lambda;
clear(): T[];
peek(): T[];
replace(newItems: T[]): T[];
find(
predicate: (item: T, index: number, array: Array<T>) => boolean, thisArg?: any, fromIndex?: number
): T | any;
remove(value: T): boolean;
}
declare interface IArrayChange<T> {
type: 'update';
object: IObservableArray<T>;
index: number;
newValue: T;
oldValue: T;
}
declare interface IArraySplice<T> {
type: 'splice';
object: IObservableArray<T>;
index: number;
added: T[];
addedCount: number;
removed: T[];
removedCount: number;
}
declare interface IArrayWillChange<T> {
type: 'update';
object: IObservableArray<T>;
index: number;
newValue: T;
}
declare interface IArrayWillSplice<T> {
type: 'splice';
object: IObservableArray<T>;
index: number;
added: T[];
removedCount: number;
}
declare interface IKeyValueMap<V> {
[key: string]: V;
}
declare interface IMapChange<T> {
object: ObservableMap<T>;
type: 'update' | 'add' | 'delete';
name: string;
newValue?: any;
oldValue?: any;
}
declare interface IMapWillChange<T> {
object: ObservableMap<T>;
type: 'update' | 'add' | 'delete';
name: string;
newValue?: any;
}
declare interface IObservableObject {}
declare interface IObjectChange {
name: string;
object: any;
type: 'update' | 'add';
oldValue?: any;
newValue: any;
}
declare interface IObjectWillChange {
object: any;
type: 'update' | 'add';
name: string;
newValue: any;
}
declare interface IValueWillChange<T> {
object: any;
type: 'update';
newValue: T;
}
declare interface IObservableValue<T> {
get(): T;
set(value: T): void;
intercept(handler: IInterceptor<IValueWillChange<T>>): Lambda;
observe(
listener: (newValue: T, oldValue: T) => void, fireImmediately?: boolean
): Lambda;
}
declare interface IObservableFactory {
(value: any, key?: string, baseDescriptor?: PropertyDescriptor): any;
}
declare class IObservableFactories {
box<T>(value?: T, name?: string): IObservableValue<T>;
shallowBox<T>(value?: T, name?: string): IObservableValue<T>;
array<T>(initialValues?: T[], name?: string): IObservableArray<T>;
shallowArray<T>(initialValues?: T[], name?: string): IObservableArray<T>;
map<T>(initialValues?: IObservableMapInitialValues<T>, name?: string): ObservableMap<T>;
shallowMap<T>(initialValues?: IObservableMapInitialValues<T>, name?: string): ObservableMap<T>;
object<T>(props: T, name?: string): T & IObservableObject;
shallowObject<T>(props: T, name?: string): T & IObservableObject;
ref(target: Object, property?: string, descriptor?: PropertyDescriptor): any;
shallow(target: Object, property?: string, descriptor?: PropertyDescriptor): any;
deep(target: Object, property?: string, descriptor?: PropertyDescriptor): any;
}
declare interface Iterator<T> {
next(): {
done: boolean,
value?: T
};
}
declare interface Lambda {
(): void;
name?: string;
}
declare interface IActionFactory {
(a1: any, a2?: any, a3?: any, a4?: any, a6?: any): any;
bound(target: Object, propertyKey: string, descriptor?: PropertyDescriptor): void;
}
declare class ObservableMap<V> {
$mobx: {};
name: string;
interceptors: any;
changeListeners: any;
constructor(initialData?: IMapEntries<V> | IKeyValueMap<V>, valueModeFunc?: Function): this;
has(key: string): boolean;
set(key: string, value: V): void;
delete(key: string): boolean;
get(key: string): V;
keys(): string[] & Iterator<string>;
values(): V[] & Iterator<V>;
entries(): IMapEntries<V> & Iterator<IMapEntry<V>>;
forEach(
callback: (value: V, key: string, object: IKeyValueMap<V>) => void, thisArg?: any
): void;
merge(other: ObservableMap<V> | IKeyValueMap<V>): ObservableMap<V>;
clear(): void;
size: number;
toJS(): IKeyValueMap<V>;
toJs(): IKeyValueMap<V>;
toJSON(): IKeyValueMap<V>;
toString(): string;
observe(listener: (changes: IMapChange<V>) => void, fireImmediately?: boolean): Lambda;
intercept(handler: IInterceptor<IMapWillChange<V>>): Lambda
}
declare function extendShallowObservable(target: any): any;
declare function action(targetOrName: any, propertyKeyOrFuc?: any, descriptor?: PropertyDescriptor): any;
declare function action<T>(name: string, func: T): T;
declare function action<T>(func: T): T;
declare function runInAction<T>(name: string, block: () => T, scope?: any): T;
declare function runInAction<T>(block: () => T, scope?: any): T;
declare function isAction(thing: any): boolean;
declare function autorun(nameOrFunction: string | (r: IReactionPublic) => void, viewOrScope?: any, scope?: any): any;
declare function when(predicate: () => boolean, effect: Lambda, scope?: any): any
declare function autorunAsync(func: (r: IReactionPublic) => void, delay?: number, scope?: any): any
declare function reaction<T>(
expression: (r: IReactionPublic) => T, effect: (arg: T, r: IReactionPublic) => void, fireImmediately?: boolean, delay?: number, scope?: any
): any
declare function computed<T>(target: any, key?: string, baseDescriptor?: PropertyDescriptor): any
declare function createTransformer<A, B>(
transformer: ITransformer<A, B>, onCleanup?: (resultObject: ?B | any, sourceObject?: A) => void
): ITransformer<A, B>
declare function expr<T>(expr: () => T, scope?: any): T
declare function extendObservable<A, B>(target: A, ...properties: B[]): A & B
declare function intercept(object: Object, property: string, handler: IInterceptor<any>): Lambda;
declare function isComputed(value: any, property?: string): boolean
declare function isObservable(value: any, property?: string): boolean
declare function observable<T>(value: T): any
declare function observe(
object: any, property: string, listener: (newValue: any, oldValue?: any) => void, fireImmediately?: boolean
): Lambda
declare function toJS(source: any, detectCycles?: boolean, ___alreadySeen?: [any, any][]): any
declare function toJSlegacy(source: any, detectCycles?: boolean, ___alreadySeen?: [any, any][]): any
declare function whyRun(thing?: any, prop?: string): string
declare function useStrict(strict: boolean): any
declare function isStrictModeEnabled(): boolean
declare function untracked<T>(action: () => T): T
declare function spy(listener: (change: any) => void): Lambda
declare function transaction<T>(action: () => T, thisArg?: any, report?: boolean): T
declare function asReference<T>(value: T): T
declare function asStructure<T>(value: T): T
declare function asFlat<T>(value: T): T
declare function asMap<T>(data: IKeyValueMap<T>, modifierFunc?: Function): ObservableMap<T>
declare function isObservableArray(thing: any): boolean
declare function map<V>(
initialValues?: IMapEntries<V> | IKeyValueMap<V>, valueModifier?: Function
): ObservableMap<V>
declare function isObservableObject<T>(thing: T): boolean
declare function isArrayLike(x: any): boolean
declare class BaseAtom {
name: string;
isPendingUnobservation: boolean;
observers: any[];
observersIndexes: {};
diffValue: number;
lastAccessedBy: number;
lowestObserverState: IDerivationState;
constructor(name?: string): this;
onBecomeUnobserved(): void;
reportObserved(): void;
reportChanged(): void;
toString(): string
}
declare class Atom {
name: string;
onBecomeObservedHandler: () => void;
onBecomeUnobservedHandler: () => void;
isPendingUnobservation: boolean;
isBeingTracked: boolean;
constructor(name?: string, onBecomeObservedHandler?: () => void, onBecomeUnobservedHandler?: () => void): this;
reportObserved(): boolean;
onBecomeUnobserved(): void
}
declare class Reaction {
name: string;
observing: any[];
newObserving: any[];
dependenciesState: IDerivationState;
diffValue: number;
runId: number;
unboundDepsCount: number;
___mapid: string;
isDisposed: boolean;
_isScheduled: boolean;
_isTrackPending: boolean;
_isRunning: boolean;
constructor(name: string, onInvalidate: () => void): this;
onBecomeStale(): void;
schedule(): void;
isScheduled(): boolean;
runReaction(): void;
track(fn: () => void): void;
recoverFromError(): void;
dispose(): void;
getDisposer(): Lambda & {
$mosbservable: Reaction
};
toString(): string;
whyRun(): string
}
}