Skip to content

Commit

Permalink
Consolidate to two Timer classes and simplify conditionals
Browse files Browse the repository at this point in the history
  • Loading branch information
trestletech committed Oct 16, 2019
1 parent d3667df commit 0ae8e4f
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions R/timer.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ getNow <- function() {
as.numeric(Sys.time()) * 1000
}

BaseTimerCallbacks <- R6Class(
TimerCallbacks <- R6Class(
'TimerCallbacks',
portable = FALSE,
class = FALSE,
Expand Down Expand Up @@ -88,22 +88,9 @@ BaseTimerCallbacks <- R6Class(
)
)

TimerCallbacks <- R6Class(
'TimerCallbacks',
inherit=BaseTimerCallbacks,
portable = FALSE,
class = FALSE,
public = list(
# Empty constructor defaults to the getNow implementation
initialize = function() {
super$initialize(getNow)
}
)
)

MockableTimerCallbacks <- R6Class(
'MockableTimerCallbacks',
inherit=BaseTimerCallbacks,
inherit=TimerCallbacks,
portable = FALSE,
class = FALSE,
public = list(
Expand All @@ -115,7 +102,7 @@ MockableTimerCallbacks <- R6Class(
return(private$time)
},
elapse = function(millis){
private$time <<- private$time + millis
private$time <- private$time + millis
}
), private = list(
time = 0L
Expand All @@ -137,10 +124,8 @@ scheduleTask <- function(millis, callback) {
#' session scheduler, but if it doesn't exist, use the global one.
#' @noRd
defineScheduler <- function(session){
if (!is.null(session)){
if (!is.null(session$scheduleTask)){
return(session$scheduleTask)
}
if (!is.null(session) && !is.null(session$scheduleTask)){
return(session$scheduleTask)
}
scheduleTask
}
Expand All @@ -151,10 +136,8 @@ defineScheduler <- function(session){
#' current system time.
#' @noRd
getTime <- function(session){
if (!is.null(session)){
if (!is.null(session$now)){
return(session$now())
}
if (!is.null(session) && !is.null(session$now)){
return(session$now())
}
Sys.time()
}

0 comments on commit 0ae8e4f

Please sign in to comment.