Skip to content

Instantly share code, notes, and snippets.

@sergey-shpak
Last active September 26, 2019 21:02
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/e2ced3c897e30acd4a4d0143a2042a2b to your computer and use it in GitHub Desktop.
Save sergey-shpak/e2ced3c897e30acd4a4d0143a2042a2b to your computer and use it in GitHub Desktop.
Hyperapp V2 computed properties
/*
Implements computed properties for Hyperapp#v2
It's not recomended approach to use
unless you really know what you're doing
Usage examples:
// action is triggered whenever state.property updated*
const action = state => {
// some compute code
}
app({
init: {
property: true
}
subscriptions: state => [
computed(state, 'property')(action)
],
//...
})
* please notice, you have to double Object.assign to invoke 'property' setter,
const propUpdate = (state, value) =>
Object.assign({}, Object.assign(state, { property: value }))
*/
const effect = ({ target, prop, action }, dispatch) => {
let value = target[prop]
Object.defineProperty(target, prop, {
get: () => value,
set: update => {
value = update
setTimeout(() => dispatch([action, value]), 0)
}
})
return () => {}
}
const computed = (target, prop) => action =>
({ target, prop, action, effect })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment