dom-event-utils
Advanced tools
Comparing version 1.0.3 to 1.1.0
export declare type Callback = (...args: any[]) => void; | ||
export declare type UnsubscribeFunction = VoidFunction; | ||
export interface ITarget { | ||
addEventListener(type: string, listener: any): void; | ||
removeEventListener(type: string, listener: any): void; | ||
export interface IEmitter { | ||
addEventListener?(type: string, listener: any): void; | ||
removeEventListener?(type: string, listener: any): void; | ||
addListener?(type: string, listener: any): void; | ||
removeListener?(type: string, listener: any): void; | ||
on?(type: string, listener: any): void; | ||
off?(type: string, listener: any): void; | ||
} | ||
export declare type Executor<T> = (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void; | ||
export declare function timedPromise<T>(fn: Executor<T>, timeout: number): Promise<T>; | ||
export declare function eventToPromise<T>(target: ITarget, eventName: string, fn: Callback, timeout?: number): Promise<T>; | ||
export declare function on(target: ITarget, eventName: string, fn: Callback): UnsubscribeFunction; | ||
export declare function once(target: ITarget, eventName: string, fn: Callback): UnsubscribeFunction; | ||
export declare function eventToPromise<T>(target: IEmitter, eventName: string, fn: Callback, timeout?: number): Promise<T>; | ||
export declare function on(target: IEmitter, eventName: string, fn: Callback): VoidFunction; | ||
export declare function once(target: IEmitter, eventName: string, fn: Callback): VoidFunction; |
@@ -34,4 +34,9 @@ "use strict"; | ||
} | ||
fn.apply(null, args); | ||
resolve(); | ||
var ret = fn.apply(null, args); | ||
if (ret && ret.then) { | ||
return ret.then(resolve); | ||
} | ||
else { | ||
resolve(); | ||
} | ||
}); | ||
@@ -42,5 +47,5 @@ }, timeout); | ||
function on(target, eventName, fn) { | ||
target.addEventListener(eventName, fn); | ||
callOn(target, eventName, fn); | ||
return function () { | ||
target.removeEventListener(eventName, fn); | ||
callOff(target, eventName, fn); | ||
}; | ||
@@ -61,1 +66,30 @@ } | ||
exports.once = once; | ||
// adapt eventemitter3 and node.js style event emitters too | ||
function callOn(target, eventName, fn) { | ||
if (target.on) { | ||
target.on(eventName, fn); | ||
} | ||
else if (target.addEventListener) { | ||
target.addEventListener(eventName, fn); | ||
} | ||
else if (target.addListener) { | ||
target.addListener(eventName, fn); | ||
} | ||
else { | ||
throw new TypeError("Cannot find \"on\"-like method on event emitter, emitting event \"" + eventName + "\""); | ||
} | ||
} | ||
function callOff(target, eventName, fn) { | ||
if (target.off) { | ||
target.off(eventName, fn); | ||
} | ||
else if (target.removeEventListener) { | ||
target.removeEventListener(eventName, fn); | ||
} | ||
else if (target.removeListener) { | ||
target.removeListener(eventName, fn); | ||
} | ||
else { | ||
throw new TypeError("Cannot find \"off\"-like method on event emitter, emitting event \"" + eventName + "\""); | ||
} | ||
} |
{ | ||
"name": "dom-event-utils", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Very small library that makes working with DOM event emitters API a little easier to work with", | ||
@@ -9,3 +9,4 @@ "main": "dist/index.js", | ||
"build": "tsc", | ||
"test": "jest --config=jest.config.json" | ||
"test": "jest --config=jest.config.json", | ||
"prepublish": "npm test" | ||
}, | ||
@@ -21,2 +22,3 @@ "keywords": [ | ||
"@types/jest": "^22.1.4", | ||
"@types/node": "^9.4.6", | ||
"jest": "^22.4.2", | ||
@@ -23,0 +25,0 @@ "ts-jest": "^22.4.1", |
export type Callback = (...args: any[]) => void | ||
// just alias it for nicer type hints | ||
export type UnsubscribeFunction = VoidFunction | ||
// minimal representation of an event emitter | ||
export interface ITarget { | ||
addEventListener(type: string, listener: any): void | ||
removeEventListener(type: string, listener: any): void | ||
// minimal representation of methods we need from an event emitter | ||
export interface IEmitter { | ||
addEventListener?(type: string, listener: any): void | ||
removeEventListener?(type: string, listener: any): void | ||
addListener?(type: string, listener: any): void | ||
removeListener?(type: string, listener: any): void | ||
on?(type: string, listener: any): void | ||
off?(type: string, listener: any): void | ||
} | ||
@@ -42,3 +43,3 @@ | ||
export function eventToPromise<T>(target: ITarget, eventName: string, fn: Callback, timeout?: number): Promise<T> { | ||
export function eventToPromise<T>(target: IEmitter, eventName: string, fn: Callback, timeout?: number): Promise<T> { | ||
return makeTimedOrNormalPromise<T>((resolve) => { | ||
@@ -56,10 +57,10 @@ once(target, eventName, (...args) => { | ||
export function on(target: ITarget, eventName: string, fn: Callback): UnsubscribeFunction { | ||
target.addEventListener(eventName, fn); | ||
export function on(target: IEmitter, eventName: string, fn: Callback): VoidFunction { | ||
callOn(target, eventName, fn); | ||
return () => { | ||
target.removeEventListener(eventName, fn); | ||
callOff(target, eventName, fn); | ||
}; | ||
} | ||
export function once(target: ITarget, eventName: string, fn: Callback): UnsubscribeFunction { | ||
export function once(target: IEmitter, eventName: string, fn: Callback): VoidFunction { | ||
const off = on(target, eventName, (...args) => { | ||
@@ -71,1 +72,32 @@ fn.apply(null, args); | ||
} | ||
// adapt eventemitter3 and node.js style event emitters too | ||
function callOn(target: IEmitter, eventName: string, fn: Callback) { | ||
if (target.on) { | ||
target.on(eventName, fn); | ||
} | ||
else if (target.addEventListener) { | ||
target.addEventListener(eventName, fn); | ||
} | ||
else if (target.addListener) { | ||
target.addListener(eventName, fn); | ||
} | ||
else { | ||
throw new TypeError(`Cannot find "on"-like method on event emitter, emitting event "${eventName}"`); | ||
} | ||
} | ||
function callOff(target: IEmitter, eventName: string, fn: Callback) { | ||
if (target.off) { | ||
target.off(eventName, fn); | ||
} | ||
else if (target.removeEventListener) { | ||
target.removeEventListener(eventName, fn); | ||
} | ||
else if (target.removeListener) { | ||
target.removeListener(eventName, fn); | ||
} | ||
else { | ||
throw new TypeError(`Cannot find "off"-like method on event emitter, emitting event "${eventName}"`); | ||
} | ||
} |
13397
251
5