Skip to content

Commit

Permalink
refactor(fromEvent): define 'EventTargetLike' to clearfy the type of …
Browse files Browse the repository at this point in the history
…'Observable.fromEvent()'
  • Loading branch information
tetsuharuohzeki committed Jan 27, 2016
1 parent 4ee5f02 commit 6f53dbf
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions src/observable/FromEventObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,59 @@ import {errorObject} from '../util/errorObject';
import {Subscription} from '../Subscription';
import {Subscriber} from '../Subscriber';

export type NodeStyleEventEmmitter = {
addListener: (eventName: string, handler: Function) => void;
removeListener: (eventName: string, handler: Function) => void;
};
function isNodeStyleEventEmmitter(sourceObj: any): sourceObj is NodeStyleEventEmmitter {
return !!sourceObj && typeof sourceObj.addListener === 'function' && typeof sourceObj.removeListener === 'function';
}

export type JQueryStyleEventEmitter = {
on: (eventName: string, handler: Function) => void;
off: (eventName: string, handler: Function) => void;
};
function isJQueryStyleEventEmitter(sourceObj: any): sourceObj is JQueryStyleEventEmitter {
return !!sourceObj && typeof sourceObj.on === 'function' && typeof sourceObj.off === 'function';
}

function isNodeList(sourceObj: any): sourceObj is NodeList {
return !!sourceObj && sourceObj.toString() === '[object NodeList]';
}

function isHTMLCollection(sourceObj: any): sourceObj is HTMLCollection {
return !!sourceObj && sourceObj.toString() === '[object HTMLCollection]';
}

function isEventTarget(sourceObj: any): sourceObj is EventTarget {
return !!sourceObj && typeof sourceObj.addEventListener === 'function' && typeof sourceObj.removeEventListener === 'function';
}

export type EventTargetLike = EventTarget | NodeStyleEventEmmitter | JQueryStyleEventEmitter | NodeList | HTMLCollection;

export class FromEventObservable<T, R> extends Observable<T> {

static create<T>(sourceObj: any, eventName: string, selector?: (...args: Array<any>) => T) {
static create<T>(sourceObj: EventTargetLike, eventName: string, selector?: (...args: Array<any>) => T): Observable<T> {
return new FromEventObservable(sourceObj, eventName, selector);
}

constructor(private sourceObj: any, private eventName: string, private selector?: (...args: Array<any>) => T) {
constructor(private sourceObj: EventTargetLike, private eventName: string, private selector?: (...args: Array<any>) => T) {
super();
}

private static setupSubscription<T>(sourceObj: any, eventName: string, handler: Function, subscriber: Subscriber<T>) {
private static setupSubscription<T>(sourceObj: EventTargetLike, eventName: string, handler: Function, subscriber: Subscriber<T>) {
let unsubscribe: () => void;
let tag = sourceObj.toString();
if (tag === '[object NodeList]' || tag === '[object HTMLCollection]') {
if (isNodeList(sourceObj) || isHTMLCollection(sourceObj)) {
for (let i = 0, len = sourceObj.length; i < len; i++) {
FromEventObservable.setupSubscription(sourceObj[i], eventName, handler, subscriber);
}
} else if (typeof sourceObj.addEventListener === 'function' && typeof sourceObj.removeEventListener === 'function') {
sourceObj.addEventListener(eventName, handler);
unsubscribe = () => sourceObj.removeEventListener(eventName, handler);
} else if (typeof sourceObj.on === 'function' && typeof sourceObj.off === 'function') {
} else if (isEventTarget(sourceObj)) {
sourceObj.addEventListener(eventName, <EventListener>handler);
unsubscribe = () => sourceObj.removeEventListener(eventName, <EventListener>handler);
} else if (isJQueryStyleEventEmitter(sourceObj)) {
sourceObj.on(eventName, handler);
unsubscribe = () => sourceObj.off(eventName, handler);
} else if (typeof sourceObj.addListener === 'function' && typeof sourceObj.removeListener === 'function') {
} else if (isNodeStyleEventEmmitter(sourceObj)) {
sourceObj.addListener(eventName, handler);
unsubscribe = () => sourceObj.removeListener(eventName, handler);
}
Expand Down

0 comments on commit 6f53dbf

Please sign in to comment.