-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.d.ts
273 lines (245 loc) · 8.08 KB
/
index.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
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
import { Observable } from 'rxjs'
import React from 'react'
import * as PropTypes from 'prop-types'
import { AnyAction } from 'redux'
export declare const recycle: recycle.Recycle
export default recycle
export as namespace recycle
type ReducerFn<S, R> = (state: S, value?: any, store?: R) => S
type ReducerObservableFn<S, R> = (stream: Observable<Event>) => Observable<{ reducer: ReducerFn<S, R>, event: Event }>
type DomEvent =
'onClick' |
'onKeyUp' |
'onKeyDown' |
'onKeyPress' |
'onBlur' |
'onChange' |
'onClose' |
'onFocus' |
'onInput' |
'onMouseMove' |
'onMouseDown' |
'onMouseUp' |
'onMouseOut' |
'onMouseOver' |
'onScroll' |
'onSubmit' |
'onSelect' |
'onTouchMove' |
'onTouchCancel' |
'onTouchStart' |
'onWheel'
declare module 'rxjs/Observable' {
interface Observable<T> {
/**
* Acts like a Redux reducer, it receives you local component state (and your Redux state, if you are using Redux),
* and returns a new local state
*
* @template S Your Component local state
* @template R Your Redux state
* @param {(state: S, store: R) => S} reducerFn PS: Param store only appears if you are using Redux (Component is inside Provider)
* @example
*
* sources.select('button')
* .addListener('onClick')
* .reducer((state, eventValue) => {
* ...state,
* counter: state.counter + eventValue
* })
*
*/
reducer<S, R>(reducerFn: ReducerFn<S, R>): ReducerObservableFn<S, R>
}
}
declare namespace recycle {
interface Listeners<T> {
/**
* Register a new DOM listener and returns a stream of DOM events
*
* @param event DOM Event to listen to
* @returns {Observable<Event>}
* @example
*
* sources.select('button')
* .addListener('onClick')
* .reducer(state => {
* ...state,
* counter: state.counter + 1
* }),
*
*/
addListener: (event: T) => Observable<Event>
}
type ReactLifeCycle =
'componentWillMount' |
'componentDidMount' |
'componentWillReceiveProps' |
'shouldComponentUpdate' |
'componentWillUpdate' |
'componentDidUpdate' |
'componentWillUnmount'
interface Sources<S, R> {
/**
* This method selects a element by tagName or a ChildComponent and returns a recycle.Listeners
*
* @param param
* @returns {recycle.Listeners} Returns { addListener: (event: DomEvent) => Observable<Event> }
* @example
*
* PS: Assuming i have a <button></button>
* sources.select('button')
*
* or
*
* PS: Assuming i have a <ChildComponent></ChildComponent> as a child element.
* sources.select(ChildComponent)
*
*/
select: <CP={}, CS={}>(param: string | React.Component<CP, CS> | React.StatelessComponent<CP>) => recycle.Listeners<DomEvent | keyof CP>;
/**
* This method selects a element by a CSS class and returns a recycle.Listeners
*
* @param className
* @returns {recycle.Listeners} Returns { addListener: (event: DomEvent) => Observable<Event> }
* @example
*
* * PS: Assuming i have a <Element class='cssClass'></Element> as a child element.
* sources.select('cssClass')
*
*/
selectClass: <CP={}>(className: string) => recycle.Listeners<DomEvent | keyof CP>;
/**
* This method selects a element by id and returns a recycle.Listeners
*
* @param id
* @returns {recycle.Listeners} Returns { addListener: (event: DomEvent) => Observable<Event> }
* @example
*
* PS: Assuming i have a <Element id='elementId'></Element> as a child element.
* sources.select('elementId')
*
*/
selectId: <CP={}>(id: string) => recycle.Listeners<DomEvent | keyof CP>
/**
* If you are using Redux (component is inside Provider) this will return a stream of your redux state
*
* @template R is a interface representing your Redux state
* @returns {Observable<R>} Returns a Observable with your latest Redux state
* @example
*
* PS: Assuming R is { count: number }
* sources.store.map((state: { count: number }) => state.count)
*
*/
store?: Observable<R>
/**
* Returns a stream of your local component state
*
* @template S is a interface representing your Component state
* @returns {Observable<S>} Returns a Observable with your latest component state
* @example
*
* sources.select('input')
* .addListener('onKeyPress')
* .filter(e => e.key === 'Enter')
* .withLatestFrom(sources.state)
* .map(([e, state]) => state.someStateValue)
*
*/
state: Observable<S>
/**
* Returns a stream of component lifecycle events
*
* @returns {Observable<ReactLifeCycle>}
* @example
*
* sources.lifecycle
* .filter(e => e === 'componentDidMount')
* .do(something)
*
*/
lifecycle: Observable<ReactLifeCycle>
}
interface Params<P, S, R> {
/**
* React props passed by JSX
*/
propTypes?: PropTypes.ValidationMap<P>
/**
* Determines the html tag name
*/
displayName?: string
/**
* Component's local initial state
*/
initialState?: S
/**
*
*
* @param sources
* @returns {Observable<AnyAction>[]} Array of Redux action streams
*/
dispatch?: (sources: recycle.Sources<S, R>) => Observable<AnyAction>[]
/**
* Acts like a Redux reducer for the component local state
*
* @param sources
* @template S Interface representing the component local state
* @template R Interface representing the Redux state (If using Redux)
* @returns {Observable<ReducerObservableFn<S, R>>[]}
* @example
*
* update: (sources) => {
* return [
* sources.store
* .reducer(function (state, store) {
* return state
* })
* ]
* },
*
*/
update?: (sources: recycle.Sources<S, R>) => ReducerObservableFn<S, R>[]
/**
* If you don't need to update a component local state or dispatch Redux action, but you still need to react
* to some kind of async operation, you can use effects.
*
* @param sources
* @returns Observable<any>[]
* @example
*
* effects: (sources) => {
* return [
* sources.select('input')
* .addListener('onKeyPress')
* .withLatestFrom(sources.props)
* .map(([e, props]) => {
* props.callParentFunction(e.target.value)
* })
* ]
* }
*
*/
effects?: (sources: recycle.Sources<S, R>) => Observable<any>[]
/**
* Returns the JSX to be rendered for the component
*
* @param props
* @param state
* @returns {JSX.Element}
* @example
*
* view: (props, state) =>
* <div>
* <div>Seconds Elapsed: {state.secondsElapsed}</div>
* <div>Times Clicked: {state.counter}</div>
* <button>Click Me</button>
* </div>
*
*/
view?: (props: P, state: S) => JSX.Element
}
interface Recycle {
<P, S, R=any>(params: recycle.Params<P, S, R>): React.ComponentClass<P>
}
}