@acutmore/rxjs
Version:
Reactive Extensions for modern JavaScript
104 lines • 2.9 kB
JavaScript
import { subscribeToResult } from '../util/subscribeToResult';
import { OuterSubscriber } from '../OuterSubscriber';
class PausableBufferedSubscriber extends OuterSubscriber {
constructor(destination, pauser) {
super(destination);
this.destination = destination;
this.pauser = pauser;
this.srcCompleted = false;
this.srcErrored = false;
this.srcError = undefined;
this.pauserCompleted = false;
this.flowing = false;
this.buffer = [];
this.draining = false;
this.add(subscribeToResult(this, pauser));
}
_next(value) {
if (this.flowing) {
this.destination.next(value);
}
else {
const { buffer } = this;
buffer[buffer.length] = value;
}
}
__drain() {
const { buffer } = this;
while (buffer.length > 0) {
this.destination.next(buffer.shift());
}
}
drainQueue() {
if (this.draining) {
return;
}
this.draining = true;
try {
this.__drain();
}
finally {
this.draining = false;
}
}
notifyNext(outerValue, flowing, outerIndex, innerIndex, innerSub) {
if (flowing === this.flowing) {
return;
}
this.flowing = flowing;
if (flowing) {
this.drainQueue();
if (this.srcCompleted) {
super._complete();
}
if (this.srcErrored) {
super._error(this.srcError);
}
}
}
_complete() {
this.srcCompleted = true;
if (this.buffer.length === 0 || this.pauserCompleted) {
this.drainQueue();
super._complete();
}
}
notifyComplete(innerSub) {
this.pauserCompleted = true;
this.remove(innerSub);
if (this.srcCompleted) {
this.drainQueue();
this.destination.complete();
}
if (this.srcErrored) {
this.drainQueue();
super._error(this.srcError);
}
}
_error(err) {
this.srcErrored = true;
this.srcError = err;
if (this.buffer.length === 0 || this.pauserCompleted) {
this.drainQueue();
super._error(err);
}
}
notifyError(err) {
this.drainQueue();
super._error(err);
}
}
class PausableBufferedOperator {
constructor(pauser) {
this.pauser = pauser;
}
call(subscriber, source) {
return source.subscribe(new PausableBufferedSubscriber(subscriber, this.pauser));
}
}
export function pausableBuffered(pauser) {
return function pausableBufferedOperatorFunction(source) {
return source.lift(new PausableBufferedOperator(pauser));
};
}
//# sourceMappingURL=pausableBuffered.js.map