| 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.observers; |
| 15 |
|
| 16 |
import java.util.concurrent.CountDownLatch; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 21 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 22 |
import io.reactivex.rxjava3.internal.util.BlockingHelper; |
| 23 |
|
| 24 |
/** |
| 25 |
* Blocks until the upstream terminates and dispatches the outcome to |
| 26 |
* the actual observer. |
| 27 |
* |
| 28 |
* @param <T> the element type of the source |
| 29 |
* @since 3.0.0 |
| 30 |
*/ |
| 31 |
public final class BlockingDisposableMultiObserver<T> |
| 32 |
extends CountDownLatch |
| 33 |
implements MaybeObserver<T>, SingleObserver<T>, CompletableObserver, Disposable { |
| 34 |
|
| 35 |
T value; |
| 36 |
Throwable error; |
| 37 |
|
| 38 |
final SequentialDisposable upstream; |
| 39 |
|
| 40 |
public BlockingDisposableMultiObserver() { |
| 41 |
super(1); |
| 42 |
upstream = new SequentialDisposable(); |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public void dispose() { |
| 47 |
upstream.dispose(); |
| 48 |
countDown(); |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public boolean isDisposed() { |
| 53 |
return upstream.isDisposed(); |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public void onSubscribe(@NonNull Disposable d) { |
| 58 |
DisposableHelper.setOnce(upstream, d); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void onSuccess(@NonNull T t) { |
| 63 |
this.value = t; |
| 64 |
upstream.lazySet(Disposable.disposed()); |
| 65 |
countDown(); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void onError(@NonNull Throwable e) { |
| 70 |
this.error = e; |
| 71 |
upstream.lazySet(Disposable.disposed()); |
| 72 |
countDown(); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void onComplete() { |
| 77 |
upstream.lazySet(Disposable.disposed()); |
| 78 |
countDown(); |
| 79 |
} |
| 80 |
|
| 81 |
public void blockingConsume(CompletableObserver observer) { |
| 82 |
if (getCount() != 0) { |
| 83 |
try { |
| 84 |
BlockingHelper.verifyNonBlocking(); |
| 85 |
await(); |
| 86 |
} catch (InterruptedException ex) { |
| 87 |
dispose(); |
| 88 |
observer.onError(ex); |
| 89 |
return; |
| 90 |
} |
| 91 |
} |
| 92 |
if (isDisposed()) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
Throwable ex = error; |
| 97 |
if (ex != null) { |
| 98 |
observer.onError(ex); |
| 99 |
} else { |
| 100 |
observer.onComplete(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public void blockingConsume(SingleObserver<? super T> observer) { |
| 105 |
if (getCount() != 0) { |
| 106 |
try { |
| 107 |
BlockingHelper.verifyNonBlocking(); |
| 108 |
await(); |
| 109 |
} catch (InterruptedException ex) { |
| 110 |
dispose(); |
| 111 |
observer.onError(ex); |
| 112 |
return; |
| 113 |
} |
| 114 |
} |
| 115 |
if (isDisposed()) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
Throwable ex = error; |
| 120 |
if (ex != null) { |
| 121 |
observer.onError(ex); |
| 122 |
} else { |
| 123 |
observer.onSuccess(value); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public void blockingConsume(MaybeObserver<? super T> observer) { |
| 128 |
if (getCount() != 0) { |
| 129 |
try { |
| 130 |
BlockingHelper.verifyNonBlocking(); |
| 131 |
await(); |
| 132 |
} catch (InterruptedException ex) { |
| 133 |
dispose(); |
| 134 |
observer.onError(ex); |
| 135 |
return; |
| 136 |
} |
| 137 |
} |
| 138 |
if (isDisposed()) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
Throwable ex = error; |
| 143 |
if (ex != null) { |
| 144 |
observer.onError(ex); |
| 145 |
} else { |
| 146 |
T v = value; |
| 147 |
if (v == null) { |
| 148 |
observer.onComplete(); |
| 149 |
} else { |
| 150 |
observer.onSuccess(v); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |