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

Add basic middleware api to default dispatcher #63

Merged
merged 4 commits into from
Jun 10, 2015
Merged
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
4 changes: 3 additions & 1 deletion examples/actions/CounterActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export function increment() {
}

export function incrementIfOdd() {
return (dispatch, { counter }) => {
return (dispatch, getState) => {
const { counter } = getState();

if (counter % 2 === 0) {
return;
}
Expand Down
4 changes: 3 additions & 1 deletion examples/components/Counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ export default class Counter {
};

render() {
const { increment, decrement, counter } = this.props;
const { increment, incrementIfOdd, decrement, counter } = this.props;
return (
<p>
Clicked: {counter} times
{' '}
<button onClick={increment}>+</button>
{' '}
<button onClick={decrement}>-</button>
{' '}
<button onClick={incrementIfOdd}>Increment if odd</button>
</p>
);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Redux.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import createDispatcher from './createDispatcher';
import composeStores from './utils/composeStores';
import thunkMiddleware from './middleware/thunk';

export default class Redux {
constructor(dispatcher, initialState) {
if (typeof dispatcher === 'object') {
// A shortcut notation to use the default dispatcher
dispatcher = createDispatcher(composeStores(dispatcher));
dispatcher = createDispatcher(
composeStores(dispatcher),
getState => [ thunkMiddleware(getState) ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I'd prefer no spaces inside arrays

);
}

this.state = initialState;
Expand Down
18 changes: 11 additions & 7 deletions src/createDispatcher.js
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);
};
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export provide from './components/provide';
export connect from './components/connect';

// Utilities
export compose from './utils/composeMiddleware';
export composeStores from './utils/composeStores';
export bindActionCreators from './utils/bindActionCreators';
10 changes: 10 additions & 0 deletions src/middleware/thunk.js
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;
};
}
3 changes: 3 additions & 0 deletions src/utils/composeMiddleware.js
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));
}