Debian Codegraph @

R/timer.R

raw
1 # Return the current time, in milliseconds from epoch.
2 getTimeMs <- function() {
3 as.numeric(Sys.time()) * 1000
4 }
5
6 TimerCallbacks <- R6Class(
7 'TimerCallbacks',
8 portable = FALSE,
9 class = FALSE,
10 public = list(
11 .nextId = 0L,
12 .funcs = 'Map',
13 .times = data.frame(),
14 .now = 'Function',
15
16 initialize = function(nowFn = getTimeMs) {
17 .funcs <<- Map$new()
18 .now <<- nowFn
19 },
20 clear = function() {
21 .nextId <<- 0L
22 .funcs$clear()
23 .times <<- data.frame()
24 },
25 schedule = function(millis, func) {
26 # If args could fail to evaluate, let's make them do that before
27 # we change any state
28 force(millis)
29 force(func)
30
31 id <- .nextId
32 .nextId <<- .nextId + 1L
33
34 t <- .now()
35
36 # TODO: Horribly inefficient, use a heap instead
37 .times <<- rbind(.times, data.frame(time=t+millis,
38 scheduled=t,
39 id=id))
40 .times <<- .times[order(.times$time),]
41
42 .funcs$set(as.character(id), func)
43
44 return(id)
45 },
46 unschedule = function(id) {
47 toRemoveIndices <- .times$id %in% id
48 toRemoveIds <- .times[toRemoveIndices, "id", drop = TRUE]
49 if (length(toRemoveIds) > 0) {
50 .times <<- .times[!toRemoveIndices,]
51 for (toRemoveId in as.character(toRemoveIds)) {
52 .funcs$remove(toRemoveId)
53 }
54 }
55 return(id %in% toRemoveIds)
56 },
57 timeToNextEvent = function() {
58 if (dim(.times)[1] == 0)
59 return(Inf)
60 return(.times[1, 'time'] - .now())
61 },
62 takeElapsed = function() {
63 t <- .now()
64 elapsed <- .times$time <= .now()
65 result <- .times[elapsed,]
66 .times <<- .times[!elapsed,]
67
68 # TODO: Examine scheduled column to check if any funny business
69 # has occurred with the system clock (e.g. if scheduled
70 # is later than .now())
71
72 return(result)
73 },
74 executeElapsed = function() {
75 elapsed <- takeElapsed()
76 if (nrow(elapsed) == 0)
77 return(FALSE)
78
79 for (id in elapsed$id) {
80 thisFunc <- .funcs$remove(as.character(id))
81 # TODO: Catch exception, and...?
82 # TODO: Detect NULL, and...?
83 thisFunc()
84 }
85 return(TRUE)
86 }
87 )
88 )
89
90 MockableTimerCallbacks <- R6Class(
91 'MockableTimerCallbacks',
92 inherit = TimerCallbacks,
93 portable = FALSE,
94 class = FALSE,
95 public = list(
96 # Empty constructor defaults to the getNow implementation
97 initialize = function() {
98 super$initialize(self$mockNow)
99 },
100 mockNow = function() {
101 return(private$time)
102 },
103 elapse = function(millis) {
104 private$time <- private$time + millis
105 },
106 getElapsed = function() {
107 private$time
108 }
109 ), private = list(
110 time = 0L
111 )
112 )
113
114 timerCallbacks <- TimerCallbacks$new()
115
116 scheduleTask <- function(millis, callback) {
117 cancelled <- FALSE
118 id <- timerCallbacks$schedule(millis, callback)
119
120 function() {
121 invisible(timerCallbacks$unschedule(id))
122 }
123 }
124
125 #' Get a scheduler function for scheduling tasks. Give priority to the
126 #' session scheduler, but if it doesn't exist, use the global one.
127 #' @noRd
128 defineScheduler <- function(session){
129 if (!is.null(session) && !is.null(session$.scheduleTask)){
130 return(session$.scheduleTask)
131 }
132 scheduleTask
133 }
134
135
136 #' Get the current time using the current reactive domain. This will try to use
137 #' the session's .now() method, but if that's not available, it will just return
138 #' the real time (from getTimeMs()). The purpose of this function is to allow
139 #' MockableTimerCallbacks to work.
140 #' @noRd
141 getDomainTimeMs <- function(session){
142 if (!is.null(session) && !is.null(session$.now)){
143 return(session$.now())
144 } else {
145 getTimeMs()
146 }
147 }