import {Subscription} from '../Subscription';
import {Subject} from '../Subject';
import {Subscriber} from '../Subscriber';
import {Observable} from '../Observable';
export class RefCountSubscription extends Subscription {
primary: Subscription;
attemptedToUnsubscribePrimary: boolean = false;
count: number = 0;
constructor() {
super();
}
setPrimary(subscription: Subscription) {
this.primary = subscription;
}
unsubscribe() {
if (!this.isUnsubscribed && !this.attemptedToUnsubscribePrimary) {
this.attemptedToUnsubscribePrimary = true;
if (this.count === 0) {
super.unsubscribe();
this.primary.unsubscribe();
}
}
}
}
export class GroupedObservable<T> extends Observable<T> {
constructor(public key: string,
private groupSubject: Subject<T>,
private refCountSubscription?: RefCountSubscription) {
super();
}
_subscribe(subscriber: Subscriber<T>) {
const subscription = new Subscription();
if (this.refCountSubscription && !this.refCountSubscription.isUnsubscribed) {
subscription.add(new InnerRefCountSubscription(this.refCountSubscription));
}
subscription.add(this.groupSubject.subscribe(subscriber));
return subscription;
}
}
export class InnerRefCountSubscription extends Subscription {
constructor(private parent: RefCountSubscription) {
super();
parent.count++;
}
unsubscribe() {
Eif (!this.parent.isUnsubscribed && !this.isUnsubscribed) {
super.unsubscribe();
this.parent.count--;
if (this.parent.count === 0 && this.parent.attemptedToUnsubscribePrimary) {
this.parent.unsubscribe();
this.parent.primary.unsubscribe();
}
}
}
}
|