Skip to content

Commit

Permalink
feat(fromEvent): remove resultSelector
Browse files Browse the repository at this point in the history
- removes resultSelector from function signature to simplify the API

BREAKING CHANGE: result selector removed, use `map` instead: `fromEvent(target, 'click', fn)` becomes `fromEvent(target, 'click').pipe(map(fn))`
  • Loading branch information
benlesh committed Mar 2, 2018
1 parent 31d7a3f commit 197f449
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 134 deletions.
108 changes: 0 additions & 108 deletions spec/observables/fromEvent-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 7 additions & 26 deletions src/internal/observable/fromEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ export type EventListenerOptions = {

/* tslint:disable:max-line-length */
export function fromEvent<T>(target: EventTargetLike, eventName: string): Observable<T>;
export function fromEvent<T>(target: EventTargetLike, eventName: string, selector: (...args: any[]) => T): Observable<T>;
export function fromEvent<T>(target: EventTargetLike, eventName: string): Observable<T>;
export function fromEvent<T>(target: EventTargetLike, eventName: string, options: EventListenerOptions): Observable<T>;
export function fromEvent<T>(target: EventTargetLike, eventName: string, options: EventListenerOptions, selector: (...args: any[]) => T): Observable<T>;
/* tslint:enable:max-line-length */

/**
Expand Down Expand Up @@ -135,35 +134,17 @@ export function fromEvent<T>(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<T>}
* @static true
* @name fromEvent
* @owner Observable
*/
export function fromEvent<T>(target: EventTargetLike,
eventName: string,
options?: EventListenerOptions | ((...args: any[]) => T),
selector?: (...args: any[]) => T): Observable<T> {
if (isFunction(options)) {
selector = options as ((...args: any[]) => T);
options = undefined;
}
export function fromEvent<T>(
target: EventTargetLike,
eventName: string,
options?: EventListenerOptions
): Observable<T> {

return new Observable<T>(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);
});
}
Expand Down

0 comments on commit 197f449

Please sign in to comment.