-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2.x Add concatMapCompletable() to Observable #5649
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
218df44
Initial implementation of Observable.concatMapCompletable()
dweebo 51bfef3
Update serialVersionUID
dweebo 0aca61f
Fix javadoc and verify prefetch is positive
dweebo 506dc7e
Put back auto-changed whitespace
dweebo bbf3b2b
Put back more whitespace intellij is removing
dweebo d0d55cc
More javadoc fixes
dweebo 376cacf
switch from testng to junit
dweebo 783aab2
Add experimental annotation and change prefetch to capacityHint
dweebo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
247 changes: 247 additions & 0 deletions
247
src/main/java/io/reactivex/internal/operators/observable/ObservableConcatMapCompletable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
package io.reactivex.internal.operators.observable; | ||
|
||
import io.reactivex.*; | ||
import io.reactivex.disposables.Disposable; | ||
import io.reactivex.exceptions.Exceptions; | ||
import io.reactivex.functions.Function; | ||
import io.reactivex.internal.disposables.DisposableHelper; | ||
import io.reactivex.internal.disposables.SequentialDisposable; | ||
import io.reactivex.internal.functions.ObjectHelper; | ||
import io.reactivex.internal.fuseable.QueueDisposable; | ||
import io.reactivex.internal.fuseable.SimpleQueue; | ||
import io.reactivex.internal.queue.SpscLinkedArrayQueue; | ||
import io.reactivex.plugins.RxJavaPlugins; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public final class ObservableConcatMapCompletable<T> extends Completable { | ||
|
||
final ObservableSource<T> source; | ||
final Function<? super T, ? extends CompletableSource> mapper; | ||
final int bufferSize; | ||
|
||
public ObservableConcatMapCompletable(ObservableSource<T> source, | ||
Function<? super T, ? extends CompletableSource> mapper, int bufferSize) { | ||
this.source = source; | ||
this.mapper = mapper; | ||
this.bufferSize = Math.max(8, bufferSize); | ||
} | ||
@Override | ||
public void subscribeActual(CompletableObserver observer) { | ||
source.subscribe(new SourceObserver<T>(observer, mapper, bufferSize)); | ||
} | ||
|
||
static final class SourceObserver<T> extends AtomicInteger implements Observer<T>, Disposable { | ||
|
||
private static final long serialVersionUID = 6893587405571511048L; | ||
final CompletableObserver actual; | ||
final SequentialDisposable sa; | ||
final Function<? super T, ? extends CompletableSource> mapper; | ||
final CompletableObserver inner; | ||
final int bufferSize; | ||
|
||
SimpleQueue<T> queue; | ||
|
||
Disposable s; | ||
|
||
volatile boolean active; | ||
|
||
volatile boolean disposed; | ||
|
||
volatile boolean done; | ||
|
||
int sourceMode; | ||
|
||
SourceObserver(CompletableObserver actual, | ||
Function<? super T, ? extends CompletableSource> mapper, int bufferSize) { | ||
this.actual = actual; | ||
this.mapper = mapper; | ||
this.bufferSize = bufferSize; | ||
this.inner = new InnerObserver(actual, this); | ||
this.sa = new SequentialDisposable(); | ||
} | ||
@Override | ||
public void onSubscribe(Disposable s) { | ||
if (DisposableHelper.validate(this.s, s)) { | ||
this.s = s; | ||
if (s instanceof QueueDisposable) { | ||
@SuppressWarnings("unchecked") | ||
QueueDisposable<T> qd = (QueueDisposable<T>) s; | ||
|
||
int m = qd.requestFusion(QueueDisposable.ANY); | ||
if (m == QueueDisposable.SYNC) { | ||
sourceMode = m; | ||
queue = qd; | ||
done = true; | ||
|
||
actual.onSubscribe(this); | ||
|
||
drain(); | ||
return; | ||
} | ||
|
||
if (m == QueueDisposable.ASYNC) { | ||
sourceMode = m; | ||
queue = qd; | ||
|
||
actual.onSubscribe(this); | ||
|
||
return; | ||
} | ||
} | ||
|
||
queue = new SpscLinkedArrayQueue<T>(bufferSize); | ||
|
||
actual.onSubscribe(this); | ||
} | ||
} | ||
@Override | ||
public void onNext(T t) { | ||
if (done) { | ||
return; | ||
} | ||
if (sourceMode == QueueDisposable.NONE) { | ||
queue.offer(t); | ||
} | ||
drain(); | ||
} | ||
@Override | ||
public void onError(Throwable t) { | ||
if (done) { | ||
RxJavaPlugins.onError(t); | ||
return; | ||
} | ||
done = true; | ||
dispose(); | ||
actual.onError(t); | ||
} | ||
@Override | ||
public void onComplete() { | ||
if (done) { | ||
return; | ||
} | ||
done = true; | ||
drain(); | ||
} | ||
|
||
void innerComplete() { | ||
active = false; | ||
drain(); | ||
} | ||
|
||
@Override | ||
public boolean isDisposed() { | ||
return disposed; | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
disposed = true; | ||
sa.dispose(); | ||
s.dispose(); | ||
|
||
if (getAndIncrement() == 0) { | ||
queue.clear(); | ||
} | ||
} | ||
|
||
void innerSubscribe(Disposable s) { | ||
sa.update(s); | ||
} | ||
|
||
void drain() { | ||
if (getAndIncrement() != 0) { | ||
return; | ||
} | ||
|
||
for (;;) { | ||
if (disposed) { | ||
queue.clear(); | ||
return; | ||
} | ||
if (!active) { | ||
|
||
boolean d = done; | ||
|
||
T t; | ||
|
||
try { | ||
t = queue.poll(); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
dispose(); | ||
queue.clear(); | ||
actual.onError(ex); | ||
return; | ||
} | ||
|
||
boolean empty = t == null; | ||
|
||
if (d && empty) { | ||
disposed = true; | ||
actual.onComplete(); | ||
return; | ||
} | ||
|
||
if (!empty) { | ||
CompletableSource c; | ||
|
||
try { | ||
c = ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null CompletableSource"); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
dispose(); | ||
queue.clear(); | ||
actual.onError(ex); | ||
return; | ||
} | ||
|
||
active = true; | ||
c.subscribe(inner); | ||
} | ||
} | ||
|
||
if (decrementAndGet() == 0) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
static final class InnerObserver implements CompletableObserver { | ||
final CompletableObserver actual; | ||
final SourceObserver<?> parent; | ||
|
||
InnerObserver(CompletableObserver actual, SourceObserver<?> parent) { | ||
this.actual = actual; | ||
this.parent = parent; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Disposable s) { | ||
parent.innerSubscribe(s); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
parent.dispose(); | ||
actual.onError(t); | ||
} | ||
@Override | ||
public void onComplete() { | ||
parent.innerComplete(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not we pass 8 here since it'll get overridden to 8 in the implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave it for now.