| 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.observable; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.core.*; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 19 |
import io.reactivex.rxjava3.internal.util.EmptyComponent; |
| 20 |
|
| 21 |
/** |
| 22 |
* Breaks the links between the upstream and the downstream (the Disposable and |
| 23 |
* the Observer references) when the sequence terminates or gets disposed. |
| 24 |
* |
| 25 |
* @param <T> the value type |
| 26 |
*/ |
| 27 |
public final class ObservableDetach<T> extends AbstractObservableWithUpstream<T, T> { |
| 28 |
|
| 29 |
public ObservableDetach(ObservableSource<T> source) { |
| 30 |
super(source); |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
protected void subscribeActual(Observer<? super T> observer) { |
| 35 |
source.subscribe(new DetachObserver<>(observer)); |
| 36 |
} |
| 37 |
|
| 38 |
static final class DetachObserver<T> implements Observer<T>, Disposable { |
| 39 |
|
| 40 |
Observer<? super T> downstream; |
| 41 |
|
| 42 |
Disposable upstream; |
| 43 |
|
| 44 |
DetachObserver(Observer<? super T> downstream) { |
| 45 |
this.downstream = downstream; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public void dispose() { |
| 50 |
Disposable d = this.upstream; |
| 51 |
this.upstream = EmptyComponent.INSTANCE; |
| 52 |
this.downstream = EmptyComponent.asObserver(); |
| 53 |
d.dispose(); |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public boolean isDisposed() { |
| 58 |
return upstream.isDisposed(); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void onSubscribe(Disposable d) { |
| 63 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 64 |
this.upstream = d; |
| 65 |
|
| 66 |
downstream.onSubscribe(this); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void onNext(T t) { |
| 72 |
downstream.onNext(t); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void onError(Throwable t) { |
| 77 |
Observer<? super T> a = downstream; |
| 78 |
this.upstream = EmptyComponent.INSTANCE; |
| 79 |
this.downstream = EmptyComponent.asObserver(); |
| 80 |
a.onError(t); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void onComplete() { |
| 85 |
Observer<? super T> a = downstream; |
| 86 |
this.upstream = EmptyComponent.INSTANCE; |
| 87 |
this.downstream = EmptyComponent.asObserver(); |
| 88 |
a.onComplete(); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|