Skip to content

Instantly share code, notes, and snippets.

@flintinatux
Created July 23, 2016 18:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save flintinatux/7ed4fc93b95c61a03cf77d4ed9bccdcd to your computer and use it in GitHub Desktop.
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.
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