Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableUnsubscribeOn.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.AtomicBoolean;
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.plugins.RxJavaPlugins;
23
24 public final class FlowableUnsubscribeOn<T> extends AbstractFlowableWithUpstream<T, T> {
25 final Scheduler scheduler;
26 public FlowableUnsubscribeOn(Flowable<T> source, Scheduler scheduler) {
27 super(source);
28 this.scheduler = scheduler;
29 }
30
31 @Override
32 protected void subscribeActual(Subscriber<? super T> s) {
33 source.subscribe(new UnsubscribeSubscriber<>(s, scheduler));
34 }
35
36 static final class UnsubscribeSubscriber<T> extends AtomicBoolean implements FlowableSubscriber<T>, Subscription {
37
38 private static final long serialVersionUID = 1015244841293359600L;
39
40 final Subscriber<? super T> downstream;
41 final Scheduler scheduler;
42
43 Subscription upstream;
44
45 UnsubscribeSubscriber(Subscriber<? super T> actual, Scheduler scheduler) {
46 this.downstream = actual;
47 this.scheduler = scheduler;
48 }
49
50 @Override
51 public void onSubscribe(Subscription s) {
52 if (SubscriptionHelper.validate(this.upstream, s)) {
53 this.upstream = s;
54 downstream.onSubscribe(this);
55 }
56 }
57
58 @Override
59 public void onNext(T t) {
60 if (!get()) {
61 downstream.onNext(t);
62 }
63 }
64
65 @Override
66 public void onError(Throwable t) {
67 if (get()) {
68 RxJavaPlugins.onError(t);
69 return;
70 }
71 downstream.onError(t);
72 }
73
74 @Override
75 public void onComplete() {
76 if (!get()) {
77 downstream.onComplete();
78 }
79 }
80
81 @Override
82 public void request(long n) {
83 upstream.request(n);
84 }
85
86 @Override
87 public void cancel() {
88 if (compareAndSet(false, true)) {
89 scheduler.scheduleDirect(new Cancellation());
90 }
91 }
92
93 final class Cancellation implements Runnable {
94 @Override
95 public void run() {
96 upstream.cancel();
97 }
98 }
99 }
100 }