Skip to content

Instantly share code, notes, and snippets.

@sergey-shpak
Last active November 7, 2019 15:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergey-shpak/75cc4162366c10743b6d6496093d8cad to your computer and use it in GitHub Desktop.
Save sergey-shpak/75cc4162366c10743b6d6496093d8cad to your computer and use it in GitHub Desktop.
Hyperapp#v2 actions composition
/*
Implements actions composition for Hyperapp#v2
Usage examples:
const actionA = state => ({ property: 'A' })
const actionB = state => ({ property: state.property + 'B' })
const composition = compose(actionA, actionB)
composition({}) // { property: 'AB' }
In case your actions return effects, or parameterized actions
please use 'trigger' function to compose actions
const actionA = (state, param) => ({ param })
const actionB = state => [actionA, 'foo']
const actionC = state => ({...state, c: true})
const composition = trigger(actionB, actionC)
composition({}) // { param: 'foo', c: true }
*/
export const compose = (...actions) => (state, ...args) =>
actions.reduce((res, action) => action(res, ...args), state)
export const trigger = (...actions) => (state, ...args) =>
[state, { effect: (_, dispatch) => actions.map(action =>
dispatch(s => action(s, ...args))) }]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment