Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleUnsubscribeOn.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.single;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21
22 /**
23 * Makes sure a dispose() call from downstream happens on the specified scheduler.
24 *
25 * @param <T> the value type
26 */
27 public final class SingleUnsubscribeOn<T> extends Single<T> {
28
29 final SingleSource<T> source;
30
31 final Scheduler scheduler;
32
33 public SingleUnsubscribeOn(SingleSource<T> source, Scheduler scheduler) {
34 this.source = source;
35 this.scheduler = scheduler;
36 }
37
38 @Override
39 protected void subscribeActual(SingleObserver<? super T> observer) {
40 source.subscribe(new UnsubscribeOnSingleObserver<>(observer, scheduler));
41 }
42
43 static final class UnsubscribeOnSingleObserver<T> extends AtomicReference<Disposable>
44 implements SingleObserver<T>, Disposable, Runnable {
45
46 private static final long serialVersionUID = 3256698449646456986L;
47
48 final SingleObserver<? super T> downstream;
49
50 final Scheduler scheduler;
51
52 Disposable ds;
53
54 UnsubscribeOnSingleObserver(SingleObserver<? super T> actual, Scheduler scheduler) {
55 this.downstream = actual;
56 this.scheduler = scheduler;
57 }
58
59 @Override
60 public void dispose() {
61 Disposable d = getAndSet(DisposableHelper.DISPOSED);
62 if (d != DisposableHelper.DISPOSED) {
63 this.ds = d;
64 scheduler.scheduleDirect(this);
65 }
66 }
67
68 @Override
69 public void run() {
70 ds.dispose();
71 }
72
73 @Override
74 public boolean isDisposed() {
75 return DisposableHelper.isDisposed(get());
76 }
77
78 @Override
79 public void onSubscribe(Disposable d) {
80 if (DisposableHelper.setOnce(this, d)) {
81 downstream.onSubscribe(this);
82 }
83 }
84
85 @Override
86 public void onSuccess(T value) {
87 downstream.onSuccess(value);
88 }
89
90 @Override
91 public void onError(Throwable e) {
92 downstream.onError(e);
93 }
94 }
95 }