Skip to content

Instantly share code, notes, and snippets.

@Bondifrench
Forked from flintinatux/component.js
Created July 24, 2016 01:37
Show Gist options
  • Save Bondifrench/957ffa872112910b8c43053750d22c45 to your computer and use it in GitHub Desktop.
Save Bondifrench/957ffa872112910b8c43053750d22c45 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