Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleDoOnDispose.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.exceptions.Exceptions;
21 import io.reactivex.rxjava3.functions.Action;
22 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
23 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
24
25 public final class SingleDoOnDispose<T> extends Single<T> {
26 final SingleSource<T> source;
27
28 final Action onDispose;
29
30 public SingleDoOnDispose(SingleSource<T> source, Action onDispose) {
31 this.source = source;
32 this.onDispose = onDispose;
33 }
34
35 @Override
36 protected void subscribeActual(final SingleObserver<? super T> observer) {
37
38 source.subscribe(new DoOnDisposeObserver<>(observer, onDispose));
39 }
40
41 static final class DoOnDisposeObserver<T>
42 extends AtomicReference<Action>
43 implements SingleObserver<T>, Disposable {
44 private static final long serialVersionUID = -8583764624474935784L;
45
46 final SingleObserver<? super T> downstream;
47
48 Disposable upstream;
49
50 DoOnDisposeObserver(SingleObserver<? super T> actual, Action onDispose) {
51 this.downstream = actual;
52 this.lazySet(onDispose);
53 }
54
55 @Override
56 public void dispose() {
57 Action a = getAndSet(null);
58 if (a != null) {
59 try {
60 a.run();
61 } catch (Throwable ex) {
62 Exceptions.throwIfFatal(ex);
63 RxJavaPlugins.onError(ex);
64 }
65 upstream.dispose();
66 }
67 }
68
69 @Override
70 public boolean isDisposed() {
71 return upstream.isDisposed();
72 }
73
74 @Override
75 public void onSubscribe(Disposable d) {
76 if (DisposableHelper.validate(this.upstream, d)) {
77 this.upstream = d;
78 downstream.onSubscribe(this);
79 }
80 }
81
82 @Override
83 public void onSuccess(T value) {
84 downstream.onSuccess(value);
85 }
86
87 @Override
88 public void onError(Throwable e) {
89 downstream.onError(e);
90 }
91 }
92
93 }