| 1 |
check_hybrid_callback <- function(callback, name) { |
| 2 |
if (!is.null(callback) && !is.function(callback)) { |
| 3 |
stop(sprintf("`%s=` must be a function or `NULL`", name)) |
| 4 |
} |
| 5 |
} |
| 6 |
|
| 7 |
|
| 8 |
#' Asynchronous or synchronous `then()` |
| 9 |
#' |
| 10 |
#' This is a helper function that behaves like `then`, however if |
| 11 |
#' `is.promising()` returns `FALSE` then the handlers will be executed |
| 12 |
#' immediately. |
| 13 |
#' |
| 14 |
#' Execution paths: |
| 15 |
#' * If `expr` evaluates to a promise (`p`), it will call `p |> then(on_success, |
| 16 |
#' on_failure)`. |
| 17 |
#' * If `expr` evaluates to a non-promise value (`x`), it will call |
| 18 |
#' `on_success(x)`. |
| 19 |
#' * If `expr` throws an error (`e`) during calculation, it will call |
| 20 |
#' `on_failure(e)`. |
| 21 |
#' |
| 22 |
#' In all cases, the `on_success` and `on_failure` callbacks are executed (when |
| 23 |
#' provided). |
| 24 |
#' |
| 25 |
#' @section Utility: |
| 26 |
#' |
| 27 |
#' This function is useful for writing functions that need to execute followup |
| 28 |
#' behavior _now_ or within a promise. This is different behavior than `then()` |
| 29 |
#' where *everything* is made into a promise. |
| 30 |
#' |
| 31 |
#' `hybrid_then()` allows authors to keep synchronous execution on the same |
| 32 |
#' _tick_ without requiring the use of a followup promise. This is particularly |
| 33 |
#' appealing for situations where the author does not control the execution flow |
| 34 |
#' for items that may be either synchronous or asynchronous, such as within |
| 35 |
#' `{plumber2}`. |
| 36 |
#' |
| 37 |
#' @section Error Handling: |
| 38 |
#' |
| 39 |
#' If no `on_failure` callback is provided and an error occurs, the error is |
| 40 |
#' re-thrown immediately (for synchronous errors) or propagated through the |
| 41 |
#' returned promise (for asynchronous errors). |
| 42 |
#' |
| 43 |
#' If an `on_failure` callback is provided but it throws an error, that new |
| 44 |
#' error replaces the original error. With `tee = TRUE`, even if `on_failure` |
| 45 |
#' executes successfully, the original error is still re-thrown. |
| 46 |
#' |
| 47 |
#' @section Callback Return Values: |
| 48 |
#' |
| 49 |
#' Callbacks can return any value, including promises. If a callback returns a |
| 50 |
#' promise, the entire `hybrid_then()` call will return a promise, even if the |
| 51 |
#' input was synchronous. This allows seamless transitions between synchronous |
| 52 |
#' and asynchronous execution. |
| 53 |
#' |
| 54 |
#' @param expr An expression that evaluates to either a promise or a non-promise |
| 55 |
#' value. |
| 56 |
#' @param on_success A function to be called when no error occurs synchronously |
| 57 |
#' or asynchronously. When invoked, the function will be called with a single |
| 58 |
#' argument: the resolved value. Optionally, the function can take a second |
| 59 |
#' parameter `.visible` if you care whether the promise was resolved with a |
| 60 |
#' visible or invisible value. Can return a value or a promise. |
| 61 |
#' @param on_failure A function to be called if an error occurs synchronously or |
| 62 |
#' asynchronously. Takes one argument: the error object. Can return a value or |
| 63 |
#' a promise to recover from the error, or throw a new error. If `on_failure` |
| 64 |
#' is provided and doesn't throw an error (or return a promise that fails) |
| 65 |
#' then this is the async equivalent of catching an error. |
| 66 |
#' @param ... Reserved for future use. Currently must be empty. |
| 67 |
#' @param tee If `TRUE`, ignore the return value of the callback, and use the |
| 68 |
#' original value of `expr` as the result. For `on_failure` with `tee = TRUE`, |
| 69 |
#' the callback executes but the original error is re-thrown afterward. |
| 70 |
#' @return |
| 71 |
#' * If `expr` evaluates to a promise, a promise with a single followup |
| 72 |
#' promise to handle the `on_success` or `on_failure` callbacks. |
| 73 |
#' * If `expr` evaluates to a non-promise value, the result of the synchronous |
| 74 |
#' operation after being processed by `on_success` or `on_failure`. |
| 75 |
#' * If a callback returns a promise, the result is always a promise. |
| 76 |
#' @seealso [then()], [is.promising()], [promise_resolve()] |
| 77 |
#' @export |
| 78 |
#' @examples |
| 79 |
#' # Basic usage - works with both sync and async values |
| 80 |
#' add_to <- function(x, k) { |
| 81 |
#' hybrid_then( |
| 82 |
#' x, |
| 83 |
#' on_success = function(value) { |
| 84 |
#' value + k |
| 85 |
#' }, |
| 86 |
#' on_failure = function(err) { |
| 87 |
#' message("Error: ", err$message) |
| 88 |
#' NA_real_ |
| 89 |
#' } |
| 90 |
#' ) |
| 91 |
#' } |
| 92 |
#' |
| 93 |
#' # Synchronous |
| 94 |
#' 42 |> add_to(100) |
| 95 |
#' #> [1] 142 |
| 96 |
#' |
| 97 |
#' # Synchronous error |
| 98 |
#' add_to({stop("Bad input!")}, 8) |
| 99 |
#' #> Error: Bad input! |
| 100 |
#' #> [1] NA |
| 101 |
#' |
| 102 |
#' \dontrun{ |
| 103 |
#' # Asynchronous |
| 104 |
#' promise_resolve(42) |> |
| 105 |
#' add_to(8) |> |
| 106 |
#' then(print) |
| 107 |
#' # When resolved... |
| 108 |
#' #> [1] 50 |
| 109 |
#' |
| 110 |
#' # Error handling - asynchronous |
| 111 |
#' promise_resolve(stop("Bad async input!")) |> |
| 112 |
#' add_to(8) |> |
| 113 |
#' then(print) |
| 114 |
#' # When resolved... |
| 115 |
#' #> Error: Bad async input! |
| 116 |
#' #> [1] NA |
| 117 |
#' |
| 118 |
#' # Chaining multiple operations |
| 119 |
#' # (Move the `promise_resolve()` around to see sync vs async behavior) |
| 120 |
#' 1 |> |
| 121 |
#' hybrid_then(on_success = \(x) x + 1) |> |
| 122 |
#' hybrid_then(on_success = \(x) promise_resolve(x * 2)) |> |
| 123 |
#' hybrid_then(on_success = \(x) x - 1) |> |
| 124 |
#' hybrid_then(print) |
| 125 |
#' # When resolved... |
| 126 |
#' #> [1] 3 |
| 127 |
#' } |
| 128 |
hybrid_then <- function( |
| 129 |
expr, |
| 130 |
on_success = NULL, |
| 131 |
on_failure = NULL, |
| 132 |
..., |
| 133 |
tee = FALSE |
| 134 |
) { |
| 135 |
check_dots(...) |
| 136 |
check_tee(tee) |
| 137 |
|
| 138 |
check_hybrid_callback(on_success, "on_success") |
| 139 |
check_hybrid_callback(on_failure, "on_failure") |
| 140 |
|
| 141 |
# Add support for `.visible` |
| 142 |
on_success <- normalizeOnFulfilled(on_success) |
| 143 |
on_failure <- normalizeOnRejected(on_failure) |
| 144 |
|
| 145 |
result_is_sync_error <- FALSE |
| 146 |
|
| 147 |
result_visible <- |
| 148 |
withVisible( |
| 149 |
tryCatch( |
| 150 |
expr, |
| 151 |
error = function(e) { |
| 152 |
# If no `on_failure` callback, re-throw the error |
| 153 |
if (is.null(on_failure)) { |
| 154 |
stop(e) |
| 155 |
} |
| 156 |
|
| 157 |
# Perform synchronous `on_failure` callback now |
| 158 |
result_on_error <- withVisible(on_failure(e)) |
| 159 |
if (tee) { |
| 160 |
# Re-throw the error |
| 161 |
stop(e) |
| 162 |
} else { |
| 163 |
result_is_sync_error <<- TRUE |
| 164 |
return_with_visible(result_on_error) |
| 165 |
} |
| 166 |
} |
| 167 |
) |
| 168 |
) |
| 169 |
|
| 170 |
if (result_is_sync_error) { |
| 171 |
# Return synchronous `tee=FALSE` `result_on_error` from above |
| 172 |
return(return_with_visible(result_visible)) |
| 173 |
} |
| 174 |
|
| 175 |
if (is.promising(result_visible$value)) { |
| 176 |
# Return async result |
| 177 |
# Will handle async success, failure callbacks (and `tee`) |
| 178 |
then(return_with_visible(result_visible), on_success, on_failure, tee = tee) |
| 179 |
} else { |
| 180 |
# If no `on_success` callback, return sync result now |
| 181 |
if (is.null(on_success)) { |
| 182 |
return(return_with_visible(result_visible)) |
| 183 |
} |
| 184 |
|
| 185 |
# Peform synchronous `on_success` callback now |
| 186 |
result_on_success_visible <- |
| 187 |
withVisible( |
| 188 |
on_success( |
| 189 |
result_visible$value, |
| 190 |
result_visible$visible |
| 191 |
) |
| 192 |
) |
| 193 |
|
| 194 |
# Return synchronous result |
| 195 |
if (tee) { |
| 196 |
return_with_visible(result_visible) |
| 197 |
} else { |
| 198 |
return_with_visible(result_on_success_visible) |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return_with_visible <- function(visible_info) { |
| 204 |
if (visible_info$visible) { |
| 205 |
visible_info$value |
| 206 |
} else { |
| 207 |
invisible(visible_info$value) |
| 208 |
} |
| 209 |
} |