-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreactivity.d.ts
148 lines (118 loc) · 4.28 KB
/
reactivity.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
/*
Typescript definitions for the
public API of the native reactivity module.
This file also doubles as the official documentation.
*/
declare module "reactivity" {
/*
Returns a cell initialized to undefined
*/
function r(): r.Cell<any> ;
/*
Proxies to run() when called with one argument of type function
*/
function r<T>( f: r.Block<T> ) : r.Result<T> ;
/*
Returns a cell initialized to @value when called with one argument that is not a function
*/
function r<T>( value: T ): r.Cell<T> ;
/*
Proxies to subscribe() when called with two arguments of type function
*/
function r<T>( f: r.Block<T>, c: r.Callback<T> ) : r.Stopper ;
module r {
function active( ): boolean;
/*
Runs function in a reactive context
and returns its result
*/
function run<T>( f: Block<T> ) : Result<T> ;
/*
Creates a subscription to the stream of values
resulting from evaluating a function every
time a reactive change is detected.
A stopper function is returned.
Call it to stop the subscription
*/
function subscribe<T>( f: Block<T>, c: Callback<T> ) : Stopper ;
/*
Creates a new Notifier.
This is used to create reactive functions.
*/
function notifier(): Notifier ;
/*
Combinator that allows you to turn any function
into a rective function by means of polling and comparing
its return value. This is not the best way to go, but it
a very common use case and included here for convenience.
*/
function poll<T>( interval: number, f: Block<T> ): Block<T> ;
/*
possible status values: ready, cancelled, changed
*/
interface Monitor extends EventEmitter, StatefulObject {
cancel(): void
}
/*
status values: ready, cancelled, changed
*/
interface Notifier extends EventEmitter, StatefulObject {
// shorthand for change
(): void
cancel(): void
change(): void
}
interface Result<T> {
result?: T
error?: Error
monitor?: Monitor
get(): T
}
/*
Function that takes no arguments and returns T
*/
interface Block<T> { (): T }
/*
Signature for the callback that subscribe expects
*/
interface Callback<T> { ( error?: Error, result?: T, monitor?: Monitor, stopper?: Stopper ): void }
/*
*/
interface Stopper { (): void }
interface EventEmitter {
addListener( e: string, l: { (): void } ): void
on( e: string, l: { (): void } ): void
once( e: string, l: { (): void } ): void
removeListener( e: string, l: { (): void } ): void
removeAllListeners( e: string ): void
// TODO: setMaxListeners listeners?
}
interface StatefulObject extends EventEmitter {
state: String
}
/*
A Cell is a "Reactive Variable".
You can put anything you want in it by calling `cell( value )` ( one parameter )
and then you can get it back by calling cell() ( with no parameters ).
How do I set a cell to an error state?
If you pass a value where ( value instanceof Error == true )
Then the cell will automatically be set to an error state.
If you wish to pass an error so that it can be stored
you will need to wrap it into something else.
*/
interface Cell<T> {
// retrieve the current value
// cells are initialized to undefined by default
( ): T
// store a value
( v: T ): void
// store an error
( v: Error ): void
// store a value or an error
// this allows you to pass a cell as
// a node.js style callback directly
( e?: Error, v?: T ): void
}
}
export = r;
}