/
FutureStack.R
158 lines (153 loc) · 3.91 KB
/
FutureStack.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#' @include aaa.R
NULL
#' @importFrom R6 R6Class
#' @importFrom uuid UUIDgenerate
#' @importFrom future future resolved value
#' @importFrom assertthat assert_that
#'
FutureStack <- R6Class('FutureStack',
public = list(
# Methods
initialize = function(server) {
private$server <- server
private$futures <- new.env(parent = emptyenv())
},
add = function(expr, then, ..., substituted = FALSE) {
if (!substituted) {
expr <- substitute(expr)
}
id <- UUIDgenerate()
private$futures[[id]] <- private$make_future(expr, then, ...)
private$ids <- append(private$ids, id)
invisible(id)
},
remove = function(id) {
private$clear(id)
},
empty = function() {
length(private$ids) == 0
},
eval = function(...) {
if (!self$empty()) {
evalIds <- private$ids[sapply(private$ids, private$do_eval)]
for (i in evalIds) {
res <- tri(value(private$futures[[i]]$expr))
if (is.error_cond(res)) {
private$server$log('error', conditionMessage(res))
}
res <- tri(do.call(private$futures[[i]]$then, list(res = res, ...)))
if (is.error_cond(res)) {
private$server$log('error', conditionMessage(res))
}
}
if (length(evalIds) != 0) private$clear(evalIds)
}
}
),
private = list(
# Data
ids = character(),
futures = NULL,
catcher = 'future',
lazy = FALSE,
server = NULL,
# Methods
make_future = function(expr, then, ...) {
if (missing(then)) {
then <- private$null_fun
} else {
assert_that(is.function(then))
}
list(
expr = do.call(private$catcher,
list(expr = expr, lazy = private$lazy)),
then = then,
...
)
},
do_eval = function(id) {
resolved(private$futures[[id]]$expr, timeout = 0.05)
},
clear = function(ids, ...) {
if (length(ids) > 0) {
private$ids <- private$ids[!private$ids %in% ids]
rm(list = ids, envir = private$futures)
}
},
null_fun = function(...) {
NULL
}
)
)
#' @importFrom R6 R6Class
#' @importFrom future sequential
#'
DelayStack <- R6Class('DelayStack',
inherit = FutureStack,
private = list(
catcher = 'sequential',
lazy = TRUE
)
)
#' @importFrom R6 R6Class
#' @importFrom future multiprocess
#'
AsyncStack <- R6Class('AsyncStack',
inherit = FutureStack,
private = list(
catcher = 'multiprocess'
)
)
#' @importFrom R6 R6Class
#' @importFrom assertthat assert_that is.number is.flag
#'
TimeStack <- R6Class('TimeStack',
inherit = DelayStack,
public = list(
remove = function(id) {
private$clear(id, force = TRUE)
},
reset = function() {
for (id in private$ids) {
private$futures[[id]]$at <- Sys.time() + private$futures[[id]]$after
}
}
),
private = list(
make_future = function(expr, then, after, loop = FALSE) {
assert_that(
is.number(after),
is.flag(loop)
)
super$make_future(expr = expr, then = then, after = after,
loop = loop, at = Sys.time() + after)
},
do_eval = function(id) {
Sys.time() > private$futures[[id]]$at
},
clear = function(ids, force = FALSE) {
if (!force) {
remove <- sapply(ids, function(id) {
if (private$futures[[id]]$loop) {
private$restore(private$futures[[id]]$expr)
private$futures[[id]]$at <- private$futures[[id]]$at +
private$futures[[id]]$after
FALSE
} else {
TRUE
}
})
ids <- ids[remove]
}
super$clear(ids)
},
restore = function(future) {
if (future$state == 'finished') {
future$state <- 'created'
if (exists('value', envir = future)) {
rm('value', envir = future)
}
}
}
)
)