-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merges as.promise.mirai() from mirai.promises
- Loading branch information
1 parent
a97445e
commit 4c5221a
Showing
9 changed files
with
161 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright (C) 2023 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 --------------------------------------------------------------- | ||
|
||
#' Make 'Mirai' 'Promise' | ||
#' | ||
#' Creates a 'promise' from a 'mirai'. | ||
#' | ||
#' @param x an object of class 'mirai'. | ||
#' | ||
#' @return A 'promise' object. | ||
#' | ||
#' @details This function is an S3 method for the generic \code{as.promise} for | ||
#' class 'mirai'. | ||
#' | ||
#' Requires the packages \CRANpkg{promises} and \CRANpkg{later}. | ||
#' | ||
#' Allows a 'mirai' to be used with the promise pipe \code{\%...>\%}, which | ||
#' schedules a function to run upon resolution of the 'mirai'. | ||
#' | ||
#' @examples | ||
#' if (interactive() && | ||
#' requireNamespace("promises", quietly = TRUE) && | ||
#' requireNamespace("later", quietly = TRUE)) { | ||
#' | ||
#' library(promises) | ||
#' | ||
#' p <- as.promise(mirai("example")) | ||
#' print(p) | ||
#' is.promise(p) | ||
#' | ||
#' p2 <- mirai("completed") %...>% identity() | ||
#' p2$then(cat) | ||
#' is.promise(p2) | ||
#' | ||
#' } | ||
#' | ||
#' @method as.promise mirai | ||
#' @export | ||
#' | ||
as.promise.mirai <- function(x) | ||
promises::promise( | ||
function(resolve, reject) { | ||
query <- function() | ||
if (unresolved(x)) | ||
later::later(query, delay = 0.1) else | ||
if (is_error_value(value <- .subset2(x, "value"))) | ||
reject(value) else | ||
resolve(value) | ||
query() | ||
} | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.