Skip to content

Commit

Permalink
use IterableWeakMaps/Sets in peristen-listeners and Datex.Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
benStre committed Nov 28, 2023
1 parent 205d103 commit 20ef5ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
8 changes: 3 additions & 5 deletions runtime/pointers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,14 +1082,12 @@ export class Pointer<T = any> extends Ref<T> {
}

static #is_local = true;
static #local_pointers = new Set<WeakRef<Pointer>>();
static #local_pointers = new IterableWeakSet<Pointer>();
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();
Expand Down Expand Up @@ -1148,7 +1146,7 @@ export class Pointer<T = any> extends Ref<T> {

// 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;
Expand Down
21 changes: 6 additions & 15 deletions utils/persistent-listeners.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IterableWeakMap } from "./iterable-weak-map.ts";

const originalAddEventListener = EventTarget.prototype.addEventListener
const originalRemoveEventListener = EventTarget.prototype.removeEventListener
Expand All @@ -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<WeakRef<EventTarget>, {event: string, handler: EventListenerOrEventListenerObject|null, options?: boolean | AddEventListenerOptions}>()
const listeners = new IterableWeakMap<EventTarget, {event: string, handler: EventListenerOrEventListenerObject|null, options?: boolean | AddEventListenerOptions}>()

0 comments on commit 20ef5ec

Please sign in to comment.