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

Scheduler.scheduleRecursive #878

Merged
merged 1 commit into from
Feb 14, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions rxjava-core/src/main/java/rx/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,57 @@ public void call(Inner inner) {
return schedule(recursiveAction, initialDelay, unit);
}

public final Subscription scheduleRecursive(final Action1<Recurse> action) {
return schedule(new Action1<Inner>() {

@Override
public void call(Inner inner) {
action.call(new Recurse(inner, action));
}

});
}

public static final class Recurse {
final Action1<Recurse> action;
final Inner inner;

private Recurse(Inner inner, Action1<Recurse> action) {
this.inner = inner;
this.action = action;
}

/**
* Schedule the current function for execution immediately.
*/
public final void schedule() {
final Recurse self = this;
inner.schedule(new Action1<Inner>() {

@Override
public void call(Inner _inner) {
action.call(self);
}

});
}

/**
* Schedule the current function for execution in the future.
*/
public final void schedule(long delay, TimeUnit unit) {
final Recurse self = this;
inner.schedule(new Action1<Inner>() {

@Override
public void call(Inner _inner) {
action.call(self);
}

}, delay, unit);
}
}

public abstract static class Inner implements Subscription {

/**
Expand Down