Last active
December 26, 2020 14:00
-
-
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.
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
// 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