Skip to content

Commit

Permalink
Merge pull request #148 from surveydown-dev/cookie-revert
Browse files Browse the repository at this point in the history
another update
  • Loading branch information
pingfan-hu authored Nov 28, 2024
2 parents 340b51b + 2f3c370 commit 0ebe681
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
32 changes: 6 additions & 26 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions inst/js/window_close.js
Original file line number Diff line number Diff line change
@@ -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 = '';
});
});

0 comments on commit 0ebe681

Please sign in to comment.