-
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
Allow use of the returned subscription to cancel periodic scheduling #1347
Allow use of the returned subscription to cancel periodic scheduling #1347
Conversation
RxJava-pull-requests #1257 SUCCESS |
@@ -119,7 +120,7 @@ public void call() { | |||
} | |||
} | |||
}; | |||
return schedule(recursiveAction, initialDelay, unit); | |||
return Subscriptions.from(this, schedule(recursiveAction, initialDelay, unit)); |
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.
This will cancel the entire worker, not just the recursive action. If one uses a scheduler for general scheduling tasks (instead of an Executor) this will disrupt the worker.
This is one step better, but for the 100% correct version, a new subscription type is required.
public Subscription schedulePeriodically(final Action0 action, long initialDelay, long period, TimeUnit unit) {
final long periodInNanos = unit.toNanos(period);
final long startInNanos = TimeUnit.MILLISECONDS.toNanos(now()) + unit.toNanos(initialDelay);
final MultipleAssignmentSubscription mas = new MultipleAssignmentSubscription();
final Action0 recursiveAction = new Action0() {
long count = 0;
@Override
public void call() {
if (!mas.isUnsubscribed()) {
action.call();
long nextTick = startInNanos + (++count * periodInNanos);
mas.set(schedule(this, nextTick - TimeUnit.MILLISECONDS.toNanos(now()), TimeUnit.NANOSECONDS));
}
}
};
mas.set(schedule(recursiveAction, initialDelay, unit));
return mas;
}
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.
Wouldn't it be correct if we just updated the test to read
if (!mas.isUnsubscribed() && !isUnsubscribed()) {
?
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.
I thought about this double-check for a minute; if the worker is unsubscribed, it may or may not happen before or after these checks in general, and if so, the next call to schedule won't do anything anyway. I.e., such eagerness is not really necessary here.
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.
That is my understanding as well, although I wondered why the if (!isUnsubscribed())
was here in the actual source code.
Shall I update the PR with your proposed changes, or do you want to do it yourself?
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.
I can't do PRs right now, so be my guest.
The documentation for schedulePeriodically indicates that the returned subscription can be used to unsubscribe from the periodic action, or to unschedule it if it has not been scheduled yet. That was the case only before the first action took place, and it was then impossible to unsubscribe using the given subscription, although unsubscribing the worker did work.
This new version contains a fixed fix by @akarnokd. |
RxJava-pull-requests #1258 SUCCESS |
Thank you. |
Allow use of the returned subscription to cancel periodic scheduling
The documentation for schedulePeriodically indicates that the returned
subscription can be used to unsubscribe from the periodic action, or to
unschedule it if it has not been scheduled yet. That was the case only
before the first action took place, and it was then impossible to
unsubscribe using the given subscription, although unsubscribing the
worker did work.
This fixes #1344.