Skip to content

Instantly share code, notes, and snippets.

@Quramy
Last active January 15, 2019 05:36
Show Gist options
  • Save Quramy/45ff6d74b4d61e51a58cfc402e2d05cd to your computer and use it in GitHub Desktop.
Save Quramy/45ff6d74b4d61e51a58cfc402e2d05cd to your computer and use it in GitHub Desktop.
minimal reproducer for nested dispatching error with spork / thunk
require('babel-polyfill');
const { createStore, applyMiddleware, bindActionCreators, compose } = require('redux');
const { enableBatching, batchActions, batchDispatchMiddleware, BATCH } = require('redux-batched-actions');
const thunk = require('redux-thunk').default;
const { fork, createMiddleware } = require('redux-spork');
function main() {
// reducer
const count = (prev = 0, action) => {
console.log('action:', action);
switch (action.type) {
case 'increment': return ++prev;
case 'decrement': return --prev;
default: return prev;
}
};
// action creators using thunk
const increment = () => ({ type: 'increment' });
const decrement = () => ({ type: 'decrement' });
const complex = (boundAction) => dispatch => {
dispatch(increment());
// the following causes an undefined action only with spork middlware.
dispatch(boundAction());
dispatch(batchActions([boundAction()]));
};
// app
const sporkMiddleware = createMiddleware();
const thunkBatchGuardMiddleware = () => dispatch => a => a && dispatch(a.type === BATCH ? { ...a, payload: a.payload.filter(_ => !!_) } : a);
// const store = createStore(compose(enableBatching)(count), applyMiddleware(thunk, sporkMiddleware)); // Errors are thrown if no guard
const store = createStore(compose(enableBatching)(count), applyMiddleware(thunk, thunkBatchGuardMiddleware, sporkMiddleware));
// const store = createStore(count, applyMiddleware(thunk)); // Errors are never thrown without spork middleware
// view, connect
store.subscribe(() => console.log('rendered:', store.getState()));
const props = bindActionCreators({ increment, decrement, complex }, store.dispatch);
// fire events
props.increment();
props.complex(props.decrement);
}
main();
require('babel-polyfill');
const { createStore, applyMiddleware, bindActionCreators } = require('redux');
const thunk = require('redux-thunk').default;
const { fork, createMiddleware } = require('redux-spork');
function main() {
// reducer
const count = (prev = 0, action) => {
console.log('action:', action);
switch (action.type) {
case 'increment': return ++prev;
case 'decrement': return --prev;
default: return prev;
}
};
// action creators using thunk
const increment = () => ({ type: 'increment' });
const decrement = () => ({ type: 'decrement' });
const complex = (boundAction) => dispatch => {
dispatch(increment());
// the following causes an undefined action only with spork middlware.
dispatch(boundAction());
};
// app
const sporkMiddleware = createMiddleware();
const store = createStore(count, applyMiddleware(thunk, sporkMiddleware));
// const store = createStore(count, applyMiddleware(thunk)); // Errors are never thrown without spork middleware
// view, connect
store.subscribe(() => console.log('rendered:', store.getState()));
const props = bindActionCreators({ increment, decrement, complex }, store.dispatch);
// fire events
props.increment();
props.complex(props.decrement);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment