Skip to content

Commit c5f9d4e

Browse files
author
Ryan Patrick Kyle
committed
🔨 set attributes on request
1 parent d9d4b2b commit c5f9d4e

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

R/dash.R

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Dash <- R6::R6Class(
9292
private$suppress_callback_exceptions <- suppress_callback_exceptions
9393
private$compress <- compress
9494
private$app_root_path <- getAppPath()
95+
private$timing_information <- list()
9596
private$app_launchtime <- as.integer(Sys.time())
9697
private$meta_tags <- meta_tags
9798
private$in_viewer <- FALSE
@@ -844,18 +845,21 @@ Dash <- R6::R6Class(
844845
#' @param description Character. A description of the resource.
845846
#'
846847
callback_context.record_timing = function(name,
847-
duration=NULL,
848+
duration=NULL,
848849
description=NULL) {
849-
timing_information <- self$server$get_data("timing-information")
850+
timing_information_ <- attributes(dynGet("request"))$timing_information
850851

851-
if (name %in% timing_information) {
852+
if (name %in% timing_information_) {
852853
stop(paste0("Duplicate resource name ", name, " found."), call.=FALSE)
853854
}
854855

855-
timing_information[[name]] <- list("dur" = round(duration * 1000),
856-
"desc" = description)
856+
timing_information_[[name]] <- list("dur" = round(duration * 1000),
857+
"desc" = description)
857858

858-
self$server$set_data("timing-information", timing_information)
859+
self$server$set_data("timing-information", timing_information_)
860+
861+
evalq(attr(req, "timing_information") <- app$server$get_data("timing-information"),
862+
envir = countEnclosingFrames("request"))
859863
},
860864

861865
# ------------------------------------------------------------------------
@@ -1246,37 +1250,43 @@ Dash <- R6::R6Class(
12461250
self$config$props_check <- dev_tools_props_check
12471251

12481252
if (private$debug && self$config$ui) {
1249-
self$server$on('before-request', function(server, ...) {
1250-
self$server$set_data("timing_information", list(
1253+
self$server$on('before-request', function(server, request, ...) {
1254+
attr(request, "timing_information") <- list(
12511255
"__dash_server" = list(
12521256
"dur" = as.numeric(Sys.time()),
12531257
"desc" = NULL
12541258
)
1255-
))
1259+
)
12561260
})
12571261

12581262
self$server$on('request', function(server, request, ...) {
1259-
timing_information <- self$server$get_data('timing-information')
1260-
dash_total <- timing_information[['__dash_server']]
1261-
dash_total[['dur']] <- round(as.numeric(Sys.time() - dash_total[['dur']]) * 1000)
1262-
1263+
timing_information_ <- attr(request, "timing_information")
1264+
dash_total <- timing_information_[['__dash_server']]
1265+
dash_total[['dur']] <- round((as.numeric(Sys.time()) - dash_total[['dur']]) * 1000)
1266+
12631267
request$response$append_header('Server-Timing',
12641268
paste0('dash_total;dur=', dash_total[['dur']]))
12651269

1266-
for (item in seq_along(timing_information)) {
1267-
header_content <- paste0(names(timing_information[item]), ';')
1270+
# ensure dash_server is not returned within the header
1271+
timing_information_ <- timing_information_[names(timing_information_) != "__dash_server"]
1272+
1273+
for (item in seq_along(timing_information_)) {
1274+
header_content <- paste0(names(timing_information_[item]), ';')
12681275

1269-
if (!is.null(timing_information[[item]]$desc)) {
1270-
header_content <- paste0(header_content, 'desc="', timing_information[[item]]$desc, '"')
1276+
if (!is.null(timing_information_[[item]]$desc)) {
1277+
header_content <- paste0(header_content, 'desc="', timing_information_[[item]]$desc, '"')
12711278
}
12721279

1273-
if (!is.null(timing_information[[item]]$dur)) {
1274-
header_content <- paste0(header_content, ';dur=', timing_information[[item]]$dur)
1280+
if (!is.null(timing_information_[[item]]$dur)) {
1281+
header_content <- paste0(header_content, ';dur=', timing_information_[[item]]$dur)
12751282
}
12761283

12771284
request$response$append_header('Server-Timing',
12781285
header_content)
12791286
}
1287+
1288+
# flush the context (probably unnecessary, but to be overly safe)
1289+
attr(request, "timing_information") <- list()
12801290
})
12811291
}
12821292

@@ -1386,16 +1396,13 @@ Dash <- R6::R6Class(
13861396

13871397
# reset the timestamp so we're able to determine when the last cycle end occurred
13881398
private$last_cycle <- as.integer(Sys.time())
1389-
1390-
# flush the context to prepare for the next request cycle
1391-
self$server$set_data("timing_information", list())
13921399
})
13931400
} else if (hot_reload == TRUE & is.null(source_dir)) {
13941401
message("\U{26A0} No source directory information available; hot reloading has been disabled.\nPlease ensure that you are loading your Dash for R application using source().\n")
13951402
} else if (hot_reload == FALSE && private$debug && self$config$ui) {
13961403
self$server$on("cycle-end", function(server, ...) {
1397-
# flush the context to prepare for the next request cycle
1398-
self$server$set_data("timing_information", list())
1404+
# ensure the timing-information store is flushed
1405+
self$server$set_data("timing-information", list())
13991406
})
14001407
}
14011408

@@ -1430,6 +1437,9 @@ Dash <- R6::R6Class(
14301437
# callback context
14311438
callback_context_ = NULL,
14321439

1440+
# timing information temporary store
1441+
timing_information = NULL,
1442+
14331443
# fields for setting modification times and paths to track state
14341444
asset_modtime = NULL,
14351445
app_launchtime = NULL,

0 commit comments

Comments
 (0)