From 391c5157799a69d195b870ce6bd582bf4f329734 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sat, 3 Apr 2021 15:38:52 -0700 Subject: [PATCH] injected: Use WeakMap instead of Symbol for attributeListener. Signed-off-by: Anders Kaseorg --- app/renderer/js/injected.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app/renderer/js/injected.ts b/app/renderer/js/injected.ts index fa56aefc0..f7c958dc7 100644 --- a/app/renderer/js/injected.ts +++ b/app/renderer/js/injected.ts @@ -37,11 +37,11 @@ interface CompatElectronBridge extends ElectronBridge { function attributeListener( type: string, ): PropertyDescriptor { - const symbol = Symbol("on" + type); + const handlers = new WeakMap unknown>(); - function listener(this: T, ev: Event): void { - if ((this as any)[symbol].call(this, ev) === false) { - ev.preventDefault(); + function listener(this: T, event: Event): void { + if (handlers.get(this)!.call(this, event) === false) { + event.preventDefault(); } } @@ -49,18 +49,18 @@ interface CompatElectronBridge extends ElectronBridge { configurable: true, enumerable: true, get(this: T) { - return (this as any)[symbol]; + return handlers.get(this); }, set(this: T, value: unknown) { if (typeof value === "function") { - if (!(symbol in this)) { + if (!handlers.has(this)) { this.addEventListener(type, listener); } - (this as any)[symbol] = value; - } else if (symbol in this) { + handlers.set(this, value as (event: Event) => unknown); + } else if (handlers.has(this)) { this.removeEventListener(type, listener); - delete (this as any)[symbol]; + handlers.delete(this); } }, };