| 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.AtomicInteger; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.*; |
| 20 |
import io.reactivex.rxjava3.internal.util.AtomicThrowable; |
| 21 |
|
| 22 |
public final class CompletableMergeArrayDelayError extends Completable { |
| 23 |
|
| 24 |
final CompletableSource[] sources; |
| 25 |
|
| 26 |
public CompletableMergeArrayDelayError(CompletableSource[] sources) { |
| 27 |
this.sources = sources; |
| 28 |
} |
| 29 |
|
| 30 |
@Override |
| 31 |
public void subscribeActual(final CompletableObserver observer) { |
| 32 |
final CompositeDisposable set = new CompositeDisposable(); |
| 33 |
final AtomicInteger wip = new AtomicInteger(sources.length + 1); |
| 34 |
|
| 35 |
final AtomicThrowable errors = new AtomicThrowable(); |
| 36 |
set.add(new TryTerminateAndReportDisposable(errors)); |
| 37 |
|
| 38 |
observer.onSubscribe(set); |
| 39 |
|
| 40 |
for (CompletableSource c : sources) { |
| 41 |
if (set.isDisposed()) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
if (c == null) { |
| 46 |
Throwable ex = new NullPointerException("A completable source is null"); |
| 47 |
errors.tryAddThrowableOrReport(ex); |
| 48 |
wip.decrementAndGet(); |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
c.subscribe(new MergeInnerCompletableObserver(observer, set, errors, wip)); |
| 53 |
} |
| 54 |
|
| 55 |
if (wip.decrementAndGet() == 0) { |
| 56 |
errors.tryTerminateConsumer(observer); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
static final class TryTerminateAndReportDisposable implements Disposable { |
| 61 |
final AtomicThrowable errors; |
| 62 |
TryTerminateAndReportDisposable(AtomicThrowable errors) { |
| 63 |
this.errors = errors; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void dispose() { |
| 68 |
errors.tryTerminateAndReport(); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public boolean isDisposed() { |
| 73 |
return errors.isTerminated(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
static final class MergeInnerCompletableObserver |
| 78 |
implements CompletableObserver { |
| 79 |
final CompletableObserver downstream; |
| 80 |
final CompositeDisposable set; |
| 81 |
final AtomicThrowable errors; |
| 82 |
final AtomicInteger wip; |
| 83 |
|
| 84 |
MergeInnerCompletableObserver(CompletableObserver observer, CompositeDisposable set, AtomicThrowable error, |
| 85 |
AtomicInteger wip) { |
| 86 |
this.downstream = observer; |
| 87 |
this.set = set; |
| 88 |
this.errors = error; |
| 89 |
this.wip = wip; |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onSubscribe(Disposable d) { |
| 94 |
set.add(d); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onError(Throwable e) { |
| 99 |
if (errors.tryAddThrowableOrReport(e)) { |
| 100 |
tryTerminate(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public void onComplete() { |
| 106 |
tryTerminate(); |
| 107 |
} |
| 108 |
|
| 109 |
void tryTerminate() { |
| 110 |
if (wip.decrementAndGet() == 0) { |
| 111 |
errors.tryTerminateConsumer(downstream); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |