Re: [w3c/webcomponents] [feature request] change when upgrade/connected happens during parsing, so that it is the first event in the following microtask (#787)

My assumption is that I **_need_** to clean up the observer by calling `disconnect()` on it when I no longer need it. Is this true?

If we don't have to call `disconnect()` and can rest assured that it will be GC'ed when there's no references to the Node, Node's children, the observer, the observer callback functions, or anything in the observer callback functions' scopes, then the above example could be simplified to the following:

```js
import Class from 'lowclass'
import Mixin from '../core/Mixin'
import { observeChildren } from '../core/Utility'

export default
Mixin(Base => Class('WithChildren').extends(Base, ({ Super, Private, Public }) => ({
    isConnected: false,

    constructor(...args) {
        const self = Super(this).constructor(...args)
        Private(self).__createObserver()
        return self
    },

    connectedCallback() {
        this.isConnected = true
        Super(this).connectedCallback && Super(this).connectedCallback()

        // We have to manually trigger childConnectedCallbacks on connect.
        const currentChildren = this.children

        // NOTE! Luckily Promise.resolve() fires AFTER all children connectedCallbacks,
        // which makes it similar to the MutationObserver events!
        Promise.resolve().then(() => {
            for (let l=currentChildren.length, i=0; i<l; i+=1) {
                this.childConnectedCallback && this.childConnectedCallback(currentChildren[i])
            }
        })
    },

    disconnectedCallback() {
        this.isConnected = false
        Super(this).disconnectedCallback && Super(this).disconnectedCallback()

        // Here we have to manually trigger childDisconnectedCallbacks on disconnect
        const lastKnownChildren = this.children

        // NOTE! Luckily Promise.resolve() fires AFTER all children disconnectedCallbacks,
        // which makes it similar to the MutationObserver events!
        Promise.resolve().then(() => {
            for (let l=lastKnownChildren.length, i=0; i<l; i+=1) {
                this.childDisconnectedCallback && this.childDisconnectedCallback(lastKnownChildren[i])
            }
        })
    },

    private: {
        __createObserver() {
            const self = Public(this)

            observeChildren(
                self,
                child => {
                    if (!self.isConnected) return
                    self.childConnectedCallback && self.childConnectedCallback(child)
                },
                child => {
                    if (!self.isConnected) return
                    self.childDisconnectedCallback && self.childDisconnectedCallback(child)
                },
                true
            )
        },
    },
})))
```

Will this work? Can we expect things to be garbage collected when the node is not used anymore?

-- 
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/787#issuecomment-459999420

Received on Saturday, 2 February 2019 21:10:59 UTC