From fdb52e0243b45212327111b2602663021dcafe68 Mon Sep 17 00:00:00 2001 From: Joe Cheng Date: Wed, 10 Aug 2016 14:50:46 -0700 Subject: [PATCH] executeElapsed gets stuck returning TRUE even when nothing was executed Fixes #1278 --- NEWS | 3 +++ R/timer.R | 7 ++++++- tests/testthat/test-timer.R | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-timer.R diff --git a/NEWS b/NEWS index a5a110b00..259b36207 100644 --- a/NEWS +++ b/NEWS @@ -87,6 +87,9 @@ shiny 0.13.2.9005 * Fixed #1253: Memory could leak when an observer was destroyed without first being invalidated. +* Fixed #1284: Reactive system was being flushed too often (usually this just + means a more-expensive no-op than necessary). + shiny 0.13.2 -------------------------------------------------------------------------------- diff --git a/R/timer.R b/R/timer.R index aee7a1801..51f237bd3 100644 --- a/R/timer.R +++ b/R/timer.R @@ -22,6 +22,11 @@ TimerCallbacks <- R6Class( .times <<- data.frame() }, schedule = function(millis, func) { + # If args could fail to evaluate, let's make them do that before + # we change any state + force(millis) + force(func) + id <- .nextId .nextId <<- .nextId + 1L @@ -56,7 +61,7 @@ TimerCallbacks <- R6Class( }, executeElapsed = function() { elapsed <- takeElapsed() - if (length(elapsed) == 0) + if (nrow(elapsed) == 0) return(FALSE) for (id in elapsed$id) { diff --git a/tests/testthat/test-timer.R b/tests/testthat/test-timer.R new file mode 100644 index 000000000..f4aef724b --- /dev/null +++ b/tests/testthat/test-timer.R @@ -0,0 +1,25 @@ +context("timer") + +test_that("Scheduling works", { + ran <- FALSE + fun <- function() { + ran <<- TRUE + } + + timerCallbacks$schedule(500, fun) + + timerCallbacks$executeElapsed() + expect_false(ran) + + Sys.sleep(0.1) + timerCallbacks$executeElapsed() + expect_false(ran) + + Sys.sleep(0.5) + expect_true(timerCallbacks$executeElapsed()) + expect_true(ran) + + # Empty timerCallbacks should return FALSE + expect_false(timerCallbacks$executeElapsed()) + expect_equal(0, nrow(timerCallbacks$takeElapsed())) +})