Debian Codegraph @

src/main/java/io/reactivex/rxjava3/core/FlowableEmitter.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.core;
15
16 import io.reactivex.rxjava3.annotations.*;
17 import io.reactivex.rxjava3.disposables.Disposable;
18 import io.reactivex.rxjava3.functions.*;
19
20 /**
21 * Abstraction over a Reactive Streams {@link org.reactivestreams.Subscriber} that allows associating
22 * a resource with it and exposes the current number of downstream
23 * requested amount.
24 * <p>
25 * The {@link #onNext(Object)}, {@link #onError(Throwable)}, {@link #tryOnError(Throwable)}
26 * and {@link #onComplete()} methods should be called in a sequential manner, just like
27 * the {@link org.reactivestreams.Subscriber Subscriber}'s methods.
28 * Use the {@code FlowableEmitter} the {@link #serialize()} method returns instead of the original
29 * {@code FlowableEmitter} instance provided by the generator routine if you want to ensure this.
30 * The other methods are thread-safe.
31 * <p>
32 * The emitter allows the registration of a single resource, in the form of a {@link Disposable}
33 * or {@link Cancellable} via {@link #setDisposable(Disposable)} or {@link #setCancellable(Cancellable)}
34 * respectively. The emitter implementations will dispose/cancel this instance when the
35 * downstream cancels the flow or after the event generator logic calls {@link #onError(Throwable)},
36 * {@link #onComplete()} or when {@link #tryOnError(Throwable)} succeeds.
37 * <p>
38 * Only one {@code Disposable} or {@code Cancellable} object can be associated with the emitter at
39 * a time. Calling either {@code set} method will dispose/cancel any previous object. If there
40 * is a need for handling multiple resources, one can create a {@link io.reactivex.rxjava3.disposables.CompositeDisposable}
41 * and associate that with the emitter instead.
42 * <p>
43 * The {@link Cancellable} is logically equivalent to {@code Disposable} but allows using cleanup logic that can
44 * throw a checked exception (such as many {@code close()} methods on Java IO components). Since
45 * the release of resources happens after the terminal events have been delivered or the sequence gets
46 * cancelled, exceptions throw within {@code Cancellable} are routed to the global error handler via
47 * {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable)}.
48 *
49 * @param <T> the value type to emit
50 */
51 public interface FlowableEmitter<@NonNull T> extends Emitter<T> {
52
53 /**
54 * Sets a Disposable on this emitter; any previous {@link Disposable}
55 * or {@link Cancellable} will be disposed/cancelled.
56 * <p>This method is thread-safe.
57 * @param d the disposable, {@code null} is allowed
58 */
59 void setDisposable(@Nullable Disposable d);
60
61 /**
62 * Sets a {@link Cancellable} on this emitter; any previous {@link Disposable}
63 * or {@code Cancellable} will be disposed/cancelled.
64 * <p>This method is thread-safe.
65 * @param c the {@code Cancellable} resource, {@code null} is allowed
66 */
67 void setCancellable(@Nullable Cancellable c);
68
69 /**
70 * The current outstanding request amount.
71 * <p>This method is thread-safe.
72 * @return the current outstanding request amount
73 */
74 long requested();
75
76 /**
77 * Returns true if the downstream cancelled the sequence or the
78 * emitter was terminated via {@link #onError(Throwable)}, {@link #onComplete} or a
79 * successful {@link #tryOnError(Throwable)}.
80 * <p>This method is thread-safe.
81 * @return true if the downstream cancelled the sequence or the emitter was terminated
82 */
83 boolean isCancelled();
84
85 /**
86 * Ensures that calls to {@code onNext}, {@code onError} and {@code onComplete} are properly serialized.
87 * @return the serialized {@link FlowableEmitter}
88 */
89 @NonNull
90 FlowableEmitter<T> serialize();
91
92 /**
93 * Attempts to emit the specified {@link Throwable} error if the downstream
94 * hasn't cancelled the sequence or is otherwise terminated, returning false
95 * if the emission is not allowed to happen due to lifecycle restrictions.
96 * <p>
97 * Unlike {@link #onError(Throwable)}, the {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable) RxjavaPlugins.onError}
98 * is not called if the error could not be delivered.
99 * <p>History: 2.1.1 - experimental
100 * @param t the throwable error to signal if possible
101 * @return true if successful, false if the downstream is not able to accept further
102 * events
103 * @since 2.2
104 */
105 boolean tryOnError(@NonNull Throwable t);
106 }