Last active
September 26, 2019 21:02
-
-
Save sergey-shpak/e2ced3c897e30acd4a4d0143a2042a2b to your computer and use it in GitHub Desktop.
Hyperapp V2 computed properties
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 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