Skip to content

Commit

Permalink
Allow element garbage collection
Browse files Browse the repository at this point in the history
  • Loading branch information
luiscastro193 committed Sep 14, 2024
1 parent f45e07f commit 5731365
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ export class AuxEvent {

addListener(element, callback) {
let event = this.event;
if (element.myCallbacks) element.myCallbacks.push(callback);
else element.myCallbacks = [callback];
let references = [new WeakRef(element), new WeakRef(callback)];

event.addEventListener('e', function listener() {
if (document.contains(element))
callback();
let [myElement, myCallback] = references.map(reference => reference.deref());
if (myElement && myCallback && document.contains(myElement))
myCallback();
else
event.removeEventListener('e', listener);
});
Expand Down

0 comments on commit 5731365

Please sign in to comment.