Skip to content

Commit

Permalink
Additional typing and method signatures for Dom event listener methods
Browse files Browse the repository at this point in the history
  • Loading branch information
craigbeck committed Oct 18, 2023
1 parent ec4f389 commit 5342135
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/Dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export type Target =
| Document
| Window;

export type EventListener<T extends Event> = (event: T) => void;

export class Dom {
controller: Controller;
_listeners: DomListener[];
Expand Down Expand Up @@ -36,25 +38,32 @@ export class Dom {
}

// type: string, target: function, listener: boolean
// addListener(type: string, listener: Function, useCapture: boolean): void;
addListener(type: string, target: Target, listener: EventListenerOrEventListenerObject, useCapture?: boolean) {
addListener<T extends Event>(type: string, listener: EventListener<T>, useCapture?: boolean): void;
addListener<T extends Event>(type: string, target: Target, listener?: EventListener<T>, useCapture?: boolean): void;
addListener<T extends Event>(type: string, target: Target | EventListener<T>, listener?: EventListener<T> | boolean, useCapture?: boolean): void {
if (typeof target === 'function') {
// useCapture = listener;
// listener = target;
// target = document;
useCapture = !!(listener as boolean);
listener = target as EventListener<T>;
target = document as Target;
}
const domListener =
(type === 'destroy') ? new DestroyListener(target, listener) :
new DomListener(type, target, listener, useCapture);
const domListener = (type === 'destroy')
? new DestroyListener(target as Target, listener as EventListener<T>)
: new DomListener(type, target as Target, listener as EventListener<T>, useCapture);
if (-1 === this._listenerIndex(domListener)) {
const listeners = this._listeners || this._initListeners();
listeners.push(domListener);
}
domListener.add();
}

on(type: string, target: Target, listener: EventListenerOrEventListenerObject, useCapture?: boolean) {
this.addListener(type, target, listener, useCapture);
on<T extends Event>(type: string, listener: EventListener<T>, useCapture?: boolean): void;
on<T extends Event>(type: string, target: Target, listener: EventListener<T>, useCapture?: boolean): void;
on<T extends Event>(type: string, target: Target | EventListener<T>, listener?: EventListener<T> | boolean, useCapture?: boolean): void {
if (typeof target === 'function') {
listener = target as EventListener<T>;
target = document as Target;
}
this.addListener(type, target as Target, listener as EventListener<T>, useCapture);
}

once(type, target, listener, useCapture) {
Expand All @@ -70,11 +79,11 @@ export class Dom {
this.addListener(type, target, wrappedListener, useCapture);
}

removeListener(type, target, listener, useCapture) {
removeListener<T extends Event>(type: string, target: Target, listener: EventListener<T>, useCapture?: boolean) {
if (typeof target === 'function') {
useCapture = listener;
listener = target;
target = document;
useCapture = !!(listener);
listener = target as EventListener<T>;
target = document as Target;
}
const domListener = new DomListener(type, target, listener, useCapture);
domListener.remove();
Expand Down

0 comments on commit 5342135

Please sign in to comment.