Skip to content

Commit

Permalink
Annotate createDispatcher and composeStores
Browse files Browse the repository at this point in the history
  • Loading branch information
acdlite committed Jun 14, 2015
1 parent f2b37cd commit 2f3325e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/createDispatcher.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
/* @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;
}

if (typeof middlewares === 'function') {
middlewares = middlewares(getState);
}

return compose(...middlewares, dispatch);
return compose(...(middlewares: Middleware[]), dispatch);
};
}
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 { 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)
);
};
}

0 comments on commit 2f3325e

Please sign in to comment.