Skip to content

Commit

Permalink
merges as.promise.mirai() from mirai.promises
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Oct 7, 2023
1 parent a97445e commit 4c5221a
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 260 deletions.
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ Depends:
Imports:
nanonext (>= 0.10.2.9000),
parallel
Suggests:
Enhances:
promises
Suggests:
later,
knitr,
rmarkdown
VignetteBuilder: knitr
Expand Down
5 changes: 1 addition & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# Generated by roxygen2: do not edit by hand

S3method(as.promise,mirai)
S3method(print,mirai)
S3method(print,miraiCluster)
S3method(print,miraiError)
S3method(print,miraiInterrupt)
S3method(print,miraiLaunchCmd)
S3method(print,miraiNode)
S3method(print,resolvedExpr)
S3method(print,unresolvedExpr)
S3method(recvData,miraiNode)
S3method(recvOneData,miraiCluster)
S3method(sendData,miraiNode)
S3method(stopCluster,miraiCluster)
export("%>>%")
export(.)
export(.daemon)
export(call_mirai)
export(daemon)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# mirai 0.11.0.9000 (development)

* Adds 'mirai' method for 'as.promise()' from the `promises` package (if both packages `promises` and `later` are available). This functionality is merged from the package `mirai.promises` for convenience, and allows use of the promise pipe `%...>%` with a 'mirai'.
* Eliminates the 'exitlinger' period that still applied to ephemeral daemons. These now synchronise with host and exit as soon as permissible.
* Removes the deprecated deferred evaluation pipe `%>>%`.
* Requires nanonext >= 0.10.2.9000.

# mirai 0.11.0
Expand Down
6 changes: 6 additions & 0 deletions R/mirai-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ NULL
registerS3method("recvOneData", "miraiCluster", recvOneData.miraiCluster, ns)
}

if (requireNamespace("promises", quietly = TRUE) && requireNamespace("later", quietly = TRUE))
registerS3method("as.promise", "mirai", as.promise.mirai, getNamespace("promises"))

}

# nocov end
Expand Down Expand Up @@ -133,3 +136,6 @@ NULL
recvData <- NULL
recvOneData <- NULL
sendData <- NULL

as.promise <- NULL

136 changes: 0 additions & 136 deletions R/pipe.R

This file was deleted.

66 changes: 66 additions & 0 deletions R/promises.R
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()
}
)
44 changes: 44 additions & 0 deletions man/as.promise.mirai.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 0 additions & 84 deletions man/deferred-execution-pipe.Rd

This file was deleted.

Loading

0 comments on commit 4c5221a

Please sign in to comment.