Skip to content

Commit

Permalink
perf: auto-populate removable rocks cache (#70)
Browse files Browse the repository at this point in the history
* refactor: move removable rocks cache to `cache` module

* perf: auto-populate removable rocks cache
  • Loading branch information
mrcjkb authored Dec 11, 2023
1 parent b10ab17 commit 7b6d361
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 37 deletions.
34 changes: 34 additions & 0 deletions lua/rocks/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
local cache = {}

local luarocks = require("rocks.luarocks")
local state = require("rocks.state")
local nio = require("nio")

---@type { [string]: Rock[] } | nil
local _cached_rocks = nil

---Used for completions only
---@type string[] | nil
local _removable_rock_cache = nil

---Query luarocks packages and populate the cache.
---@type async fun()
cache.populate_cached_rocks = nio.create(function()
Expand Down Expand Up @@ -50,4 +55,33 @@ function cache.try_get_rocks()
return _cached_rocks
end

---Query the state for rocks that can be removed
---and populate the cache.
---@type async fun()
cache.populate_removable_rock_cache = nio.create(function()
if _removable_rock_cache then
return
end
_removable_rock_cache = state.query_removable_rocks()
end)

---Tries to get the cached removable rocks.
---Returns an empty list if the cache is not ready,
---and triggers an async task to populate the cache.
---@return { [string]: Rock[] } rocks indexed by name
function cache.try_get_removable_rocks()
if not _removable_rock_cache then
nio.run(cache.populate_removable_rock_cache)
local rocks = vim.empty_dict()
---@cast rocks { [string]: Rock[] }
return rocks
end
return _removable_rock_cache
end

---Invalidate the removable rocks cache
cache.invalidate_removable_rocks = function()
_removable_rock_cache = nil
end

return cache
15 changes: 13 additions & 2 deletions lua/rocks/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ local function complete_names(query)
return fzy.fuzzy_filter(query, rock_names)
end

---Completion for installed rocks that are not dependencies of other rocks
---and can be removed.
---@param query string | nil
---@return string[]
local function complete_removable_rocks(query)
local removable_rocks = cache.try_get_removable_rocks()
if not query then
return {}
end
return fzy.fuzzy_filter(query, removable_rocks)
end

---@type { [string]: RocksCmd }
local rocks_command_tbl = {
update = {
Expand Down Expand Up @@ -113,8 +125,7 @@ local rocks_command_tbl = {
require("rocks.operations").prune(package)
end,
complete = function(query)
local state = require("rocks.state")
local rocks_list = state.complete_removable_rocks(query)
local rocks_list = complete_removable_rocks(query)
return rocks_list
end,
},
Expand Down
9 changes: 7 additions & 2 deletions lua/rocks/operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local log = require("rocks.log")
local fs = require("rocks.fs")
local config = require("rocks.config.internal")
local state = require("rocks.state")
local cache = require("rocks.cache")
local luarocks = require("rocks.luarocks")
local nio = require("nio")
local progress = require("fidget.progress")
Expand All @@ -46,7 +47,7 @@ end
---@param progress_handle? ProgressHandle
---@return Future
operations.install = function(name, version, progress_handle)
state.invalidate_cache()
cache.invalidate_removable_rocks()
local message = version and ("Installing: %s -> %s"):format(name, version) or ("Installing: %s"):format(name)
log.info(message)
if progress_handle then
Expand Down Expand Up @@ -105,7 +106,7 @@ end
---@param progress_handle? ProgressHandle
---@return Future
operations.remove = function(name, progress_handle)
state.invalidate_cache()
cache.invalidate_removable_rocks()
local message = ("Uninstalling: %s"):format(name)
log.info(message)
if progress_handle then
Expand Down Expand Up @@ -371,6 +372,7 @@ operations.sync = function(user_rocks)
else
progress_handle:finish()
end
cache.populate_removable_rock_cache()
end)
end

Expand Down Expand Up @@ -446,6 +448,7 @@ operations.update = function()
else
progress_handle:finish()
end
cache.populate_removable_rock_cache()
end)
end

Expand Down Expand Up @@ -499,6 +502,7 @@ operations.add = function(rock_name, version)
progress_handle:cancel()
end
end)
cache.populate_removable_rock_cache()
end)
end

Expand Down Expand Up @@ -535,6 +539,7 @@ operations.prune = function(rock_name)
progress_handle:cancel()
end
end)
cache.populate_removable_rock_cache()
end)
end

Expand Down
32 changes: 0 additions & 32 deletions lua/rocks/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@

local state = {}

---Used for completions only
---@type string[] | nil
local _removable_rock_cache = nil

local luarocks = require("rocks.luarocks")
local fzy = require("rocks.fzy")
local log = require("rocks.log")
local nio = require("nio")

Expand Down Expand Up @@ -149,31 +144,4 @@ state.query_removable_rocks = nio.create(function()
:totable()
end)

---@type async fun()
local populate_removable_rock_cache = nio.create(function()
if _removable_rock_cache then
return
end
_removable_rock_cache = state.query_removable_rocks()
end)

---Completion for installed rocks that are not dependencies of other rocks
---and can be removed.
---@param query string | nil
---@return string[]
state.complete_removable_rocks = function(query)
if not _removable_rock_cache then
nio.run(populate_removable_rock_cache)
return {}
end
if not query then
return {}
end
return fzy.fuzzy_filter(query, _removable_rock_cache)
end

state.invalidate_cache = function()
_removable_rock_cache = nil
end

return state
9 changes: 8 additions & 1 deletion plugin/rocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@ require("rocks.commands").create_commands()
local config = require("rocks.config.internal")

if not config.lazy then
require("nio").run(require("rocks.cache").populate_cached_rocks)
local nio = require("nio")
nio.run(function()
local cache = require("rocks.cache")
nio.gather({
cache.populate_cached_rocks,
cache.populate_removable_rock_cache,
})
end)
end

0 comments on commit 7b6d361

Please sign in to comment.