Last active
February 22, 2018 00:07
-
-
Save barneycarroll/3c0076162c7baa5ac343c38ecebf7423 to your computer and use it in GitHub Desktop.
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
const findOBR = v => { | |
while(v.length || !(v.tag.onbeforeremove || v.attrs && v.attrs.onbeforeremove)) | |
v = v.instance || v.children || v[0] | |
return v | |
} | |
const persist = v => { | |
const target = findOBR(v.instance || v.children) | |
if(v.attrs.link) | |
v.attrs.link.set(target.dom, target) | |
} | |
const Blocker = { | |
oninit: v => { | |
v.state.link = new Map | |
}, | |
view: v => ( | |
v.attrs.view | |
? | |
v.attrs.view(v.state.link) | |
: | |
v.children | |
), | |
oncreate: persist, | |
onupdate: persist, | |
onremove: v => { | |
if(v.attrs.link) | |
v.attrs.link.delete(v.dom) | |
}, | |
onbeforeremove: v => | |
Promise.all( | |
[...v.state.link.values()] | |
.map(v => | |
Promise.all( | |
['attrs', 'tag'] | |
.map(host => | |
v[host].onbeforeremove | |
) | |
.map(obr => | |
obr && obr.call(v.state, v) | |
) | |
) | |
) | |
), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment