1 | import { OuterSubscriber } from '../OuterSubscriber';
|
---|
2 | import { subscribeToResult } from '../util/subscribeToResult';
|
---|
3 | export function withLatestFrom(...args) {
|
---|
4 | return (source) => {
|
---|
5 | let project;
|
---|
6 | if (typeof args[args.length - 1] === 'function') {
|
---|
7 | project = args.pop();
|
---|
8 | }
|
---|
9 | const observables = args;
|
---|
10 | return source.lift(new WithLatestFromOperator(observables, project));
|
---|
11 | };
|
---|
12 | }
|
---|
13 | class WithLatestFromOperator {
|
---|
14 | constructor(observables, project) {
|
---|
15 | this.observables = observables;
|
---|
16 | this.project = project;
|
---|
17 | }
|
---|
18 | call(subscriber, source) {
|
---|
19 | return source.subscribe(new WithLatestFromSubscriber(subscriber, this.observables, this.project));
|
---|
20 | }
|
---|
21 | }
|
---|
22 | class WithLatestFromSubscriber extends OuterSubscriber {
|
---|
23 | constructor(destination, observables, project) {
|
---|
24 | super(destination);
|
---|
25 | this.observables = observables;
|
---|
26 | this.project = project;
|
---|
27 | this.toRespond = [];
|
---|
28 | const len = observables.length;
|
---|
29 | this.values = new Array(len);
|
---|
30 | for (let i = 0; i < len; i++) {
|
---|
31 | this.toRespond.push(i);
|
---|
32 | }
|
---|
33 | for (let i = 0; i < len; i++) {
|
---|
34 | let observable = observables[i];
|
---|
35 | this.add(subscribeToResult(this, observable, undefined, i));
|
---|
36 | }
|
---|
37 | }
|
---|
38 | notifyNext(_outerValue, innerValue, outerIndex) {
|
---|
39 | this.values[outerIndex] = innerValue;
|
---|
40 | const toRespond = this.toRespond;
|
---|
41 | if (toRespond.length > 0) {
|
---|
42 | const found = toRespond.indexOf(outerIndex);
|
---|
43 | if (found !== -1) {
|
---|
44 | toRespond.splice(found, 1);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 | notifyComplete() {
|
---|
49 | }
|
---|
50 | _next(value) {
|
---|
51 | if (this.toRespond.length === 0) {
|
---|
52 | const args = [value, ...this.values];
|
---|
53 | if (this.project) {
|
---|
54 | this._tryProject(args);
|
---|
55 | }
|
---|
56 | else {
|
---|
57 | this.destination.next(args);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 | _tryProject(args) {
|
---|
62 | let result;
|
---|
63 | try {
|
---|
64 | result = this.project.apply(this, args);
|
---|
65 | }
|
---|
66 | catch (err) {
|
---|
67 | this.destination.error(err);
|
---|
68 | return;
|
---|
69 | }
|
---|
70 | this.destination.next(result);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | //# sourceMappingURL=withLatestFrom.js.map |
---|