@@ -246,9 +246,11 @@ 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_ **pure** 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`. **IMPORTANT**: the result of
252+ # ' this function should be solely determined by it's input value (i.e., it
253+ # ' must be a pure function).
252254# ' @export
253255# '
254256# ' @examples
@@ -308,7 +310,16 @@ bs_dependency <- function(input = list(), theme, name, version,
308310# '
309311# ' @rdname bs_dependency
310312bs_dependency_defer <- function (func ) {
311- force(func )
313+ # func() most likely calls stuff like sass_file() and bs_dependency() ->
314+ # sass_partial() -> sass() (e.g., see example section above). Even though
315+ # sass() calls can be cached, we still have two performance issues:
316+ # * Computing the cache key itself is not necessarily cheap since it
317+ # involves file-level operations like getting the mtimes of file attachments
318+ # * It doesn't generally make much sense to cache sass::sass_file(),
319+ # which also does some file-level operations.
320+ # So, to help with performance (in the case where we repeatedly call the same
321+ # func with the same theme), memoise func with a short-lived cache
322+ mfunc <- memoise :: memoise(func , cache = .dependency_cache )
312323
313324 tagFunction(function () {
314325 if (is_shiny_app()) {
@@ -320,11 +331,11 @@ bs_dependency_defer <- function(func) {
320331 # resulting htmlDependency so that it can be embedded in the static page.
321332 shiny :: registerThemeDependency(func )
322333
323- return (func (get_current_theme()))
334+ return (mfunc (get_current_theme()))
324335 }
325336
326337 # Outside of a Shiny context, we'll just get the global theme.
327- func (bs_global_get())
338+ mfunc (bs_global_get())
328339 })
329340}
330341
0 commit comments