Last active
November 7, 2019 15:07
-
-
Save sergey-shpak/75cc4162366c10743b6d6496093d8cad to your computer and use it in GitHub Desktop.
Hyperapp#v2 actions composition
This file contains 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
/* | |
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