Skip to content

Commit 7b292b0

Browse files
committed
Annotate Redux.js
1 parent ca935a1 commit 7b292b0

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

.flowconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[ignore]
2+
.*/lib
3+
.*/test
4+
5+
[include]
6+
7+
[libs]
8+
./type-definitions
9+
10+
[options]

src/Redux.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
/* @flow */
2+
13
import createDispatcher from './createDispatcher';
24
import composeStores from './utils/composeStores';
35
import thunkMiddleware from './middleware/thunk';
46

7+
type Dispatch = (action: mixed) => mixed;
8+
type Dispatcher = (state: mixed, setState: (nextState: mixed) => mixed) => Dispatch;
9+
510
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 {
717
if (typeof dispatcher === 'object') {
818
// A shortcut notation to use the default dispatcher
919
dispatcher = createDispatcher(
@@ -17,35 +27,35 @@ export default class Redux {
1727
this.replaceDispatcher(dispatcher);
1828
}
1929

20-
getDispatcher() {
30+
getDispatcher(): Dispatcher {
2131
return this.dispatcher;
2232
}
2333

24-
replaceDispatcher(nextDispatcher) {
34+
replaceDispatcher(nextDispatcher: Dispatcher): void {
2535
this.dispatcher = nextDispatcher;
26-
this.dispatchFn = nextDispatcher(this.state, ::this.setState);
36+
this.dispatchFn = nextDispatcher(this.state, this.setState.bind(this));
2737
}
2838

29-
dispatch(action) {
39+
dispatch(action: mixed): mixed {
3040
return this.dispatchFn(action);
3141
}
3242

33-
getState() {
43+
getState(): mixed {
3444
return this.state;
3545
}
3646

37-
setState(nextState) {
47+
setState(nextState: mixed): mixed {
3848
this.state = nextState;
3949
this.listeners.forEach(listener => listener());
4050
return nextState;
4151
}
4252

43-
subscribe(listener) {
44-
const { listeners } = this;
53+
subscribe(listener: () => mixed): () => mixed {
54+
var { listeners } = this;
4555
listeners.push(listener);
4656

4757
return function unsubscribe () {
48-
const index = listeners.indexOf(listener);
58+
var index = listeners.indexOf(listener);
4959
listeners.splice(index, 1);
5060
};
5161
}

0 commit comments

Comments
 (0)