From 395d1cee700b4d9addd13a3d9d9cac03deeffeac Mon Sep 17 00:00:00 2001 From: Joe Cheng Date: Wed, 20 May 2015 13:29:56 -0700 Subject: [PATCH] Ensure that callbacks fire in a FIFO order Version bump required so Leaflet can detect this fix --- R/map.R | 3 +++ R/utils.R | 6 +++++- inst/tests/test-utils.R | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/R/map.R b/R/map.R index e1d4f4822..91790b8b0 100644 --- a/R/map.R +++ b/R/map.R @@ -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) diff --git a/R/utils.R b/R/utils.R index e314401e6..84fe4fcd5 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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 { diff --git a/inst/tests/test-utils.R b/inst/tests/test-utils.R index 391ff941b..6477dc638 100644 --- a/inst/tests/test-utils.R +++ b/inst/tests/test-utils.R @@ -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)) +})