| 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.observers; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.core.Observer; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 19 |
import io.reactivex.rxjava3.functions.*; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 21 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 22 |
|
| 23 |
public final class DisposableLambdaObserver<T> implements Observer<T>, Disposable { |
| 24 |
final Observer<? super T> downstream; |
| 25 |
final Consumer<? super Disposable> onSubscribe; |
| 26 |
final Action onDispose; |
| 27 |
|
| 28 |
Disposable upstream; |
| 29 |
|
| 30 |
public DisposableLambdaObserver(Observer<? super T> actual, |
| 31 |
Consumer<? super Disposable> onSubscribe, |
| 32 |
Action onDispose) { |
| 33 |
this.downstream = actual; |
| 34 |
this.onSubscribe = onSubscribe; |
| 35 |
this.onDispose = onDispose; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public void onSubscribe(Disposable d) { |
| 40 |
// this way, multiple calls to onSubscribe can show up in tests that use doOnSubscribe to validate behavior |
| 41 |
try { |
| 42 |
onSubscribe.accept(d); |
| 43 |
} catch (Throwable e) { |
| 44 |
Exceptions.throwIfFatal(e); |
| 45 |
d.dispose(); |
| 46 |
this.upstream = DisposableHelper.DISPOSED; |
| 47 |
EmptyDisposable.error(e, downstream); |
| 48 |
return; |
| 49 |
} |
| 50 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 51 |
this.upstream = d; |
| 52 |
downstream.onSubscribe(this); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public void onNext(T t) { |
| 58 |
downstream.onNext(t); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void onError(Throwable t) { |
| 63 |
if (upstream != DisposableHelper.DISPOSED) { |
| 64 |
upstream = DisposableHelper.DISPOSED; |
| 65 |
downstream.onError(t); |
| 66 |
} else { |
| 67 |
RxJavaPlugins.onError(t); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onComplete() { |
| 73 |
if (upstream != DisposableHelper.DISPOSED) { |
| 74 |
upstream = DisposableHelper.DISPOSED; |
| 75 |
downstream.onComplete(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void dispose() { |
| 81 |
Disposable d = upstream; |
| 82 |
if (d != DisposableHelper.DISPOSED) { |
| 83 |
upstream = DisposableHelper.DISPOSED; |
| 84 |
try { |
| 85 |
onDispose.run(); |
| 86 |
} catch (Throwable e) { |
| 87 |
Exceptions.throwIfFatal(e); |
| 88 |
RxJavaPlugins.onError(e); |
| 89 |
} |
| 90 |
d.dispose(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public boolean isDisposed() { |
| 96 |
return upstream.isDisposed(); |
| 97 |
} |
| 98 |
} |