Skip to content

Commit

Permalink
keep dot options exported for autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 13, 2025
1 parent 30b146e commit deb92d2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 27 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ S3method(promises::as.promise,mirai)
S3method(promises::is.promising,mirai)
S3method(utils::.DollarNames,miraiError)
S3method(with,miraiDaemons)
export(.flat)
export(.progress)
export(.stop)
export(call_mirai)
export(call_mirai_)
export(collect_mirai)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#### Updates

* `mirai_map()[.stop]` now reports the index number that errored, and uses the cli package, if available, for richer formatting of the error message.
* `mirai_map()[.progress]` now automatically uses `cli` progress bars (over a simple text alternative) if the cli package is installed.
* `mirai_map()[.progress]` now automatically uses `cli` progress bars if the cli package is installed, and `[.progress_cli]` is no longer a separate option.
* `daemon()` now returns an integer exit code to indicate the reason for termination.
* Adds `nextcode()` to provide a human-readable translation of the exit codes returned by `daemon()`.

Expand Down
62 changes: 39 additions & 23 deletions R/map.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,48 @@ print.mirai_map <- function(x, ...) {

}

#' mirai Map Options
#'
#' Expressions to be provided to the \code{[]} method for \sQuote{mirai_map}
#' objects, or the \code{...} argument of \code{\link{collect_mirai}}.
#'
#' @inheritSection mirai_map Collection Options
#'
#' @keywords internal
#' @export
#'
.flat <- compiler::compile(
quote(
if (i == 0L) xi <- TRUE else
if (i == 1L) typ <<- typeof(xi) else
if (i <= xlen) is_error_value(xi) && stop(xi, call. = FALSE) || typeof(xi) == typ || stop(sprintf("[.flat]: cannot flatten outputs of differing type: %s / %s", typ, typeof(xi)), call. = FALSE)
)
)

#' @rdname dot-flat
#' @export
#'
.progress <- compiler::compile(
quote(
if (i == 0L) cat(sprintf("\r[ 0 / %d .... ]", xlen), file = stderr()) else
if (i < xlen) cat(sprintf("\r[ %d / %d .... ]", i, xlen), file = stderr()) else
if (i == xlen) cat(sprintf("\r[ %d / %d done ]\n", i, xlen), file = stderr())
)
)

#' @rdname dot-flat
#' @export
#'
.stop <- compiler::compile(
quote(if (is_error_value(xi)) { stop_mirai(x); stop(sprintf("In index %d:\n%s", i, xi), call. = FALSE) })
)

# internals --------------------------------------------------------------------

map <- function(x, ...) {

if (is.null(.[[".progress"]]) && requireNamespace("cli", quietly = TRUE))
`[[<-`(`[[<-`(., ".progress", .progress_cli), ".stop", .stop_cli)
`[[<-`(`[[<-`(., ".progress", progress_cli), ".stop", stop_cli)

dots <- eval(`[[<-`(substitute(alist(...)), 1L, quote(list)), envir = .)
expr <- if (length(dots) > 1L) do.call(expression, dots) else dots[[1L]]
Expand All @@ -250,34 +286,14 @@ map <- function(x, ...) {

}

.flat <- compiler::compile(
quote(
if (i == 0L) xi <- TRUE else
if (i == 1L) typ <<- typeof(xi) else
if (i <= xlen) is_error_value(xi) && stop(xi, call. = FALSE) || typeof(xi) == typ || stop(sprintf("[.flat]: cannot flatten outputs of differing type: %s / %s", typ, typeof(xi)), call. = FALSE)
)
)

.progress <- compiler::compile(
quote(
if (i == 0L) cat(sprintf("\r[ 0 / %d .... ]", xlen), file = stderr()) else
if (i < xlen) cat(sprintf("\r[ %d / %d .... ]", i, xlen), file = stderr()) else
if (i == xlen) cat(sprintf("\r[ %d / %d done ]\n", i, xlen), file = stderr())
)
)

.progress_cli <- compiler::compile(
progress_cli <- compiler::compile(
quote(
if (i == 0L) cli::cli_progress_bar(type = NULL, total = xlen, auto_terminate = TRUE, .envir = .) else
if (i <= xlen) cli::cli_progress_update(.envir = .)
)
)

.stop <- compiler::compile(
quote(if (is_error_value(xi)) { stop_mirai(x); stop(sprintf("In index %d:\n%s", i, xi), call. = FALSE) })
)

.stop_cli <- compiler::compile(
stop_cli <- compiler::compile(
quote(
if (is_error_value(xi)) {
stop_mirai(x)
Expand Down
52 changes: 52 additions & 0 deletions man/dot-flat.Rd

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

5 changes: 2 additions & 3 deletions tests/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ connection && Sys.getenv("NOT_CRAN") == "true" && {
Sys.sleep(1L)
cfg <- serial_config("custom", function(x) serialize(x, NULL), unserialize)
test_zero(daemons(url = "tls+tcp://127.0.0.1:0", pass = "test", serial = cfg))
test_equal(launch_local(1L), 1L)
test_equal(launch_local(2L), 2L)
Sys.sleep(1L)
test_true(grepl("CERTIFICATE", launch_remote(1L), fixed = TRUE))
q <- quote(list2env(list(b = 2), envir = .GlobalEnv))
Expand Down Expand Up @@ -296,13 +296,12 @@ connection && Sys.getenv("NOT_CRAN") == "true" && {
test_zero(res$connections)
test_identical(res$events, c(129L, -129L))
test_zero(daemons(0))
Sys.sleep(1L)
test_equal(daemons(1, dispatcher = FALSE, maxtasks = 1L), 1L)
test_zero(mirai(0L)[])
Sys.sleep(0.5)
test_zero(status()$connections)
test_equal(launch_local(1, idletime = 200L, walltime = 1000L), 1L)
Sys.sleep(0.5)
Sys.sleep(1L)
test_zero(status()$connections)
test_zero(daemons(0))
}
Expand Down

0 comments on commit deb92d2

Please sign in to comment.