-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Operators toList and toSortedList now support backpressure #2901
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
Merged
benjchristensen
merged 1 commit into
ReactiveX:1.x
from
akarnokd:ToSortedListBackpressure
Apr 29, 2015
Merged
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
87 changes: 87 additions & 0 deletions
87
src/main/java/rx/internal/operators/SingleDelayedProducer.java
This file contains hidden or 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,87 @@ | ||
package rx.internal.operators; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import rx.*; | ||
|
||
/** | ||
* A producer that holds a single value until it is requested and emits it followed by an onCompleted. | ||
*/ | ||
public final class SingleDelayedProducer<T> extends AtomicInteger implements Producer { | ||
/** */ | ||
private static final long serialVersionUID = 4721551710164477552L; | ||
/** The actual child. */ | ||
final Subscriber<? super T> child; | ||
/** The value to emit, acquired and released by compareAndSet. */ | ||
T value; | ||
/** State flag: request() called with positive value. */ | ||
static final int REQUESTED = 1; | ||
/** State flag: set() called. */ | ||
static final int SET = 2; | ||
/** | ||
* Constructs a SingleDelayedProducer with the given child as output. | ||
* @param child the subscriber to emit the value and completion events | ||
*/ | ||
public SingleDelayedProducer(Subscriber<? super T> child) { | ||
this.child = child; | ||
} | ||
@Override | ||
public void request(long n) { | ||
if (n > 0) { | ||
for (;;) { | ||
int s = get(); | ||
// if already requested | ||
if ((s & REQUESTED) != 0) { | ||
break; | ||
} | ||
int u = s | REQUESTED; | ||
if (compareAndSet(s, u)) { | ||
if ((s & SET) != 0) { | ||
emit(); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Sets the value to be emitted and emits it if there was a request. | ||
* Should be called only once and from a single thread | ||
* @param value the value to set and possibly emit | ||
*/ | ||
public void set(T value) { | ||
for (;;) { | ||
int s = get(); | ||
// if already set | ||
if ((s & SET) != 0) { | ||
break; | ||
} | ||
int u = s | SET; | ||
this.value = value; | ||
if (compareAndSet(s, u)) { | ||
if ((s & REQUESTED) != 0) { | ||
emit(); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
/** | ||
* Emits the set value if the child is not unsubscribed and bounces back | ||
* exceptions caught from child.onNext. | ||
*/ | ||
void emit() { | ||
try { | ||
T v = value; | ||
value = null; // do not hold onto the value | ||
if (child.isUnsubscribed()) { | ||
return; | ||
} | ||
child.onNext(v); | ||
} catch (Throwable t) { | ||
child.onError(t); | ||
return; | ||
} | ||
child.onCompleted(); | ||
} | ||
} |
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.
Why can't
o
just be passed into theSubscriber
constructor?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.
Because if the result
Subscriber
receives itsProducer
viasetProducer
, the default behavior is to callsetProducer
onop
set by the constructor and thus it can overwrite ourSingleDelayedProducer
.