|
22 | 22 | #' @param initiate (logical) If TRUE, the progressor will signal a |
23 | 23 | #' [progression] 'initiate' condition when created. |
24 | 24 | #' |
| 25 | +#' @param finalize (logical) If TRUE, the progressor signals a [progression] |
| 26 | +#' 'shutdown' condition when finalized by the garbage collector. |
| 27 | +#' |
25 | 28 | #' @param auto_finish (logical) If TRUE, then the progressor will signal a |
26 | 29 | #' [progression] 'finish' condition as soon as the last step has been reached. |
27 | 30 | #' |
@@ -60,7 +63,7 @@ progressor <- local({ |
60 | 63 | environment(void_progressor)$enable <- FALSE |
61 | 64 | class(void_progressor) <- c("progressor", class(void_progressor)) |
62 | 65 |
|
63 | | - function(steps = length(along), along = NULL, offset = 0L, scale = 1L, transform = function(steps) scale * steps + offset, message = character(0L), label = NA_character_, trace = FALSE, initiate = TRUE, auto_finish = TRUE, on_exit = !identical(envir, globalenv()), enable = getOption("progressr.enable", TRUE), envir = parent.frame()) { |
| 66 | + function(steps = length(along), along = NULL, offset = 0L, scale = 1L, transform = function(steps) scale * steps + offset, message = character(0L), label = NA_character_, trace = FALSE, initiate = TRUE, finalize = FALSE, auto_finish = TRUE, on_exit = !identical(envir, globalenv()), enable = getOption("progressr.enable", TRUE), envir = parent.frame()) { |
64 | 67 | stop_if_not(is.logical(enable), length(enable) == 1L, !is.na(enable)) |
65 | 68 |
|
66 | 69 | ## Quickly return a moot progressor function? |
@@ -113,7 +116,7 @@ progressor <- local({ |
113 | 116 | muffleProgression = function(p) NULL |
114 | 117 | ) |
115 | 118 | invisible(cond) |
116 | | - } |
| 119 | + } ## fcn() |
117 | 120 | formals(fcn)$message <- message |
118 | 121 | class(fcn) <- c("progressor", class(fcn)) |
119 | 122 |
|
@@ -163,6 +166,18 @@ progressor <- local({ |
163 | 166 | do.call(base::on.exit, args = list(call, add = TRUE), envir = envir) |
164 | 167 | } |
165 | 168 |
|
| 169 | + if (finalize) { |
| 170 | + ## Self-reference progressor function needed by the finalizer function |
| 171 | + progressor_envir$self <- fcn |
| 172 | + reg.finalizer(progressor_envir, function(e) { |
| 173 | + print(utils::ls.str(e)) |
| 174 | + if (is.function(e$self)) { |
| 175 | + e$self(type = "shutdown") |
| 176 | + e$self <- NULL |
| 177 | + } |
| 178 | + }) |
| 179 | + } |
| 180 | + |
166 | 181 | fcn |
167 | 182 | } |
168 | 183 | }) |
|
0 commit comments