## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## -----------------------------------------------------------------------------
# output$mtcars_tb <- DT::renderDT({
#   lazy_mtcars_query() |>
#     dplyr::collect()
# })

## -----------------------------------------------------------------------------
# lazy_mtcars_query() |>
#   dplyr::collect() |>
#   promises::then(function(df) {
#     # `df` is the collected tibble
#     head(df)
#   })

## -----------------------------------------------------------------------------
# lazy_mtcars_query() |>
#   dplyr::collect() %...>%
#   head()

## -----------------------------------------------------------------------------
# lazy_mtcars_query() |>
#   dplyr::collect() %...>%
#   head() %...!%
#   (function(err) {
#     shiny::showNotification(conditionMessage(err), type = "error")
#   })

## -----------------------------------------------------------------------------
# # Holds the value computed in the browser.
# oldest_age <- shiny::reactiveVal(NULL)
# 
# # Pipeline: compute max(age) and store it. No UI update here.
# shiny::observeEvent(input$update, {
#   lazy_data() |>
#     dplyr::summarise("max_age = max(age)") |>
#     dplyr::collect() %...>% {
#     oldest_age(.$max_age)
#   }
# })
# 
# # Separate observer: update the input when the computed value changes.
# shiny::observeEvent(oldest_age(), {
#   shiny::updateSelectInput(
#     session,
#     inputId = "age",
#     selected = oldest_age()
#   )
# })

## -----------------------------------------------------------------------------
# # Gated on the button: returns the collect() promise. Computes only.
# oldest_age <- shiny::eventReactive(input$update, {
#   lazy_data() |>
#     dplyr::summarise("max_age = max(age)") |>
#     dplyr::collect()
# })
# 
# # Separate observer: resolve the promise and update the input.
# shiny::observe({
#   oldest_age() %...>% {
#     shiny::updateSelectInput(
#       session,
#       inputId = "age",
#       selected = .$max_age
#     )
#   }
# })

