blob: d65525a920b9043de7e6beba513706cc5836b5d1 [file] [log] [blame]
import { subscribeToResult } from '../util/subscribeToResult';
import { OuterSubscriber } from '../OuterSubscriber';
import { InnerSubscriber } from '../InnerSubscriber';
export function mergeScan(accumulator, seed, concurrent = Number.POSITIVE_INFINITY) {
return (source) => source.lift(new MergeScanOperator(accumulator, seed, concurrent));
}
export class MergeScanOperator {
constructor(accumulator, seed, concurrent) {
this.accumulator = accumulator;
this.seed = seed;
this.concurrent = concurrent;
}
call(subscriber, source) {
return source.subscribe(new MergeScanSubscriber(subscriber, this.accumulator, this.seed, this.concurrent));
}
}
export class MergeScanSubscriber extends OuterSubscriber {
constructor(destination, accumulator, acc, concurrent) {
super(destination);
this.accumulator = accumulator;
this.acc = acc;
this.concurrent = concurrent;
this.hasValue = false;
this.hasCompleted = false;
this.buffer = [];
this.active = 0;
this.index = 0;
}
_next(value) {
if (this.active < this.concurrent) {
const index = this.index++;
const destination = this.destination;
let ish;
try {
const { accumulator } = this;
ish = accumulator(this.acc, value, index);
}
catch (e) {
return destination.error(e);
}
this.active++;
this._innerSub(ish, value, index);
}
else {
this.buffer.push(value);
}
}
_innerSub(ish, value, index) {
const innerSubscriber = new InnerSubscriber(this, undefined, undefined);
const destination = this.destination;
destination.add(innerSubscriber);
subscribeToResult(this, ish, value, index, innerSubscriber);
}
_complete() {
this.hasCompleted = true;
if (this.active === 0 && this.buffer.length === 0) {
if (this.hasValue === false) {
this.destination.next(this.acc);
}
this.destination.complete();
}
this.unsubscribe();
}
notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
const { destination } = this;
this.acc = innerValue;
this.hasValue = true;
destination.next(innerValue);
}
notifyComplete(innerSub) {
const buffer = this.buffer;
const destination = this.destination;
destination.remove(innerSub);
this.active--;
if (buffer.length > 0) {
this._next(buffer.shift());
}
else if (this.active === 0 && this.hasCompleted) {
if (this.hasValue === false) {
this.destination.next(this.acc);
}
this.destination.complete();
}
}
}
//# sourceMappingURL=mergeScan.js.map