Skip to content

Commit

Permalink
update_data better functioning
Browse files Browse the repository at this point in the history
  • Loading branch information
pingfan-hu committed Nov 28, 2024
1 parent 68a8757 commit dac24f7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,14 @@ sd_server <- function(
session$sendCustomMessage("closeWindow", list())
})

# Update checkpoint 4 - when session is ended
# Update checkpoint 4 - when window is closed
shiny::observeEvent(input$window_closing, {
shiny::isolate({
update_data(time_last = TRUE)
})
}, ignoreInit = TRUE)

# Update checkpoint 5 - when session is ended (backup)
shiny::onSessionEnded(function() {
shiny::isolate({
update_data(time_last = TRUE)
Expand Down
1 change: 1 addition & 0 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ sd_ui <- function() {
"countdown.js",
"enter_key.js",
"keep_alive.js",
"window_close.js",
"surveydown.css"
),
if (any(theme == "default")) {
Expand Down
10 changes: 10 additions & 0 deletions inst/js/window_close.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$(document).ready(function() {
window.addEventListener('beforeunload', function(e) {
// Send message to Shiny
Shiny.setInputValue('window_closing', true, {priority: 'event'});

// Small delay to allow data to save
var start = Date.now();
while(Date.now() - start < 200) {}
});
});

0 comments on commit dac24f7

Please sign in to comment.