fix rLog$reset to work as an installed package. added a dummy context reactId (different from noReactId)

This commit is contained in:
Barret Schloerke
2018-12-14 12:05:22 -05:00
parent beecf60db7
commit 0f13056aa2
3 changed files with 36 additions and 12 deletions

View File

@@ -149,6 +149,8 @@ RLog <- R6Class(
noReactIdLabel = "NoCtxReactId",
noReactId = reactIdStr("NoCtxReactId"),
dummyReactIdLabel = "DummyReactId",
dummyReactId = reactIdStr("DummyReactId"),
asList = function() {
ret <- self$logStack$as_list()
@@ -173,20 +175,21 @@ RLog <- R6Class(
paste0(reactId, "$", key)
},
initialize = function(rlogOption = "shiny.reactlog", msgOption = "shiny.reactlog.console") {
private$option <- rlogOption
private$msgOption <- msgOption
self$reset()
},
reset = function() {
.globals$reactIdCounter <- 0L
self$logStack <- Stack$new()
self$msg <- MessageLogger$new(option = private$msgOption)
self$msg$setReact(list(reactId = self$noReactId, label = self$noReactIdLabel))
# setup dummy and missing react information
self$msg$setReact(force = TRUE, list(reactId = self$noReactId, label = self$noReactIdLabel))
self$msg$setReact(force = TRUE, list(reactId = self$dummyReactId, label = self$dummyReactIdLabel))
},
isLogging = function() {
isTRUE(getOption(private$option, FALSE))
@@ -448,6 +451,7 @@ MessageLogger = R6Class(
if (!missing(depth)) self$depth <- depth
if (!missing(option)) self$option <- option
},
isLogging = function() {
isTRUE(getOption(self$option))
},
@@ -467,16 +471,17 @@ MessageLogger = R6Class(
!is.null(self$getReact(reactId))
},
getReact = function(reactId) {
if (self$isNotLogging()) return(NULL)
# ok to not check for logging as it would only retrieve from a minimal list, which is NULL
self$reactCache[[reactId]]
},
setReact = function(reactObj) {
if (self$isNotLogging()) return(NULL)
setReact = function(reactObj, force = FALSE) {
if (identical(force, FALSE) && self$isNotLogging()) return(NULL)
self$reactCache[[reactObj$reactId]] <- reactObj
},
reactStr = function(reactId) {
if (self$isNotLogging()) return(NULL)
reactInfo <- self$getReact(reactId)
if (is.null(reactInfo)) return(" <UNKNOWN_REACTID>")
paste0(
" ", reactInfo$reactId, ":", reactInfo$label
)

View File

@@ -202,7 +202,7 @@ hasCurrentContext <- function() {
getDummyContext <- function() {
Context$new(
getDefaultReactiveDomain(), '[none]', type = 'isolate',
id = "Dummy"
id = "Dummy", reactId = rLog$dummyReactId
)
}

View File

@@ -16,7 +16,9 @@ withOption <- function(key, value, oldVal = NULL, expr) {
}
withLogging <- function(expr) {
rLog$reset()
# reset ctx counter
reactiveEnvr <- .getReactiveEnvironment()
reactiveEnvr$.nextId <- 0L
@@ -44,6 +46,23 @@ expect_logs <- function(expr, ...) {
)
}
test_that("rLog resets when options are FALSE", {
withOption("shiny.reactlog", FALSE, FALSE, {
withOption("shiny.reactlog.console", FALSE, FALSE, {
rLog$reset()
# check for dummy and no reactid information
expect_true(!is.null(rLog$noReactId))
expect_true(!is.null(rLog$dummyReactId))
expect_equal(rLog$msg$getReact(rLog$noReactId)$reactId, rLog$noReactId)
expect_equal(rLog$msg$getReact(rLog$dummyReactId)$reactId, rLog$dummyReactId)
expect_equal(length(rLog$msg$reactCache), 2)
})
})
})
test_that("message logger appears", {
withLogging({
@@ -75,7 +94,7 @@ test_that("message logger appears", {
react()
},
"- createContext: ctxDummy - isolate",
"- dependsOn: rNoCtxReactId:NoCtxReactId on r3:reactive(val() + values$a) in ctxDummy",
"- dependsOn: rDummyReactId:DummyReactId on r3:reactive(val() + values$a) in ctxDummy",
"- createContext: ctx1 - observable",
"- enter: r3:reactive(val() + values$a) in ctx1 - observable",
"= - dependsOn: r3:reactive(val() + values$a) on r1:val in ctx1",
@@ -91,9 +110,9 @@ test_that("message logger appears", {
"- valueChange: r1:val",
"- invalidateStart: r1:val",
"= - invalidateStart: r3:reactive(val() + values$a) in ctx1 - observable",
"= = - isolateInvalidateStart: rNoCtxReactId:NoCtxReactId in ctxDummy",
"= = = - dependsOnRemove: rNoCtxReactId:NoCtxReactId on r3:reactive(val() + values$a) in ctxDummy",
"= = - isolateInvalidateEnd: rNoCtxReactId:NoCtxReactId in ctxDummy",
"= = - isolateInvalidateStart: rDummyReactId:DummyReactId in ctxDummy",
"= = = - dependsOnRemove: rDummyReactId:DummyReactId on r3:reactive(val() + values$a) in ctxDummy",
"= = - isolateInvalidateEnd: rDummyReactId:DummyReactId in ctxDummy",
"= = - dependsOnRemove: r3:reactive(val() + values$a) on r1:val in ctx1",
"= = - dependsOnRemove: r3:reactive(val() + values$a) on r2$a:values$a in ctx1",
"= - invalidateEnd: r3:reactive(val() + values$a) in ctx1 - observable",