From 2f3c370f706fb91bd0e08b6076d6a2a2c2235c8c Mon Sep 17 00:00:00 2001 From: Pingfan Hu Date: Thu, 28 Nov 2024 13:42:23 -0500 Subject: [PATCH] another update --- R/server.R | 32 ++++++-------------------------- inst/js/window_close.js | 9 +++++++++ 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/R/server.R b/R/server.R index 0dd1ac1..a05b4bd 100644 --- a/R/server.R +++ b/R/server.R @@ -579,36 +579,16 @@ sd_server <- function( # Update checkpoint 4 - when window is closed shiny::observeEvent(input$window_closing, { shiny::isolate({ - # Force a synchronous database write - tryCatch({ - if (!is.null(db)) { - # Get the current data - data_list <- latest_data() - data_list[['time_end']] <- get_utc_timestamp() - - # Direct database write using poolWithTransaction - pool::poolWithTransaction(db$db, function(conn) { - # Update the data - database_uploading(data_list, conn, db$table, names(data_list)) - # Explicitly commit - DBI::dbCommit(conn) - }) - - # Force pool closing - pool::poolClose(db$db) - } - }, error = function(e) { - warning("Error during forced data save: ", e$message) - }) + 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) - }) - }) + # shiny::onSessionEnded(function() { + # shiny::isolate({ + # update_data(time_last = TRUE) + # }) + # }) } #' Define skip conditions for survey pages diff --git a/inst/js/window_close.js b/inst/js/window_close.js index 55db7ce..a6660f4 100644 --- a/inst/js/window_close.js +++ b/inst/js/window_close.js @@ -1,5 +1,14 @@ $(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) {} // Block for 200ms + + // Standard beforeunload dialog + e.preventDefault(); + e.returnValue = ''; }); });