Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/completable/CompletableDetach.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.completable;
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 Completable terminates.
22 * <p>History: 2.1.5 - experimental
23 * @since 2.2
24 */
25 public final class CompletableDetach extends Completable {
26
27 final CompletableSource source;
28
29 public CompletableDetach(CompletableSource source) {
30 this.source = source;
31 }
32
33 @Override
34 protected void subscribeActual(CompletableObserver observer) {
35 source.subscribe(new DetachCompletableObserver(observer));
36 }
37
38 static final class DetachCompletableObserver implements CompletableObserver, Disposable {
39
40 CompletableObserver downstream;
41
42 Disposable upstream;
43
44 DetachCompletableObserver(CompletableObserver downstream) {
45 this.downstream = downstream;
46 }
47
48 @Override
49 public void dispose() {
50 downstream = null;
51 upstream.dispose();
52 upstream = DisposableHelper.DISPOSED;
53 }
54
55 @Override
56 public boolean isDisposed() {
57 return upstream.isDisposed();
58 }
59
60 @Override
61 public void onSubscribe(Disposable d) {
62 if (DisposableHelper.validate(this.upstream, d)) {
63 this.upstream = d;
64
65 downstream.onSubscribe(this);
66 }
67 }
68
69 @Override
70 public void onError(Throwable e) {
71 upstream = DisposableHelper.DISPOSED;
72 CompletableObserver a = downstream;
73 if (a != null) {
74 downstream = null;
75 a.onError(e);
76 }
77 }
78
79 @Override
80 public void onComplete() {
81 upstream = DisposableHelper.DISPOSED;
82 CompletableObserver a = downstream;
83 if (a != null) {
84 downstream = null;
85 a.onComplete();
86 }
87 }
88 }
89 }