Skip to content

Commit 140bf51

Browse files
author
Ryan Patrick Kyle
committed
✨ initial support for callback context
1 parent 151e838 commit 140bf51

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

R/dash.R

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ Dash <- R6::R6Class(
208208

209209
dash_update <- paste0(self$config$routes_pathname_prefix, "_dash-update-component")
210210
route$add_handler("post", dash_update, function(request, response, keys, ...) {
211-
212211
request <- request_parse_json(request)
213212

214213
if (!"output" %in% names(request$body)) {
@@ -255,6 +254,9 @@ Dash <- R6::R6Class(
255254
}
256255
}
257256

257+
# set the callback context associated with this invocation of the callback
258+
private$callback_context_ <- setCallbackContext(request$body)
259+
258260
output_value <- getStackTrace(do.call(callback, callback_args),
259261
debug = private$debug,
260262
pruned_errors = private$pruned_errors)
@@ -463,8 +465,22 @@ Dash <- R6::R6Class(
463465
state=state,
464466
func=func
465467
)
468+
469+
# # register the callback_context elements
470+
# private$callback_context_[[paste(output$id, output$property, sep='.')]] <- list(
471+
# states=setCallbackContext(state),
472+
# triggered=list(),
473+
# inputs=getContextElements(inputs)
474+
# )
466475
},
467476

477+
# ------------------------------------------------------------------------
478+
# request and return callback context
479+
# ------------------------------------------------------------------------
480+
callback_context = function() {
481+
private$callback_context_
482+
},
483+
468484
# ------------------------------------------------------------------------
469485
# convenient fiery wrappers
470486
# ------------------------------------------------------------------------
@@ -517,7 +533,10 @@ Dash <- R6::R6Class(
517533
# initialize flags for debug mode and stack pruning,
518534
debug = NULL,
519535
pruned_errors = NULL,
520-
536+
537+
# callback context
538+
callback_context_ = NULL,
539+
521540
# fields for tracking HTML dependencies
522541
dependencies = list(),
523542
dependencies_user = list(),

R/utils.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,3 +732,33 @@ removeHandlers <- function(fnList) {
732732
"withRestarts")
733733
return(fnList[!fnList %in% omittedStrings])
734734
}
735+
736+
setCallbackContext <- function(callback_elements) {
737+
states <- lapply(callback_elements$states, function(x) {
738+
setNames(x$value, paste(x$id, x$property, sep="."))
739+
})
740+
741+
splitIdProp <- function(x) unlist(strsplit(x, split = "[.]"))
742+
743+
triggered <- lapply(callback_elements$changedPropIds,
744+
function(x) {
745+
input_id <- splitIdProp(x)[1]
746+
prop <- splitIdProp(x)[2]
747+
748+
id_match <- vapply(callback_elements$inputs, function(x) x$id %in% input_id, logical(1))
749+
prop_match <- vapply(callback_elements$inputs, function(x) x$property %in% prop, logical(1))
750+
751+
value <- sapply(callback_elements$inputs[id_match & prop_match], `[[`, "value")
752+
753+
list(`prop_id` = x, `value` = value)
754+
}
755+
)
756+
757+
inputs <- sapply(callback_elements$inputs, function(x) {
758+
setNames(list(x$value), paste(x$id, x$property, sep="."))
759+
})
760+
761+
return(list(states=states,
762+
triggered=unlist(triggered, recursive=FALSE),
763+
inputs=inputs))
764+
}

0 commit comments

Comments
 (0)