Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleDoOnLifecycle.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.annotations.NonNull;
17 import io.reactivex.rxjava3.core.*;
18 import io.reactivex.rxjava3.disposables.Disposable;
19 import io.reactivex.rxjava3.exceptions.Exceptions;
20 import io.reactivex.rxjava3.functions.*;
21 import io.reactivex.rxjava3.internal.disposables.*;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 /**
25 * Invokes callbacks upon {@code onSubscribe} from upstream and
26 * {@code dispose} from downstream.
27 *
28 * @param <T> the element type of the flow
29 * @since 3.0.0
30 */
31 public final class SingleDoOnLifecycle<T> extends Single<T> {
32
33 final Single<T> source;
34
35 final Consumer<? super Disposable> onSubscribe;
36
37 final Action onDispose;
38
39 public SingleDoOnLifecycle(Single<T> upstream, Consumer<? super Disposable> onSubscribe,
40 Action onDispose) {
41 this.source = upstream;
42 this.onSubscribe = onSubscribe;
43 this.onDispose = onDispose;
44 }
45
46 @Override
47 protected void subscribeActual(SingleObserver<? super T> observer) {
48 source.subscribe(new SingleLifecycleObserver<>(observer, onSubscribe, onDispose));
49 }
50
51 static final class SingleLifecycleObserver<T> implements SingleObserver<T>, Disposable {
52
53 final SingleObserver<? super T> downstream;
54
55 final Consumer<? super Disposable> onSubscribe;
56
57 final Action onDispose;
58
59 Disposable upstream;
60
61 SingleLifecycleObserver(SingleObserver<? super T> downstream, Consumer<? super Disposable> onSubscribe, Action onDispose) {
62 this.downstream = downstream;
63 this.onSubscribe = onSubscribe;
64 this.onDispose = onDispose;
65 }
66
67 @Override
68 public void onSubscribe(@NonNull Disposable d) {
69 // this way, multiple calls to onSubscribe can show up in tests that use doOnSubscribe to validate behavior
70 try {
71 onSubscribe.accept(d);
72 } catch (Throwable e) {
73 Exceptions.throwIfFatal(e);
74 d.dispose();
75 this.upstream = DisposableHelper.DISPOSED;
76 EmptyDisposable.error(e, downstream);
77 return;
78 }
79 if (DisposableHelper.validate(this.upstream, d)) {
80 this.upstream = d;
81 downstream.onSubscribe(this);
82 }
83 }
84
85 @Override
86 public void onSuccess(@NonNull T t) {
87 if (upstream != DisposableHelper.DISPOSED) {
88 upstream = DisposableHelper.DISPOSED;
89 downstream.onSuccess(t);
90 }
91 }
92
93 @Override
94 public void onError(@NonNull Throwable e) {
95 if (upstream != DisposableHelper.DISPOSED) {
96 upstream = DisposableHelper.DISPOSED;
97 downstream.onError(e);
98 } else {
99 RxJavaPlugins.onError(e);
100 }
101 }
102
103 @Override
104 public void dispose() {
105 try {
106 onDispose.run();
107 } catch (Throwable e) {
108 Exceptions.throwIfFatal(e);
109 RxJavaPlugins.onError(e);
110 }
111 upstream.dispose();
112 upstream = DisposableHelper.DISPOSED;
113 }
114
115 @Override
116 public boolean isDisposed() {
117 return upstream.isDisposed();
118 }
119 }
120 }