Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableRepeatWhen.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.flowable;
15
16 import java.util.Objects;
17 import java.util.concurrent.atomic.*;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.exceptions.Exceptions;
23 import io.reactivex.rxjava3.functions.Function;
24 import io.reactivex.rxjava3.internal.subscriptions.*;
25 import io.reactivex.rxjava3.processors.*;
26 import io.reactivex.rxjava3.subscribers.SerializedSubscriber;
27
28 public final class FlowableRepeatWhen<T> extends AbstractFlowableWithUpstream<T, T> {
29 final Function<? super Flowable<Object>, ? extends Publisher<?>> handler;
30
31 public FlowableRepeatWhen(Flowable<T> source,
32 Function<? super Flowable<Object>, ? extends Publisher<?>> handler) {
33 super(source);
34 this.handler = handler;
35 }
36
37 @Override
38 public void subscribeActual(Subscriber<? super T> s) {
39
40 SerializedSubscriber<T> z = new SerializedSubscriber<>(s);
41
42 FlowableProcessor<Object> processor = UnicastProcessor.create(8).toSerialized();
43
44 Publisher<?> when;
45
46 try {
47 when = Objects.requireNonNull(handler.apply(processor), "handler returned a null Publisher");
48 } catch (Throwable ex) {
49 Exceptions.throwIfFatal(ex);
50 EmptySubscription.error(ex, s);
51 return;
52 }
53
54 WhenReceiver<T, Object> receiver = new WhenReceiver<>(source);
55
56 RepeatWhenSubscriber<T> subscriber = new RepeatWhenSubscriber<>(z, processor, receiver);
57
58 receiver.subscriber = subscriber;
59
60 s.onSubscribe(subscriber);
61
62 when.subscribe(receiver);
63
64 receiver.onNext(0);
65 }
66
67 static final class WhenReceiver<T, U>
68 extends AtomicInteger
69 implements FlowableSubscriber<Object>, Subscription {
70
71 private static final long serialVersionUID = 2827772011130406689L;
72
73 final Publisher<T> source;
74
75 final AtomicReference<Subscription> upstream;
76
77 final AtomicLong requested;
78
79 WhenSourceSubscriber<T, U> subscriber;
80
81 WhenReceiver(Publisher<T> source) {
82 this.source = source;
83 this.upstream = new AtomicReference<>();
84 this.requested = new AtomicLong();
85 }
86
87 @Override
88 public void onSubscribe(Subscription s) {
89 SubscriptionHelper.deferredSetOnce(upstream, requested, s);
90 }
91
92 @Override
93 public void onNext(Object t) {
94 if (getAndIncrement() == 0) {
95 for (;;) {
96 if (upstream.get() == SubscriptionHelper.CANCELLED) {
97 return;
98 }
99
100 source.subscribe(subscriber);
101
102 if (decrementAndGet() == 0) {
103 break;
104 }
105 }
106 }
107 }
108
109 @Override
110 public void onError(Throwable t) {
111 subscriber.cancel();
112 subscriber.downstream.onError(t);
113 }
114
115 @Override
116 public void onComplete() {
117 subscriber.cancel();
118 subscriber.downstream.onComplete();
119 }
120
121 @Override
122 public void request(long n) {
123 SubscriptionHelper.deferredRequest(upstream, requested, n);
124 }
125
126 @Override
127 public void cancel() {
128 SubscriptionHelper.cancel(upstream);
129 }
130 }
131
132 abstract static class WhenSourceSubscriber<T, U> extends SubscriptionArbiter implements FlowableSubscriber<T> {
133
134 private static final long serialVersionUID = -5604623027276966720L;
135
136 protected final Subscriber<? super T> downstream;
137
138 protected final FlowableProcessor<U> processor;
139
140 protected final Subscription receiver;
141
142 private long produced;
143
144 WhenSourceSubscriber(Subscriber<? super T> actual, FlowableProcessor<U> processor,
145 Subscription receiver) {
146 super(false);
147 this.downstream = actual;
148 this.processor = processor;
149 this.receiver = receiver;
150 }
151
152 @Override
153 public final void onSubscribe(Subscription s) {
154 setSubscription(s);
155 }
156
157 @Override
158 public final void onNext(T t) {
159 produced++;
160 downstream.onNext(t);
161 }
162
163 protected final void again(U signal) {
164 setSubscription(EmptySubscription.INSTANCE);
165 long p = produced;
166 if (p != 0L) {
167 produced = 0L;
168 produced(p);
169 }
170 receiver.request(1);
171 processor.onNext(signal);
172 }
173
174 @Override
175 public final void cancel() {
176 super.cancel();
177 receiver.cancel();
178 }
179 }
180
181 static final class RepeatWhenSubscriber<T> extends WhenSourceSubscriber<T, Object> {
182
183 private static final long serialVersionUID = -2680129890138081029L;
184
185 RepeatWhenSubscriber(Subscriber<? super T> actual, FlowableProcessor<Object> processor,
186 Subscription receiver) {
187 super(actual, processor, receiver);
188 }
189
190 @Override
191 public void onError(Throwable t) {
192 receiver.cancel();
193 downstream.onError(t);
194 }
195
196 @Override
197 public void onComplete() {
198 again(0);
199 }
200 }
201 }