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