| 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.completable; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 21 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 22 |
|
| 23 |
/** |
| 24 |
* Terminates the sequence if either the main or the other Completable terminate. |
| 25 |
* <p>History: 2.1.17 - experimental |
| 26 |
* @since 2.2 |
| 27 |
*/ |
| 28 |
public final class CompletableTakeUntilCompletable extends Completable { |
| 29 |
|
| 30 |
final Completable source; |
| 31 |
|
| 32 |
final CompletableSource other; |
| 33 |
|
| 34 |
public CompletableTakeUntilCompletable(Completable source, |
| 35 |
CompletableSource other) { |
| 36 |
this.source = source; |
| 37 |
this.other = other; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
protected void subscribeActual(CompletableObserver observer) { |
| 42 |
TakeUntilMainObserver parent = new TakeUntilMainObserver(observer); |
| 43 |
observer.onSubscribe(parent); |
| 44 |
|
| 45 |
other.subscribe(parent.other); |
| 46 |
source.subscribe(parent); |
| 47 |
} |
| 48 |
|
| 49 |
static final class TakeUntilMainObserver extends AtomicReference<Disposable> |
| 50 |
implements CompletableObserver, Disposable { |
| 51 |
|
| 52 |
private static final long serialVersionUID = 3533011714830024923L; |
| 53 |
|
| 54 |
final CompletableObserver downstream; |
| 55 |
|
| 56 |
final OtherObserver other; |
| 57 |
|
| 58 |
final AtomicBoolean once; |
| 59 |
|
| 60 |
TakeUntilMainObserver(CompletableObserver downstream) { |
| 61 |
this.downstream = downstream; |
| 62 |
this.other = new OtherObserver(this); |
| 63 |
this.once = new AtomicBoolean(); |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void dispose() { |
| 68 |
if (once.compareAndSet(false, true)) { |
| 69 |
DisposableHelper.dispose(this); |
| 70 |
DisposableHelper.dispose(other); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public boolean isDisposed() { |
| 76 |
return once.get(); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void onSubscribe(Disposable d) { |
| 81 |
DisposableHelper.setOnce(this, d); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void onComplete() { |
| 86 |
if (once.compareAndSet(false, true)) { |
| 87 |
DisposableHelper.dispose(other); |
| 88 |
downstream.onComplete(); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onError(Throwable e) { |
| 94 |
if (once.compareAndSet(false, true)) { |
| 95 |
DisposableHelper.dispose(other); |
| 96 |
downstream.onError(e); |
| 97 |
} else { |
| 98 |
RxJavaPlugins.onError(e); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void innerComplete() { |
| 103 |
if (once.compareAndSet(false, true)) { |
| 104 |
DisposableHelper.dispose(this); |
| 105 |
downstream.onComplete(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
void innerError(Throwable e) { |
| 110 |
if (once.compareAndSet(false, true)) { |
| 111 |
DisposableHelper.dispose(this); |
| 112 |
downstream.onError(e); |
| 113 |
} else { |
| 114 |
RxJavaPlugins.onError(e); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
static final class OtherObserver extends AtomicReference<Disposable> |
| 119 |
implements CompletableObserver { |
| 120 |
|
| 121 |
private static final long serialVersionUID = 5176264485428790318L; |
| 122 |
final TakeUntilMainObserver parent; |
| 123 |
|
| 124 |
OtherObserver(TakeUntilMainObserver parent) { |
| 125 |
this.parent = parent; |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public void onSubscribe(Disposable d) { |
| 130 |
DisposableHelper.setOnce(this, d); |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public void onComplete() { |
| 135 |
parent.innerComplete(); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void onError(Throwable e) { |
| 140 |
parent.innerError(e); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |