@@ -246,9 +246,15 @@ bs_dependency <- function(input = list(), theme, name, version,
246246 do.call(htmlDependency , c(dep_args , .dep_args ))
247247}
248248
249- # ' @param func a _non-anonymous_ function, with a _single_ argument. This function
250- # ' should accept a [bs_theme()] object and return a single [htmlDependency()],
251- # ' a list of them, or `NULL`.
249+ # ' @param func a _non-anonymous_ function, with a _single_ argument.
250+ # ' This function should accept a [bs_theme()] object and return a single
251+ # ' [htmlDependency()], a list of them, or `NULL`.
252+ # ' @param memoise whether or not to memoise (i.e., cache) `func` results for a
253+ # ' short period of time. The default, `TRUE`, can have large performance
254+ # ' benefits when many instances of the same themable widget are rendered. Note
255+ # ' that you may want to avoid memoisation if `func` relies on side-effects
256+ # ' (e.g., files on-disk) that need to change for each themable widget
257+ # ' instance.
252258# ' @export
253259# '
254260# ' @examples
@@ -307,24 +313,37 @@ bs_dependency <- function(input = list(), theme, name, version,
307313# ' }
308314# '
309315# ' @rdname bs_dependency
310- bs_dependency_defer <- function (func ) {
311- force(func )
316+ bs_dependency_defer <- function (func , memoise = TRUE ) {
317+ # func() most likely calls stuff like sass_file() and bs_dependency() ->
318+ # sass_partial() -> sass() (e.g., see example section above) Even though
319+ # sass() calls can be cached, there is still considerable overhead involved
320+ # in computing the cache key itself because it involves file-level operations
321+ # (e.g. getting the mtimes of file attachments).
322+ # So, to help with performance (in the case where we repeatedly call the same
323+ # func with the same theme), memoise func with a short-lived cache
324+ if (memoise ) {
325+ mfunc <- memoise :: memoise(func , cache = .dependency_cache )
326+ } else {
327+ mfunc <- func
328+ }
312329
313330 tagFunction(function () {
314331 if (is_shiny_app()) {
315332 # If we're in a Shiny app, do two things:
316333 # (1) Register this function as a dependency so that Shiny will know to
317- # update it later if the theme dynamically changes. Repeated registrations
318- # are harmless because Shiny will de-duplicate them.
334+ # update it later if the theme dynamically changes (i.e.,
335+ # session$setCurrentTheme() is called). Note that we *don't* provide
336+ # the memoised version since Shiny will de-duplicate identical()
337+ # functions anyway.
319338 # (2) Call the user's `func()` with the current theme, and return the
320339 # resulting htmlDependency so that it can be embedded in the static page.
321340 shiny :: registerThemeDependency(func )
322341
323- return (func (get_current_theme()))
342+ return (mfunc (get_current_theme()))
324343 }
325344
326345 # Outside of a Shiny context, we'll just get the global theme.
327- func (bs_global_get())
346+ mfunc (bs_global_get())
328347 })
329348}
330349
0 commit comments