| 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.atomic.AtomicBoolean; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.exceptions.*; |
| 23 |
import io.reactivex.rxjava3.functions.*; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 25 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 26 |
|
| 27 |
public final class FlowableUsing<T, D> extends Flowable<T> { |
| 28 |
final Supplier<? extends D> resourceSupplier; |
| 29 |
final Function<? super D, ? extends Publisher<? extends T>> sourceSupplier; |
| 30 |
final Consumer<? super D> disposer; |
| 31 |
final boolean eager; |
| 32 |
|
| 33 |
public FlowableUsing(Supplier<? extends D> resourceSupplier, |
| 34 |
Function<? super D, ? extends Publisher<? extends T>> sourceSupplier, |
| 35 |
Consumer<? super D> disposer, |
| 36 |
boolean eager) { |
| 37 |
this.resourceSupplier = resourceSupplier; |
| 38 |
this.sourceSupplier = sourceSupplier; |
| 39 |
this.disposer = disposer; |
| 40 |
this.eager = eager; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public void subscribeActual(Subscriber<? super T> s) { |
| 45 |
D resource; |
| 46 |
|
| 47 |
try { |
| 48 |
resource = resourceSupplier.get(); |
| 49 |
} catch (Throwable e) { |
| 50 |
Exceptions.throwIfFatal(e); |
| 51 |
EmptySubscription.error(e, s); |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
Publisher<? extends T> source; |
| 56 |
try { |
| 57 |
source = Objects.requireNonNull(sourceSupplier.apply(resource), "The sourceSupplier returned a null Publisher"); |
| 58 |
} catch (Throwable e) { |
| 59 |
Exceptions.throwIfFatal(e); |
| 60 |
try { |
| 61 |
disposer.accept(resource); |
| 62 |
} catch (Throwable ex) { |
| 63 |
Exceptions.throwIfFatal(ex); |
| 64 |
EmptySubscription.error(new CompositeException(e, ex), s); |
| 65 |
return; |
| 66 |
} |
| 67 |
EmptySubscription.error(e, s); |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
UsingSubscriber<T, D> us = new UsingSubscriber<>(s, resource, disposer, eager); |
| 72 |
|
| 73 |
source.subscribe(us); |
| 74 |
} |
| 75 |
|
| 76 |
static final class UsingSubscriber<T, D> extends AtomicBoolean implements FlowableSubscriber<T>, Subscription { |
| 77 |
|
| 78 |
private static final long serialVersionUID = 5904473792286235046L; |
| 79 |
|
| 80 |
final Subscriber<? super T> downstream; |
| 81 |
final D resource; |
| 82 |
final Consumer<? super D> disposer; |
| 83 |
final boolean eager; |
| 84 |
|
| 85 |
Subscription upstream; |
| 86 |
|
| 87 |
UsingSubscriber(Subscriber<? super T> actual, D resource, Consumer<? super D> disposer, boolean eager) { |
| 88 |
this.downstream = actual; |
| 89 |
this.resource = resource; |
| 90 |
this.disposer = disposer; |
| 91 |
this.eager = eager; |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void onSubscribe(Subscription s) { |
| 96 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 97 |
this.upstream = s; |
| 98 |
downstream.onSubscribe(this); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void onNext(T t) { |
| 104 |
downstream.onNext(t); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void onError(Throwable t) { |
| 109 |
if (eager) { |
| 110 |
Throwable innerError = null; |
| 111 |
if (compareAndSet(false, true)) { |
| 112 |
try { |
| 113 |
disposer.accept(resource); |
| 114 |
} catch (Throwable e) { |
| 115 |
Exceptions.throwIfFatal(e); |
| 116 |
innerError = e; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if (innerError != null) { |
| 121 |
downstream.onError(new CompositeException(t, innerError)); |
| 122 |
} else { |
| 123 |
downstream.onError(t); |
| 124 |
} |
| 125 |
} else { |
| 126 |
downstream.onError(t); |
| 127 |
disposeResource(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public void onComplete() { |
| 133 |
if (eager) { |
| 134 |
if (compareAndSet(false, true)) { |
| 135 |
try { |
| 136 |
disposer.accept(resource); |
| 137 |
} catch (Throwable e) { |
| 138 |
Exceptions.throwIfFatal(e); |
| 139 |
downstream.onError(e); |
| 140 |
return; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
downstream.onComplete(); |
| 145 |
} else { |
| 146 |
downstream.onComplete(); |
| 147 |
disposeResource(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
public void request(long n) { |
| 153 |
upstream.request(n); |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public void cancel() { |
| 158 |
if (eager) { |
| 159 |
disposeResource(); |
| 160 |
upstream.cancel(); |
| 161 |
upstream = SubscriptionHelper.CANCELLED; |
| 162 |
} else { |
| 163 |
upstream.cancel(); |
| 164 |
upstream = SubscriptionHelper.CANCELLED; |
| 165 |
disposeResource(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
void disposeResource() { |
| 170 |
if (compareAndSet(false, true)) { |
| 171 |
try { |
| 172 |
disposer.accept(resource); |
| 173 |
} catch (Throwable e) { |
| 174 |
Exceptions.throwIfFatal(e); |
| 175 |
// can't call actual.onError unless it is serialized, which is expensive |
| 176 |
RxJavaPlugins.onError(e); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |