Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Track all observable subscriptions

Exmaple usage:

  • Add this piece of code in your angular application's main.ts, above the bootstrap call
  • Execute subscriptions.clear() in the console, because a lot of subscriptions are created at bootstrap time
  • Trigger the actions you want to investigate
  • Copy the subscriptions map to prevent changes while you're investigating
  • Investigate
import {Observable, Subscription} from 'rxjs';
declare const subscriptions: Map<Observable<any>, Subscription[]>;
(window as any).subscriptions = new Map<Observable<any>, Subscription[]>();
Observable.prototype.subscribe = (function (_super) {
return function(this: Observable<any>, ...args: any[]) {
const subscription = _super.apply(this, args)
const list = subscriptions.get(this);
if (list != null) {
list.push(subscription);
} else {
subscriptions.set(this, [subscription]);
}
return subscription;
};
})(Observable.prototype.subscribe);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment