Debian Codegraph @

src/main/java/io/reactivex/rxjava3/core/CompletableEmitter.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.Cancellable;
19
20 /**
21 * Abstraction over an RxJava {@link CompletableObserver} that allows associating
22 * a resource with it.
23 * <p>
24 * All methods are safe to call from multiple threads, but note that there is no guarantee
25 * whose terminal event will win and get delivered to the downstream.
26 * <p>
27 * Calling {@link #onComplete()} multiple times has no effect.
28 * Calling {@link #onError(Throwable)} multiple times or after {@code onComplete} will route the
29 * exception into the global error handler via {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable)}.
30 * <p>
31 * The emitter allows the registration of a single resource, in the form of a {@link Disposable}
32 * or {@link Cancellable} via {@link #setDisposable(Disposable)} or {@link #setCancellable(Cancellable)}
33 * respectively. The emitter implementations will dispose/cancel this instance when the
34 * downstream cancels the flow or after the event generator logic calls
35 * {@link #onError(Throwable)}, {@link #onComplete()} or when {@link #tryOnError(Throwable)} succeeds.
36 * <p>
37 * Only one {@code Disposable} or {@code Cancellable} object can be associated with the emitter at
38 * a time. Calling either {@code set} method will dispose/cancel any previous object. If there
39 * is a need for handling multiple resources, one can create a {@link io.reactivex.rxjava3.disposables.CompositeDisposable}
40 * and associate that with the emitter instead.
41 * <p>
42 * The {@link Cancellable} is logically equivalent to {@code Disposable} but allows using cleanup logic that can
43 * throw a checked exception (such as many {@code close()} methods on Java IO components). Since
44 * the release of resources happens after the terminal events have been delivered or the sequence gets
45 * cancelled, exceptions throw within {@code Cancellable} are routed to the global error handler via
46 * {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable)}.
47 */
48 public interface CompletableEmitter {
49
50 /**
51 * Signal the completion.
52 */
53 void onComplete();
54
55 /**
56 * Signal an exception.
57 * @param t the exception, not null
58 */
59 void onError(@NonNull Throwable t);
60
61 /**
62 * Sets a Disposable on this emitter; any previous {@link Disposable}
63 * or {@link Cancellable} will be disposed/cancelled.
64 * @param d the disposable, null is allowed
65 */
66 void setDisposable(@Nullable Disposable d);
67
68 /**
69 * Sets a Cancellable on this emitter; any previous {@link Disposable}
70 * or {@link Cancellable} will be disposed/cancelled.
71 * @param c the cancellable resource, null is allowed
72 */
73 void setCancellable(@Nullable Cancellable c);
74
75 /**
76 * Returns true if the downstream disposed the sequence or the
77 * emitter was terminated via {@link #onError(Throwable)},
78 * {@link #onComplete} or a successful {@link #tryOnError(Throwable)}.
79 * <p>This method is thread-safe.
80 * @return true if the downstream disposed the sequence or the emitter was terminated
81 */
82 boolean isDisposed();
83
84 /**
85 * Attempts to emit the specified {@link Throwable} error if the downstream
86 * hasn't cancelled the sequence or is otherwise terminated, returning false
87 * if the emission is not allowed to happen due to lifecycle restrictions.
88 * <p>
89 * Unlike {@link #onError(Throwable)}, the {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable) RxjavaPlugins.onError}
90 * is not called if the error could not be delivered.
91 * <p>History: 2.1.1 - experimental
92 * @param t the throwable error to signal if possible
93 * @return true if successful, false if the downstream is not able to accept further
94 * events
95 * @since 2.2
96 */
97 boolean tryOnError(@NonNull Throwable t);
98 }