Guest User

Untitled

a guest
Dec 15th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import { Observable } from 'rxjs/Observable';
  2. import 'rxjs/add/operator/do';
  3. import 'rxjs/add/operator/publishReplay';
  4. import { Subscriber } from 'rxjs/Subscriber';
  5. import { MonoTypeOperatorFunction } from 'rxjs/interfaces';
  6. import { Subscription, TeardownLogic } from 'rxjs/Subscription';
  7. import { Operator } from 'rxjs/Operator';
  8.  
  9. export function debugUnsubscribe<T>(text: string,
  10. thisArg?: any): MonoTypeOperatorFunction<T> {
  11. return function debugUnsubscribeFunction(source: Observable<T>): Observable<T> {
  12. return source.lift(new DebugUnsubscribeOperator(text, thisArg));
  13. };
  14. }
  15.  
  16.  
  17. class DebugUnsubscribeOperator<T> implements Operator<T, T> {
  18.  
  19. constructor(private text: string, private source: Observable<T>) {
  20. }
  21.  
  22. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  23.  
  24. const {text} = this;
  25.  
  26. const debugUnsubscribeSubscriber = new DebugUnsubscribeSubscriber(subscriber, source, text);
  27.  
  28. console.log('subscribing', text);
  29.  
  30. const subscription = source.subscribe(debugUnsubscribeSubscriber);
  31.  
  32. (<any> debugUnsubscribeSubscriber).sub = debugUnsubscribeSubscriber;
  33.  
  34. return subscription;
  35. }
  36. }
  37.  
  38. class DebugUnsubscribeSubscriber<T> extends Subscriber<T> {
  39. private sub: Subscription;
  40.  
  41. constructor(destination: Subscriber<T>,
  42. private source: Observable<T>,
  43. private text: string) {
  44. super(destination);
  45. }
  46.  
  47. protected _unsubscribe() {
  48. const {sub, text} = this;
  49. if (sub) {
  50. sub.unsubscribe();
  51. }
  52. console.log('unsubscribing', text, sub ? 'sucess' : 'fail');
  53. }
  54. }
Add Comment
Please, Sign In to add comment