Skip to content

Commit 391c515

Browse files
committed
injected: Use WeakMap instead of Symbol for attributeListener.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 077f3e6 commit 391c515

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

app/renderer/js/injected.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,30 @@ interface CompatElectronBridge extends ElectronBridge {
3737
function attributeListener<T extends EventTarget>(
3838
type: string,
3939
): PropertyDescriptor {
40-
const symbol = Symbol("on" + type);
40+
const handlers = new WeakMap<T, (event: Event) => unknown>();
4141

42-
function listener(this: T, ev: Event): void {
43-
if ((this as any)[symbol].call(this, ev) === false) {
44-
ev.preventDefault();
42+
function listener(this: T, event: Event): void {
43+
if (handlers.get(this)!.call(this, event) === false) {
44+
event.preventDefault();
4545
}
4646
}
4747

4848
return {
4949
configurable: true,
5050
enumerable: true,
5151
get(this: T) {
52-
return (this as any)[symbol];
52+
return handlers.get(this);
5353
},
5454
set(this: T, value: unknown) {
5555
if (typeof value === "function") {
56-
if (!(symbol in this)) {
56+
if (!handlers.has(this)) {
5757
this.addEventListener(type, listener);
5858
}
5959

60-
(this as any)[symbol] = value;
61-
} else if (symbol in this) {
60+
handlers.set(this, value as (event: Event) => unknown);
61+
} else if (handlers.has(this)) {
6262
this.removeEventListener(type, listener);
63-
delete (this as any)[symbol];
63+
handlers.delete(this);
6464
}
6565
},
6666
};

0 commit comments

Comments
 (0)