Skip to content

Commit b8e4f38

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

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

R/dash.R

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -844,18 +844,21 @@ Dash <- R6::R6Class(
844844
#' @param description Character. A description of the resource.
845845
#'
846846
callback_context.record_timing = function(name,
847-
duration=NULL,
847+
duration=NULL,
848848
description=NULL) {
849-
timing_information <- self$server$get_data("timing-information")
849+
timing_information_ <- attributes(dynGet("request"))$timing_information
850850

851-
if (name %in% timing_information) {
851+
if (name %in% timing_information_) {
852852
stop(paste0("Duplicate resource name ", name, " found."), call.=FALSE)
853853
}
854854

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

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

861864
# ------------------------------------------------------------------------
@@ -1246,37 +1249,43 @@ Dash <- R6::R6Class(
12461249
self$config$props_check <- dev_tools_props_check
12471250

12481251
if (private$debug && self$config$ui) {
1249-
self$server$on('before-request', function(server, ...) {
1250-
self$server$set_data("timing_information", list(
1252+
self$server$on('before-request', function(server, request, ...) {
1253+
attr(request, "timing_information") <- list(
12511254
"__dash_server" = list(
12521255
"dur" = as.numeric(Sys.time()),
12531256
"desc" = NULL
12541257
)
1255-
))
1258+
)
12561259
})
12571260

12581261
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-
1262+
timing_information_ <- attr(request, "timing_information")
1263+
dash_total <- timing_information_[['__dash_server']]
1264+
dash_total[['dur']] <- round((as.numeric(Sys.time()) - dash_total[['dur']]) * 1000)
1265+
12631266
request$response$append_header('Server-Timing',
12641267
paste0('dash_total;dur=', dash_total[['dur']]))
12651268

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

1269-
if (!is.null(timing_information[[item]]$desc)) {
1270-
header_content <- paste0(header_content, 'desc="', timing_information[[item]]$desc, '"')
1272+
for (item in seq_along(timing_information_)) {
1273+
header_content <- paste0(names(timing_information_[item]), ';')
1274+
1275+
if (!is.null(timing_information_[[item]]$desc)) {
1276+
header_content <- paste0(header_content, 'desc="', timing_information_[[item]]$desc, '"')
12711277
}
12721278

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

12771283
request$response$append_header('Server-Timing',
12781284
header_content)
12791285
}
1286+
1287+
# flush the context (probably unnecessary, but to be overly safe)
1288+
attr(request, "timing_information") <- list()
12801289
})
12811290
}
12821291

@@ -1386,16 +1395,13 @@ Dash <- R6::R6Class(
13861395

13871396
# reset the timestamp so we're able to determine when the last cycle end occurred
13881397
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())
13921398
})
13931399
} else if (hot_reload == TRUE & is.null(source_dir)) {
13941400
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")
13951401
} else if (hot_reload == FALSE && private$debug && self$config$ui) {
13961402
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())
1403+
# ensure the timing-information store is flushed
1404+
self$server$set_data("timing-information", list())
13991405
})
14001406
}
14011407

0 commit comments

Comments
 (0)