Created
January 31, 2018 17:17
-
-
Save barneycarroll/427272e54b54f852745f8a8cb8711dc9 to your computer and use it in GitHub Desktop.
WiP: mechanism for persisting DOM across the tree in Mithril
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
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