Skip to content
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

Merged
merged 1 commit into from
Jun 12, 2014
Merged

Allow use of the returned subscription to cancel periodic scheduling #1347

merged 1 commit into from
Jun 12, 2014

Conversation

samueltardieu
Copy link
Contributor

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.

@cloudbees-pull-request-builder

RxJava-pull-requests #1257 SUCCESS
This pull request looks good

@@ -119,7 +120,7 @@ public void call() {
}
}
};
return schedule(recursiveAction, initialDelay, unit);
return Subscriptions.from(this, schedule(recursiveAction, initialDelay, unit));
Copy link
Member

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;
        }

Copy link
Contributor Author

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()) {

?

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.
@samueltardieu
Copy link
Contributor Author

This new version contains a fixed fix by @akarnokd.

@cloudbees-pull-request-builder

RxJava-pull-requests #1258 SUCCESS
This pull request looks good

@benjchristensen
Copy link
Member

Thank you.

benjchristensen added a commit that referenced this pull request Jun 12, 2014
Allow use of the returned subscription to cancel periodic scheduling
@benjchristensen benjchristensen merged commit 473a567 into ReactiveX:master Jun 12, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants