Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableFromAction.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.observable;
15
16 import io.reactivex.rxjava3.core.*;
17 import io.reactivex.rxjava3.exceptions.Exceptions;
18 import io.reactivex.rxjava3.functions.*;
19 import io.reactivex.rxjava3.internal.fuseable.CancellableQueueFuseable;
20 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
21
22 /**
23 * Executes an {@link Action} and signals its exception or completes normally.
24 *
25 * @param <T> the value type
26 * @since 3.0.0
27 */
28 public final class ObservableFromAction<T> extends Observable<T> implements Supplier<T> {
29
30 final Action action;
31
32 public ObservableFromAction(Action action) {
33 this.action = action;
34 }
35
36 @Override
37 protected void subscribeActual(Observer<? super T> observer) {
38 CancellableQueueFuseable<T> qs = new CancellableQueueFuseable<>();
39 observer.onSubscribe(qs);
40
41 if (!qs.isDisposed()) {
42
43 try {
44 action.run();
45 } catch (Throwable ex) {
46 Exceptions.throwIfFatal(ex);
47 if (!qs.isDisposed()) {
48 observer.onError(ex);
49 } else {
50 RxJavaPlugins.onError(ex);
51 }
52 return;
53 }
54
55 if (!qs.isDisposed()) {
56 observer.onComplete();
57 }
58 }
59 }
60
61 @Override
62 public T get() throws Throwable {
63 action.run();
64 return null; // considered as onComplete()
65 }
66 }