@connectv/core
Version:
agent-based reactive programming library for typescript/javascript
65 lines • 1.72 kB
JavaScript
import { Subscription } from 'rxjs';
/**
*
* A parent class for sub-classes who would want to track
* some [`Subscription`s](https://rxjs-dev.firebaseapp.com/guide/subscription)
* and clear them later.
*
*/
export class Tracker {
/**
*
* Tracks given subscription, to clear it up later when
* `.clear()` is called.
*
* @param sub
* @returns the given subscription (for convenience).
*
*/
track(sub) {
if (!this._sub) {
this._sub = new Subscription();
}
this._sub.add(sub);
return sub;
}
/**
*
* Untracks given subscription, removing it from subscriptions
* it will clear up when `.clear()` is called. This is useful when you
* clear up some subscriptions yourself before clearing the tracker object.
*
* @param sub
*
*/
untrack(sub) {
if (this._sub)
this._sub.remove(sub);
return this;
}
/**
*
* @returns `true` if this tracker object was ever tracking anything.
* @returns `true` even after you `.untrack()` everything.
* @returns `false` after invoking `.clear()`.
*
*/
get tracking() { return !!this._sub; }
/**
*
* Clears out all tracked subscriptions by unsibscribing them.
* Also clears out all references to tracked subscriptions.
*
* @warning most tracker objects will become useless after calling `.clear()` on them,
* so do not call this prematurely!
*
*/
clear() {
if (this._sub) {
this._sub.unsubscribe();
this._sub = undefined;
}
return this;
}
}
//# sourceMappingURL=tracker.js.map