- From: Joseph Orbegoso Pea <notifications@github.com>
- Date: Sat, 11 Mar 2017 13:35:47 -0800
- To: w3c/webcomponents <webcomponents@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
Received on Saturday, 11 March 2017 21:36:20 UTC
This is how my base class implements `childConnectedCallback`/`childDisconnectedCallback `:
```js
// NOTE: If a child is disconnected then connected to the same parent in the
// same turn, then the onConnect and onDisconnect callbacks won't be called
// because the DOM tree will be back in the exact state as before.
function observeChildren(ctx, onConnect, onDisconnect) {
const observer = new MutationObserver(changes => {
const weights = new Map
for (const change of changes) {
if (change.type != 'childList') continue
for (const addedNode of change.addedNodes)
weights.set(addedNode, (weights.get(addedNode) || 0) + 1)
for (const removedNode of change.removedNodes)
weights.set(removedNode, (weights.get(removedNode) || 0) - 1)
}
for (const [node, weight] of weights)
if (weight > 0)
onConnect.call(ctx, node)
else if (weight < 0)
onDisconnect.call(ctx, node)
})
observer.observe(ctx, { childList: true })
return observer
}
// ... then inside the base class ...
observeChildren(this, this.childConnectedCallback, this.childDisconnectedCallback)
```
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webcomponents/issues/629#issuecomment-285902044
Received on Saturday, 11 March 2017 21:36:20 UTC