Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableSkipUntil.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.concurrent.atomic.*;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
22 import io.reactivex.rxjava3.internal.util.*;
23 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
24
25 public final class FlowableSkipUntil<T, U> extends AbstractFlowableWithUpstream<T, T> {
26 final Publisher<U> other;
27 public FlowableSkipUntil(Flowable<T> source, Publisher<U> other) {
28 super(source);
29 this.other = other;
30 }
31
32 @Override
33 protected void subscribeActual(Subscriber<? super T> child) {
34 SkipUntilMainSubscriber<T> parent = new SkipUntilMainSubscriber<>(child);
35 child.onSubscribe(parent);
36
37 other.subscribe(parent.other);
38
39 source.subscribe(parent);
40 }
41
42 static final class SkipUntilMainSubscriber<T> extends AtomicInteger
43 implements ConditionalSubscriber<T>, Subscription {
44 private static final long serialVersionUID = -6270983465606289181L;
45
46 final Subscriber<? super T> downstream;
47
48 final AtomicReference<Subscription> upstream;
49
50 final AtomicLong requested;
51
52 final OtherSubscriber other;
53
54 final AtomicThrowable error;
55
56 volatile boolean gate;
57
58 SkipUntilMainSubscriber(Subscriber<? super T> downstream) {
59 this.downstream = downstream;
60 this.upstream = new AtomicReference<>();
61 this.requested = new AtomicLong();
62 this.other = new OtherSubscriber();
63 this.error = new AtomicThrowable();
64 }
65
66 @Override
67 public void onSubscribe(Subscription s) {
68 SubscriptionHelper.deferredSetOnce(this.upstream, requested, s);
69 }
70
71 @Override
72 public void onNext(T t) {
73 if (!tryOnNext(t)) {
74 upstream.get().request(1);
75 }
76 }
77
78 @Override
79 public boolean tryOnNext(T t) {
80 if (gate) {
81 HalfSerializer.onNext(downstream, t, this, error);
82 return true;
83 }
84 return false;
85 }
86
87 @Override
88 public void onError(Throwable t) {
89 SubscriptionHelper.cancel(other);
90 HalfSerializer.onError(downstream, t, SkipUntilMainSubscriber.this, error);
91 }
92
93 @Override
94 public void onComplete() {
95 SubscriptionHelper.cancel(other);
96 HalfSerializer.onComplete(downstream, this, error);
97 }
98
99 @Override
100 public void request(long n) {
101 SubscriptionHelper.deferredRequest(upstream, requested, n);
102 }
103
104 @Override
105 public void cancel() {
106 SubscriptionHelper.cancel(upstream);
107 SubscriptionHelper.cancel(other);
108 }
109
110 final class OtherSubscriber extends AtomicReference<Subscription>
111 implements FlowableSubscriber<Object> {
112
113 private static final long serialVersionUID = -5592042965931999169L;
114
115 @Override
116 public void onSubscribe(Subscription s) {
117 SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
118 }
119
120 @Override
121 public void onNext(Object t) {
122 gate = true;
123 get().cancel();
124 }
125
126 @Override
127 public void onError(Throwable t) {
128 SubscriptionHelper.cancel(upstream);
129 HalfSerializer.onError(downstream, t, SkipUntilMainSubscriber.this, error);
130 }
131
132 @Override
133 public void onComplete() {
134 gate = true;
135 }
136 }
137 }
138 }