diff --git a/spec/observables/fromEvent-spec.ts b/spec/observables/fromEvent-spec.ts index ec2dde09b8..4f599252aa 100644 --- a/spec/observables/fromEvent-spec.ts +++ b/spec/observables/fromEvent-spec.ts @@ -178,114 +178,6 @@ describe('fromEvent', () => { send('test'); }); - it('should pass through events that occur and use the selector if provided', (done: MochaDone) => { - let send; - const obj = { - on: (name: string, handler: Function) => { - send = handler; - }, - off: () => { - //noop - } - }; - - function selector(x) { - return x + '!'; - } - - fromEvent(obj, 'click', selector).take(1) - .subscribe((e: any) => { - expect(e).to.equal('test!'); - }, (err: any) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - - send('test'); - }); - - it('should not fail if no event arguments are passed and the selector does not return', (done: MochaDone) => { - let send; - const obj = { - on: (name: string, handler: Function) => { - send = handler; - }, - off: () => { - //noop - } - }; - - function selector() { - //noop - } - - fromEvent(obj, 'click', selector).take(1) - .subscribe((e: any) => { - expect(e).not.exist; - }, (err: any) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - - send(); - }); - - it('should return a value from the selector if no event arguments are passed', (done: MochaDone) => { - let send; - const obj = { - on: (name: string, handler: Function) => { - send = handler; - }, - off: () => { - //noop - } - }; - - function selector() { - return 'no arguments'; - } - - fromEvent(obj, 'click', selector).take(1) - .subscribe((e: any) => { - expect(e).to.equal('no arguments'); - }, (err: any) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - - send(); - }); - - it('should pass multiple arguments to selector from event emitter', (done: MochaDone) => { - let send; - const obj = { - on: (name: string, handler: Function) => { - send = handler; - }, - off: () => { - //noop - } - }; - - function selector(x, y, z) { - return [].slice.call(arguments); - } - - fromEvent(obj, 'click', selector).take(1) - .subscribe((e: any) => { - expect(e).to.deep.equal([1, 2, 3]); - }, (err: any) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - - send(1, 2, 3); - }); - it('should not throw an exception calling toString on obj with a null prototype', (done: MochaDone) => { // NOTE: Can not test with Object.create(null) or `class Foo extends null` // due to TypeScript bug. https://github.com/Microsoft/TypeScript/issues/1108 diff --git a/src/internal/observable/fromEvent.ts b/src/internal/observable/fromEvent.ts index 4d7198ba0a..96e8c954d4 100644 --- a/src/internal/observable/fromEvent.ts +++ b/src/internal/observable/fromEvent.ts @@ -22,9 +22,8 @@ export type EventListenerOptions = { /* tslint:disable:max-line-length */ export function fromEvent(target: EventTargetLike, eventName: string): Observable; -export function fromEvent(target: EventTargetLike, eventName: string, selector: (...args: any[]) => T): Observable; +export function fromEvent(target: EventTargetLike, eventName: string): Observable; export function fromEvent(target: EventTargetLike, eventName: string, options: EventListenerOptions): Observable; -export function fromEvent(target: EventTargetLike, eventName: string, options: EventListenerOptions, selector: (...args: any[]) => T): Observable; /* tslint:enable:max-line-length */ /** @@ -135,35 +134,17 @@ export function fromEvent(target: EventTargetLike, eventName: string, options * @param {string} eventName The event name of interest, being emitted by the * `target`. * @param {EventListenerOptions} [options] Options to pass through to addEventListener - * @param {(...args: any[]) => T} [selector] An optional function to - * post-process results. It takes the arguments from the event handler and - * should return a single value. * @return {Observable} - * @static true * @name fromEvent - * @owner Observable */ -export function fromEvent(target: EventTargetLike, - eventName: string, - options?: EventListenerOptions | ((...args: any[]) => T), - selector?: (...args: any[]) => T): Observable { - if (isFunction(options)) { - selector = options as ((...args: any[]) => T); - options = undefined; - } +export function fromEvent( + target: EventTargetLike, + eventName: string, + options?: EventListenerOptions +): Observable { return new Observable(subscriber => { - const handler = selector ? (...args: any[]) => { - let result: any; - try { - result = selector(...args); - } catch (err) { - subscriber.error(err); - return; - } - subscriber.next(result); - } : (e: any) => subscriber.next(e); - + const handler = (e: T) => subscriber.next(e); setupSubscription(target, eventName, handler, subscriber, options as EventListenerOptions); }); }