-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(finalize): add higher-order lettable version of finally, called …
…finalize
- Loading branch information
Showing
3 changed files
with
42 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters