Re: [w3c/webcomponents] How to avoid memory leaks (cleanup promises) when detecting custom elements? (#674)

@tomalec Actually, the problem is that I am letting users define the element names. That way they have controls of the names they use in their DOM incase there's ever a conflict with some other library, or etc. So I myself (as author of my library) am not specifying the element names unless the user calls an API of mine that will use default element names. But the user has the option to skip use of the API and name them anything they want.

F.e.:

```js
import {useDefaultNames} from 'joes-lib'

useDefaultNames() // registers elements with default names defined in the library.
```

They don't have to use that function, they can import and register the classes themselves.

So because I don't necessarily control the element names, this is the code I have at the moment in order to try and give them a useful Error message in the console:

```js
        async _checkElementIsLibraryElement(element) {
            if ( element.nodeName.includes('-') ) {
                const whenDefined = customElements.whenDefined(element.nodeName.toLowerCase())
                    .then(() => {
                        if (element instanceof Mesh) return true
                        else return false
                    })
                const timeout = new Promise(r => setTimeout(r, 10000))

                const isMesh = await Promise.race([whenDefined, timeout])

                if (!isMesh) throw new Error(`
                    Either The element you're using the mesh behavior on is not a Mesh
                    element, or there was a timeout waiting for the Mesh element definition
                    after 10 seconds.
                `)
            }
        },
```

As you can see, in this case if the `timeout` wins the race, then the `whenDefined` promise will be left dangling (though hopefully the error will lead to them fixing the situation).

---

Another case could be a name conflict, someone may want to unregister them (and not be leaking memory).

---

In general, I think `Promise`s just need a way to be cleaned up (cancelled, or at least unlistened to without affecting other listeners).

-- 
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/674#issuecomment-445033546

Received on Thursday, 6 December 2018 21:21:24 UTC