forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobservable.d.ts
189 lines (159 loc) · 6.38 KB
/
mobservable.d.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
// Type definitions for mobservable v0.6.10
// Project: https://mweststrate.github.io/mobservable
// Definitions by: Michel Weststrate <https://github.com/mweststrate/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
interface _IMobservableStatic {
/**
* Turns an object, array or function into a reactive structure.
* @param value the value which should become observable.
*/
makeReactive: IMakeReactive;
/**
* Extends an object with reactive capabilities.
* @param target the object to which reactive properties should be added
* @param properties the properties that should be added and made reactive
* @returns targer
*/
extendReactive(target: Object, properties: Object):Object;
/**
* Returns true if the provided value is reactive.
* @param value object, function or array
* @param propertyName if propertyName is specified, checkes whether value.propertyName is reactive.
*/
isReactive(value: any, propertyName?:string): boolean;
/**
* Can be used in combination with makeReactive / extendReactive.
* Enforces that a reference to 'value' is stored as property,
* but that 'value' itself is not turned into something reactive.
* Future assignments to the same property will inherit this behavior.
* @param value initial value of the reactive property that is being defined.
*/
asReference<T>(value: any):{value:T};
/**
* ES6 / Typescript decorator which can to make class properties and getter functions reactive.
*/
observable(target: Object, key: string):any; // decorator / annotation
/**
* Creates a reactive view and keeps it alive, so that the view is always
* updated if one of the dependencies changes, even when the view is not further used by something else.
* @param func The reactive view
* @param scope (optional)
* @returns disposer function, which can be used to stop the view from being updated in the future.
*/
observe(func: Mobservable.Lambda, scope?: any): Mobservable.Lambda;
/**
* Deprecated, use mobservable.observe instead.
*/
sideEffect(func: Mobservable.Lambda, scope?: any): Mobservable.Lambda;
/**
* Similar to 'observer', observes the given predicate until it returns true.
* Once it returns true, the 'effect' function is invoked an the observation is cancelled.
* @param predicate
* @param effect
* @param scope (optional)
* @returns disposer function to prematurely end the observer.
*/
observeUntil(predicate: ()=>boolean, effect: Mobservable.Lambda, scope?: any): Mobservable.Lambda;
/**
* During a transaction no views are updated until the end of the transaction.
* The transaction will be run synchronously nonetheless.
* @param action a function that updates some reactive state
* @returns any value that was returned by the 'action' parameter.
*/
transaction<T>(action: ()=>T): T;
/**
* Converts a reactive structure into a non-reactive structure.
* Basically a deep-clone.
*/
toJSON<T>(value: T): T;
/**
* Sets the reporting level Defaults to 1. Use 0 for production or 2 for increased verbosity.
*/
logLevel: number; // 0 = production, 1 = development, 2 = debugging
extras: {
getDependencyTree(thing:any, property?:string): Mobservable.IDependencyTree;
getObserverTree(thing:any, property?:string): Mobservable.IObserverTree;
trackTransitions(extensive?:boolean, onReport?:(lines:Mobservable.ITransitionEvent) => void) : Mobservable.Lambda;
}
}
interface IMakeReactive {
<T>(value: T[], opts?: Mobservable.IMakeReactiveOptions): Mobservable.IObservableArray<T>;
<T>(value: ()=>T, opts?: Mobservable.IMakeReactiveOptions): Mobservable.IObservableValue<T>;
<T extends string|number|boolean|Date|RegExp|Function|void>(value: T, opts?: Mobservable.IMakeReactiveOptions): Mobservable.IObservableValue<T>;
<T extends Object>(value: Object, opts?: Mobservable.IMakeReactiveOptions): T;
}
interface IMobservableStatic extends _IMobservableStatic, IMakeReactive {
}
declare namespace Mobservable {
interface IMakeReactiveOptions {
as?: string /* "auto" | "reference" | TODO: see #8 "structure" */
scope?: Object,
context?: Object,
recurse?: boolean;
name?: string;
// protected: boolean TODO: see #9
}
export interface IContextInfoStruct {
object: Object;
name: string;
}
export type IContextInfo = IContextInfoStruct | string;
interface Lambda {
(): void;
name?: string;
}
interface IObservable {
observe(callback: (...args: any[])=>void, fireImmediately?: boolean): Lambda;
}
interface IObservableValue<T> extends IObservable {
(): T;
(value: T):void;
observe(callback: (newValue: T, oldValue: T)=>void, fireImmediately?: boolean): Lambda;
}
interface IObservableArray<T> extends IObservable, Array<T> {
spliceWithArray(index: number, deleteCount?: number, newItems?: T[]): T[];
observe(listener: (changeData: IArrayChange<T>|IArraySplice<T>)=>void, fireImmediately?: boolean): Lambda;
clear(): T[];
replace(newItems: T[]): T[];
find(predicate: (item: T,index: number,array: IObservableArray<T>)=>boolean,thisArg?: any,fromIndex?: number): T;
remove(value: T): boolean;
}
interface IArrayChange<T> {
type: string; // Always: 'update'
object: IObservableArray<T>;
index: number;
oldValue: T;
}
interface IArraySplice<T> {
type: string; // Always: 'splice'
object: IObservableArray<T>;
index: number;
removed: T[];
addedCount: number;
}
interface IDependencyTree {
id: number;
name: string;
context: any;
dependencies?: IDependencyTree[];
}
interface IObserverTree {
id: number;
name: string;
context: any;
observers?: IObserverTree[];
listeners?: number; // amount of functions manually attached using an .observe method
}
interface ITransitionEvent {
id: number;
name: string;
context: Object;
state: string;
changed: boolean;
newValue: string;
}
}
declare module "mobservable" {
var m : IMobservableStatic;
export = m;
}