Ensure that callbacks fire in a FIFO order

Version bump required so Leaflet can detect this fix
This commit is contained in:
Joe Cheng
2015-05-20 13:29:56 -07:00
parent 8f893a9752
commit 395d1cee70
3 changed files with 25 additions and 1 deletions

View File

@@ -23,6 +23,9 @@ Map <- R6Class(
env[[key]] <- value
value
},
mget = function(keys) {
base::mget(keys, env)
},
mset = function(...) {
args <- list(...)
if (length(args) == 0)

View File

@@ -580,7 +580,11 @@ Callbacks <- R6Class(
})
},
invoke = function(..., onError=NULL) {
for (callback in .callbacks$values()) {
# Ensure that calls are invoked in the order that they were registered
keys <- as.character(sort(as.integer(.callbacks$keys()), decreasing = TRUE))
callbacks <- .callbacks$mget(keys)
for (callback in callbacks) {
if (is.null(onError)) {
callback(...)
} else {

View File

@@ -100,3 +100,20 @@ test_that("anyUnnamed works as expected", {
x <- x[3:4]
expect_true(anyUnnamed(x))
})
test_that("Callbacks fire in predictable order", {
cb <- Callbacks$new()
x <- numeric(0)
cb$register(function() {
x <<- c(x, 1)
})
cb$register(function() {
x <<- c(x, 2)
})
cb$register(function() {
x <<- c(x, 3)
})
cb$invoke()
expect_equal(x, c(1, 2, 3))
})