Skip to content

Commit

Permalink
feat(fromEventPattern): removed resultSelector
Browse files Browse the repository at this point in the history
BREAKING CHANGE: no longer supports a result selector, use `map` instead: `fromEventPattern(fn1, fn2, fn3)` becomes `fromEventPattern(fn1, fn2).pipe(map(fn3))`
  • Loading branch information
benlesh committed Mar 2, 2018
1 parent 30350cd commit 6b34f9f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 79 deletions.
61 changes: 0 additions & 61 deletions spec/observables/fromEventPattern-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,65 +70,4 @@ describe('fromEventPattern', () => {
done();
}, () => done(new Error('should not be called')));
});

it('should accept a selector that maps outgoing values', (done: MochaDone) => {
let target;
const trigger = function (...args) {
if (target) {
target.apply(null, arguments);
}
};

const addHandler = (handler: any) => {
target = handler;
};
const removeHandler = (handler: any) => {
target = null;
};
const selector = (a: any, b: any) => {
return a + b + '!';
};

fromEventPattern(addHandler, removeHandler, selector).take(1)
.subscribe((x: any) => {
expect(x).to.equal('testme!');
}, (err: any) => {
done(new Error('should not be called'));
}, () => {
done();
});

trigger('test', 'me');
});

it('should send errors in the selector down the error path', (done: MochaDone) => {
let target;
const trigger = (value: any) => {
if (target) {
target(value);
}
};

const addHandler = (handler: any) => {
target = handler;
};
const removeHandler = (handler: any) => {
target = null;
};
const selector = (x: any) => {
throw 'bad';
};

fromEventPattern(addHandler, removeHandler, selector)
.subscribe((x: any) => {
done(new Error('should not be called'));
}, (err: any) => {
expect(err).to.equal('bad');
done();
}, () => {
done(new Error('should not be called'));
});

trigger('test');
});
});
21 changes: 3 additions & 18 deletions src/internal/observable/fromEventPattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { isFunction } from '../util/isFunction';
* <img src="./img/fromEventPattern.png" width="100%">
*
* Creates an Observable by using the `addHandler` and `removeHandler`
* functions to add and remove the handlers, with an optional selector
* function to project the event arguments to a result. The `addHandler` is
* functions to add and remove the handlers. The `addHandler` is
* called when the output Observable is subscribed, and `removeHandler` is
* called when the Subscription is unsubscribed.
*
Expand Down Expand Up @@ -41,29 +40,15 @@ import { isFunction } from '../util/isFunction';
* takes a `handler` function as argument and removes it in case it was
* previously attached using `addHandler`. if addHandler returns signal to teardown when remove,
* removeHandler function will forward it.
* @param {function(...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>}
* @name fromEventPattern
*/
export function fromEventPattern<T>(addHandler: (handler: Function) => any,
removeHandler?: (handler: Function, signal?: any) => void,
selector?: (...args: any[]) => T) {
removeHandler?: (handler: Function, signal?: any) => void) {
return new Observable<T>(subscriber => {
const handler = selector ? (...args: any[]) => {
let result: T;
try {
result = selector(...args);
} catch (err) {
subscriber.error(err);
return;
}
subscriber.next(result);
} : (e: T) => subscriber.next(e);
const handler = (e: T) => subscriber.next(e);

let retValue: any;

try {
retValue = addHandler(handler);
} catch (err) {
Expand Down

0 comments on commit 6b34f9f

Please sign in to comment.