diff --git a/runtime/pointers.ts b/runtime/pointers.ts index ff98a471..b28c94cd 100644 --- a/runtime/pointers.ts +++ b/runtime/pointers.ts @@ -1082,14 +1082,12 @@ export class Pointer extends Ref { } static #is_local = true; - static #local_pointers = new Set>(); + static #local_pointers = new IterableWeakSet(); public static set is_local(local: boolean) { this.#is_local = local; // update pointer ids if no longer local if (!this.#is_local) { - for (const pointerRef of this.#local_pointers) { - const pointer = pointerRef.deref(); - if (!pointer) continue; + for (const pointer of this.#local_pointers) { pointer.id = Pointer.getUniquePointerID(pointer); } this.#local_pointers.clear(); @@ -1148,7 +1146,7 @@ export class Pointer extends Ref { // add to local pointers list if no global endpoint id yet -> update pointer id as soon as global id available if (Pointer.is_local) { - this.#local_pointers.add(new WeakRef(forPointer)) + this.#local_pointers.add(forPointer) } return id; diff --git a/utils/persistent-listeners.ts b/utils/persistent-listeners.ts index 82e1056d..c946c416 100644 --- a/utils/persistent-listeners.ts +++ b/utils/persistent-listeners.ts @@ -1,3 +1,4 @@ +import { IterableWeakMap } from "./iterable-weak-map.ts"; const originalAddEventListener = EventTarget.prototype.addEventListener const originalRemoveEventListener = EventTarget.prototype.removeEventListener @@ -18,31 +19,21 @@ export function overrideEventTargetPrototype() { */ export function addPersistentListener(target: EventTarget, event: string, handler: EventListenerOrEventListenerObject|null, options?: boolean | AddEventListenerOptions) { originalAddEventListener.call(target, event, handler, options) - listeners.set(new WeakRef(target), {event, handler, options}); + listeners.set(target, {event, handler, options}); } export function removePersistentListener(target: EventTarget, event: string, handler: EventListenerOrEventListenerObject|null, options?: boolean | AddEventListenerOptions) { originalRemoveEventListener.call(target, event, handler, options) - for (const [targetRef] of listeners) { - const possibleTarget = targetRef.deref(); - if (!possibleTarget) { - listeners.delete(targetRef); - continue; - } - if (target === possibleTarget) listeners.delete(targetRef); + for (const [possibleTarget] of listeners) { + if (target === possibleTarget) listeners.delete(possibleTarget); } } export function recreatePersistentListeners() { - for (const [targetRef, {event, handler, options}] of listeners) { - const target = targetRef.deref(); - if (!target) { - listeners.delete(targetRef); - continue; - } + for (const [target, {event, handler, options}] of listeners) { console.debug("recreated a persistent event listener for '" + event + "'") originalAddEventListener.call(target, event, handler, options) } } -const listeners = new Map, {event: string, handler: EventListenerOrEventListenerObject|null, options?: boolean | AddEventListenerOptions}>() +const listeners = new IterableWeakMap()