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 1 commit
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 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
29 changes: 21 additions & 8 deletions R/later_recurring.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
#' @describeIn later Schedules a recurring task
r2evans marked this conversation as resolved.
Show resolved Hide resolved
#' @param limit Number of times to repeat the function. If `NA` (the default)
#' then no limit.
#' @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
#' later_recurring(~cat("Hello from the past\n"), 3, limit = 2)
#' # 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 = NA, loop = current_loop()) {
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 'NA' or a positive number")
if (is.na(limit) || limit < 1)
stop("'limit' must be a positive number")
func2 <- function() {
limit <<- limit - 1L
ret <- func()
if (is.logical(ret) && !anyNA(ret)) cancelled <<- !ret[1]
if (!cancelled && (is.na(limit) || limit > 0))
if (is_false(ret)) cancelled <<- !ret[1]
r2evans marked this conversation as resolved.
Show resolved Hide resolved
if (!cancelled && limit > 0)
handle <<- later(func2, delay, loop)
}
handle <- later(func2, delay, loop)
Expand Down
19 changes: 15 additions & 4 deletions 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)
})