- From: Carlos Lopez <notifications@github.com>
- Date: Fri, 09 Apr 2021 07:44:47 -0700
- To: whatwg/dom <dom@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/dom/issues/583/816734465@github.com>
My concerns with a getter/setter is a hard reference could leave an object in memory, prohibiting it from being garbage collection. With DOM nodes, once a child is detached from its parent, `.parentNode` becomes `null` (or `undefined`, not sure). The parent can then be garbage collected if it's no longer referenced. That's one of the reasons why I leaned to a function.
The other idea I had with my custom implementation is a EventTarget parent registry of sorts. Basically, instead of the child explicitly stating what it's parent is, a global registry can be used. eg: `parent = EventTargetParentRegistry.get(child)`. Here, a user agent can basically keep a dictionary of sorts since no child can have multiple parents. 
A pure JavaScript solution (polyfill) could look something like this:
````js
class EventTargetParentRegistry {
  /** @type {WeakMap<EventTarget, WeakRef<EventTarget>>} */
  static #registry = new WeakMap();
  static get(child) {
    return this.#registry.get(child)?.deref();
  }
  static set(parent, child) {
    this.#registry.set(child, new WeakRef(parent));
  }
  static delete(child) {
    this.#registry.delete(child);
  }
}
````
-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/583#issuecomment-816734465
Received on Friday, 9 April 2021 14:45:00 UTC