| 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 java.util.Objects; |
| 17 |
import java.util.concurrent.*; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.functions.*; |
| 21 |
import io.reactivex.rxjava3.internal.functions.*; |
| 22 |
import io.reactivex.rxjava3.internal.observers.*; |
| 23 |
import io.reactivex.rxjava3.internal.util.*; |
| 24 |
|
| 25 |
/** |
| 26 |
* Utility methods to consume an Observable in a blocking manner with callbacks or Observer. |
| 27 |
*/ |
| 28 |
public final class ObservableBlockingSubscribe { |
| 29 |
|
| 30 |
/** Utility class. */ |
| 31 |
private ObservableBlockingSubscribe() { |
| 32 |
throw new IllegalStateException("No instances!"); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Subscribes to the source and calls the Observer methods on the current thread. |
| 37 |
* <p> |
| 38 |
* @param o the source ObservableSource |
| 39 |
* The call to dispose() is composed through. |
| 40 |
* @param observer the subscriber to forward events and calls to in the current thread |
| 41 |
* @param <T> the value type |
| 42 |
* @throws NullPointerException if {@code observer} is {@code null} |
| 43 |
*/ |
| 44 |
public static <T> void subscribe(ObservableSource<? extends T> o, Observer<? super T> observer) { |
| 45 |
final BlockingQueue<Object> queue = new LinkedBlockingQueue<>(); |
| 46 |
|
| 47 |
BlockingObserver<T> bs = new BlockingObserver<>(queue); |
| 48 |
observer.onSubscribe(bs); |
| 49 |
|
| 50 |
o.subscribe(bs); |
| 51 |
for (;;) { |
| 52 |
if (bs.isDisposed()) { |
| 53 |
break; |
| 54 |
} |
| 55 |
Object v = queue.poll(); |
| 56 |
if (v == null) { |
| 57 |
try { |
| 58 |
v = queue.take(); |
| 59 |
} catch (InterruptedException ex) { |
| 60 |
bs.dispose(); |
| 61 |
observer.onError(ex); |
| 62 |
return; |
| 63 |
} |
| 64 |
} |
| 65 |
if (bs.isDisposed() |
| 66 |
|| v == BlockingObserver.TERMINATED |
| 67 |
|| NotificationLite.acceptFull(v, observer)) { |
| 68 |
break; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Runs the source observable to a terminal event, ignoring any values and rethrowing any exception. |
| 75 |
* @param o the source ObservableSource |
| 76 |
* @param <T> the value type |
| 77 |
*/ |
| 78 |
public static <T> void subscribe(ObservableSource<? extends T> o) { |
| 79 |
BlockingIgnoringReceiver callback = new BlockingIgnoringReceiver(); |
| 80 |
LambdaObserver<T> ls = new LambdaObserver<>(Functions.emptyConsumer(), |
| 81 |
callback, callback, Functions.emptyConsumer()); |
| 82 |
|
| 83 |
o.subscribe(ls); |
| 84 |
|
| 85 |
BlockingHelper.awaitForComplete(callback, ls); |
| 86 |
Throwable e = callback.error; |
| 87 |
if (e != null) { |
| 88 |
throw ExceptionHelper.wrapOrThrow(e); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Subscribes to the source and calls the given actions on the current thread. |
| 94 |
* @param o the source ObservableSource |
| 95 |
* @param onNext the callback action for each source value |
| 96 |
* @param onError the callback action for an error event |
| 97 |
* @param onComplete the callback action for the completion event. |
| 98 |
* @param <T> the value type |
| 99 |
*/ |
| 100 |
public static <T> void subscribe(ObservableSource<? extends T> o, final Consumer<? super T> onNext, |
| 101 |
final Consumer<? super Throwable> onError, final Action onComplete) { |
| 102 |
Objects.requireNonNull(onNext, "onNext is null"); |
| 103 |
Objects.requireNonNull(onError, "onError is null"); |
| 104 |
Objects.requireNonNull(onComplete, "onComplete is null"); |
| 105 |
subscribe(o, new LambdaObserver<T>(onNext, onError, onComplete, Functions.emptyConsumer())); |
| 106 |
} |
| 107 |
} |