Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDetach.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 org.reactivestreams.*;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
20 import io.reactivex.rxjava3.internal.util.EmptyComponent;
21
22 public final class FlowableDetach<T> extends AbstractFlowableWithUpstream<T, T> {
23
24 public FlowableDetach(Flowable<T> source) {
25 super(source);
26 }
27
28 @Override
29 protected void subscribeActual(Subscriber<? super T> s) {
30 source.subscribe(new DetachSubscriber<>(s));
31 }
32
33 static final class DetachSubscriber<T> implements FlowableSubscriber<T>, Subscription {
34
35 Subscriber<? super T> downstream;
36
37 Subscription upstream;
38
39 DetachSubscriber(Subscriber<? super T> downstream) {
40 this.downstream = downstream;
41 }
42
43 @Override
44 public void request(long n) {
45 upstream.request(n);
46 }
47
48 @Override
49 public void cancel() {
50 Subscription s = this.upstream;
51 this.upstream = EmptyComponent.INSTANCE;
52 this.downstream = EmptyComponent.asSubscriber();
53 s.cancel();
54 }
55
56 @Override
57 public void onSubscribe(Subscription s) {
58 if (SubscriptionHelper.validate(this.upstream, s)) {
59 this.upstream = s;
60
61 downstream.onSubscribe(this);
62 }
63 }
64
65 @Override
66 public void onNext(T t) {
67 downstream.onNext(t);
68 }
69
70 @Override
71 public void onError(Throwable t) {
72 Subscriber<? super T> a = downstream;
73 this.upstream = EmptyComponent.INSTANCE;
74 this.downstream = EmptyComponent.asSubscriber();
75 a.onError(t);
76 }
77
78 @Override
79 public void onComplete() {
80 Subscriber<? super T> a = downstream;
81 this.upstream = EmptyComponent.INSTANCE;
82 this.downstream = EmptyComponent.asSubscriber();
83 a.onComplete();
84 }
85 }
86 }
87