bufferWhen.js
2.18 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
74
75
import { Subscription } from '../Subscription';
import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
export function bufferWhen(closingSelector) {
return function (source) {
return source.lift(new BufferWhenOperator(closingSelector));
};
}
class BufferWhenOperator {
constructor(closingSelector) {
this.closingSelector = closingSelector;
}
call(subscriber, source) {
return source.subscribe(new BufferWhenSubscriber(subscriber, this.closingSelector));
}
}
class BufferWhenSubscriber extends SimpleOuterSubscriber {
constructor(destination, closingSelector) {
super(destination);
this.closingSelector = closingSelector;
this.subscribing = false;
this.openBuffer();
}
_next(value) {
this.buffer.push(value);
}
_complete() {
const buffer = this.buffer;
if (buffer) {
this.destination.next(buffer);
}
super._complete();
}
_unsubscribe() {
this.buffer = undefined;
this.subscribing = false;
}
notifyNext() {
this.openBuffer();
}
notifyComplete() {
if (this.subscribing) {
this.complete();
}
else {
this.openBuffer();
}
}
openBuffer() {
let { closingSubscription } = this;
if (closingSubscription) {
this.remove(closingSubscription);
closingSubscription.unsubscribe();
}
const buffer = this.buffer;
if (this.buffer) {
this.destination.next(buffer);
}
this.buffer = [];
let closingNotifier;
try {
const { closingSelector } = this;
closingNotifier = closingSelector();
}
catch (err) {
return this.error(err);
}
closingSubscription = new Subscription();
this.closingSubscription = closingSubscription;
this.add(closingSubscription);
this.subscribing = true;
closingSubscription.add(innerSubscribe(closingNotifier, new SimpleInnerSubscriber(this)));
this.subscribing = false;
}
}
//# sourceMappingURL=bufferWhen.js.map