Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeDoOnLifecycle.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.maybe;
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 MaybeDoOnLifecycle<T> extends AbstractMaybeWithUpstream<T, T> {
32
33 final Consumer<? super Disposable> onSubscribe;
34
35 final Action onDispose;
36
37 public MaybeDoOnLifecycle(Maybe<T> upstream, Consumer<? super Disposable> onSubscribe,
38 Action onDispose) {
39 super(upstream);
40 this.onSubscribe = onSubscribe;
41 this.onDispose = onDispose;
42 }
43
44 @Override
45 protected void subscribeActual(MaybeObserver<? super T> observer) {
46 source.subscribe(new MaybeLifecycleObserver<>(observer, onSubscribe, onDispose));
47 }
48
49 static final class MaybeLifecycleObserver<T> implements MaybeObserver<T>, Disposable {
50
51 final MaybeObserver<? super T> downstream;
52
53 final Consumer<? super Disposable> onSubscribe;
54
55 final Action onDispose;
56
57 Disposable upstream;
58
59 MaybeLifecycleObserver(MaybeObserver<? super T> downstream, Consumer<? super Disposable> onSubscribe, Action onDispose) {
60 this.downstream = downstream;
61 this.onSubscribe = onSubscribe;
62 this.onDispose = onDispose;
63 }
64
65 @Override
66 public void onSubscribe(@NonNull Disposable d) {
67 // this way, multiple calls to onSubscribe can show up in tests that use doOnSubscribe to validate behavior
68 try {
69 onSubscribe.accept(d);
70 } catch (Throwable e) {
71 Exceptions.throwIfFatal(e);
72 d.dispose();
73 this.upstream = DisposableHelper.DISPOSED;
74 EmptyDisposable.error(e, downstream);
75 return;
76 }
77 if (DisposableHelper.validate(this.upstream, d)) {
78 this.upstream = d;
79 downstream.onSubscribe(this);
80 }
81 }
82
83 @Override
84 public void onSuccess(@NonNull T t) {
85 if (upstream != DisposableHelper.DISPOSED) {
86 upstream = DisposableHelper.DISPOSED;
87 downstream.onSuccess(t);
88 }
89 }
90
91 @Override
92 public void onError(@NonNull Throwable e) {
93 if (upstream != DisposableHelper.DISPOSED) {
94 upstream = DisposableHelper.DISPOSED;
95 downstream.onError(e);
96 } else {
97 RxJavaPlugins.onError(e);
98 }
99 }
100
101 @Override
102 public void onComplete() {
103 if (upstream != DisposableHelper.DISPOSED) {
104 upstream = DisposableHelper.DISPOSED;
105 downstream.onComplete();
106 }
107 }
108
109 @Override
110 public void dispose() {
111 try {
112 onDispose.run();
113 } catch (Throwable e) {
114 Exceptions.throwIfFatal(e);
115 RxJavaPlugins.onError(e);
116 }
117 upstream.dispose();
118 upstream = DisposableHelper.DISPOSED;
119 }
120
121 @Override
122 public boolean isDisposed() {
123 return upstream.isDisposed();
124 }
125 }
126 }