0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-30 09:08:00 -04:00
denoland-deno/js/event_target.ts
Ryan Dahl 9dfebbc949
Fix eslint warnings (#2151)
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Co-authored-by: LE GOFF Vincent <g_n_s@hotmail.fr>
2019-04-21 16:40:10 -04:00

60 lines
2 KiB
TypeScript

// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as domTypes from "./dom_types";
import { requiredArguments, hasOwnProperty } from "./util";
/* TODO: This is an incomplete implementation to provide functionality
* for Event. A proper spec is still required for a proper Web API.
*/
export class EventTarget implements domTypes.EventTarget {
public listeners: {
[type in string]: domTypes.EventListenerOrEventListenerObject[]
} = {};
public addEventListener(
type: string,
listener: domTypes.EventListenerOrEventListenerObject | null,
_options?: boolean | domTypes.AddEventListenerOptions
): void {
requiredArguments("EventTarget.addEventListener", arguments.length, 2);
if (!hasOwnProperty(this.listeners, type)) {
this.listeners[type] = [];
}
if (listener !== null) {
this.listeners[type].push(listener);
}
}
public removeEventListener(
type: string,
callback: domTypes.EventListenerOrEventListenerObject | null,
_options?: domTypes.EventListenerOptions | boolean
): void {
requiredArguments("EventTarget.removeEventListener", arguments.length, 2);
if (hasOwnProperty(this.listeners, type) && callback !== null) {
this.listeners[type] = this.listeners[type].filter(
(listener): boolean => listener !== callback
);
}
}
public dispatchEvent(event: domTypes.Event): boolean {
requiredArguments("EventTarget.dispatchEvent", arguments.length, 1);
if (!hasOwnProperty(this.listeners, event.type)) {
return true;
}
const stack = this.listeners[event.type].slice();
for (const stackElement of stack) {
if ((stackElement as domTypes.EventListenerObject).handleEvent) {
(stackElement as domTypes.EventListenerObject).handleEvent(event);
} else {
(stackElement as domTypes.EventListener).call(this, event);
}
}
return !event.defaultPrevented;
}
get [Symbol.toStringTag](): string {
return "EventTarget";
}
}