Created
July 23, 2016 18:12
-
-
Save flintinatux/7ed4fc93b95c61a03cf77d4ed9bccdcd to your computer and use it in GitHub Desktop.
Component wrapper for mithril 1.x. Registers lifecycle methods as streams, so that components may be contructed as single-closure factories that return view functions.
This file contains hidden or 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
const m = require('mithril') | |
const stated = hook => vnode => vnode.state[hook](vnode) | |
const oninit = Comp => vnode => { | |
vnode.state.oncreate = m.prop() | |
vnode.state.onremove = m.prop() | |
vnode.state.view = Comp(vnode) | |
} | |
const onbeforeupdate = (vnode, old) => old.instance !== vnode.state.view() | |
const oncreate = stated('oncreate') | |
const onremove = stated('onremove') | |
const view = stated('view') | |
module.exports = function(Comp) { | |
return { oninit: oninit(Comp), oncreate, onbeforeupdate, onremove, view } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment