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

add later_recurring #133

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(destroy_loop)
export(exists_loop)
export(global_loop)
export(later)
export(later_recurring)
export(loop_empty)
export(next_op_secs)
export(run_now)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## later 1.1.0.9000

* Added `later_recurring`, allowing a function to recur in its 'later' loop until an iteration limit (if set) or explicit cancellation. PR #133

## later 1.1.0.1

Expand Down
1 change: 1 addition & 0 deletions R/is_false.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
is_false <- function(x) is.logical(x) && length(x) == 1L && !is.na(x) && !x
2 changes: 1 addition & 1 deletion R/later.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ create_loop <- function(parent = current_loop(), autorun = NULL) {
# This is for backward compatibility, if `create_loop(autorun=FALSE)` is called.
parent <- NULL
}
if (identical(parent, FALSE)) {
if (is_false(parent)) {
# This is for backward compatibility, if `create_loop(FALSE)` is called.
# (Previously the first and only parameter was `autorun`.)
parent <- NULL
Expand Down
37 changes: 37 additions & 0 deletions R/later_recurring.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' @describeIn later Schedules a recurring task
r2evans marked this conversation as resolved.
Show resolved Hide resolved
#' @details
#'
#' In `later_recurring`, if `func` returns an explicit `FALSE` then
#' this is interpreted as self-cancelling the loop. Anything else
#' returned (including multiple `FALSE`) is ignored.
#'
#' @param limit Number of times to repeat the function. If `Inf` (the
#' default) then no limit.
#' @examples
#' # Limit number of executions to 3 times
#' later_recurring(~ message("Hello from the past"), 1, limit = 3)
#'
#' # Stop recurring when the return value is `FALSE`
#' later_recurring(function() {
#' message("Flipping a coin to see if we run again...")
#' sample(c(TRUE, FALSE), size = 1L)
#' }, 0.25, limit = Inf)
#' @export
later_recurring <- function(func, delay, limit = Inf, loop = current_loop()) {
func <- rlang::as_function(func)
cancelled <- FALSE
schloerke marked this conversation as resolved.
Show resolved Hide resolved
if (is.na(limit) || limit < 1)
stop("'limit' must be a positive number")
func2 <- function() {
limit <<- limit - 1L
ret <- func()
if (is_false(ret)) cancelled <<- TRUE
if (!cancelled && limit > 0)
handle <<- later(func2, delay, loop)
}
handle <- later(func2, delay, loop)
invisible(function() {
cancelled <<- TRUE
schloerke marked this conversation as resolved.
Show resolved Hide resolved
handle()
})
}
r2evans marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 24 additions & 1 deletion man/later.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/testthat/test-recurring.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
context("test-recurring")

test_that("Limited recurrence", {
# Repeat until the limit reached, stopped by limit
x <- 0
later_recurring(function() { x <<- x + 1 }, delay = 0.5, limit = 2)
run_now(0.5)
expect_identical(x, 1)
run_now(0.5)
expect_identical(x, 2)
run_now(1)
expect_identical(x, 2)
})

test_that("Self-cancelling recurrence", {
# Repeat until the function returns FALSE, self-cancelling
x <- 0
cancel <- later_recurring(function() { x <<- x + 1; (x < 2) }, delay = 0.5, limit = 4)
expect_identical(length(list_queue()), 1L)
run_now(0.5)
expect_identical(x, 1)
run_now(0.5)
expect_identical(x, 2)
run_now(1)
expect_identical(x, 2)
expect_identical(length(list_queue()), 0L)
})