Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableDelaySubscriptionOther.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.observable;
15
16 import io.reactivex.rxjava3.core.*;
17 import io.reactivex.rxjava3.disposables.Disposable;
18 import io.reactivex.rxjava3.internal.disposables.SequentialDisposable;
19 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
20
21 /**
22 * Delays the subscription to the main source until the other
23 * observable fires an event or completes.
24 * @param <T> the main type
25 * @param <U> the other value type, ignored
26 */
27 public final class ObservableDelaySubscriptionOther<T, U> extends Observable<T> {
28 final ObservableSource<? extends T> main;
29 final ObservableSource<U> other;
30
31 public ObservableDelaySubscriptionOther(ObservableSource<? extends T> main, ObservableSource<U> other) {
32 this.main = main;
33 this.other = other;
34 }
35
36 @Override
37 public void subscribeActual(final Observer<? super T> child) {
38 final SequentialDisposable serial = new SequentialDisposable();
39 child.onSubscribe(serial);
40
41 Observer<U> otherObserver = new DelayObserver(serial, child);
42
43 other.subscribe(otherObserver);
44 }
45
46 final class DelayObserver implements Observer<U> {
47 final SequentialDisposable serial;
48 final Observer<? super T> child;
49 boolean done;
50
51 DelayObserver(SequentialDisposable serial, Observer<? super T> child) {
52 this.serial = serial;
53 this.child = child;
54 }
55
56 @Override
57 public void onSubscribe(Disposable d) {
58 serial.update(d);
59 }
60
61 @Override
62 public void onNext(U t) {
63 onComplete();
64 }
65
66 @Override
67 public void onError(Throwable e) {
68 if (done) {
69 RxJavaPlugins.onError(e);
70 return;
71 }
72 done = true;
73 child.onError(e);
74 }
75
76 @Override
77 public void onComplete() {
78 if (done) {
79 return;
80 }
81 done = true;
82
83 main.subscribe(new OnComplete());
84 }
85
86 final class OnComplete implements Observer<T> {
87 @Override
88 public void onSubscribe(Disposable d) {
89 serial.update(d);
90 }
91
92 @Override
93 public void onNext(T value) {
94 child.onNext(value);
95 }
96
97 @Override
98 public void onError(Throwable e) {
99 child.onError(e);
100 }
101
102 @Override
103 public void onComplete() {
104 child.onComplete();
105 }
106 }
107 }
108 }