Skip to contents

Registers an asynchronous evaluator for routes, allowing freshwater context to be propagated to mirai::mirai workers. This means that contextful helpers such as current_method, current_path, and current_query (among others) will work in async routes.

Usage

register_async_evaluator(set_default = TRUE)

Arguments

set_default

whether to set the default async evaluator to "freshwater"

Details

Registration affects global plumber2 state, not just the current API process.

Context is not inherently portable across asynchronous request contexts, this function creates a portable snapshot of the current context that is passed to a mirai worker.

Hooks are not applied to the async route, but may be provided to any associated then handlers. If error pages are installed on the main process, errors from the worker will be appropriately converted into freshwater error pages. If CSRF protection is enabled, tokens will be propagated to the worker, ensuring async routes are still protected.

As cache() is process-local by default, memoised functions are not ported to workers. Likewise, clear_cache() and invalidate_cache() will only impact the local process' cache. If a shared cache is desired, consider configuring cachem::cache_disk() for caching, which will allow all process to utilise a shared cache. Note that TTL is process-local regardless of backend strategy used.

Requires the promises::promises, mirai::mirai, and mori::mori packages.

Examples

register_async_evaluator()
#* @async
#* @get /async
function() {
 current_path()
}
#> function () 
#> {
#>     current_path()
#> }
#> <environment: 0x565114d75790>