diff --git a/src/createDispatcher.js b/src/createDispatcher.js index 9a928b8c94f..04780ecc3f1 100644 --- a/src/createDispatcher.js +++ b/src/createDispatcher.js @@ -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 { 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; } @@ -17,6 +27,6 @@ export default function createDispatcher(store, middlewares = []) { middlewares = middlewares(getState); } - return compose(...middlewares, dispatch); + return compose(...(middlewares: Middleware[]), dispatch); }; } diff --git a/src/utils/composeStores.js b/src/utils/composeStores.js index 9aa2442a802..78b49094d1b 100644 --- a/src/utils/composeStores.js +++ b/src/utils/composeStores.js @@ -1,9 +1,13 @@ +/* @flow */ + import mapValues from 'lodash/object/mapValues'; -export default function composeStores(stores) { - return function Composition(atom = {}, action) { +import { 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) ); }; }