-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpromises.R
100 lines (89 loc) · 2.99 KB
/
promises.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright (C) 2023-2024 Hibiki AI Limited <[email protected]>
#
# This file is part of mirai.
#
# mirai is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# mirai is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# mirai. If not, see <https://www.gnu.org/licenses/>.
# mirai.promises ---------------------------------------------------------------
# Joe Cheng (3 Apr 2024):
#
# We are going through some effort here to ensure that any error we raise here
# has "deep stacks" preserved while run in a Shiny app. For this to happen, we
# either need to raise the error while the `as.promise.mirai` call is still on
# the call stack, or, we are within an `onFulfilled` or `onRejected` callback
# from a `promises::then` call (assuming that `then()` was called while
# `as.promise.mirai` was still on the call stack).
#
# The only way we would violate those rules is by raising the error from
# within a `later::later` callback. So this code is factored to isolate that
# `later::later` code
#' Make Mirai Promise
#'
#' Creates a \sQuote{promise} from a \sQuote{mirai}.
#'
#' This function is an S3 method for the generic \code{as.promise} for class
#' \sQuote{mirai}.
#'
#' Requires the \CRANpkg{promises} package.
#'
#' Allows a \sQuote{mirai} to be used with the promise pipe \code{\%...>\%},
#' which schedules a function to run upon resolution of the \sQuote{mirai}.
#'
#' @param x an object of class \sQuote{mirai}.
#'
#' @return A \sQuote{promise} object.
#'
#' @examples
#' if (interactive() && requireNamespace("promises", quietly = TRUE)) {
#'
#' library(promises)
#'
#' p <- as.promise(mirai("example"))
#' print(p)
#' is.promise(p)
#'
#' p2 <- mirai("completed") %...>% identity()
#' p2$then(cat)
#' is.promise(p2)
#'
#' }
#'
#' @exportS3Method promises::as.promise
#'
as.promise.mirai <- function(x) {
promise <- .subset2(x, "promise")
if (is.null(promise)) {
promise <- if (unresolved(x)) {
promises::promise(
function(resolve, reject) .keep(x, environment())
)$then(
onFulfilled = function(value, .visible)
if (is_error_value(value) && !is_mirai_interrupt(value))
stop(if (is_mirai_error(value)) value else nng_error(value)) else
value
)
} else {
value <- .subset2(x, "value")
promises::promise(
function(resolve, reject)
if (is_error_value(value) && !is_mirai_interrupt(value))
reject(value) else
resolve(value)
)
}
assign("promise", promise, x)
}
promise
}
#' @exportS3Method promises::is.promising
#'
is.promising.mirai <- function(x) TRUE