Skip to content

Commit

Permalink
Fix windows port-in-use
Browse files Browse the repository at this point in the history
- Don't refresh with empty files
- Test empty files arent checked with watcher
- Fix windows error with port in use
  • Loading branch information
ElianHugh committed Jun 17, 2024
1 parent d9bd2cc commit 200ac9e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
12 changes: 10 additions & 2 deletions R/engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ new_engine <- function(path, dirs, port, host, ignore) {
ignore <- ignore %||% c("*.sqlite", "*.git*") |>
paste0(collapse = "|") |>
utils::glob2rx()
runner_port <- port %||% httpuv::randomPort(
host = host,
n = 100L
)
socket_port <- httpuv::randomPort(
host = host,
n = 100L
)

list2env(
list(
runner = NULL,
config = list(
port = port %||% httpuv::randomPort(host = host),
port = port %||% runner_port,
path = dirname(path),
plumber_path = path,
dirs = dirs,
Expand All @@ -20,7 +28,7 @@ new_engine <- function(path, dirs, port, host, ignore) {
listen = sprintf(
"ws://%s:%s",
host,
httpuv::randomPort(host = host)
socket_port
),
autostart = TRUE
)
Expand Down
1 change: 1 addition & 0 deletions R/watcher.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ directory_state <- function(paths, ignore_pattern) {
extra_cols = FALSE
)
res <- res[grep(pattern = ignore_pattern, x = row.names(res), invert = TRUE), ]
res <- res[res$size > 0L, ]
stats::setNames(res$mtime, row.names(res))
}
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ devtools::install_github("ElianHugh/hotwater")

## Example

This is a basic example which shows you how to solve a common problem:
Hotwater can be run via an R session:

``` r
hotwater::run(
Expand All @@ -39,3 +39,14 @@ hotwater::run(
Server running on <127.0.0.1:9999> [17ms]
Watching ./path/to/ for changes...
```

or a terminal using the bash script:

```sh
hotwater -f my/plumber/api.R -p 9999
```

``` r
Server running on <127.0.0.1:9999> [17ms]
Watching ./path/to/ for changes...
```
12 changes: 0 additions & 12 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,10 @@

- It would be nice to remove callr dependency and just rely on mirai/nanonext

## 4

- Empty files ideally shouldn't force a refresh

## 5

- Creating a file without any content shouldn't cause a refresh

## 6

- Updating CSS shouldn't typically cause a refresh

## 7

- Nanonext is currently only used because I want an async connection for the local webpage refresh. May not need to use nanonext, considering we fake the protocol on the JS side of things anyway

## 9

- text in the CLI can often be out of order
3 changes: 2 additions & 1 deletion tests/testthat/test-watcher.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ test_that("directory_state returns expected file state", {
watcher_dir <<- withr::local_tempdir("testdir")
writeLines("Hello world", file.path(watcher_dir, "hello.R"))
writeLines("Bye world", file.path(watcher_dir, "bye.R"))
state <<- directory_state(watcher_dir, "*.gitignore")
file.create(file.path(watcher_dir, "empty.R"))
state <- directory_state(watcher_dir, "*.gitignore")
expect_length(state, 2L)
expect_length(names(state), 2L)
expect_s3_class(state, c("POSIXct", "POSIXt"))
Expand Down

0 comments on commit 200ac9e

Please sign in to comment.