redux-generator
A middleware for redux, allows to dispatch an action which is a generator function.
Get Started
Installation
npm install redux-generator --save
Usage
First, apply middleware:
;// import redux-generator;// reducers in your project.;// apply redux-generatorconst createStoreWithMiddleware = createStore;const store = ;
You can write an action creator like this:
// in ./actions/userActionsCreator.jsconst doSaveUser = { return { // async dispatch an action ; // sync waterfall: a plain object yield let params = Object; // async dispatch an action ; // sync waterfall: a promise yield let payload = { process; }; // async dispatch an action ; // sync waterfall: a thunk yield let redirect = { if payload && payloadid return type: 'ROUTING_POP' payload: `/user/`; }; // Only this ending action will be dispatched by generator middleware after all return redirect; };}
Dispatch in anywhere:
;store;
These series of actions as follow will be dispatched one by one:
dispatch action: { type: 'LOADING', payload: 'Loading' }
dispatch action: { type: 'WILL_SAVE_USER', payload: { lastModified: 1476005411707, name: 'xyx' } }
dispatch action: { type: 'DID_SAVE_USER', payload: { lastModified: 1476005411707, name: 'xyx', id: '1' } }
dispatch action: { type: 'ROUTING_POP', payload: '/user/1' }
Each action was returned by syntax followed yield
will be executed step by step. If one of yield
throws error, the others behind will be terminated.
Both errors, whether thunk yield
threw or promise yield
rejected will be translated to FSA standard error action and dispatched:
;; const user = { console; return state;}const reducers = ;const store = createStorereducers; // action>>>reducer: { type: 'ERROR', error: true, payload: [Error: foo] }store; // action>>>reducer: { type: 'ERROR', error: true, payload: [Error: bar] }store; // action>>>reducer: { type: 'ERROR', error: true, payload: [Error: Inner error] }store;
Contribution
test
npm test
build
npm run release