| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/domains.R |
| 3 |
\name{with_promise_domain} |
| 4 |
\alias{with_promise_domain} |
| 5 |
\alias{new_promise_domain} |
| 6 |
\title{Promise domains} |
| 7 |
\usage{ |
| 8 |
with_promise_domain(domain, expr, replace = FALSE) |
| 9 |
|
| 10 |
new_promise_domain( |
| 11 |
wrapOnFulfilled = identity, |
| 12 |
wrapOnRejected = identity, |
| 13 |
wrapSync = force, |
| 14 |
onError = force, |
| 15 |
..., |
| 16 |
wrapOnFinally = NULL |
| 17 |
) |
| 18 |
} |
| 19 |
\arguments{ |
| 20 |
\item{domain}{A promise domain object to install while \code{expr} is evaluated.} |
| 21 |
|
| 22 |
\item{expr}{Any R expression, to be evaluated under the influence of |
| 23 |
\code{domain}.} |
| 24 |
|
| 25 |
\item{replace}{If \code{FALSE}, then the effect of the \code{domain} will be added |
| 26 |
to the effect of any currently active promise domain(s). If \code{TRUE}, then |
| 27 |
the current promise domain(s) will be ignored for the duration of the |
| 28 |
\code{with_promise_domain} call.} |
| 29 |
|
| 30 |
\item{wrapOnFulfilled}{A function that takes a single argument: a function |
| 31 |
that was passed as an \code{onFulfilled} argument to \code{\link[=then]{then()}}. The |
| 32 |
\code{wrapOnFulfilled} function should return a function that is suitable for |
| 33 |
\code{onFulfilled} duty.} |
| 34 |
|
| 35 |
\item{wrapOnRejected}{A function that takes a single argument: a function |
| 36 |
that was passed as an \code{onRejected} argument to \code{\link[=then]{then()}}. The |
| 37 |
\code{wrapOnRejected} function should return a function that is suitable for |
| 38 |
\code{onRejected} duty.} |
| 39 |
|
| 40 |
\item{wrapSync}{A function that takes a single argument: a (lazily evaluated) |
| 41 |
expression that the function should \code{\link[=force]{force()}}. This expression represents |
| 42 |
the \code{expr} argument passed to \code{\link[=with_promise_domain]{with_promise_domain()}}; |
| 43 |
\code{wrapSync} allows the domain to manipulate the environment before/after |
| 44 |
\code{expr} is evaluated.} |
| 45 |
|
| 46 |
\item{onError}{A function that takes a single argument: an error. \code{onError} |
| 47 |
will be called whenever an exception occurs in a domain (that isn't caught |
| 48 |
by a \code{tryCatch}). Providing an \code{onError} callback doesn't cause errors to |
| 49 |
be caught, necessarily; instead, \code{onError} callbacks behave like calling |
| 50 |
handlers.} |
| 51 |
|
| 52 |
\item{...}{Arbitrary named values that will become elements of the promise |
| 53 |
domain object, and can be accessed as items in an environment (i.e. using |
| 54 |
\code{[[} or \code{$}).} |
| 55 |
|
| 56 |
\item{wrapOnFinally}{A function that takes a single argument: a function |
| 57 |
that was passed as an \code{onFinally} argument to \code{\link[=then]{then()}}. The |
| 58 |
\code{wrapOnFinally} function should return a function that is suitable for |
| 59 |
\code{onFinally} duty. If \code{wrapOnFinally} is \code{NULL} (the default), then the |
| 60 |
domain will use both \code{wrapOnFulfilled} and \code{wrapOnRejected} to wrap the |
| 61 |
\code{onFinally}. If it's important to distinguish between normal |
| 62 |
fulfillment/rejection handlers and finally handlers, then be sure to |
| 63 |
provide \code{wrapOnFinally}, even if it's just \code{\link[base:identity]{base::identity()}}.} |
| 64 |
} |
| 65 |
\description{ |
| 66 |
Promise domains are used to temporarily set up custom environments that |
| 67 |
intercept and influence the registration of callbacks. Create new promise |
| 68 |
domain objects using \code{new_promise_domain}, and temporarily activate a promise |
| 69 |
domain object (for the duration of evaluating a given expression) using |
| 70 |
\code{with_promise_domain}. |
| 71 |
} |
| 72 |
\details{ |
| 73 |
While \code{with_promise_domain} is on the call stack, any calls to |
| 74 |
\code{\link[=then]{then()}} (or higher level functions or operators, like |
| 75 |
\code{\link[=catch]{catch()}}) will belong to the promise domain. In addition, when a |
| 76 |
\code{then} callback that belongs to a promise domain is invoked, then any new |
| 77 |
calls to \code{then} will also belong to that promise domain. In other words, a |
| 78 |
promise domain "infects" not only the immediate calls to \code{then}, but also to |
| 79 |
"nested" calls to \code{then}. |
| 80 |
|
| 81 |
For more background, read the |
| 82 |
\href{https://gist.github.com/jcheng5/b1c87bb416f6153643cd0470ac756231}{original design doc}. |
| 83 |
|
| 84 |
For examples, see the source code of the Shiny package, which uses promise |
| 85 |
domains extensively to manage graphics devices and reactivity. |
| 86 |
} |