Skip to content

Commit 86960c2

Browse files
committed
feat(fromEventPattern): support optional removeHandler
- relates to #2088
1 parent 01d0622 commit 86960c2

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/observable/FromEventPatternObservable.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isFunction } from '../util/isFunction';
12
import { Observable } from '../Observable';
23
import { Subscription } from '../Subscription';
34
import { Subscriber } from '../Subscriber';
@@ -45,7 +46,7 @@ export class FromEventPatternObservable<T> extends Observable<T> {
4546
* @param {function(handler: Function): any} addHandler A function that takes
4647
* a `handler` function as argument and attaches it somehow to the actual
4748
* source of events.
48-
* @param {function(handler: Function, signal?: any): void} removeHandler A function that
49+
* @param {function(handler: Function, signal?: any): void} [removeHandler] An optional function that
4950
* takes a `handler` function as argument and removes it in case it was
5051
* previously attached using `addHandler`. if addHandler returns signal to teardown when remove,
5152
* removeHandler function will forward it.
@@ -58,13 +59,13 @@ export class FromEventPatternObservable<T> extends Observable<T> {
5859
* @owner Observable
5960
*/
6061
static create<T>(addHandler: (handler: Function) => any,
61-
removeHandler: (handler: Function, signal?: any) => void,
62+
removeHandler?: (handler: Function, signal?: any) => void,
6263
selector?: (...args: Array<any>) => T) {
6364
return new FromEventPatternObservable(addHandler, removeHandler, selector);
6465
}
6566

6667
constructor(private addHandler: (handler: Function) => any,
67-
private removeHandler: (handler: Function, signal?: any) => void,
68+
private removeHandler?: (handler: Function, signal?: any) => void,
6869
private selector?: (...args: Array<any>) => T) {
6970
super();
7071
}
@@ -78,6 +79,10 @@ export class FromEventPatternObservable<T> extends Observable<T> {
7879

7980
const retValue = this._callAddHandler(handler, subscriber);
8081

82+
if (!isFunction(removeHandler)) {
83+
return;
84+
}
85+
8186
subscriber.add(new Subscription(() => {
8287
//TODO: determine whether or not to forward to error handler
8388
removeHandler(handler, retValue) ;

0 commit comments

Comments
 (0)