-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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
Fix for RxJava 0.18.+ #468
Closed
Closed
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9488e86
added inner RetrofitScheduler
chrisjenx 7e3bebc
removed unused imports
chrisjenx 966254b
checkstyle fixes
chrisjenx a048035
migrated Scheduler into the Schedulers class for shared usage.
chrisjenx ee29804
checkstyle
chrisjenx 53fee10
cleaned up comments as-per feedback.
chrisjenx 37ef1a3
Comment to end-of-line style
chrisjenx f584287
fix checkstyle
chrisjenx a350444
promoted RxSupport to retrofit.
chrisjenx 17360ca
as per @jhump's suggestions simplified executing a task. passed to th…
chrisjenx e009ea8
Comments cleanup
chrisjenx 2765e40
Moved away from Scheduler and Execute inside OnSubscribe.
chrisjenx 2c19f42
Fixed MockRestAdapter to mimic new OnSubscribe, added RxSupportTests
chrisjenx 97db5c9
comment clean up
chrisjenx 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
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
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
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,110 @@ | ||
package retrofit; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import rx.Scheduler; | ||
import rx.Subscription; | ||
import rx.functions.Action0; | ||
import rx.subscriptions.CompositeSubscription; | ||
import rx.subscriptions.Subscriptions; | ||
|
||
/** | ||
* Indirect access to Scheduler API for | ||
*/ | ||
/*package*/ final class Schedulers { | ||
|
||
/** | ||
* RetrofitScheduler, similar to the {@link rx.schedulers.EventLoopsScheduler} in the same way | ||
* it dumps requests onto a Executor, but we can pass in the Executor. | ||
* <p/> | ||
* This does not support Scheduled execution, which may cause issues with peoples implementations. | ||
* If they are doing, wait() or debouncing() on this scheduler. Future implementations, should | ||
* either add {@code schedule()} support, or let the user provide the {@link rx.Scheduler} to | ||
* RestAdapter builder. | ||
*/ | ||
static class RetrofitScheduler extends Scheduler { | ||
private final Executor executorService; | ||
|
||
/*package*/ RetrofitScheduler(Executor executorService) { | ||
this.executorService = executorService; | ||
} | ||
|
||
@Override | ||
public Worker createWorker() { | ||
return new EventLoopScheduler(executorService); | ||
} | ||
|
||
static class EventLoopScheduler extends Scheduler.Worker implements Subscription { | ||
private final CompositeSubscription innerSubscription = new CompositeSubscription(); | ||
private final Executor executor; | ||
|
||
/* package */ EventLoopScheduler(Executor executor) { | ||
this.executor = executor; | ||
} | ||
|
||
@Override | ||
public Subscription schedule(final Action0 action) { | ||
if (innerSubscription.isUnsubscribed()) { | ||
// don't schedule, we are unsubscribed | ||
return Subscriptions.empty(); | ||
} | ||
|
||
final AtomicReference<Subscription> sf = new AtomicReference<Subscription>(); | ||
final Subscription s; | ||
if (executor instanceof ExecutorService) { | ||
s = Subscriptions.from(((ExecutorService) executor).submit( | ||
getActionRunnable(action, sf))); | ||
} else { | ||
/* | ||
This is not ideal, we should use a ExecutorService, that way we can pass future | ||
back to the subscription, so if the user un-subscribe from the parent we can | ||
request the Future to cancel. This will always execute, meaning we could | ||
lock of the retrofit threads if a request is active for a long time. | ||
I would potentially force an API change to make sure this is always an ExecutorService | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you switch this to a |
||
*/ | ||
s = Subscriptions.empty(); | ||
executor.execute(getActionRunnable(action, sf)); | ||
} | ||
|
||
sf.set(s); | ||
innerSubscription.add(s); | ||
return s; | ||
} | ||
|
||
private Runnable getActionRunnable(final Action0 action, | ||
final AtomicReference<Subscription> sf) { | ||
return new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
if (innerSubscription.isUnsubscribed()) return; | ||
action.call(); | ||
} finally { | ||
// remove the subscription now that we're completed | ||
Subscription s = sf.get(); | ||
if (s != null) innerSubscription.remove(s); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Subscription schedule(final Action0 action, long delayTime, TimeUnit unit) { | ||
throw new UnsupportedOperationException("This Scheduler does not support timed requests"); | ||
} | ||
|
||
@Override | ||
public void unsubscribe() { | ||
innerSubscription.unsubscribe(); | ||
} | ||
|
||
@Override | ||
public boolean isUnsubscribed() { | ||
return innerSubscription.isUnsubscribed(); | ||
} | ||
} | ||
} | ||
} |
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.
Capitalize + end with a period. Comments should be sentences (or at least punctuated like them).