/
promise.R
519 lines (477 loc) · 14.3 KB
/
promise.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#' @useDynLib promises
#' @importFrom Rcpp evalCpp
#' @import later
NULL
#' @import R6
Promise <- R6::R6Class("Promise",
private = list(
state = "pending",
value = NULL,
visible = TRUE,
publicResolveRejectCalled = FALSE,
onFulfilled = list(),
onRejected = list(),
onFinally = list(),
rejectionHandled = FALSE,
# Private resolve/reject differs from public resolve/reject
# in that the private versions are allowed to be called
# more than once, whereas public ones no-op after the first
# time they are invoked.
doResolve = function(value) {
val <- withVisible(value)
value <- val$value
visible <- val$visible
if (is.promising(value)) {
value <- as.promise(value)
if (identical(self, attr(value, "promise_impl", exact = TRUE))) {
return(private$doReject(simpleError("Chaining cycle detected for promise")))
}
# This then() call doesn't need promise domains; semantically, it doesn't
# really exist, as it's just a convenient way to implement the new promise
# inhabiting the old promise's corpse.
without_promise_domain({
value$then(
private$doResolve,
private$doReject
)
})
} else {
private$doResolveFinalValue(value, visible)
}
},
doReject = function(reason) {
if (is.promising(reason)) {
reason <- as.promise(reason)
# This then() call doesn't need promise domains; semantically, it doesn't
# really exist, as it's just a convenient way to implement the new promise
# inhabiting the old promise's corpse.
without_promise_domain({
reason$then(
private$doResolve,
private$doReject
)
})
} else {
private$doRejectFinalReason(reason)
}
},
# These "final" versions of resolve/reject are for when we've
# established that the value/reason is not itself a promise.
doResolveFinalValue = function(value, visible) {
private$value <- value
private$visible <- visible
private$state <- "fulfilled"
later::later(function() {
lapply(private$onFulfilled, function(f) {
f(private$value, private$visible)
})
private$onFulfilled <- list()
})
},
doRejectFinalReason = function(reason) {
private$value <- reason
private$state <- "rejected"
later::later(function() {
lapply(private$onRejected, function(f) {
private$rejectionHandled <- TRUE
f(private$value)
})
private$onRejected <- list()
later::later(~{
if (!private$rejectionHandled) {
# warning() was unreliable here
cat(file=stderr(), "Unhandled promise error: ", reason$message, "\n", sep = "")
}
})
})
}
),
public = list(
# "pending", "fulfilled", "rejected"
status = function() {
private$state
},
resolve = function(value) {
# Only allow this to be called once, then no-op.
if (private$publicResolveRejectCalled)
return(invisible())
private$publicResolveRejectCalled <- TRUE
tryCatch(
{
# Important: Do not trigger evaluation of value before
# passing to doResolve. doResolve calls withVisible() on
# value, so evaluating it before that point will cause
# the visibility to be lost.
private$doResolve(value)
},
error = function(err) {
private$doReject(err)
}
)
invisible()
},
reject = function(reason) {
# Only allow this to be called once, then no-op.
if (private$publicResolveRejectCalled)
return(invisible())
private$publicResolveRejectCalled <- TRUE
tryCatch(
{
force(reason)
if (is.character(reason)) {
reason <- simpleError(reason)
}
private$doReject(reason)
},
error = function(err) {
private$doReject(err)
}
)
invisible()
},
then = function(onFulfilled = NULL, onRejected = NULL, onFinally = NULL) {
onFulfilled <- normalizeOnFulfilled(onFulfilled)
onRejected <- normalizeOnRejected(onRejected)
if (!is.function(onFinally)) {
onFinally <- NULL
}
promise2 <- promise(function(resolve, reject) {
res <- promiseDomain$onThen(onFulfilled, onRejected, onFinally)
if (!is.null(res)) {
onFulfilled <- res$onFulfilled
onRejected <- res$onRejected
}
handleFulfill <- function(value, visible) {
if (is.function(onFulfilled)) {
resolve(onFulfilled(value, visible))
} else {
resolve(if (visible) value else invisible(value))
}
}
handleReject <- function(reason) {
if (is.function(onRejected)) {
# Yes, resolve, not reject.
resolve(onRejected(reason))
} else {
# Yes, reject, not resolve.
reject(reason)
}
}
if (private$state == "pending") {
private$onFulfilled <- c(private$onFulfilled, list(
handleFulfill
))
private$onRejected <- c(private$onRejected, list(
handleReject
))
} else if (private$state == "fulfilled") {
later::later(function() {
handleFulfill(private$value, private$visible)
})
} else if (private$state == "rejected") {
later::later(function() {
private$rejectionHandled <- TRUE
handleReject(private$value)
})
} else {
stop("Unexpected state ", private$state)
}
})
invisible(promise2)
},
catch = function(onRejected) {
invisible(self$then(onRejected = onRejected))
},
finally = function(onFinally) {
invisible(self$then(
onFinally = onFinally
))
},
format = function() {
if (private$state == "pending") {
"<Promise [pending]>"
} else {
classname <- class(private$value)[[1]]
if (length(classname) == 0) classname <- ""
sprintf("<Promise [%s: %s]>", private$state, classname)
}
}
)
)
normalizeOnFulfilled <- function(onFulfilled) {
if (!is.function(onFulfilled)) {
if (!is.null(onFulfilled)) {
warning("`onFulfilled` must be a function or `NULL`")
}
return(NULL)
}
args <- formals(onFulfilled)
arg_count <- length(args)
if (arg_count >= 2 && names(args)[[2]] == ".visible") {
onFulfilled
} else if (arg_count > 0) {
function(value, .visible) {
if (isTRUE(.visible)) {
onFulfilled(value)
} else {
onFulfilled(invisible(value))
}
}
} else {
function(value, .visible) {
onFulfilled()
}
}
}
normalizeOnRejected <- function(onRejected) {
if (!is.function(onRejected)) {
if (!is.null(onRejected)) {
warning("`onRejected` must be a function or `NULL`")
}
return(NULL)
}
args <- formals(onRejected)
arg_count <- length(args)
if (arg_count >= 1) {
onRejected
} else if (arg_count == 0) {
function(reason) {
onRejected()
}
}
}
#' Create a new promise object
#'
#' `promise()` creates a new promise. A promise is a placeholder object for the
#' eventual result (or error) of an asynchronous operation. This function is not
#' generally needed to carry out asynchronous programming tasks; instead, it is
#' intended to be used mostly by package authors who want to write asynchronous
#' functions that return promises.
#'
#' The `action` function should be a piece of code that returns quickly, but
#' initiates a potentially long-running, asynchronous task. If/when the task
#' successfully completes, call `resolve(value)` where `value` is the result of
#' the computation (like the return value). If the task fails, call
#' `reject(reason)`, where `reason` is either an error object, or a character
#' string.
#'
#' It's important that asynchronous tasks kicked off from `action` be coded very
#' carefully--in particular, all errors must be caught and passed to `reject()`.
#' Failure to do so will cause those errors to be lost, at best; and the caller
#' of the asynchronous task will never receive a response (the asynchronous
#' equivalent of a function call that never returns, i.e. hangs).
#'
#' The return value of `action` will be ignored.
#'
#' @param action A function with signature `function(resolve, reject)`, or a
#' one-sided formula. See Details.
#'
#' @return A promise object (see \code{\link{then}}).
#'
#' @examples
#' # Create a promise that resolves to a random value after 2 secs
#' p1 <- promise(function(resolve, reject) {
#' later::later(~resolve(runif(1)), delay = 2)
#' })
#'
#' p1 %...>% print()
#'
#' # Create a promise that errors immediately
#' p2 <- promise(~{
#' reject("An error has occurred")
#' })
#' then(p2,
#' onFulfilled = ~message("Success"),
#' onRejected = ~message("Failure")
#' )
#'
#' @export
promise <- function(action) {
if (inherits(action, "formula")) {
if (length(action) != 2) {
stop("'action' must be a function or one-sided formula")
}
} else if (is.function(action)) {
if (length(formals(action)) != 2) {
stop("'action' function must have two arguments")
}
} else {
stop("Invalid action argument--must be a function or formula")
}
p <- Promise$new()
tryCatch(
{
if (is.function(action)) {
action(p$resolve, p$reject)
} else if (inherits(action, "formula")) {
eval(action[[2]], list(
resolve = p$resolve,
reject = p$reject,
return = function(value) {
warning("Can't return a value from a promise, use resolve/reject")
}
), environment(action))
}
},
error = function(e) {
if (p$status() == "pending") {
p$reject(e)
} else {
# Too late to do anything useful. Just notify.
warning(e)
}
}
)
structure(
list(
then = p$then,
catch = p$catch,
finally = p$finally
),
class = "promise",
promise_impl = p
)
}
#' Create a resolved or rejected promise
#'
#' Helper functions to conveniently create a promise that is resolved to the
#' given value (or rejected with the given reason).
#'
#' @param value A value, or promise, that the new promise should be resolved to.
#' This expression will be lazily evaluated, and if evaluating the expression
#' raises an error, then the new promise will be rejected with that error as
#' the reason.
#' @param reason An error message string, or error object.
#'
#' @examples
#' promise_resolve(mtcars) %...>%
#' head() %...>%
#' print()
#'
#' promise_reject("Something went wrong") %...T!%
#' { message(conditionMessage(.)) }
#'
#' @export
promise_resolve <- function(value) {
promise(~resolve(value))
}
#' @rdname promise_resolve
#' @export
promise_reject <- function(reason) {
promise(~reject(reason))
}
#' Coerce to a promise
#'
#' Use `is.promise` to determine whether an R object is a promise. Use
#' `as.promise` (an S3 generic method) to attempt to coerce an R object to a
#' promise, and `is.promising` (another S3 generic method) to test whether
#' `as.promise` is supported. This package includes support for converting
#' [future::Future] objects into promises.
#'
#' @param x An R object to test or coerce.
#'
#' @return `as.promise` returns a promise object, or throws an error if the
#' object cannot be converted.
#'
#' `is.promise` returns `TRUE` if the given value is a promise object, and
#' `FALSE` otherwise.
#'
#' `is.promising` returns `TRUE` if the given value is a promise object or
#' if it can be converted to a promise object using `as.promise`, and `FALSE`
#' otherwise.
#'
#' @export
is.promise <- function(x) {
inherits(x, "promise")
}
#' @rdname is.promise
#' @export
is.promising <- function(x) {
UseMethod("is.promising")
}
#' @export
is.promising.default <- function(x) {
FALSE
}
#' @export
is.promising.promise <- function(x) {
TRUE
}
#' @export
is.promising.Future <- function(x) {
TRUE
}
#' @rdname is.promise
#' @export
as.promise <- function(x) {
UseMethod("as.promise", x)
}
#' @export
as.promise.promise <- function(x) {
x
}
#' @export
as.promise.Future <- function(x) {
# We want to create a promise only once for each Future object, and cache it
# as an attribute. This spares us from having multiple polling loops waiting
# for the same Future.
cached <- attr(x, "converted_promise", exact = TRUE)
if (!is.null(cached)) {
return(cached)
}
p <- promise(function(resolve, reject) {
poll_interval <- 0.1
check <- function() {
# timeout = 0 is important, the default waits for 200ms
is_resolved <- tryCatch({
future::resolved(x, timeout = 0)
}, FutureError = function(e) {
reject(e)
TRUE
})
if (is_resolved) {
tryCatch(
{
result <- future::value(x, signal = TRUE)
resolve(result)
}, FutureError = function(e) {
reject(e)
TRUE
}, error = function(e) {
reject(e)
}
)
} else {
later::later(check, poll_interval)
}
}
check()
})
# Store the new promise for next time
attr(x, "converted_promise") <- p
p
}
#' @export
as.promise.default <- function(x) {
# TODO: If x is an error or try-error, should this return a rejected promise?
stop("Don't know how to convert object of class ", class(x)[[1L]], " into a promise")
}
#' Fulfill a promise
#'
#' Use these functions to satisfy a promise with either success (\code{resolve})
#' or failure (\code{reject}). These functions are not exported, but rather, are
#' passed as arguments to the \code{action} function you pass to a [promise]
#' constructor.
#'
#' @param value The result from a successful calculation.
#' @param reason An error or string that explains why the operation failed.
#'
#' @keywords internal
resolve <- function(value = NULL) {
stop("resolve() must be called from within a promise constructor")
}
#' @rdname resolve
#' @keywords internal
reject <- function(reason) {
stop("reject() must be called from within a promise constructor")
}