Skip to content

Instantly share code, notes, and snippets.

@artalar
Created September 17, 2019 05:56
Show Gist options
  • Select an option

  • Save artalar/dae3effdb549f297c22d7ee99c2f2c51 to your computer and use it in GitHub Desktop.

Select an option

Save artalar/dae3effdb549f297c22d7ee99c2f2c51 to your computer and use it in GitHub Desktop.
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