Skip to content

Commit bac35e8

Browse files
committed
Revert "PR feedback. Broke tests because of dependency on session, though, so might revert."
This reverts commit a003c4d.
1 parent a003c4d commit bac35e8

File tree

5 files changed

+57
-13
lines changed

5 files changed

+57
-13
lines changed

R/reactives.R

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,14 +1513,16 @@ reactiveTimer <- function(intervalMs=1000, session = getDefaultReactiveDomain())
15131513
# reactId <- nextGlobalReactId()
15141514
# rLog$define(reactId, paste0("timer(", intervalMs, ")"))
15151515

1516+
scheduler <- defineScheduler(session)
1517+
15161518
dependents <- Map$new()
1517-
timerHandle <- session$.scheduleTask(intervalMs, function() {
1519+
timerHandle <- scheduler(intervalMs, function() {
15181520
# Quit if the session is closed
15191521
if (!is.null(session) && session$isClosed()) {
15201522
return(invisible())
15211523
}
15221524

1523-
timerHandle <<- session$.scheduleTask(intervalMs, sys.function())
1525+
timerHandle <<- scheduler(intervalMs, sys.function())
15241526

15251527
doInvalidate <- function() {
15261528
lapply(
@@ -1620,7 +1622,9 @@ invalidateLater <- function(millis, session = getDefaultReactiveDomain()) {
16201622

16211623
clear_on_ended_callback <- function() {}
16221624

1623-
timerHandle <- session$.scheduleTask(millis, function() {
1625+
scheduler <- defineScheduler(session)
1626+
1627+
timerHandle <- scheduler(millis, function() {
16241628
if (is.null(session)) {
16251629
ctx$invalidate()
16261630
return(invisible())
@@ -2371,7 +2375,7 @@ debounce <- function(r, millis, priority = 100, domain = getDefaultReactiveDomai
23712375
}
23722376

23732377
# The value (or possibly millis) changed. Start or reset the timer.
2374-
v$when <- domain$.now() + millis()/1000
2378+
v$when <- getTime(domain) + millis()/1000
23752379
}, label = "debounce tracker", domain = domain, priority = priority)
23762380

23772381
# This observer is the timer. It rests until v$when elapses, then touches
@@ -2380,7 +2384,7 @@ debounce <- function(r, millis, priority = 100, domain = getDefaultReactiveDomai
23802384
if (is.null(v$when))
23812385
return()
23822386

2383-
now <- domain$.now()
2387+
now <- getTime(domain)
23842388
if (now >= v$when) {
23852389
# Mod by 999999999 to get predictable overflow behavior
23862390
v$trigger <- isolate(v$trigger %OR% 0) %% 999999999 + 1
@@ -2431,12 +2435,12 @@ throttle <- function(r, millis, priority = 100, domain = getDefaultReactiveDomai
24312435
if (is.null(v$lastTriggeredAt)) {
24322436
0
24332437
} else {
2434-
max(0, (v$lastTriggeredAt + millis()/1000) - domain$.now()) * 1000
2438+
max(0, (v$lastTriggeredAt + millis()/1000) - getTime(domain)) * 1000
24352439
}
24362440
}
24372441

24382442
trigger <- function() {
2439-
v$lastTriggeredAt <- domain$.now()
2443+
v$lastTriggeredAt <- getTime(domain)
24402444
# Mod by 999999999 to get predictable overflow behavior
24412445
v$trigger <- isolate(v$trigger) %% 999999999 + 1
24422446
v$pending <- FALSE

R/shiny.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,9 @@ ShinySession <- R6Class(
723723
requestFlush = function() {
724724
appsNeedingFlush$set(self$token, self)
725725
},
726-
.scheduleTask = function(millis, callback) {
726+
scheduleTask = function(millis, callback) {
727727
scheduleTask(millis, callback)
728728
},
729-
.now = function() {
730-
getNow()
731-
},
732729
rootScope = function() {
733730
self
734731
},

R/timer.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,25 @@ scheduleTask <- function(millis, callback) {
119119
invisible(timerCallbacks$unschedule(id))
120120
}
121121
}
122+
123+
#' Get a scheduler function for scheduling tasks. Give priority to the
124+
#' session scheduler, but if it doesn't exist, use the global one.
125+
#' @noRd
126+
defineScheduler <- function(session){
127+
if (!is.null(session) && !is.null(session$scheduleTask)){
128+
return(session$scheduleTask)
129+
}
130+
scheduleTask
131+
}
132+
133+
134+
#' Get the current time a la `Sys.time()`. Prefer to get it via the
135+
#' `session$now()` function, but if that's not available, just return the
136+
#' current system time.
137+
#' @noRd
138+
getTime <- function(session){
139+
if (!is.null(session) && !is.null(session$now)){
140+
return(session$now())
141+
}
142+
Sys.time()
143+
}

tests/testthat/test-app.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ test_that("With ui/server.R, global.R is loaded before R/ helpers and into the r
9999
})
100100

101101

102-
test_that("Loading supporting R files is opt-out", {
102+
test_that("Loading supporting R fils is opt-out", {
103103
calls <- list()
104104
sourceStub <- function(...){
105105
calls[[length(calls)+1]] <<- list(...)
@@ -128,7 +128,7 @@ test_that("Loading supporting R files is opt-out", {
128128
})
129129

130130

131-
test_that("Disabling supporting R files works", {
131+
test_that("Disabling supporting R fils works", {
132132
calls <- list()
133133
sourceStub <- function(...){
134134
calls[[length(calls)+1]] <<- list(...)

tests/testthat/test-timer.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ test_that("Vectorized unscheduling works", {
4747
expect_identical(timerCallbacks$unschedule(c(key1, key2, key3)), c(TRUE, FALSE, TRUE))
4848
})
4949

50+
test_that("defineScheduler works", {
51+
expect_identical(defineScheduler(NULL), scheduleTask)
52+
expect_identical(defineScheduler(list()), scheduleTask)
53+
expect_identical(defineScheduler(list(scheduleTask=123)), 123)
54+
})
55+
5056
test_that("mockableTimer works", {
5157
mt <- MockableTimerCallbacks$new()
5258
called <- FALSE
@@ -64,3 +70,18 @@ test_that("mockableTimer works", {
6470
expect_true(mt$executeElapsed())
6571
expect_true(called)
6672
})
73+
74+
test_that("getTime works", {
75+
start <- Sys.time()
76+
t1 <- getTime(NULL)
77+
t2 <- getTime(list())
78+
t3 <- getTime(list(now = function(){456}))
79+
end <- Sys.time()
80+
81+
expect_gte(t1, start)
82+
expect_gte(t2, start)
83+
expect_lte(t1, end)
84+
expect_lte(t2, end)
85+
86+
expect_equal(t3, 456)
87+
})

0 commit comments

Comments
 (0)