Skip to content

Commit

Permalink
feat(finalize): add higher-order lettable version of finally, called …
Browse files Browse the repository at this point in the history
…finalize
  • Loading branch information
benlesh committed Aug 9, 2017
1 parent 6ec8a19 commit cfeae9f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
28 changes: 3 additions & 25 deletions src/operator/finally.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';
import { Subscription, TeardownLogic } from '../Subscription';

import { Observable } from '../Observable';
import { finalize } from '../operators';

/**
* Returns an Observable that mirrors the source Observable, but will call a specified function when
Expand All @@ -12,26 +11,5 @@ import { Observable } from '../Observable';
* @owner Observable
*/
export function _finally<T>(this: Observable<T>, callback: () => void): Observable<T> {
return this.lift(new FinallyOperator(callback));
}

class FinallyOperator<T> implements Operator<T, T> {
constructor(private callback: () => void) {
}

call(subscriber: Subscriber<T>, source: any): TeardownLogic {
return source.subscribe(new FinallySubscriber(subscriber, this.callback));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class FinallySubscriber<T> extends Subscriber<T> {
constructor(destination: Subscriber<T>, callback: () => void) {
super(destination);
this.add(new Subscription(callback));
}
return finalize(callback)(this);
}
38 changes: 38 additions & 0 deletions src/operators/finalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';
import { Subscription, TeardownLogic } from '../Subscription';
import { Observable } from '../Observable';
import { MonoTypeOperatorFunction } from '../interfaces';

/**
* Returns an Observable that mirrors the source Observable, but will call a specified function when
* the source terminates on complete or error.
* @param {function} callback Function to be called when source terminates.
* @return {Observable} An Observable that mirrors the source, but will call the specified function on termination.
* @method finally
* @owner Observable
*/
export function finalize<T>(callback: () => void): MonoTypeOperatorFunction<T> {
return (source: Observable<T>) => source.lift(new FinallyOperator(callback));
}

class FinallyOperator<T> implements Operator<T, T> {
constructor(private callback: () => void) {
}

call(subscriber: Subscriber<T>, source: any): TeardownLogic {
return source.subscribe(new FinallySubscriber(subscriber, this.callback));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class FinallySubscriber<T> extends Subscriber<T> {
constructor(destination: Subscriber<T>, callback: () => void) {
super(destination);
this.add(new Subscription(callback));
}
}
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export { exhaust } from './exhaust';
export { exhaustMap } from './exhaustMap';
export { expand } from './expand';
export { filter } from './filter';
export { finalize } from './finalize';
export { ignoreElements } from './ignoreElements';
export { map } from './map';
export { materialize } from './materialize';
Expand Down

0 comments on commit cfeae9f

Please sign in to comment.