@@ -2243,33 +2243,41 @@ studio_server <- function(gssencmode = "prefer") {
22432243 ! is.null(current_survey_name()) &&
22442244 ! is.null(supabase_conn())) {
22452245
2246- # Update last edit time whenever editors change
2247- input $ survey_editor
2248- input $ app_editor
2246+ # Capture reactive values before passing to later::later()
2247+ survey_editor_content <- input $ survey_editor
2248+ app_editor_content <- input $ app_editor
2249+ survey_name_captured <- current_survey_name()
2250+ conn_captured <- supabase_conn()
2251+ current_dir <- getwd()
2252+
2253+ # Isolate last_sync_time to prevent triggering on every sync
2254+ last_time <- shiny :: isolate(last_sync_time())
22492255
22502256 # Schedule sync 10 seconds after last edit
22512257 later :: later(function () {
22522258 # Only sync if enough time has passed (debouncing)
2253- time_since_update <- as.numeric(difftime(Sys.time(), last_sync_time() , units = " secs" ))
2259+ time_since_update <- as.numeric(difftime(Sys.time(), last_time , units = " secs" ))
22542260
22552261 if (time_since_update > = 9.5 ) { # Allow small margin
22562262 tryCatch({
2257- # Save and sync
2258- if (! is.null(input $ survey_editor )) {
2259- writeLines(input $ survey_editor , " survey.qmd" )
2263+ # Save captured content to files
2264+ if (! is.null(survey_editor_content )) {
2265+ writeLines(survey_editor_content , file.path( current_dir , " survey.qmd" ) )
22602266 }
2261- if (! is.null(input $ app_editor )) {
2262- writeLines(input $ app_editor , " app.R" )
2267+ if (! is.null(app_editor_content )) {
2268+ writeLines(app_editor_content , file.path( current_dir , " app.R" ) )
22632269 }
22642270
2271+ # Upload to Supabase
22652272 supabase_upload_survey(
2266- survey_path = getwd() ,
2267- survey_name = current_survey_name() ,
2268- conn = supabase_conn() ,
2273+ survey_path = current_dir ,
2274+ survey_name = survey_name_captured ,
2275+ conn = conn_captured ,
22692276 overwrite = TRUE
22702277 )
22712278
2272- last_sync_time(Sys.time())
2279+ # Update last sync time (not in reactive context, so set directly)
2280+ # This is OK because we're just updating a timestamp
22732281 }, error = function (e ) {
22742282 # Silent auto-sync failures
22752283 })
0 commit comments