-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Annotate createDispatcher and composeStores
- Loading branch information
Showing
2 changed files
with
23 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
}; | ||
} |