Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Last active December 26, 2020 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/ccc7f2b579a2d0e392125aab229e3819 to your computer and use it in GitHub Desktop.
Save barneycarroll/ccc7f2b579a2d0e392125aab229e3819 to your computer and use it in GitHub Desktop.
Decorates Mithril hyperscript function with a logger which assigns a persistent numeric id to nodes and logs their lifecycles. Useful for debugging Mithril rendering sequence.
// imort m from 'mithril'
export default Object.assign(
decorate(m),
m,
{
fragment: decorate(m.fragment),
},
)
const nodes = new WeakMap
let count = 0
function decorate(visitor){
return function(){
const node = visitor.apply(this, arguments)
if(!node.attrs)
node.attrs = {}
void
'oninit view oncreate onbeforeupdate onupdate onbeforeremove onremove'
.split(' ')
.forEach(key => {
const method = node.attrs[key]
node.attrs[key] = function(){
const id = xet(nodes, this, () => count++)
console.log(id, key, node)
if(method)
return method.apply(this, arguments)
}
})
return node
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment