1
+ /* @flow */
2
+
1
3
import createDispatcher from './createDispatcher' ;
2
4
import composeStores from './utils/composeStores' ;
3
5
import thunkMiddleware from './middleware/thunk' ;
4
6
7
+ type Dispatch = ( action : mixed ) => mixed ;
8
+ type Dispatcher = ( state : mixed , setState : ( nextState : mixed ) => mixed ) => Dispatch ;
9
+
5
10
export default class Redux {
6
- constructor ( dispatcher , initialState ) {
11
+ state : mixed ;
12
+ listeners : Array < ( ) => mixed > ;
13
+ dispatcher : Dispatcher ;
14
+ dispatchFn : Dispatch ;
15
+
16
+ constructor ( dispatcher : Dispatcher | Object , initialState : mixed ) : void {
7
17
if ( typeof dispatcher === 'object' ) {
8
18
// A shortcut notation to use the default dispatcher
9
19
dispatcher = createDispatcher (
@@ -17,35 +27,35 @@ export default class Redux {
17
27
this . replaceDispatcher ( dispatcher ) ;
18
28
}
19
29
20
- getDispatcher ( ) {
30
+ getDispatcher ( ) : Dispatcher {
21
31
return this . dispatcher ;
22
32
}
23
33
24
- replaceDispatcher ( nextDispatcher ) {
34
+ replaceDispatcher ( nextDispatcher : Dispatcher ) : void {
25
35
this . dispatcher = nextDispatcher ;
26
- this . dispatchFn = nextDispatcher ( this . state , :: this . setState ) ;
36
+ this . dispatchFn = nextDispatcher ( this . state , this . setState . bind ( this ) ) ;
27
37
}
28
38
29
- dispatch ( action ) {
39
+ dispatch ( action : mixed ) : mixed {
30
40
return this . dispatchFn ( action ) ;
31
41
}
32
42
33
- getState ( ) {
43
+ getState ( ) : mixed {
34
44
return this . state ;
35
45
}
36
46
37
- setState ( nextState ) {
47
+ setState ( nextState : mixed ) : mixed {
38
48
this . state = nextState ;
39
49
this . listeners . forEach ( listener => listener ( ) ) ;
40
50
return nextState ;
41
51
}
42
52
43
- subscribe ( listener ) {
44
- const { listeners } = this ;
53
+ subscribe ( listener : ( ) = > mixed ) : ( ) = > mixed {
54
+ var { listeners } = this ;
45
55
listeners . push ( listener ) ;
46
56
47
57
return function unsubscribe ( ) {
48
- const index = listeners . indexOf ( listener ) ;
58
+ var index = listeners . indexOf ( listener ) ;
49
59
listeners . splice ( index , 1 ) ;
50
60
} ;
51
61
}
0 commit comments