change all nodeId to reactId

This commit is contained in:
Barret Schloerke
2018-04-16 09:17:04 -04:00
parent 0467d6666a
commit f9fc3a46b5
4 changed files with 124 additions and 110 deletions

View File

@@ -1,15 +1,6 @@
# A scope where we can put mutable global state
.globals <- new.env(parent = emptyenv())
# used to help define truely global node id's.
# should work accross session and in global namespace
.globals$logNodeId <- 0L
.globalsIncrementLogNodeId <- function() {
.globals$logNodeId <- .globals$logNodeId + 1L
as.character(.globals$logNodeId)
}
.onLoad <- function(libname, pkgname) {
# R's lazy-loading package scheme causes the private seed to be cached in the
# package itself, making our PRNG completely deterministic. This line resets

136
R/graph.R
View File

@@ -1,3 +1,15 @@
# TODO - remove dot syntax
# used to help define truely global node id's.
# should work accross session and in global namespace
.globals$reactIdCounter <- 0L
nextGlobalReactId <- function() {
.globals$reactIdCounter <- .globals$reactIdCounter + 1L
as.character(.globals$reactIdCounter)
}
writeReactLog <- function(file=stdout(), sessionToken = NULL) {
log <- .rlogStack$as_list()
if (!is.null(sessionToken)) {
@@ -94,77 +106,77 @@ displayRLog <- function() {
message(msg)
}
}
.rlogDependsOnReactiveValueKey <- function(nodeId, depOnNodeId, key) {
.pm("dependsOnReactiveValueKey: ", pn(nodeId), " ", pn(depOnNodeId), " ", key)
.rlogDependsOnReactiveValueKey <- function(reactId, depOnReactId, key) {
.pm("dependsOnReactiveValueKey: ", pn(reactId), " ", pn(depOnReactId), " ", key)
.rlogAppend(list(
action = "depReactiveValueKey",
nodeId = nodeId,
depOnNodeId = depOnNodeId,
reactId = reactId,
depOnReactId = depOnReactId,
key = key
))
}
.rlogDependsOnReactiveValueNames <- function(nodeId, depOnNodeId) {
.pm("dependsOnReactiveValueNames: ", pn(nodeId), " ", pn(depOnNodeId))
.rlogDependsOnReactiveValueNames <- function(reactId, depOnReactId) {
.pm("dependsOnReactiveValueNames: ", pn(reactId), " ", pn(depOnReactId))
.rlogAppend(list(
action = "depReactiveValueNames",
nodeId = nodeId,
depOnNodeId = depOnNodeId
reactId = reactId,
depOnReactId = depOnReactId
))
}
.rlogDependsOnReactiveValueToList <- function(nodeId, depOnNodeId) {
.pm("dependsOnReactiveValuetoList: ", pn(nodeId), " ", pn(depOnNodeId))
.rlogDependsOnReactiveValueToList <- function(reactId, depOnReactId) {
.pm("dependsOnReactiveValueToList: ", pn(reactId), " ", pn(depOnReactId))
.rlogAppend(list(
action = 'dependsOnReactiveValuetoList',
nodeId = nodeId,
depOnNodeId = depOnNodeId
action = "dependsOnReactiveValuetoList",
reactId = reactId,
depOnReactId = depOnReactId
))
}
.rlogDependsOn <- function(nodeId, depOnNodeId) {
.pm("dependsOn: ", pn(nodeId), " on ", pn(depOnNodeId))
.rlogDependsOn <- function(reactId, depOnReactId) {
.pm("dependsOn: ", pn(reactId), " on ", pn(depOnReactId))
.rlogAppend(list(
action = "dep",
nodeId = nodeId,
depOnNodeId = depOnNodeId
reactId = reactId,
depOnReactId = depOnReactId
))
}
.rlogDependsOnRemove <- function(nodeId, depOnNodeId) {
.pm("dependsOnRemove: ", pn(nodeId), " on ", pn(depOnNodeId))
.rlogDependsOnRemove <- function(reactId, depOnReactId) {
.pm("dependsOnRemove: ", pn(reactId), " on ", pn(depOnReactId))
.rlogAppend(list(
action = "depOnRemove",
nodeId = nodeId,
depOnNodeId = depOnNodeId
reactId = reactId,
depOnReactId = depOnReactId
))
}
nodeCache <- list()
pn <- function(nodeId) {
nodeInfo <- nodeCache[[nodeId]]
pn <- function(reactId) {
nodeInfo <- nodeCache[[reactId]]
paste(
nodeInfo$nodeId, nodeInfo$type, nodeInfo$label,
nodeInfo$reactId, nodeInfo$type, nodeInfo$label,
sep = ":"
)
}
# init a node id with a label
.rlogAddNodeDef <- function(nodeId, label, type) {
if (!is.null(nodeCache[[nodeId]])) {
stop("node definition for id: ", nodeId, " already found!!", "Label: ", label, "Type: ", type)
.rlogAddNodeDef <- function(reactId, label, type) {
if (!is.null(nodeCache[[reactId]])) {
stop("node definition for id: ", reactId, " already found!!", "Label: ", label, "Type: ", type)
}
nodeCache[[nodeId]] <<- list(nodeId = nodeId, label = label, type = type)
.pm("nodeDef: ", pn(nodeId))
nodeCache[[reactId]] <<- list(reactId = reactId, label = label, type = type)
.pm("nodeDef: ", pn(reactId))
.rlogAppend(list(
action = "nodeDef",
nodeId = nodeId,
reactId = reactId,
label = label,
type = type
))
}
.rlogUpdateNodeLabel <- function(nodeId, label) {
nodeCache[[nodeId]]$label <<- label
.pm("updateNodeLabel: ", pn(nodeId))
.rlogUpdateNodeLabel <- function(reactId, label) {
nodeCache[[reactId]]$label <<- label
.pm("updateNodeLabel: ", pn(reactId))
.rlogAppend(list(
action = "updateNodeLabel",
nodeId = nodeId,
reactId = reactId,
label = label
))
}
@@ -178,23 +190,23 @@ pn <- function(nodeId) {
# ), domain = domain)
# }
.rlogEnter <- function(nodeId, ctxId, type) {
.pm("enter: ", pn(nodeId), " ", ctxId, " ", type)
.rlogEnter <- function(reactId, ctxId, type) {
.pm("enter: ", pn(reactId), " ", ctxId, " ", type)
.pmDepthIncrement()
.rlogAppend(list(
action = 'enter',
nodeId = nodeId,
reactId = reactId,
ctxId = ctxId,
type = type
))
}
.rlogExit <- function(nodeId, ctxId, type) {
.rlogExit <- function(reactId, ctxId, type) {
.pmDepthDecrement()
.pm("exit: ", pn(nodeId), " ", ctxId, " ", type)
.pm("exit: ", pn(reactId), " ", ctxId, " ", type)
.rlogAppend(list(
action = 'exit',
nodeId = nodeId,
reactId = reactId,
ctxId = ctxId,
type = type
))
@@ -202,79 +214,79 @@ pn <- function(nodeId) {
# id = ctx id
# domain is like session
.rlogValueChange <- function(nodeId, value) {
.pm("valueChange: ", pn(nodeId), " '", paste(utils::capture.output(utils::str(value)), collapse='\n'), "'")
.rlogValueChange <- function(reactId, value) {
.pm("valueChange: ", pn(reactId), " '", paste(utils::capture.output(utils::str(value)), collapse='\n'), "'")
.pmDepthIncrement()
.rlogAppend(
list(
action = 'valueChange',
nodeId = nodeId,
reactId = reactId,
value = paste(utils::capture.output(utils::str(value)), collapse='\n')
)
)
}
.rlogValueChangeEnd <- function(nodeId, value) {
.rlogValueChangeEnd <- function(reactId, value) {
.pmDepthDecrement()
.pm("valueChangeEnd: ", pn(nodeId), " '", paste(utils::capture.output(utils::str(value)), collapse='\n'), "'")
.pm("valueChangeEnd: ", pn(reactId), " '", paste(utils::capture.output(utils::str(value)), collapse='\n'), "'")
.rlogAppend(
list(
action = 'valueChangeEnd',
nodeId = nodeId,
reactId = reactId,
value = paste(utils::capture.output(utils::str(value)), collapse='\n')
)
)
}
.rlogReactValueNames <- function(nodeId, values) {
.rlogReactValueNames <- function(reactId, values) {
namesStr <- paste(utils::capture.output(utils::str(ls(values, all.names=TRUE))), collapse='\n')
.pm("valueChangeReactValueNames: ", pn(nodeId), " ", namesStr)
.pm("valueChangeReactValueNames: ", pn(reactId), " ", namesStr)
.rlogAppend(list(
action = 'valueChangeReactValueNames',
nodeId = nodeId,
reactId = reactId,
value = namesStr
))
}
.rlogReactValueValues <- function(nodeId, values) {
.rlogReactValueValues <- function(reactId, values) {
valuesStr <- paste(utils::capture.output(utils::str(as.list(values))), collapse='\n')
# pm("valueChangeReactValue: ", nodeId, " ", valuesStr)
.pm("valueChangeReactValueValues: ", pn(nodeId))
# pm("valueChangeReactValue: ", reactId, " ", valuesStr)
.pm("valueChangeReactValueValues: ", pn(reactId))
.rlogAppend(list(
action = 'valueChangeReactValueValues',
nodeId = nodeId,
reactId = reactId,
value = valuesStr
))
}
.rlogReactValueKey <- function(nodeId, key, value) {
.rlogReactValueKey <- function(reactId, key, value) {
valueStr <- paste(utils::capture.output(utils::str(value)), collapse='\n')
.pm("valueChangeReactValueKey: ", pn(nodeId), " ", key, " ", valueStr)
.pm("valueChangeReactValueKey: ", pn(reactId), " ", key, " ", valueStr)
.rlogAppend(list(
action = 'valueChangeReactValueKey',
nodeId = nodeId, key = key,
reactId = reactId, key = key,
value = valueStr
))
}
# id = ctx id
# domain is like session
.rlogInvalidateStart <- function(nodeId, ctxId, type, domain) {
.pm("invalidateStart: ", pn(nodeId), " ", ctxId, " ", type)
.rlogInvalidateStart <- function(reactId, ctxId, type, domain) {
.pm("invalidateStart: ", pn(reactId), " ", ctxId, " ", type)
.pmDepthIncrement()
.rlogAppend(
list(
action = 'invalidateStart',
nodeId = nodeId,
reactId = reactId,
ctxId = ctxId,
type = type
),
domain
)
}
.rlogInvalidateEnd <- function(nodeId, ctxId, type, domain) {
.rlogInvalidateEnd <- function(reactId, ctxId, type, domain) {
.pmDepthDecrement()
.pm("invalidateEnd: ", pn(nodeId), " ", ctxId, " ", type)
.pm("invalidateEnd: ", pn(reactId), " ", ctxId, " ", type)
.rlogAppend(
list(
action = 'invalidateEnd',
nodeId = nodeId,
reactId = reactId,
ctxId = ctxId,
type = type
),

View File

@@ -22,8 +22,8 @@ Context <- R6Class(
class = FALSE,
public = list(
id = character(0),
.rlogNodeId = integer(0),
.rlogType = "other",
.reactId = character(0),
.reactType = "other",
.label = character(0), # For debug purposes
.invalidated = FALSE,
.invalidateCallbacks = list(),
@@ -31,13 +31,13 @@ Context <- R6Class(
.domain = NULL,
.pid = NULL,
initialize = function(domain, label='', type='other', prevId='', rlogNodeId = NULL) {
initialize = function(domain, label='', type='other', prevId='', reactId = NULL) {
id <<- .getReactiveEnvironment()$nextId()
.label <<- label
.domain <<- domain
.pid <<- processId()
.rlogNodeId <<- rlogNodeId
.rlogType <<- type
.reactId <<- reactId
.reactType <<- type
# .graphCreateContext(id, label, type, prevId, domain)
},
run = function(func) {
@@ -46,8 +46,8 @@ Context <- R6Class(
promises::with_promise_domain(reactivePromiseDomain(), {
withReactiveDomain(.domain, {
env <- .getReactiveEnvironment()
.rlogEnter(.rlogNodeId, id, .rlogType)
on.exit(.rlogExit(.rlogNodeId, id, .rlogType), add = TRUE)
.rlogEnter(.reactId, id, .reactType)
on.exit(.rlogExit(.reactId, id, .reactType), add = TRUE)
env$runWith(self, func)
})
})
@@ -64,11 +64,14 @@ Context <- R6Class(
return()
.invalidated <<- TRUE
.rlogInvalidateStart(.rlogNodeId, id, .rlogType, .domain)
.rlogInvalidateStart(.reactId, id, .reactType, .domain)
on.exit(add = TRUE, {
.rlogInvalidateEnd(.reactId, id, .reactType, .domain)
})
lapply(.invalidateCallbacks, function(func) {
func()
})
.rlogInvalidateEnd(.rlogNodeId, id, .rlogType, .domain)
.invalidateCallbacks <<- list()
NULL
},

View File

@@ -1,6 +1,8 @@
#' @include utils.R
NULL
# TODO pass the domain around
Dependents <- R6Class(
'Dependents',
portable = FALSE,
@@ -52,7 +54,7 @@ ReactiveVal <- R6Class(
'ReactiveVal',
portable = FALSE,
private = list(
id = character(0),
reactId = character(0),
value = NULL,
label = NULL,
frozen = FALSE,
@@ -60,12 +62,12 @@ ReactiveVal <- R6Class(
),
public = list(
initialize = function(value, label = NULL) {
id <- .globalsIncrementLogNodeId()
private$id <- id
reactId <- nextGlobalReactId()
private$reactId <- reactId
private$value <- value
private$label <- label
private$dependents <- Dependents$new(rlogNodeId = private$id)
.rlogAddNodeDef(private$id, private$label, type = "reactiveVal")
private$dependents <- Dependents$new(reactId = private$reactId)
.rlogAddNodeDef(private$reactId, private$label, type = "reactiveVal")
},
get = function() {
private$dependents$register(depLabel = private$label)
@@ -80,8 +82,11 @@ ReactiveVal <- R6Class(
return(invisible(FALSE))
}
private$value <- value
.rlogValueChange(private$id, value)
.rlogValueChange(private$reactId, value)
# .rlogInvalidateStart - TODO
# TODO add it for every $invalidate()
private$dependents$invalidate()
# .rlogInvalidateEnd - TODO
invisible(TRUE)
},
freeze = function(session = getDefaultReactiveDomain()) {
@@ -279,7 +284,7 @@ ReactiveValues <- R6Class(
portable = FALSE,
public = list(
# For debug purposes
.id = character(0),
.reactId = character(0),
.label = character(0),
.values = 'environment',
.metadata = 'environment',
@@ -292,17 +297,17 @@ ReactiveValues <- R6Class(
.valuesDeps = 'Dependents',
initialize = function() {
.id <<- .globalsIncrementLogNodeId()
.reactId <<- nextGlobalReactId()
.label <<- paste('reactiveValues',
p_randomInt(1000, 10000),
sep="")
.values <<- new.env(parent=emptyenv())
.metadata <<- new.env(parent=emptyenv())
.dependents <<- new.env(parent=emptyenv())
.namesDeps <<- Dependents$new(rlogNodeId = .id)
.allValuesDeps <<- Dependents$new(rlogNodeId = .id)
.valuesDeps <<- Dependents$new(rlogNodeId = .id)
.rlogAddNodeDef(.id, .label, type = "reactiveValues")
.namesDeps <<- Dependents$new(rlogNodeId = .reactId)
.allValuesDeps <<- Dependents$new(rlogNodeId = .reactId)
.valuesDeps <<- Dependents$new(rlogNodeId = .reactId)
.rlogAddNodeDef(.reactId, .label, type = "reactiveValues")
},
get = function(key) {
@@ -311,8 +316,9 @@ ReactiveValues <- R6Class(
ctx <- .getReactiveEnvironment()$currentContext()
dep.key <- paste(key, ':', ctx$id, sep='')
if (!exists(dep.key, envir=.dependents, inherits=FALSE)) {
.rlogDependsOnReactiveValueKey(ctx$.rlogNodeId, .id, key)
.rlogDependsOnReactiveValueKey(ctx$.reactId, .id, key)
.dependents[[dep.key]] <- ctx
# TODO add dependences remove
ctx$onInvalidate(function() {
rm(list=dep.key, envir=.dependents, inherits=FALSE)
})
@@ -336,7 +342,7 @@ ReactiveValues <- R6Class(
}
}
else {
.rlogReactValueNames(.id, .values)
.rlogReactValueNames(.reactId, .values)
.namesDeps$invalidate()
}
@@ -347,8 +353,8 @@ ReactiveValues <- R6Class(
.values[[key]] <- value
.rlogReactValueValues(.id, .values)
.rlogReactValueKey(.id, key, value)
.rlogReactValueValues(.reactId, .values)
.rlogReactValueKey(.reactId, key, value)
dep.keys <- objects(
envir=.dependents,
@@ -374,8 +380,8 @@ ReactiveValues <- R6Class(
names = function() {
.rlogDependsOnReactiveValueNames(
.getReactiveEnvironment()$currentContext()$.rlogNodeId,
.id
.getReactiveEnvironment()$currentContext()$.reactId,
.reactId
)
.namesDeps$register()
return(ls(.values, all.names=TRUE))
@@ -416,8 +422,8 @@ ReactiveValues <- R6Class(
toList = function(all.names=FALSE) {
.rlogDependsOnReactiveValueToList(
.getReactiveEnvironment()$currentContext()$.rlogNodeId,
.id
.getReactiveEnvironment()$currentContext()$.reactId,
.reactId
)
if (all.names)
.allValuesDeps$register()
@@ -428,7 +434,7 @@ ReactiveValues <- R6Class(
},
.setLabel = function(label) {
.rlogUpdateNodeLabel(.id, label)
.rlogUpdateNodeLabel(.reactId, label)
.label <<- label
}
)
@@ -706,7 +712,7 @@ Observable <- R6Class(
'Observable',
portable = FALSE,
public = list(
.id = character(0),
.reactId = character(0),
.origFunc = 'function',
.func = 'function',
.label = character(0),
@@ -737,22 +743,24 @@ Observable <- R6Class(
funcLabel <- paste0("<reactive:", label, ">")
}
.id <<- .globalsIncrementLogNodeId()
.reactId <<- nextGlobalReactId()
.origFunc <<- func
.func <<- wrapFunctionLabel(func, funcLabel,
..stacktraceon = ..stacktraceon)
.label <<- label
.domain <<- domain
.dependents <<- Dependents$new(rlogNodeId = .id)
.dependents <<- Dependents$new(reactId = .reactId)
.invalidated <<- TRUE
.running <<- FALSE
.execCount <<- 0L
.mostRecentCtxId <<- ""
.rlogAddNodeDef(.id, .label, type = "observable")
.rlogAddNodeDef(.reactId, .label, type = "observable")
},
getValue = function() {
.dependents$register()
# TODO add dependsOn
if (.invalidated || .running) {
..stacktraceoff..(
self$.updateValue()
@@ -774,7 +782,7 @@ Observable <- R6Class(
},
.updateValue = function() {
ctx <- Context$new(.domain, .label, type = 'observable',
prevId = .mostRecentCtxId, rlogNodeId = .id)
prevId = .mostRecentCtxId, reactId = .reactId)
.mostRecentCtxId <<- ctx$id
ctx$onInvalidate(function() {
.invalidated <<- TRUE
@@ -965,7 +973,7 @@ Observer <- R6Class(
'Observer',
portable = FALSE,
public = list(
.id = character(0),
.reactId = character(0),
.func = 'function',
.label = character(0),
.domain = 'ANY',
@@ -1016,14 +1024,14 @@ registerDebugHook("observerFunc", environment(), label)
.autoDestroyHandle <<- NULL
setAutoDestroy(autoDestroy)
.id <<- .globalsIncrementLogNodeId()
.rlogAddNodeDef(.id, .label, type = "observer")
.reactId <<- nextGlobalReactId()
.rlogAddNodeDef(.reactId, .label, type = "observer")
# Defer the first running of this until flushReact is called
.createContext()$invalidate()
},
.createContext = function() {
ctx <- Context$new(.domain, .label, type='observer', prevId=.prevId, rlogNodeId = .id)
ctx <- Context$new(.domain, .label, type='observer', prevId=.prevId, reactId = .reactId)
.prevId <<- ctx$id
if (!is.null(.ctx)) {