| 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.observers; |
| 31 |
|
| 32 |
import java.util.concurrent.atomic.AtomicReference; |
| 33 |
|
| 34 |
import io.reactivex.rxjava3.disposables.*; |
| 35 |
import io.reactivex.rxjava3.exceptions.*; |
| 36 |
import io.reactivex.rxjava3.functions.*; |
| 37 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 38 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 39 |
import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection; |
| 40 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 41 |
|
| 42 |
/** |
| 43 |
* Wraps lambda callbacks and when the upstream terminates or the observer gets disposed, |
| 44 |
* removes itself from a {@link io.reactivex.rxjava3.disposables.CompositeDisposable}. |
| 45 |
* <p>History: 0.18.0 @ RxJavaExtensions |
| 46 |
* @since 3.1.0 |
| 47 |
*/ |
| 48 |
abstract class AbstractDisposableAutoRelease |
| 49 |
extends AtomicReference<Disposable> |
| 50 |
implements Disposable, LambdaConsumerIntrospection { |
| 51 |
|
| 52 |
private static final long serialVersionUID = 8924480688481408726L; |
| 53 |
|
| 54 |
final AtomicReference<DisposableContainer> composite; |
| 55 |
|
| 56 |
final Consumer<? super Throwable> onError; |
| 57 |
|
| 58 |
final Action onComplete; |
| 59 |
|
| 60 |
AbstractDisposableAutoRelease( |
| 61 |
DisposableContainer composite, |
| 62 |
Consumer<? super Throwable> onError, |
| 63 |
Action onComplete |
| 64 |
) { |
| 65 |
this.onError = onError; |
| 66 |
this.onComplete = onComplete; |
| 67 |
this.composite = new AtomicReference<>(composite); |
| 68 |
} |
| 69 |
|
| 70 |
public final void onError(Throwable t) { |
| 71 |
if (get() != DisposableHelper.DISPOSED) { |
| 72 |
lazySet(DisposableHelper.DISPOSED); |
| 73 |
try { |
| 74 |
onError.accept(t); |
| 75 |
} catch (Throwable e) { |
| 76 |
Exceptions.throwIfFatal(e); |
| 77 |
RxJavaPlugins.onError(new CompositeException(t, e)); |
| 78 |
} |
| 79 |
} else { |
| 80 |
RxJavaPlugins.onError(t); |
| 81 |
} |
| 82 |
removeSelf(); |
| 83 |
} |
| 84 |
|
| 85 |
public final void onComplete() { |
| 86 |
if (get() != DisposableHelper.DISPOSED) { |
| 87 |
lazySet(DisposableHelper.DISPOSED); |
| 88 |
try { |
| 89 |
onComplete.run(); |
| 90 |
} catch (Throwable e) { |
| 91 |
Exceptions.throwIfFatal(e); |
| 92 |
RxJavaPlugins.onError(e); |
| 93 |
} |
| 94 |
} |
| 95 |
removeSelf(); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public final void dispose() { |
| 100 |
DisposableHelper.dispose(this); |
| 101 |
removeSelf(); |
| 102 |
} |
| 103 |
|
| 104 |
final void removeSelf() { |
| 105 |
DisposableContainer c = composite.getAndSet(null); |
| 106 |
if (c != null) { |
| 107 |
c.delete(this); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public final boolean isDisposed() { |
| 113 |
return DisposableHelper.isDisposed(get()); |
| 114 |
} |
| 115 |
|
| 116 |
public final void onSubscribe(Disposable d) { |
| 117 |
DisposableHelper.setOnce(this, d); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public final boolean hasCustomOnError() { |
| 122 |
return onError != Functions.ON_ERROR_MISSING; |
| 123 |
} |
| 124 |
|
| 125 |
} |