I have problems understanding the onFlush function in shiny on when registered function get exactly called.
Looking at its reference: https://www.rdocumentation.org/packages/shiny/versions/1.6.0/topics/onFlush it suggest that registered functions will be triggered before the observers and render functions get recalculated.
Since it states the following:
onFlush registers a function that will be called before Shiny flushes the reactive system.
What I understand as a flush is the re-computation of all the observers and render functions. This idea is supported by one of their official articles, stating:
Once all the descendants are invalidated, a flush occurs. When this happens, all invalidated observers re-execute.
Thus when I run the following minimal example:
library(shiny)
ui <- fluidPage( actionButton("invalidate", "invalidate") )
server <- function(input,output,session){
session$onFlush( function() message("Start flush"), once = FALSE)
session$onFlushed( function() message("End flush"), once = FALSE )
observe({
Sys.sleep(3) # Long computation
input$invalidate
})
}
shinyApp(ui, server)
I expect to see first the message: Start flush then a 3 second pause and then the message End flush.
In reality however, the start flush is only executed after the observe was executed, while I would have thought that this would get executed before the observe.
At first, I thought this was a bug, however I then found this official shiny example: https://github.com/rstudio/shiny-examples/blob/master/127-async-flush/app.R and learned that this order of execution is actually the intended behavior.
So now I am actually not sure how I need to interpret the onFlush function or what the flush is in the first place.