Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleDelayWithObservable.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.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.internal.observers.ResumeSingleObserver;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 public final class SingleDelayWithObservable<T, U> extends Single<T> {
25
26 final SingleSource<T> source;
27
28 final ObservableSource<U> other;
29
30 public SingleDelayWithObservable(SingleSource<T> source, ObservableSource<U> other) {
31 this.source = source;
32 this.other = other;
33 }
34
35 @Override
36 protected void subscribeActual(SingleObserver<? super T> observer) {
37 other.subscribe(new OtherSubscriber<>(observer, source));
38 }
39
40 static final class OtherSubscriber<T, U>
41 extends AtomicReference<Disposable>
42 implements Observer<U>, Disposable {
43
44 private static final long serialVersionUID = -8565274649390031272L;
45
46 final SingleObserver<? super T> downstream;
47
48 final SingleSource<T> source;
49
50 boolean done;
51
52 OtherSubscriber(SingleObserver<? super T> actual, SingleSource<T> source) {
53 this.downstream = actual;
54 this.source = source;
55 }
56
57 @Override
58 public void onSubscribe(Disposable d) {
59 if (DisposableHelper.setOnce(this, d)) {
60
61 downstream.onSubscribe(this);
62 }
63 }
64
65 @Override
66 public void onNext(U value) {
67 get().dispose();
68 onComplete();
69 }
70
71 @Override
72 public void onError(Throwable e) {
73 if (done) {
74 RxJavaPlugins.onError(e);
75 return;
76 }
77 done = true;
78 downstream.onError(e);
79 }
80
81 @Override
82 public void onComplete() {
83 if (done) {
84 return;
85 }
86 done = true;
87 source.subscribe(new ResumeSingleObserver<>(this, downstream));
88 }
89
90 @Override
91 public void dispose() {
92 DisposableHelper.dispose(this);
93 }
94
95 @Override
96 public boolean isDisposed() {
97 return DisposableHelper.isDisposed(get());
98 }
99 }
100 }