Question
From the documentation it is clear, that variables in server.R
have a different life span than the ones in global.R
.
For cleanup operations in server
, there is the session$onSessionEnded
callback function. An example to use this can be found here.
Is there something similar for the
global.R
script?
Context
In my setup, I have two variables with reference semantics (instances of R6
classes) in the global scope that need cleanup. The cleanup operation is dependend on both objects. Some pseudocode.
# context global.R
# -----------------------------------------------------------------------------------------
A = classA$new()
B = classB$new()
# call after the last sesion has ended
cleanupFunction = function(){
# call this before B$finalize()
A$saveToSQL(B)
A$finalize()
}
A
and B
are both operaing on server.R
.
Current workaround
Since both classA
and classB
are R6
classes, I have access to the finalize
method (a destructor in C++
terms). So the following workaround is possible.
classA$finalize = function(){
if(cleanupFlag){
cleanupFunction()
cleanupFlag <<- FALSE
}
}
classB$finalize = function(){
if(cleanupFlag){
cleanupFunction()
cleanupFlag <<- FALSE
}
}
This requires me to use callbacks already because of scoping.
If someone knows a "proper" way to solve this, I would be very interested.