summaryrefslogtreecommitdiff
path: root/cli/js/event_target.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2020-03-05 08:36:13 -0500
committerGitHub <noreply@github.com>2020-03-05 08:36:13 -0500
commitc850b258b4e2487b0456a95cd6b65c169ffb9de2 (patch)
treed631f6995c048b0c7cddb600d4f33297922ba5b1 /cli/js/event_target.ts
parent54a1688868810af0ef16f5c186dfb839f2fce23f (diff)
Support async function and EventListenerObject as listeners (#4240)
Diffstat (limited to 'cli/js/event_target.ts')
-rw-r--r--cli/js/event_target.ts25
1 files changed, 13 insertions, 12 deletions
diff --git a/cli/js/event_target.ts b/cli/js/event_target.ts
index daa73eb23..03557526a 100644
--- a/cli/js/event_target.ts
+++ b/cli/js/event_target.ts
@@ -25,7 +25,7 @@ export const eventTargetHasActivationBehavior: unique symbol = Symbol();
export class EventTarget implements domTypes.EventTarget {
public [domTypes.eventTargetHost]: domTypes.EventTarget | null = null;
public [domTypes.eventTargetListeners]: {
- [type in string]: domTypes.EventListener[];
+ [type in string]: domTypes.EventTargetListener[];
} = {};
public [domTypes.eventTargetMode] = "";
public [domTypes.eventTargetNodeType]: domTypes.NodeType =
@@ -35,7 +35,7 @@ export class EventTarget implements domTypes.EventTarget {
public addEventListener(
type: string,
- callback: (event: domTypes.Event) => void | null,
+ callback: domTypes.EventListenerOrEventListenerObject | null,
options?: domTypes.AddEventListenerOptions | boolean
): void {
const this_ = this || globalThis;
@@ -68,20 +68,15 @@ export class EventTarget implements domTypes.EventTarget {
}
}
- // eslint-disable-next-line @typescript-eslint/no-this-alias
- const eventTarget = this;
listeners[type].push({
callback,
- options: normalizedOptions,
- handleEvent(event: domTypes.Event): void {
- this.callback.call(eventTarget, event);
- }
- } as domTypes.EventListener);
+ options: normalizedOptions
+ });
}
public removeEventListener(
type: string,
- callback: (event: domTypes.Event) => void | null,
+ callback: domTypes.EventListenerOrEventListenerObject | null,
options?: domTypes.EventListenerOptions | boolean
): void {
const this_ = this || globalThis;
@@ -359,7 +354,7 @@ const eventTargetHelpers = {
innerInvokeEventListeners(
targetImpl: EventTarget,
eventImpl: domTypes.Event,
- targetListeners: { [type in string]: domTypes.EventListener[] }
+ targetListeners: { [type in string]: domTypes.EventTargetListener[] }
): boolean {
let found = false;
@@ -413,7 +408,13 @@ const eventTargetHelpers = {
}
try {
- listener.handleEvent(eventImpl);
+ if (typeof listener.callback === "object") {
+ if (typeof listener.callback.handleEvent === "function") {
+ listener.callback.handleEvent(eventImpl);
+ }
+ } else {
+ listener.callback.call(eventImpl.currentTarget, eventImpl);
+ }
} catch (error) {
// TODO(bartlomieju): very likely that different error
// should be thrown here (DOMException?)