scan.js
1.54 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
import { Subscriber } from '../Subscriber';
export function scan(accumulator, seed) {
let hasSeed = false;
if (arguments.length >= 2) {
hasSeed = true;
}
return function scanOperatorFunction(source) {
return source.lift(new ScanOperator(accumulator, seed, hasSeed));
};
}
class ScanOperator {
constructor(accumulator, seed, hasSeed = false) {
this.accumulator = accumulator;
this.seed = seed;
this.hasSeed = hasSeed;
}
call(subscriber, source) {
return source.subscribe(new ScanSubscriber(subscriber, this.accumulator, this.seed, this.hasSeed));
}
}
class ScanSubscriber extends Subscriber {
constructor(destination, accumulator, _seed, hasSeed) {
super(destination);
this.accumulator = accumulator;
this._seed = _seed;
this.hasSeed = hasSeed;
this.index = 0;
}
get seed() {
return this._seed;
}
set seed(value) {
this.hasSeed = true;
this._seed = value;
}
_next(value) {
if (!this.hasSeed) {
this.seed = value;
this.destination.next(value);
}
else {
return this._tryNext(value);
}
}
_tryNext(value) {
const index = this.index++;
let result;
try {
result = this.accumulator(this.seed, value, index);
}
catch (err) {
this.destination.error(err);
}
this.seed = result;
this.destination.next(result);
}
}
//# sourceMappingURL=scan.js.map