Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Observable } from 'rxjs/Observable';
- import 'rxjs/add/operator/do';
- import 'rxjs/add/operator/publishReplay';
- import { Subscriber } from 'rxjs/Subscriber';
- import { MonoTypeOperatorFunction } from 'rxjs/interfaces';
- import { Subscription, TeardownLogic } from 'rxjs/Subscription';
- import { Operator } from 'rxjs/Operator';
- export function debugUnsubscribe<T>(text: string,
- thisArg?: any): MonoTypeOperatorFunction<T> {
- return function debugUnsubscribeFunction(source: Observable<T>): Observable<T> {
- return source.lift(new DebugUnsubscribeOperator(text, thisArg));
- };
- }
- class DebugUnsubscribeOperator<T> implements Operator<T, T> {
- constructor(private text: string, private source: Observable<T>) {
- }
- call(subscriber: Subscriber<T>, source: any): TeardownLogic {
- const {text} = this;
- const debugUnsubscribeSubscriber = new DebugUnsubscribeSubscriber(subscriber, source, text);
- console.log('subscribing', text);
- const subscription = source.subscribe(debugUnsubscribeSubscriber);
- (<any> debugUnsubscribeSubscriber).sub = debugUnsubscribeSubscriber;
- return subscription;
- }
- }
- class DebugUnsubscribeSubscriber<T> extends Subscriber<T> {
- private sub: Subscription;
- constructor(destination: Subscriber<T>,
- private source: Observable<T>,
- private text: string) {
- super(destination);
- }
- protected _unsubscribe() {
- const {sub, text} = this;
- if (sub) {
- sub.unsubscribe();
- }
- console.log('unsubscribing', text, sub ? 'sucess' : 'fail');
- }
- }
Add Comment
Please, Sign In to add comment