Skip to content

Commit 68be6a9

Browse files
authored
Fixes for hot reloading interval handling and refreshing apps within viewer pane (#148)
1 parent 2ca3f98 commit 68be6a9

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

R/dash.R

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Dash <- R6::R6Class(
187187
private$app_root_path <- getAppPath()
188188
private$app_launchtime <- as.integer(Sys.time())
189189
private$meta_tags <- meta_tags
190+
private$in_viewer <- FALSE
190191

191192
# config options
192193
self$config$routes_pathname_prefix <- resolve_prefix(routes_pathname_prefix, "DASH_ROUTES_PATHNAME_PREFIX")
@@ -550,19 +551,19 @@ Dash <- R6::R6Class(
550551
server$on("start", function(server, ...) {
551552
private$generateReloadHash()
552553
private$index()
554+
555+
viewer <- !(is.null(getOption("viewer"))) && (dynGet("use_viewer") == TRUE)
553556

554-
use_viewer <- !(is.null(getOption("viewer"))) && (dynGet("use_viewer") == TRUE)
555-
host <- dynGet("host")
556-
port <- dynGet("port")
557+
app_url <- paste0("http://", self$server$host, ":", self$server$port)
557558

558-
app_url <- paste0("http://", host, ":", port)
559-
560-
if (use_viewer && host %in% c("localhost", "127.0.0.1"))
559+
if (viewer && self$server$host %in% c("localhost", "127.0.0.1")) {
561560
rstudioapi::viewer(app_url)
562-
else if (use_viewer) {
561+
private$in_viewer <- TRUE
562+
}
563+
else if (viewer) {
563564
warning("\U{26A0} RStudio viewer not supported; ensure that host is 'localhost' or '127.0.0.1' and that you are using RStudio to run your app. Opening default browser...")
564565
utils::browseURL(app_url)
565-
}
566+
}
566567
})
567568

568569
# user-facing fields
@@ -702,7 +703,7 @@ Dash <- R6::R6Class(
702703
hot_reload_watch_interval <- getServerParam(dev_tools_hot_reload_watch_interval, "double", 0.5)
703704
hot_reload_max_retry <- getServerParam(as.integer(dev_tools_hot_reload_max_retry), "integer", 8)
704705
# convert from seconds to msec as used by js `setInterval`
705-
self$config$hot_reload <- list(interval = hot_reload_watch_interval * 1000, max_retry = hot_reload_max_retry)
706+
self$config$hot_reload <- list(interval = hot_reload_interval * 1000, max_retry = hot_reload_max_retry)
706707
} else {
707708
hot_reload <- FALSE
708709
}
@@ -713,14 +714,16 @@ Dash <- R6::R6Class(
713714
if (hot_reload == TRUE & !(is.null(source_dir))) {
714715
self$server$on('cycle-end', function(server, ...) {
715716
# handle case where assets are not present, since we can still hot reload the app itself
716-
# private$last_refresh will get set after the asset_map is refreshed
717+
#
718+
# private$last_refresh is set after the asset_map is refreshed
719+
# private$last_reload stores the time of the last hard or soft reload event
717720
# private$last_cycle will be set when the cycle-end handler terminates
718-
if (!is.null(private$last_cycle) & !is.null(hot_reload_interval)) {
719-
# determine if the time since last cycle end is equal to or longer than the requested check interval
720-
permit_reload <- (as.integer(Sys.time()) - private$last_cycle) >= hot_reload_interval
721+
#
722+
if (!is.null(private$last_cycle) & !is.null(hot_reload_watch_interval)) {
723+
permit_reload <- (Sys.time() - private$last_reload) >= hot_reload_watch_interval
721724
} else {
722725
permit_reload <- FALSE
723-
}
726+
}
724727

725728
if (permit_reload) {
726729
if (dir.exists(private$assets_folder)) {
@@ -787,6 +790,9 @@ Dash <- R6::R6Class(
787790
if (!hard_reload) {
788791
# refresh the index but don't restart the server
789792
private$index()
793+
# while not a "hard" reload, update last_reload to reflect "soft" reloads also
794+
# since we determine whether to perform subsequent reloads based this value
795+
private$last_reload <- as.integer(Sys.time())
790796
} else {
791797
# if the server was started via Rscript or via source()
792798
# then update the app object here
@@ -800,6 +806,9 @@ Dash <- R6::R6Class(
800806
private$callback_map <- get("callback_map", envir=get("app", envir=app_env)$.__enclos_env__$private)
801807
private$layout_ <- get("layout_", envir=get("app", envir=app_env)$.__enclos_env__$private)
802808
private$index()
809+
# if using the viewer, reload app there
810+
if (private$in_viewer)
811+
rstudioapi::viewer(paste0("http://", self$server$host, ":", self$server$port))
803812
# tear down the temporary environment
804813
rm(app_env)
805814
}
@@ -846,11 +855,16 @@ Dash <- R6::R6Class(
846855
app_launchtime = NULL,
847856
app_root_path = NULL,
848857
app_root_modtime = NULL,
849-
last_reload = NULL,
858+
859+
# fields for controlling hot reloading state
860+
last_reload = numeric(1),
850861
last_refresh = NULL,
851862
last_cycle = NULL,
852863
modified_since_reload = NULL,
853864

865+
# field to store whether viewer has been requested
866+
in_viewer = NULL,
867+
854868
# fields for tracking HTML dependencies
855869
dependencies = list(),
856870
dependencies_user = list(),

0 commit comments

Comments
 (0)