Skip to content

Commit

Permalink
injected: Use WeakMap instead of Symbol for attributeListener.
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <anders@zulip.com>
  • Loading branch information
andersk committed Apr 3, 2021
1 parent 077f3e6 commit 391c515
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions app/renderer/js/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,30 @@ interface CompatElectronBridge extends ElectronBridge {
function attributeListener<T extends EventTarget>(
type: string,
): PropertyDescriptor {
const symbol = Symbol("on" + type);
const handlers = new WeakMap<T, (event: Event) => 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();
}
}

return {
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);
}
},
};
Expand Down

0 comments on commit 391c515

Please sign in to comment.