From 417f7f7236cfc8cf4c583307c4e153d05352cf25 Mon Sep 17 00:00:00 2001 From: Joe Cheng Date: Fri, 6 Jan 2017 18:00:03 -0800 Subject: [PATCH] Clean up correctly after API caller leaves --- R/middleware-shiny.R | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/R/middleware-shiny.R b/R/middleware-shiny.R index e3d970333..913e6ce62 100644 --- a/R/middleware-shiny.R +++ b/R/middleware-shiny.R @@ -83,6 +83,14 @@ apiHandler <- function(serverFuncSource) { ) shinysession <- ShinySession$new(ws) + on.exit({ + try({ + # Clean up the session. Very important, so that observers + # and such don't hang around, and to let memory get gc'd. + shinysession$wsClosed() + appsByToken$remove(shinysession$token) + }) + }, add = TRUE) appsByToken$set(shinysession$token, shinysession) shinysession$setShowcase(.globals$showcaseDefault) @@ -173,7 +181,13 @@ apiWsHandler <- function(serverFuncSource) { ws$close() }) - # TODO: Handle ws$onClose() + ws$onClose(function() { + # Clean up the session. Very important, so that observers + # and such don't hang around, and to let memory get gc'd. + shinysession$wsClosed() + appsByToken$remove(shinysession$token) + }) + # TODO: What to do on ws$onMessage? } }