1 | import { Subscription } from '../Subscription';
|
---|
2 | import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
|
---|
3 | export function bufferWhen(closingSelector) {
|
---|
4 | return function (source) {
|
---|
5 | return source.lift(new BufferWhenOperator(closingSelector));
|
---|
6 | };
|
---|
7 | }
|
---|
8 | class BufferWhenOperator {
|
---|
9 | constructor(closingSelector) {
|
---|
10 | this.closingSelector = closingSelector;
|
---|
11 | }
|
---|
12 | call(subscriber, source) {
|
---|
13 | return source.subscribe(new BufferWhenSubscriber(subscriber, this.closingSelector));
|
---|
14 | }
|
---|
15 | }
|
---|
16 | class BufferWhenSubscriber extends SimpleOuterSubscriber {
|
---|
17 | constructor(destination, closingSelector) {
|
---|
18 | super(destination);
|
---|
19 | this.closingSelector = closingSelector;
|
---|
20 | this.subscribing = false;
|
---|
21 | this.openBuffer();
|
---|
22 | }
|
---|
23 | _next(value) {
|
---|
24 | this.buffer.push(value);
|
---|
25 | }
|
---|
26 | _complete() {
|
---|
27 | const buffer = this.buffer;
|
---|
28 | if (buffer) {
|
---|
29 | this.destination.next(buffer);
|
---|
30 | }
|
---|
31 | super._complete();
|
---|
32 | }
|
---|
33 | _unsubscribe() {
|
---|
34 | this.buffer = undefined;
|
---|
35 | this.subscribing = false;
|
---|
36 | }
|
---|
37 | notifyNext() {
|
---|
38 | this.openBuffer();
|
---|
39 | }
|
---|
40 | notifyComplete() {
|
---|
41 | if (this.subscribing) {
|
---|
42 | this.complete();
|
---|
43 | }
|
---|
44 | else {
|
---|
45 | this.openBuffer();
|
---|
46 | }
|
---|
47 | }
|
---|
48 | openBuffer() {
|
---|
49 | let { closingSubscription } = this;
|
---|
50 | if (closingSubscription) {
|
---|
51 | this.remove(closingSubscription);
|
---|
52 | closingSubscription.unsubscribe();
|
---|
53 | }
|
---|
54 | const buffer = this.buffer;
|
---|
55 | if (this.buffer) {
|
---|
56 | this.destination.next(buffer);
|
---|
57 | }
|
---|
58 | this.buffer = [];
|
---|
59 | let closingNotifier;
|
---|
60 | try {
|
---|
61 | const { closingSelector } = this;
|
---|
62 | closingNotifier = closingSelector();
|
---|
63 | }
|
---|
64 | catch (err) {
|
---|
65 | return this.error(err);
|
---|
66 | }
|
---|
67 | closingSubscription = new Subscription();
|
---|
68 | this.closingSubscription = closingSubscription;
|
---|
69 | this.add(closingSubscription);
|
---|
70 | this.subscribing = true;
|
---|
71 | closingSubscription.add(innerSubscribe(closingNotifier, new SimpleInnerSubscriber(this)));
|
---|
72 | this.subscribing = false;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | //# sourceMappingURL=bufferWhen.js.map |
---|