Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow type annotations #87

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"strict": [2, "never"],
"react/jsx-uses-react": 2,
"react/jsx-uses-vars": 2,
"react/react-in-jsx-scope": 2
"react/react-in-jsx-scope": 2,
"no-unused-vars": 0
},
"plugins": [
"react"
Expand Down
9 changes: 9 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[ignore]
.*/lib
.*/test

[include]

[libs]

[options]
35 changes: 22 additions & 13 deletions src/Redux.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,60 @@
/* @flow */

import createDispatcher from './createDispatcher';
import composeStores from './utils/composeStores';
import thunkMiddleware from './middleware/thunk';

import { State, Action, Dispatch, Dispatcher } from './types';

export default class Redux {
constructor(dispatcher, initialState) {
state: State;
listeners: Function[];
dispatcher: Dispatcher;
dispatchFn: Dispatch;

constructor(dispatcher: Dispatcher, initialState: State): void {
if (typeof dispatcher === 'object') {
// A shortcut notation to use the default dispatcher
dispatcher = createDispatcher(
dispatcher = (createDispatcher(
composeStores(dispatcher),
getState => [thunkMiddleware(getState)]
);
));
}

this.state = initialState;
this.listeners = [];
this.replaceDispatcher(dispatcher);
}

getDispatcher() {
getDispatcher(): Dispatcher {
return this.dispatcher;
}

replaceDispatcher(nextDispatcher) {
replaceDispatcher(nextDispatcher: Dispatcher): void {
this.dispatcher = nextDispatcher;
this.dispatchFn = nextDispatcher(this.state, ::this.setState);
this.dispatchFn = nextDispatcher(this.state, this.setState.bind(this));
}

dispatch(action) {
dispatch(action: Action): any {
return this.dispatchFn(action);
}

getState() {
getState(): State {
return this.state;
}

setState(nextState) {
setState(nextState: State): State {
this.state = nextState;
this.listeners.forEach(listener => listener());
return nextState;
}

subscribe(listener) {
const { listeners } = this;
subscribe(listener: Function): () => void {
var { listeners } = this;
listeners.push(listener);

return function unsubscribe () {
const index = listeners.indexOf(listener);
return function unsubscribe() {
var index = listeners.indexOf(listener);
listeners.splice(index, 1);
};
}
Expand Down
20 changes: 15 additions & 5 deletions src/createDispatcher.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
/* @flow */

import compose from './utils/composeMiddleware';

export default function createDispatcher(store, middlewares = []) {
return function dispatcher(initialState, setState) {
let state = setState(store(initialState, {}));
import type { Middleware, Store, Action, State, Dispatcher } from './types';

export default function createDispatcher(
store: Store,
middlewares: (Middleware[] | (getState: () => State) => Middleware[]) = []
): Dispatcher {
return function dispatcher(
initialState: State,
setState: (state: State) => State
) {
var state: State = setState(store(initialState, {}));

function dispatch(action) {
function dispatch(action: Action): Action {
state = setState(store(state, action));
return action;
}

function getState() {
function getState(): State {
return state;
}

Expand Down
13 changes: 13 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* @flow */

export type State = any;

export type Action = any;

export type Store = (state: State, action: Action) => State;

export type Dispatch = (action: Action) => any;

export type Dispatcher = (state: State, setState: (nextState: State) => State) => Dispatch;

export type Middleware = (next: Dispatch | Middleware) => (Dispatch | Middleware);
10 changes: 8 additions & 2 deletions src/utils/composeMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export default function compose(...middlewares) {
return middlewares.reduceRight((composed, m) => m(composed));
/* @flow */

import { Middleware, Dispatch } from '../types';

export default function compose(...middlewares: Middleware[]): Middleware {
return middlewares.reduceRight(
(composed: Middleware | Dispatch, m: Middleware | Dispatch) => m(composed)
);
}
10 changes: 7 additions & 3 deletions src/utils/composeStores.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/* @flow */

import mapValues from 'lodash/object/mapValues';

export default function composeStores(stores) {
return function Composition(atom = {}, action) {
import type { Store, Action, State } from '../types';

export default function composeStores(stores: Store[]): Store {
return function Composition(state: State = {}, action: Action) {
return mapValues(stores, (store, key) =>
store(atom[key], action)
store(state[key], action)
);
};
}