Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
updates promises method for better Shiny integration
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Apr 8, 2024
1 parent 86500de commit 4d09dd7
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
permissions:
contents: write
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand All @@ -41,7 +41,7 @@ jobs:

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action@v4.4.1
uses: JamesIves/github-pages-deploy-action@v4
with:
clean: false
branch: gh-pages
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Encoding: UTF-8
Depends:
R (>= 3.5)
Imports:
nanonext (>= 0.10.1),
promises (>= 1.1.0)
nanonext (>= 0.13.3),
promises (>= 1.3.0)
Suggests:
mirai (>= 0.10.0)
mirai (>= 0.12.1)
RoxygenNote: 7.3.1
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

S3method(as.promise,mirai)
S3method(as.promise,recvAio)
S3method(is.promising,recvAio)
export(polling)
importFrom(nanonext,is_error_value)
importFrom(nanonext,unresolved)
importFrom(promises,as.promise)
importFrom(promises,is.promising)
importFrom(promises,promise)
importFrom(promises,then)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# mirai.promises 0.4.1.9000 (development)

* Updates promises method for improved integration with Shiny (thanks @jcheng5).
* Adds is.promising() method for 'recvAio'.
* Requires promises >= 1.3.0 and nanonext >= 0.13.3.

# mirai.promises 0.4.1

* Performance enhancements.
Expand Down
59 changes: 37 additions & 22 deletions R/core.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,11 @@
#' (\href{https://orcid.org/0000-0002-0750-061X}{ORCID})
#'
#' @importFrom nanonext is_error_value unresolved
#' @importFrom promises as.promise promise
#' @importFrom promises as.promise is.promising promise then
#'
"_PACKAGE"

# nocov start
# tested implicitly

.onLoad <- function(libname, pkgname) {

as.promise.recvAio <<- as.promise.mirai <<- as.promise.mirai()

}

# nocov end
. <- list2env(list(later = .getNamespace("later")[["later"]], pollfreq = 0.1))

#' Make 'Mirai' 'Promise'
#'
Expand Down Expand Up @@ -85,27 +76,51 @@
#' @export
#'
as.promise.mirai <- function(x) {
later <- .getNamespace("later")[["later"]]
pollfreq <- 0.1
function(x)
promise(
force(x)
then(
promise = promise(
function(resolve, reject) {
query <- function()
if (unresolved(x))
later(query, delay = pollfreq) else
if (is_error_value(value <- .subset2(x, "value")))
reject(value) else
resolve(value)
.[["later"]](query, delay = .[["pollfreq"]]) else
resolve(.subset2(x, "value"))
query()
}
)
),
onFulfilled = function(value)
if (is_error_value(value) && !mirai::is_mirai_interrupt(value))
stop(value) else
value
)
}

#' @rdname as.promise.mirai
#' @method as.promise recvAio
#' @export
#'
as.promise.recvAio <- as.promise.mirai
as.promise.recvAio <- function(x) {
force(x)
then(
promise = promise(
function(resolve, reject) {
query <- function()
if (unresolved(x))
.[["later"]](query, delay = .[["pollfreq"]]) else
resolve(.subset2(x, "value"))
query()
}
),
onFulfilled = function(value)
if (is_error_value(value))
stop(value) else
value
)
}

#' @method is.promising recvAio
#' @export
#'
is.promising.recvAio <- function(x) TRUE

#' Set Polling Frequency
#'
Expand All @@ -128,7 +143,7 @@ as.promise.recvAio <- as.promise.mirai
polling <- function(freq = 100L) {

is.numeric(freq) || stop("'freq' must be a numeric value")
`[[<-`(environment(as.promise.mirai), "pollfreq", freq / 1000L)
`[[<-`(., "pollfreq", freq / 1000L)
invisible()

}
6 changes: 4 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ knitr::opts_chunk$set(

<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/mirai.promises?color=112d4e)](https://CRAN.R-project.org/package=mirai.promises)
[![mirai.promises status badge](https://shikokuchuo.r-universe.dev/badges/mirai.promises?color=24a60e)](https://shikokuchuo.r-universe.dev/mirai.promises)
[![mirai.promises status badge](https://shikokuchuo.r-universe.dev/badges/mirai.promises?color=ddcacc)](https://shikokuchuo.r-universe.dev/mirai.promises)
[![R-CMD-check](https://github.com/shikokuchuo/mirai.promises/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/shikokuchuo/mirai.promises/actions/workflows/R-CMD-check.yaml)
[![Codecov](https://codecov.io/gh/shikokuchuo/mirai.promises/branch/main/graph/badge.svg)](https://app.codecov.io/gh/shikokuchuo/mirai.promises)
[![DOI](https://zenodo.org/badge/647242817.svg)](https://zenodo.org/badge/latestdoi/647242817)
Expand All @@ -37,7 +37,7 @@ Install the latest release from CRAN:
install.packages("mirai.promises")
```

or the development version from rOpenSci R-universe:
Or the development version from R-universe:

```{r runiv, eval=FALSE}
install.packages("mirai.promises", repos = "https://shikokuchuo.r-universe.dev")
Expand Down Expand Up @@ -97,6 +97,8 @@ shinyApp(ui = ui, server = server)

### Thanks

[Joe Cheng](https://github.com/jcheng5) for optimising the promises method to work seamlessly within Shiny.

[Daniel Falbel](https://github.com/dfalbel/) for the original version of the above example and agreeing to its use here, as well as the specific use case that motivated this package.

### Links
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![CRAN
status](https://www.r-pkg.org/badges/version/mirai.promises?color=112d4e)](https://CRAN.R-project.org/package=mirai.promises)
[![mirai.promises status
badge](https://shikokuchuo.r-universe.dev/badges/mirai.promises?color=24a60e)](https://shikokuchuo.r-universe.dev/mirai.promises)
badge](https://shikokuchuo.r-universe.dev/badges/mirai.promises?color=ddcacc)](https://shikokuchuo.r-universe.dev/mirai.promises)
[![R-CMD-check](https://github.com/shikokuchuo/mirai.promises/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/shikokuchuo/mirai.promises/actions/workflows/R-CMD-check.yaml)
[![Codecov](https://codecov.io/gh/shikokuchuo/mirai.promises/branch/main/graph/badge.svg)](https://app.codecov.io/gh/shikokuchuo/mirai.promises)
[![DOI](https://zenodo.org/badge/647242817.svg)](https://zenodo.org/badge/latestdoi/647242817)
Expand All @@ -34,7 +34,7 @@ Install the latest release from CRAN:
install.packages("mirai.promises")
```

or the development version from rOpenSci R-universe:
Or the development version from R-universe:

``` r
install.packages("mirai.promises", repos = "https://shikokuchuo.r-universe.dev")
Expand Down Expand Up @@ -99,6 +99,9 @@ shinyApp(ui = ui, server = server)

### Thanks

[Joe Cheng](https://github.com/jcheng5) for optimising the promises
method to work seamlessly within Shiny.

[Daniel Falbel](https://github.com/dfalbel/) for the original version of
the above example and agreeing to its use here, as well as the specific
use case that motivated this package.
Expand Down
16 changes: 9 additions & 7 deletions tests/tests.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
library(mirai.promises)
library(nanonext)
requireNamespace("mirai", quietly = TRUE)
library(promises)
library(mirai.promises)
nanotest <- function(x) invisible(x || stop("is not TRUE when expected to be TRUE"))

s <- socket()
r <- recv_aio(s)
nanotest(is.promise(as.promise(r)))
nanotest(is.promising(r))
close(s)
if (requireNamespace("mirai", quietly = TRUE)) {
nanotest(is.promise(p1 <- as.promise(mirai::mirai("completed"))))
nanotest(is.promise(p2 <- mirai::mirai("completed") %...>% identity()))
nanotest(environment(mirai.promises:::as.promise.mirai)[["pollfreq"]] == 0.1)
nanotest(is.null(polling(freq = 1000)))
nanotest(environment(mirai.promises:::as.promise.mirai)[["pollfreq"]] == 1L)
nanotest(is.null(polling()))
nanotest(environment(mirai.promises:::as.promise.mirai)[["pollfreq"]] == 0.1)
Sys.sleep(3L)
}
Sys.sleep(3L)
nanotest(mirai.promises:::.[["pollfreq"]] == 0.1)
nanotest(is.null(polling(freq = 1000)))
nanotest(mirai.promises:::.[["pollfreq"]] == 1L)
nanotest(is.null(polling()))
nanotest(mirai.promises:::.[["pollfreq"]] == 0.1)

0 comments on commit 4d09dd7

Please sign in to comment.