Use safer method to remove observer

This commit is contained in:
Winston Chang
2019-08-26 20:58:23 -05:00
committed by Joe Cheng
parent 89c97458c4
commit 8c19450b10
2 changed files with 30 additions and 21 deletions

View File

@@ -1733,7 +1733,17 @@ reactivePoll <- function(intervalMillis, session, checkFunc, valueFunc) {
rv <- reactiveValues(cookie = isolate(checkFunc()))
re_finalized <- FALSE
o <- observe({
# When no one holds a reference to the reactive returned from
# reactivePoll, destroy and remove the observer so that it doesn't keep
# firing and hold onto resources.
if (re_finalized) {
o$destroy()
rm(o, envir = parent.env(environment()))
}
rv$cookie <- checkFunc()
invalidateLater(intervalMillis(), session)
})
@@ -1746,11 +1756,8 @@ reactivePoll <- function(intervalMillis, session, checkFunc, valueFunc) {
}, label = NULL)
# When no one holds a reference to this object anymore, destroy and remove the
# observer so that it doesn't keep firing, and hold onto resources.
safe_finalizer(attr(re, "observable"), function(e) {
o$destroy()
rm(o, envir = parent.env(environment()))
reg.finalizer(attr(re, "observable"), function(e) {
re_finalized <<- TRUE
})
# So that the observer and finalizer function don't (indirectly) hold onto a