From cfeae9f48659997ed833f17a0f21b6c9fae6af97 Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Tue, 8 Aug 2017 21:06:18 -0700 Subject: [PATCH] feat(finalize): add higher-order lettable version of finally, called finalize --- src/operator/finally.ts | 28 +++------------------------- src/operators/finalize.ts | 38 ++++++++++++++++++++++++++++++++++++++ src/operators/index.ts | 1 + 3 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 src/operators/finalize.ts diff --git a/src/operator/finally.ts b/src/operator/finally.ts index 269858a580..bd944f81f1 100644 --- a/src/operator/finally.ts +++ b/src/operator/finally.ts @@ -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 @@ -12,26 +11,5 @@ import { Observable } from '../Observable'; * @owner Observable */ export function _finally(this: Observable, callback: () => void): Observable { - return this.lift(new FinallyOperator(callback)); -} - -class FinallyOperator implements Operator { - constructor(private callback: () => void) { - } - - call(subscriber: Subscriber, source: any): TeardownLogic { - return source.subscribe(new FinallySubscriber(subscriber, this.callback)); - } -} - -/** - * We need this JSDoc comment for affecting ESDoc. - * @ignore - * @extends {Ignored} - */ -class FinallySubscriber extends Subscriber { - constructor(destination: Subscriber, callback: () => void) { - super(destination); - this.add(new Subscription(callback)); - } + return finalize(callback)(this); } diff --git a/src/operators/finalize.ts b/src/operators/finalize.ts new file mode 100644 index 0000000000..cd5d905591 --- /dev/null +++ b/src/operators/finalize.ts @@ -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(callback: () => void): MonoTypeOperatorFunction { + return (source: Observable) => source.lift(new FinallyOperator(callback)); +} + +class FinallyOperator implements Operator { + constructor(private callback: () => void) { + } + + call(subscriber: Subscriber, source: any): TeardownLogic { + return source.subscribe(new FinallySubscriber(subscriber, this.callback)); + } +} + +/** + * We need this JSDoc comment for affecting ESDoc. + * @ignore + * @extends {Ignored} + */ +class FinallySubscriber extends Subscriber { + constructor(destination: Subscriber, callback: () => void) { + super(destination); + this.add(new Subscription(callback)); + } +} diff --git a/src/operators/index.ts b/src/operators/index.ts index 1cc743c473..4ef6ee0338 100644 --- a/src/operators/index.ts +++ b/src/operators/index.ts @@ -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';