Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableBlockingSubscribe.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.flowable;
15
16 import java.util.Objects;
17 import java.util.concurrent.*;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.functions.*;
22 import io.reactivex.rxjava3.internal.functions.*;
23 import io.reactivex.rxjava3.internal.subscribers.*;
24 import io.reactivex.rxjava3.internal.util.*;
25
26 /**
27 * Utility methods to consume a Publisher in a blocking manner with callbacks or Subscriber.
28 */
29 public final class FlowableBlockingSubscribe {
30
31 /** Utility class. */
32 private FlowableBlockingSubscribe() {
33 throw new IllegalStateException("No instances!");
34 }
35
36 /**
37 * Subscribes to the source and calls the Subscriber methods on the current thread.
38 * <p>
39 * @param source the source publisher
40 * The cancellation and backpressure is composed through.
41 * @param subscriber the subscriber to forward events and calls to in the current thread
42 * @param <T> the value type
43 */
44 public static <T> void subscribe(Publisher<? extends T> source, Subscriber<? super T> subscriber) {
45 final BlockingQueue<Object> queue = new LinkedBlockingQueue<>();
46
47 BlockingSubscriber<T> bs = new BlockingSubscriber<>(queue);
48
49 source.subscribe(bs);
50
51 try {
52 for (;;) {
53 if (bs.isCancelled()) {
54 break;
55 }
56 Object v = queue.poll();
57 if (v == null) {
58 if (bs.isCancelled()) {
59 break;
60 }
61 BlockingHelper.verifyNonBlocking();
62 v = queue.take();
63 }
64 if (bs.isCancelled()) {
65 break;
66 }
67 if (v == BlockingSubscriber.TERMINATED
68 || NotificationLite.acceptFull(v, subscriber)) {
69 break;
70 }
71 }
72 } catch (InterruptedException e) {
73 bs.cancel();
74 subscriber.onError(e);
75 }
76 }
77
78 /**
79 * Runs the source observable to a terminal event, ignoring any values and rethrowing any exception.
80 * @param source the source to await
81 * @param <T> the value type
82 */
83 public static <T> void subscribe(Publisher<? extends T> source) {
84 BlockingIgnoringReceiver callback = new BlockingIgnoringReceiver();
85 LambdaSubscriber<T> ls = new LambdaSubscriber<>(Functions.emptyConsumer(),
86 callback, callback, Functions.REQUEST_MAX);
87
88 source.subscribe(ls);
89
90 BlockingHelper.awaitForComplete(callback, ls);
91 Throwable e = callback.error;
92 if (e != null) {
93 throw ExceptionHelper.wrapOrThrow(e);
94 }
95 }
96
97 /**
98 * Subscribes to the source and calls the given actions on the current thread.
99 * @param o the source publisher
100 * @param onNext the callback action for each source value
101 * @param onError the callback action for an error event
102 * @param onComplete the callback action for the completion event.
103 * @param <T> the value type
104 */
105 public static <T> void subscribe(Publisher<? extends T> o, final Consumer<? super T> onNext,
106 final Consumer<? super Throwable> onError, final Action onComplete) {
107 Objects.requireNonNull(onNext, "onNext is null");
108 Objects.requireNonNull(onError, "onError is null");
109 Objects.requireNonNull(onComplete, "onComplete is null");
110 subscribe(o, new LambdaSubscriber<T>(onNext, onError, onComplete, Functions.REQUEST_MAX));
111 }
112
113 /**
114 * Subscribes to the source and calls the given actions on the current thread.
115 * @param o the source publisher
116 * @param onNext the callback action for each source value
117 * @param onError the callback action for an error event
118 * @param onComplete the callback action for the completion event.
119 * @param bufferSize the number of elements to prefetch from the source Publisher
120 * @param <T> the value type
121 */
122 public static <T> void subscribe(Publisher<? extends T> o, final Consumer<? super T> onNext,
123 final Consumer<? super Throwable> onError, final Action onComplete, int bufferSize) {
124 Objects.requireNonNull(onNext, "onNext is null");
125 Objects.requireNonNull(onError, "onError is null");
126 Objects.requireNonNull(onComplete, "onComplete is null");
127 ObjectHelper.verifyPositive(bufferSize, "number > 0 required");
128 subscribe(o, new BoundedSubscriber<T>(onNext, onError, onComplete, Functions.boundedConsumer(bufferSize),
129 bufferSize));
130 }
131 }