Skip to content

Instantly share code, notes, and snippets.

@zaceno
Last active April 13, 2020 19:27
Show Gist options
  • Save zaceno/04e2c435a11eb0622d5c73ef5b5c3ca5 to your computer and use it in GitHub Desktop.
Save zaceno/04e2c435a11eb0622d5c73ef5b5c3ca5 to your computer and use it in GitHub Desktop.
define effect creator for hyperapp
export default (g => f => (...a) => [g, { f, a }])((d, { f, a }) => f(d, ...a))
/*
Usage like:
import fx from 'fx.js'
const jsonLoader = fx((dispatch, url, then) =>
fetch(url)
.then(response => response.json())
.then(data => dispatch(then, data)
)
...
const LoadAction => (state, url) => [
{...state, loading: true},
jsonLoader(url, ResultAction)
]
...or...
const jsonLoader = fx((dispatch, {url, action}) =>
fetch(url)
.then(respnse => response.json())
.then(data => dispatch(action, data))
)
...
const LoadAction => (state, url) => [
{...state, loading: true},
jsonLoader({url, action: ResultAction})
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment