Created
September 17, 2019 05:56
-
-
Save artalar/dae3effdb549f297c22d7ee99c2f2c51 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { Store, ActionCreator } from '.' | |
| import { identity } from './shared' | |
| function declareEffect(cb) {} | |
| type Step = { | |
| type: 'next' | 'catch' | string | |
| reducer: (promise: Promise<any>) => Promise<any> | |
| } | |
| class Flow { | |
| _steps: Step[] | |
| constructor(steps: Step[]) { | |
| this._steps = steps | |
| } | |
| next(onfulfilled: (...a: any) => any) { | |
| return new Flow( | |
| this._steps.concat({ | |
| type: 'next', | |
| reducer: promise => promise.then(onfulfilled), | |
| }), | |
| ) | |
| } | |
| catch(onrejected: (...a: any) => any) { | |
| return new Flow( | |
| this._steps.concat({ | |
| type: 'catch', | |
| reducer: promise => promise.catch(onrejected), | |
| }), | |
| ) | |
| } | |
| dispatch(actionCreator: ActionCreator, mapper = identity) { | |
| return new Flow( | |
| this._steps.concat({ | |
| type: actionCreator.getType(), | |
| reducer: promise => | |
| promise.next(function(payload) { | |
| this[STORE].dispatch(actionCreator(mapper(payload))) | |
| return payload | |
| }), | |
| }), | |
| ) | |
| } | |
| run(store: Store, payload) { | |
| return this._steps.reduce( | |
| (acc, step) => step.reducer(acc), | |
| new Promise(r => r(payload)), | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment