-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from gaearon/middleware-api
Add basic middleware api to default dispatcher
- Loading branch information
Showing
7 changed files
with
36 additions
and
10 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
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
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
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,20 +1,24 @@ | ||
export default function createDispatcher(store) { | ||
import compose from './utils/composeMiddleware'; | ||
|
||
export default function createDispatcher(store, middlewares = []) { | ||
return function dispatcher(initialState, setState) { | ||
let state = store(initialState, {}); | ||
setState(state); | ||
|
||
function dispatchSync(action) { | ||
function dispatch(action) { | ||
state = store(state, action); | ||
setState(state); | ||
return action; | ||
} | ||
|
||
function dispatch(action) { | ||
return typeof action === 'function' ? | ||
action(dispatch, state) : | ||
dispatchSync(action); | ||
function getState() { | ||
return state; | ||
} | ||
|
||
if (typeof middlewares === 'function') { | ||
middlewares = middlewares(getState); | ||
} | ||
|
||
return dispatch; | ||
return compose(...middlewares, 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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default function thunkMiddleware(getState) { | ||
return (next) => { | ||
const recurse = (action) => | ||
typeof action === 'function' ? | ||
action(recurse, getState) : | ||
next(action); | ||
|
||
return recurse; | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function compose(...middlewares) { | ||
return middlewares.reduceRight((composed, m) => m(composed)); | ||
} |