source: trip-planner-front/node_modules/rxjs/src/internal/operators/mergeScan.ts@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.7 KB
Line 
1import { Operator } from '../Operator';
2import { Observable } from '../Observable';
3import { Subscriber } from '../Subscriber';
4import { Subscription } from '../Subscription';
5import { ObservableInput, OperatorFunction } from '../types';
6import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
7
8/**
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.
12 *
13 * <span class="informal">It's like {@link scan}, but the Observables returned
14 * by the accumulator are merged into the outer Observable.</span>
15 *
16 * ## Example
17 * Count the number of click events
18 * ```ts
19 * import { fromEvent, of } from 'rxjs';
20 * import { mapTo, mergeScan } from 'rxjs/operators';
21 *
22 * const click$ = fromEvent(document, 'click');
23 * const one$ = click$.pipe(mapTo(1));
24 * const seed = 0;
25 * const count$ = one$.pipe(
26 * mergeScan((acc, one) => of(acc + one), seed),
27 * );
28 * count$.subscribe(x => console.log(x));
29 *
30 * // Results:
31 * // 1
32 * // 2
33 * // 3
34 * // 4
35 * // ...and so on for each click
36 * ```
37 *
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.
44 * @method mergeScan
45 * @owner Observable
46 */
47export function mergeScan<T, R>(accumulator: (acc: R, value: T, index: number) => ObservableInput<R>,
48 seed: R,
49 concurrent: number = Number.POSITIVE_INFINITY): OperatorFunction<T, R> {
50 return (source: Observable<T>) => source.lift(new MergeScanOperator(accumulator, seed, concurrent));
51}
52
53export class MergeScanOperator<T, R> implements Operator<T, R> {
54 constructor(private accumulator: (acc: R, value: T, index: number) => ObservableInput<R>,
55 private seed: R,
56 private concurrent: number) {
57 }
58
59 call(subscriber: Subscriber<R>, source: any): any {
60 return source.subscribe(new MergeScanSubscriber(
61 subscriber, this.accumulator, this.seed, this.concurrent
62 ));
63 }
64}
65
66/**
67 * We need this JSDoc comment for affecting ESDoc.
68 * @ignore
69 * @extends {Ignored}
70 */
71export 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;
77
78 constructor(destination: Subscriber<R>,
79 private accumulator: (acc: R, value: T, index: number) => ObservableInput<R>,
80 private acc: R,
81 private concurrent: number) {
82 super(destination);
83 }
84
85 protected _next(value: any): void {
86 if (this.active < this.concurrent) {
87 const index = this.index++;
88 const destination = this.destination;
89 let ish;
90 try {
91 const { accumulator } = this;
92 ish = accumulator(this.acc, value, index);
93 } catch (e) {
94 return destination.error!(e);
95 }
96 this.active++;
97 this._innerSub(ish);
98 } else {
99 this.buffer.push(value);
100 }
101 }
102
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);
113 }
114 }
115
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);
121 }
122 this.destination.complete!();
123 }
124 this.unsubscribe();
125 }
126
127 notifyNext(innerValue: R): void {
128 const { destination } = this;
129 this.acc = innerValue;
130 this.hasValue = true;
131 destination.next!(innerValue);
132 }
133
134 notifyComplete(): void {
135 const buffer = this.buffer;
136 this.active--;
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);
142 }
143 this.destination.complete!();
144 }
145 }
146}
Note: See TracBrowser for help on using the repository browser.