Create a gist now

Instantly share code, notes, and snippets.

A component that ensures the underlying children list does not update during node removal
export default {
onbeforeupdate(now, then){
const keys = now.children.map(({key}) => key)
const departed = then.children.filter(({key}) => !keys.includes(key))
if(!departed.length)
return true
const departures = departed.map(vnode =>
Promise.all([
vnode.state.onbeforeremove,
vnode.attrs.onbeforeremove
].map(fn =>
! fn
? Promise.resolve()
: fn.call(vnode.state, vnode)
))
return Promise.all(departures).then(() => {
this.removing = false
m.redraw()
})
},
view : ({children}) =>
children.map(vnode =>
Object.assign(vnode, {
attrs: Object.assign(
vnode.attrs, {
onbeforeremove: Function.prototype
}
),
state: Object.assign(
vnode.state, {
onbeforeremove: Function.prototype
}
)
})
)
}
export default {
onbeforeupdate(now, then){
if(this.removing)
return false
const keys = now.children.map(({key}) => key)
const departed = then.children.filter(({key}) => !keys.includes(key))
if(!departed.length)
return true
this.removing = true
const departures = departed.map(vnode =>
Promise.all([
vnode.state.onbeforeremove,
vnode.attrs.onbeforeremove
].map(fn =>
! fn
? Promise.resolve()
: fn.call(vnode.state, vnode)
))
Promise.all(departures).then(() => {
this.removing = false
m.redraw()
})
return false
},
view : ({children}) =>
children.map(vnode =>
Object.assign(vnode, {
attrs: Object.assign(
vnode.attrs, {
onbeforeremove: Function.prototype
}
),
state: Object.assign(
vnode.state, {
onbeforeremove: Function.prototype
}
)
})
)
}
},
view : ({children}) =>
children.map(vnode =>
Object.assign(vnode, {
attrs: Object.assign(
vnode.attrs, {
onbeforeremove: Function.prototype
}
),
state: Object.assign(
vnode.state, {
onbeforeremove: Function.prototype
}
)
})
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment