stop waiter in shiny when interrupting execution R

If I stop this code (clic on red button in R studio or close cmd window executing it), I want the waiter to stop turning with the illusion that the code is still running. best case senario, when I close the cmd I want the shiny browser to close.

library(shiny)
library(waiter)

# Define UI
ui <- fluidPage(
  use_waiter(),  # Include waiter dependencies
  waiter_show_on_load(html = spin_3()),  # Show spinner on load
  titlePanel("Shiny App with Waiter"),
  sidebarLayout(
    sidebarPanel(
      actionButton("load", "Load Data")
    ),
    mainPanel(
      tableOutput("data")
    )
  )
)

# Define server logic
server <- function(input, output, session) {
  # Show waiter on load
  waiter <- Waiter$new()
  waiter$show()
  
  # Simulate data loading
  observeEvent(input$load, {
    Sys.sleep(2)  # Simulate a delay
    data <- head(mtcars)
    output$data <- renderTable(data)
    waiter$hide()  # Hide waiter after loading data
  })
}

# Run the application
shinyApp(ui = ui, server = server)

Read more here: Source link