aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/func.lua
diff options
context:
space:
mode:
authorRiley Bruins <ribru17@hotmail.com>2024-09-01 16:54:30 -0700
committerChristian Clason <ch.clason+github@icloud.com>2025-01-05 12:28:01 +0100
commitb61051ccb4c23958d43d285b8b801af11620264f (patch)
treeade40815a7e92af29910f3750d37e0192b6f4d7c /runtime/lua/vim/func.lua
parent54ac406649b9e93d756ea62c1a6a587db462039c (diff)
downloadrneovim-b61051ccb4c23958d43d285b8b801af11620264f.tar.gz
rneovim-b61051ccb4c23958d43d285b8b801af11620264f.tar.bz2
rneovim-b61051ccb4c23958d43d285b8b801af11620264f.zip
feat(func): allow manual cache invalidation for _memoize
This commit also adds some tests for the existing memoization functionality.
Diffstat (limited to 'runtime/lua/vim/func.lua')
-rw-r--r--runtime/lua/vim/func.lua14
1 files changed, 8 insertions, 6 deletions
diff --git a/runtime/lua/vim/func.lua b/runtime/lua/vim/func.lua
index f71659ffb4..fc8fa62c71 100644
--- a/runtime/lua/vim/func.lua
+++ b/runtime/lua/vim/func.lua
@@ -3,9 +3,6 @@ local M = {}
-- TODO(lewis6991): Private for now until:
-- - There are other places in the codebase that could benefit from this
-- (e.g. LSP), but might require other changes to accommodate.
--- - Invalidation of the cache needs to be controllable. Using weak tables
--- is an acceptable invalidation policy, but it shouldn't be the only
--- one.
-- - I don't think the story around `hash` is completely thought out. We
-- may be able to have a good default hash by hashing each argument,
-- so basically a better 'concat'.
@@ -17,6 +14,10 @@ local M = {}
--- Internally uses a |lua-weaktable| to cache the results of {fn} meaning the
--- cache will be invalidated whenever Lua does garbage collection.
---
+--- The cache can also be manually invalidated by calling `:clear()` on the returned object.
+--- Calling this function with no arguments clears the entire cache; otherwise, the arguments will
+--- be interpreted as function inputs, and only the cache entry at their hash will be cleared.
+---
--- The memoized function returns shared references so be wary about
--- mutating return values.
---
@@ -32,11 +33,12 @@ local M = {}
--- first n arguments passed to {fn}.
---
--- @param fn F Function to memoize.
---- @param strong? boolean Do not use a weak table
+--- @param weak? boolean Use a weak table (default `true`)
--- @return F # Memoized version of {fn}
--- @nodoc
-function M._memoize(hash, fn, strong)
- return require('vim.func._memoize')(hash, fn, strong)
+function M._memoize(hash, fn, weak)
+ -- this is wrapped in a function to lazily require the module
+ return require('vim.func._memoize')(hash, fn, weak)
end
return M