| 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 |
/* |
| 15 |
* Copyright 2016-2019 David Karnok |
| 16 |
* |
| 17 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 18 |
* you may not use this file except in compliance with the License. |
| 19 |
* You may obtain a copy of the License at |
| 20 |
* |
| 21 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 22 |
* |
| 23 |
* Unless required by applicable law or agreed to in writing, software |
| 24 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 25 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 |
* See the License for the specific language governing permissions and |
| 27 |
* limitations under the License. |
| 28 |
*/ |
| 29 |
|
| 30 |
package io.reactivex.rxjava3.internal.subscribers; |
| 31 |
|
| 32 |
import java.util.concurrent.atomic.AtomicReference; |
| 33 |
|
| 34 |
import org.reactivestreams.Subscription; |
| 35 |
|
| 36 |
import io.reactivex.rxjava3.core.FlowableSubscriber; |
| 37 |
import io.reactivex.rxjava3.disposables.*; |
| 38 |
import io.reactivex.rxjava3.exceptions.*; |
| 39 |
import io.reactivex.rxjava3.functions.*; |
| 40 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 41 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 42 |
import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection; |
| 43 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 44 |
|
| 45 |
/** |
| 46 |
* Wraps lambda callbacks and when the upstream terminates or this subscriber gets disposed, |
| 47 |
* removes itself from a {@link io.reactivex.rxjava3.disposables.CompositeDisposable}. |
| 48 |
* <p>History: 0.18.0 @ RxJavaExtensions |
| 49 |
* @param <T> the element type consumed |
| 50 |
* @since 3.1.0 |
| 51 |
*/ |
| 52 |
public final class DisposableAutoReleaseSubscriber<T> |
| 53 |
extends AtomicReference<Subscription> |
| 54 |
implements FlowableSubscriber<T>, Disposable, LambdaConsumerIntrospection { |
| 55 |
|
| 56 |
private static final long serialVersionUID = 8924480688481408726L; |
| 57 |
|
| 58 |
final AtomicReference<DisposableContainer> composite; |
| 59 |
|
| 60 |
final Consumer<? super T> onNext; |
| 61 |
|
| 62 |
final Consumer<? super Throwable> onError; |
| 63 |
|
| 64 |
final Action onComplete; |
| 65 |
|
| 66 |
public DisposableAutoReleaseSubscriber( |
| 67 |
DisposableContainer composite, |
| 68 |
Consumer<? super T> onNext, |
| 69 |
Consumer<? super Throwable> onError, |
| 70 |
Action onComplete |
| 71 |
) { |
| 72 |
this.onNext = onNext; |
| 73 |
this.onError = onError; |
| 74 |
this.onComplete = onComplete; |
| 75 |
this.composite = new AtomicReference<>(composite); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void onNext(T t) { |
| 80 |
if (get() != SubscriptionHelper.CANCELLED) { |
| 81 |
try { |
| 82 |
onNext.accept(t); |
| 83 |
} catch (Throwable e) { |
| 84 |
Exceptions.throwIfFatal(e); |
| 85 |
get().cancel(); |
| 86 |
onError(e); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public void onError(Throwable t) { |
| 93 |
if (get() != SubscriptionHelper.CANCELLED) { |
| 94 |
lazySet(SubscriptionHelper.CANCELLED); |
| 95 |
try { |
| 96 |
onError.accept(t); |
| 97 |
} catch (Throwable e) { |
| 98 |
Exceptions.throwIfFatal(e); |
| 99 |
RxJavaPlugins.onError(new CompositeException(t, e)); |
| 100 |
} |
| 101 |
} else { |
| 102 |
RxJavaPlugins.onError(t); |
| 103 |
} |
| 104 |
removeSelf(); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void onComplete() { |
| 109 |
if (get() != SubscriptionHelper.CANCELLED) { |
| 110 |
lazySet(SubscriptionHelper.CANCELLED); |
| 111 |
try { |
| 112 |
onComplete.run(); |
| 113 |
} catch (Throwable e) { |
| 114 |
Exceptions.throwIfFatal(e); |
| 115 |
RxJavaPlugins.onError(e); |
| 116 |
} |
| 117 |
} |
| 118 |
removeSelf(); |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public void dispose() { |
| 123 |
SubscriptionHelper.cancel(this); |
| 124 |
removeSelf(); |
| 125 |
} |
| 126 |
|
| 127 |
void removeSelf() { |
| 128 |
DisposableContainer c = composite.getAndSet(null); |
| 129 |
if (c != null) { |
| 130 |
c.delete(this); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public boolean isDisposed() { |
| 136 |
return SubscriptionHelper.CANCELLED == get(); |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public void onSubscribe(Subscription s) { |
| 141 |
if (SubscriptionHelper.setOnce(this, s)) { |
| 142 |
s.request(Long.MAX_VALUE); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public boolean hasCustomOnError() { |
| 148 |
return onError != Functions.ON_ERROR_MISSING; |
| 149 |
} |
| 150 |
|
| 151 |
} |