1 | import { isScheduler } from '../util/isScheduler';
|
---|
2 | import { isArray } from '../util/isArray';
|
---|
3 | import { OuterSubscriber } from '../OuterSubscriber';
|
---|
4 | import { subscribeToResult } from '../util/subscribeToResult';
|
---|
5 | import { fromArray } from './fromArray';
|
---|
6 | const NONE = {};
|
---|
7 | export function combineLatest(...observables) {
|
---|
8 | let resultSelector = undefined;
|
---|
9 | let scheduler = undefined;
|
---|
10 | if (isScheduler(observables[observables.length - 1])) {
|
---|
11 | scheduler = observables.pop();
|
---|
12 | }
|
---|
13 | if (typeof observables[observables.length - 1] === 'function') {
|
---|
14 | resultSelector = observables.pop();
|
---|
15 | }
|
---|
16 | if (observables.length === 1 && isArray(observables[0])) {
|
---|
17 | observables = observables[0];
|
---|
18 | }
|
---|
19 | return fromArray(observables, scheduler).lift(new CombineLatestOperator(resultSelector));
|
---|
20 | }
|
---|
21 | export class CombineLatestOperator {
|
---|
22 | constructor(resultSelector) {
|
---|
23 | this.resultSelector = resultSelector;
|
---|
24 | }
|
---|
25 | call(subscriber, source) {
|
---|
26 | return source.subscribe(new CombineLatestSubscriber(subscriber, this.resultSelector));
|
---|
27 | }
|
---|
28 | }
|
---|
29 | export class CombineLatestSubscriber extends OuterSubscriber {
|
---|
30 | constructor(destination, resultSelector) {
|
---|
31 | super(destination);
|
---|
32 | this.resultSelector = resultSelector;
|
---|
33 | this.active = 0;
|
---|
34 | this.values = [];
|
---|
35 | this.observables = [];
|
---|
36 | }
|
---|
37 | _next(observable) {
|
---|
38 | this.values.push(NONE);
|
---|
39 | this.observables.push(observable);
|
---|
40 | }
|
---|
41 | _complete() {
|
---|
42 | const observables = this.observables;
|
---|
43 | const len = observables.length;
|
---|
44 | if (len === 0) {
|
---|
45 | this.destination.complete();
|
---|
46 | }
|
---|
47 | else {
|
---|
48 | this.active = len;
|
---|
49 | this.toRespond = len;
|
---|
50 | for (let i = 0; i < len; i++) {
|
---|
51 | const observable = observables[i];
|
---|
52 | this.add(subscribeToResult(this, observable, undefined, i));
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 | notifyComplete(unused) {
|
---|
57 | if ((this.active -= 1) === 0) {
|
---|
58 | this.destination.complete();
|
---|
59 | }
|
---|
60 | }
|
---|
61 | notifyNext(_outerValue, innerValue, outerIndex) {
|
---|
62 | const values = this.values;
|
---|
63 | const oldVal = values[outerIndex];
|
---|
64 | const toRespond = !this.toRespond
|
---|
65 | ? 0
|
---|
66 | : oldVal === NONE ? --this.toRespond : this.toRespond;
|
---|
67 | values[outerIndex] = innerValue;
|
---|
68 | if (toRespond === 0) {
|
---|
69 | if (this.resultSelector) {
|
---|
70 | this._tryResultSelector(values);
|
---|
71 | }
|
---|
72 | else {
|
---|
73 | this.destination.next(values.slice());
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | _tryResultSelector(values) {
|
---|
78 | let result;
|
---|
79 | try {
|
---|
80 | result = this.resultSelector.apply(this, values);
|
---|
81 | }
|
---|
82 | catch (err) {
|
---|
83 | this.destination.error(err);
|
---|
84 | return;
|
---|
85 | }
|
---|
86 | this.destination.next(result);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | //# sourceMappingURL=combineLatest.js.map |
---|