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