blob: a2dca4f02604473a7e98916a86e1a8f6079ad645 [file] [log] [blame]
import { Subscription } from '../Subscription';
import { OuterSubscriber } from '../OuterSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
export function bufferWhen(closingSelector) {
return function (source) {
return source.lift(new BufferWhenOperator(closingSelector));
};
}
class BufferWhenOperator {
constructor(closingSelector) {
this.closingSelector = closingSelector;
}
call(subscriber, source) {
return source.subscribe(new BufferWhenSubscriber(subscriber, this.closingSelector));
}
}
class BufferWhenSubscriber extends OuterSubscriber {
constructor(destination, closingSelector) {
super(destination);
this.closingSelector = closingSelector;
this.subscribing = false;
this.openBuffer();
}
_next(value) {
this.buffer.push(value);
}
_complete() {
const buffer = this.buffer;
if (buffer) {
this.destination.next(buffer);
}
super._complete();
}
_unsubscribe() {
this.buffer = null;
this.subscribing = false;
}
notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
this.openBuffer();
}
notifyComplete() {
if (this.subscribing) {
this.complete();
}
else {
this.openBuffer();
}
}
openBuffer() {
let { closingSubscription } = this;
if (closingSubscription) {
this.remove(closingSubscription);
closingSubscription.unsubscribe();
}
const buffer = this.buffer;
if (this.buffer) {
this.destination.next(buffer);
}
this.buffer = [];
let closingNotifier;
try {
const { closingSelector } = this;
closingNotifier = closingSelector();
}
catch (err) {
return this.error(err);
}
closingSubscription = new Subscription();
this.closingSubscription = closingSubscription;
this.add(closingSubscription);
this.subscribing = true;
closingSubscription.add(subscribeToResult(this, closingNotifier));
this.subscribing = false;
}
}
//# sourceMappingURL=bufferWhen.js.map