1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4 import { Subscription } from '../Subscription';
5 import { ObservableInput, OperatorFunction } from '../types';
6 import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
9 * Applies an accumulator function over the source Observable where the
10 * accumulator function itself returns an Observable, then each intermediate
11 * Observable returned is merged into the output Observable.
13 * <span class="informal">It's like {@link scan}, but the Observables returned
14 * by the accumulator are merged into the outer Observable.</span>
17 * Count the number of click events
19 * import { fromEvent, of } from 'rxjs';
20 * import { mapTo, mergeScan } from 'rxjs/operators';
22 * const click$ = fromEvent(document, 'click');
23 * const one$ = click$.pipe(mapTo(1));
25 * const count$ = one$.pipe(
26 * mergeScan((acc, one) => of(acc + one), seed),
28 * count$.subscribe(x => console.log(x));
35 * // ...and so on for each click
38 * @param {function(acc: R, value: T): Observable<R>} accumulator
39 * The accumulator function called on each source value.
40 * @param seed The initial accumulation value.
41 * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of
42 * input Observables being subscribed to concurrently.
43 * @return {Observable<R>} An observable of the accumulated values.
47 export function mergeScan<T, R>(accumulator: (acc: R, value: T, index: number) => ObservableInput<R>,
49 concurrent: number = Number.POSITIVE_INFINITY): OperatorFunction<T, R> {
50 return (source: Observable<T>) => source.lift(new MergeScanOperator(accumulator, seed, concurrent));
53 export class MergeScanOperator<T, R> implements Operator<T, R> {
54 constructor(private accumulator: (acc: R, value: T, index: number) => ObservableInput<R>,
56 private concurrent: number) {
59 call(subscriber: Subscriber<R>, source: any): any {
60 return source.subscribe(new MergeScanSubscriber(
61 subscriber, this.accumulator, this.seed, this.concurrent
67 * We need this JSDoc comment for affecting ESDoc.
71 export class MergeScanSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
72 private hasValue: boolean = false;
73 private hasCompleted: boolean = false;
74 private buffer: Observable<any>[] = [];
75 private active: number = 0;
76 protected index: number = 0;
78 constructor(destination: Subscriber<R>,
79 private accumulator: (acc: R, value: T, index: number) => ObservableInput<R>,
81 private concurrent: number) {
85 protected _next(value: any): void {
86 if (this.active < this.concurrent) {
87 const index = this.index++;
88 const destination = this.destination;
91 const { accumulator } = this;
92 ish = accumulator(this.acc, value, index);
94 return destination.error!(e);
99 this.buffer.push(value);
103 private _innerSub(ish: any): void {
104 const innerSubscriber = new SimpleInnerSubscriber(this);
105 const destination = this.destination as Subscription;
106 destination.add(innerSubscriber);
107 const innerSubscription = innerSubscribe(ish, innerSubscriber);
108 // The returned subscription will usually be the subscriber that was
109 // passed. However, interop subscribers will be wrapped and for
110 // unsubscriptions to chain correctly, the wrapper needs to be added, too.
111 if (innerSubscription !== innerSubscriber) {
112 destination.add(innerSubscription);
116 protected _complete(): void {
117 this.hasCompleted = true;
118 if (this.active === 0 && this.buffer.length === 0) {
119 if (this.hasValue === false) {
120 this.destination.next!(this.acc);
122 this.destination.complete!();
127 notifyNext(innerValue: R): void {
128 const { destination } = this;
129 this.acc = innerValue;
130 this.hasValue = true;
131 destination.next!(innerValue);
134 notifyComplete(): void {
135 const buffer = this.buffer;
137 if (buffer.length > 0) {
138 this._next(buffer.shift());
139 } else if (this.active === 0 && this.hasCompleted) {
140 if (this.hasValue === false) {
141 this.destination.next!(this.acc);
143 this.destination.complete!();