Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleDetach.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 io.reactivex.rxjava3.core.*;
17 import io.reactivex.rxjava3.disposables.Disposable;
18 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
19
20 /**
21 * Breaks the references between the upstream and downstream when the Maybe terminates.
22 * <p>History: 2.1.5 - experimental
23 * @param <T> the value type
24 * @since 2.2
25 */
26 public final class SingleDetach<T> extends Single<T> {
27
28 final SingleSource<T> source;
29
30 public SingleDetach(SingleSource<T> source) {
31 this.source = source;
32 }
33
34 @Override
35 protected void subscribeActual(SingleObserver<? super T> observer) {
36 source.subscribe(new DetachSingleObserver<>(observer));
37 }
38
39 static final class DetachSingleObserver<T> implements SingleObserver<T>, Disposable {
40
41 SingleObserver<? super T> downstream;
42
43 Disposable upstream;
44
45 DetachSingleObserver(SingleObserver<? super T> downstream) {
46 this.downstream = downstream;
47 }
48
49 @Override
50 public void dispose() {
51 downstream = null;
52 upstream.dispose();
53 upstream = DisposableHelper.DISPOSED;
54 }
55
56 @Override
57 public boolean isDisposed() {
58 return upstream.isDisposed();
59 }
60
61 @Override
62 public void onSubscribe(Disposable d) {
63 if (DisposableHelper.validate(this.upstream, d)) {
64 this.upstream = d;
65
66 downstream.onSubscribe(this);
67 }
68 }
69
70 @Override
71 public void onSuccess(T value) {
72 upstream = DisposableHelper.DISPOSED;
73 SingleObserver<? super T> a = downstream;
74 if (a != null) {
75 downstream = null;
76 a.onSuccess(value);
77 }
78 }
79
80 @Override
81 public void onError(Throwable e) {
82 upstream = DisposableHelper.DISPOSED;
83 SingleObserver<? super T> a = downstream;
84 if (a != null) {
85 downstream = null;
86 a.onError(e);
87 }
88 }
89 }
90 }