Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Last active February 22, 2018 00:07
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/3c0076162c7baa5ac343c38ecebf7423 to your computer and use it in GitHub Desktop.
Save barneycarroll/3c0076162c7baa5ac343c38ecebf7423 to your computer and use it in GitHub Desktop.
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