diff --git a/src/Dom.ts b/src/Dom.ts index da6c3b3e..7e32e8f3 100644 --- a/src/Dom.ts +++ b/src/Dom.ts @@ -5,6 +5,8 @@ export type Target = | Document | Window; +export type EventListener = (event: T) => void; + export class Dom { controller: Controller; _listeners: DomListener[]; @@ -36,16 +38,17 @@ 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(type: string, listener: EventListener, useCapture?: boolean): void; + addListener(type: string, target: Target, listener?: EventListener, useCapture?: boolean): void; + addListener(type: string, target: Target | EventListener, listener?: EventListener | boolean, useCapture?: boolean): void { if (typeof target === 'function') { - // useCapture = listener; - // listener = target; - // target = document; + useCapture = !!(listener as boolean); + listener = target as EventListener; + 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) + : new DomListener(type, target as Target, listener as EventListener, useCapture); if (-1 === this._listenerIndex(domListener)) { const listeners = this._listeners || this._initListeners(); listeners.push(domListener); @@ -53,8 +56,14 @@ export class Dom { domListener.add(); } - on(type: string, target: Target, listener: EventListenerOrEventListenerObject, useCapture?: boolean) { - this.addListener(type, target, listener, useCapture); + on(type: string, listener: EventListener, useCapture?: boolean): void; + on(type: string, target: Target, listener: EventListener, useCapture?: boolean): void; + on(type: string, target: Target | EventListener, listener?: EventListener | boolean, useCapture?: boolean): void { + if (typeof target === 'function') { + listener = target as EventListener; + target = document as Target; + } + this.addListener(type, target as Target, listener as EventListener, useCapture); } once(type, target, listener, useCapture) { @@ -70,11 +79,11 @@ export class Dom { this.addListener(type, target, wrappedListener, useCapture); } - removeListener(type, target, listener, useCapture) { + removeListener(type: string, target: Target, listener: EventListener, useCapture?: boolean) { if (typeof target === 'function') { - useCapture = listener; - listener = target; - target = document; + useCapture = !!(listener); + listener = target as EventListener; + target = document as Target; } const domListener = new DomListener(type, target, listener, useCapture); domListener.remove();