Observable.using should use unsafeSubscribe and enable eager disposal#2759
Merged
benjchristensen merged 1 commit intoReactiveX:1.xfrom Feb 21, 2015
Merged
Observable.using should use unsafeSubscribe and enable eager disposal#2759benjchristensen merged 1 commit intoReactiveX:1.xfrom
benjchristensen merged 1 commit intoReactiveX:1.xfrom
Conversation
This was referenced Feb 19, 2015
Member
There was a problem hiding this comment.
I'd have a class that implements both Action0 and subscription to avoid creating too many objects:
static final class DisposeAction<R> extends AtomicBoolean implements Action0, Subscription {
Action1<? super R> dispose;
R resource;
public DisposeAction(Action1<? super R> dispose, R resource) {
this.dispose = dispose;
this.resource = resource;
lazySet(false); // StoreStore barrier
}
@Override
public void call() {
if (compareAndSet(false, true)) {
try {
dispose.call(resource);
} finally {
resource = null;
dispose = null;
}
}
}
@Override
public boolean isUnsubscribed() {
return get();
}
public void unsubscribe() {
call();
}
}18393e4 to
dba1fa1
Compare
Collaborator
Author
|
Thanks @akarnokd, I've made the change and squashed commits. |
src/main/java/rx/Observable.java
Outdated
Member
There was a problem hiding this comment.
Could you add @Experimental here? In addition, could you replace tabs with 4 spaces in OnSubscribeUsing?
687adcd to
ffe0fa1
Compare
Collaborator
Author
|
Replaced tabs with spaces, added @experimental annotation to new overload and fixed typo in javadoc, squashed commits |
Member
|
This seems reasonable to me. I don't use 'using' so trust the conversation you've had to address the necessary functionality. Reading through the other PR this does seem necessary and I agree that 'unsafeSubscribe' is appropriate for this. |
benjchristensen
added a commit
that referenced
this pull request
Feb 21, 2015
Observable.using should use unsafeSubscribe and enable eager disposal
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
See #2604 where it was discovered that
Observable.usingusedsubscribeinstead ofunsafeSubscribewhich provoked a race condition leading to an IllegalArgumentException from the merge operator.This PR uses
unsafeSubscribeand adds an overload forusingto optionally dispose of resources eagerly (just before completion or error). The use case for this is a synchronous observable where a downstream operation wants to reuse a resource (but because the observable is synchronous the resource cannot get disposed till the downstream completes).Unit tests included.