1 | import { Operator } from '../Operator';
|
---|
2 | import { Subscriber } from '../Subscriber';
|
---|
3 | import { Observable } from '../Observable';
|
---|
4 | import { OuterSubscriber } from '../OuterSubscriber';
|
---|
5 | import { InnerSubscriber } from '../InnerSubscriber';
|
---|
6 | import { subscribeToResult } from '../util/subscribeToResult';
|
---|
7 | import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types';
|
---|
8 |
|
---|
9 | /* tslint:disable:max-line-length */
|
---|
10 | export function withLatestFrom<T, R>(project: (v1: T) => R): OperatorFunction<T, R>;
|
---|
11 | export function withLatestFrom<T, O2 extends ObservableInput<any>, R>(source2: O2, project: (v1: T, v2: ObservedValueOf<O2>) => R): OperatorFunction<T, R>;
|
---|
12 | export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, R>(v2: O2, v3: O3, project: (v1: T, v2: ObservedValueOf<O2>, v3: ObservedValueOf<O3>) => R): OperatorFunction<T, R>;
|
---|
13 | export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, R>(v2: O2, v3: O3, v4: O4, project: (v1: T, v2: ObservedValueOf<O2>, v3: ObservedValueOf<O3>, v4: ObservedValueOf<O4>) => R): OperatorFunction<T, R>;
|
---|
14 | export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>, R>(v2: O2, v3: O3, v4: O4, v5: O5, project: (v1: T, v2: ObservedValueOf<O2>, v3: ObservedValueOf<O3>, v4: ObservedValueOf<O4>, v5: ObservedValueOf<O5>) => R): OperatorFunction<T, R>;
|
---|
15 | export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>, O6 extends ObservableInput<any>, R>(v2: O2, v3: O3, v4: O4, v5: O5, v6: O6, project: (v1: T, v2: ObservedValueOf<O2>, v3: ObservedValueOf<O3>, v4: ObservedValueOf<O4>, v5: ObservedValueOf<O5>, v6: ObservedValueOf<O6>) => R): OperatorFunction<T, R>;
|
---|
16 | export function withLatestFrom<T, O2 extends ObservableInput<any>>(source2: O2): OperatorFunction<T, [T, ObservedValueOf<O2>]>;
|
---|
17 | export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>>(v2: O2, v3: O3): OperatorFunction<T, [T, ObservedValueOf<O2>, ObservedValueOf<O3>]>;
|
---|
18 | export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>>(v2: O2, v3: O3, v4: O4): OperatorFunction<T, [T, ObservedValueOf<O2>, ObservedValueOf<O3>, ObservedValueOf<O4>]>;
|
---|
19 | export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>>(v2: O2, v3: O3, v4: O4, v5: O5): OperatorFunction<T, [T, ObservedValueOf<O2>, ObservedValueOf<O3>, ObservedValueOf<O4>, ObservedValueOf<O5>]>;
|
---|
20 | export function withLatestFrom<T, O2 extends ObservableInput<any>, O3 extends ObservableInput<any>, O4 extends ObservableInput<any>, O5 extends ObservableInput<any>, O6 extends ObservableInput<any>>(v2: O2, v3: O3, v4: O4, v5: O5, v6: O6): OperatorFunction<T, [T, ObservedValueOf<O2>, ObservedValueOf<O3>, ObservedValueOf<O4>, ObservedValueOf<O5>, ObservedValueOf<O6>]>;
|
---|
21 | export function withLatestFrom<T, R>(...observables: Array<ObservableInput<any> | ((...values: Array<any>) => R)>): OperatorFunction<T, R>;
|
---|
22 | export function withLatestFrom<T, R>(array: ObservableInput<any>[]): OperatorFunction<T, R>;
|
---|
23 | export function withLatestFrom<T, R>(array: ObservableInput<any>[], project: (...values: Array<any>) => R): OperatorFunction<T, R>;
|
---|
24 |
|
---|
25 | /* tslint:enable:max-line-length */
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Combines the source Observable with other Observables to create an Observable
|
---|
29 | * whose values are calculated from the latest values of each, only when the
|
---|
30 | * source emits.
|
---|
31 | *
|
---|
32 | * <span class="informal">Whenever the source Observable emits a value, it
|
---|
33 | * computes a formula using that value plus the latest values from other input
|
---|
34 | * Observables, then emits the output of that formula.</span>
|
---|
35 | *
|
---|
36 | * ![](withLatestFrom.png)
|
---|
37 | *
|
---|
38 | * `withLatestFrom` combines each value from the source Observable (the
|
---|
39 | * instance) with the latest values from the other input Observables only when
|
---|
40 | * the source emits a value, optionally using a `project` function to determine
|
---|
41 | * the value to be emitted on the output Observable. All input Observables must
|
---|
42 | * emit at least one value before the output Observable will emit a value.
|
---|
43 | *
|
---|
44 | * ## Example
|
---|
45 | * On every click event, emit an array with the latest timer event plus the click event
|
---|
46 | * ```ts
|
---|
47 | * import { fromEvent, interval } from 'rxjs';
|
---|
48 | * import { withLatestFrom } from 'rxjs/operators';
|
---|
49 | *
|
---|
50 | * const clicks = fromEvent(document, 'click');
|
---|
51 | * const timer = interval(1000);
|
---|
52 | * const result = clicks.pipe(withLatestFrom(timer));
|
---|
53 | * result.subscribe(x => console.log(x));
|
---|
54 | * ```
|
---|
55 | *
|
---|
56 | * @see {@link combineLatest}
|
---|
57 | *
|
---|
58 | * @param {ObservableInput} other An input Observable to combine with the source
|
---|
59 | * Observable. More than one input Observables may be given as argument.
|
---|
60 | * @param {Function} [project] Projection function for combining values
|
---|
61 | * together. Receives all values in order of the Observables passed, where the
|
---|
62 | * first parameter is a value from the source Observable. (e.g.
|
---|
63 | * `a.pipe(withLatestFrom(b, c), map(([a1, b1, c1]) => a1 + b1 + c1))`). If this is not
|
---|
64 | * passed, arrays will be emitted on the output Observable.
|
---|
65 | * @return {Observable} An Observable of projected values from the most recent
|
---|
66 | * values from each input Observable, or an array of the most recent values from
|
---|
67 | * each input Observable.
|
---|
68 | * @method withLatestFrom
|
---|
69 | * @owner Observable
|
---|
70 | */
|
---|
71 | export function withLatestFrom<T, R>(...args: Array<ObservableInput<any> | ((...values: Array<any>) => R)>): OperatorFunction<T, R> {
|
---|
72 | return (source: Observable<T>) => {
|
---|
73 | let project: any;
|
---|
74 | if (typeof args[args.length - 1] === 'function') {
|
---|
75 | project = args.pop();
|
---|
76 | }
|
---|
77 | const observables = <Observable<any>[]>args;
|
---|
78 | return source.lift(new WithLatestFromOperator(observables, project));
|
---|
79 | };
|
---|
80 | }
|
---|
81 |
|
---|
82 | class WithLatestFromOperator<T, R> implements Operator<T, R> {
|
---|
83 | constructor(private observables: Observable<any>[],
|
---|
84 | private project?: (...values: any[]) => Observable<R>) {
|
---|
85 | }
|
---|
86 |
|
---|
87 | call(subscriber: Subscriber<R>, source: any): any {
|
---|
88 | return source.subscribe(new WithLatestFromSubscriber(subscriber, this.observables, this.project));
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * We need this JSDoc comment for affecting ESDoc.
|
---|
94 | * @ignore
|
---|
95 | * @extends {Ignored}
|
---|
96 | */
|
---|
97 | class WithLatestFromSubscriber<T, R> extends OuterSubscriber<T, R> {
|
---|
98 | private values: any[];
|
---|
99 | private toRespond: number[] = [];
|
---|
100 |
|
---|
101 | constructor(destination: Subscriber<R>,
|
---|
102 | private observables: Observable<any>[],
|
---|
103 | private project?: (...values: any[]) => Observable<R>) {
|
---|
104 | super(destination);
|
---|
105 | const len = observables.length;
|
---|
106 | this.values = new Array(len);
|
---|
107 |
|
---|
108 | for (let i = 0; i < len; i++) {
|
---|
109 | this.toRespond.push(i);
|
---|
110 | }
|
---|
111 |
|
---|
112 | for (let i = 0; i < len; i++) {
|
---|
113 | let observable = observables[i];
|
---|
114 | this.add(subscribeToResult<T, R>(this, observable, undefined, i));
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | notifyNext(_outerValue: T, innerValue: R,
|
---|
119 | outerIndex: number): void {
|
---|
120 | this.values[outerIndex] = innerValue;
|
---|
121 | const toRespond = this.toRespond;
|
---|
122 | if (toRespond.length > 0) {
|
---|
123 | const found = toRespond.indexOf(outerIndex);
|
---|
124 | if (found !== -1) {
|
---|
125 | toRespond.splice(found, 1);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | notifyComplete() {
|
---|
131 | // noop
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected _next(value: T) {
|
---|
135 | if (this.toRespond.length === 0) {
|
---|
136 | const args = [value, ...this.values];
|
---|
137 | if (this.project) {
|
---|
138 | this._tryProject(args);
|
---|
139 | } else {
|
---|
140 | this.destination.next!(args);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | private _tryProject(args: any[]) {
|
---|
146 | let result: any;
|
---|
147 | try {
|
---|
148 | result = this.project!.apply(this, args);
|
---|
149 | } catch (err) {
|
---|
150 | this.destination.error!(err);
|
---|
151 | return;
|
---|
152 | }
|
---|
153 | this.destination.next!(result);
|
---|
154 | }
|
---|
155 | }
|
---|