Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created January 31, 2018 17:17
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/427272e54b54f852745f8a8cb8711dc9 to your computer and use it in GitHub Desktop.
Save barneycarroll/427272e54b54f852745f8a8cb8711dc9 to your computer and use it in GitHub Desktop.
WiP: mechanism for persisting DOM across the tree in Mithril
import m from 'mithril'
const find = predicate => generator => {
for(const item of generator)
if(predicate(item))
return item
}
const horizontalElementQuery = function * (root = document){
const backlog = []
if(root.children)
for(child of root.children){
yield child
backlog.push(child)
}
for(child of backlog)
yield * horizontalElementQuery(child)
}
const heap = new Map
const now = new Map
const then = new Map
let root
const remap = () => {
if(!root)
root = find($ => $.vnodes, horizontalElementQuery())
}
const intercede = key => vnode => {
if(!heap.size)
setTimeout(remap)
now.set(key, vnode)
}
export default ({key}, ...children) => {
const intercession =
intercede(key)
const vnode =
m.fragment({
oninit : intercession,
onbeforeupdate : intercession
})
heap.set(key, vnode)
return vnode
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment