Skip to content

Commit

Permalink
feat(api): emit user events for cache updates (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Aug 22, 2024
1 parent 9290a2b commit 4b56808
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
49 changes: 31 additions & 18 deletions doc/rocks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,38 @@ RocksOpts *RocksOpts*
==============================================================================
rocks.nvim |User| |event|s *rocks.user-event*

The following |User| |event|s are available:

RocksInstallPost Invoked after installing or updating a rock
The `data` is of type
|rocks.user-events.data.RocksInstallPost|.

RocksCachePopulated Invoked when the luarocks rocks cache has been populated.
The `data` is a reference to the cached rocks,
of type `table<rock_name, Rock[]>`

RocksRemovableRocksCachePopulated Invoked when the removable rocks cache has been populated.
The `data` is a reference to the rock names,
of type `string[]`

RocksOutdatedRocksCachePopulated Invoked when the outdated rocks cache has been populated.
The `data` is a reference to the outdated rocks,
of type `table<rock_name, OutdatedRock>`.


To create an autocommand for an event:
>lua
vim.api.nvim_create_autocmd("User", {
pattern = "RocksInstallPost",
callback = function(ev)
---@type rocks.user-events.data.RocksInstallPost
local data = ev.data
-- ...
end,
})
<

rocks.user-events.data.RocksInstallPost*rocks.user-events.data.RocksInstallPost*
The following |User| |event|s are available:

RocksInstallPost Invoked after installing or updating a rock
The `data` is of type
|rocks.user-events.data.RocksInstallPost|.


To create an autocommand for an event:
>lua
vim.api.nvim_create_autocmd("User", {
pattern = "RocksInstallPost",
callback = function(ev)
---@type rocks.user-events.data.RocksInstallPost
local data = ev.data
-- ...
end,
})
<

Fields: ~
{spec} (RockSpec)
Expand Down
21 changes: 21 additions & 0 deletions lua/rocks/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ cache.populate_cached_rocks = nio.create(function()
luarocks.search_all(function(rocks)
if not vim.tbl_isempty(rocks) then
_cached_rocks = rocks
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", {
pattern = "RocksCachePopulated",
modeline = false,
data = _cached_rocks,
})
end)
end
end, {
dev = true,
Expand All @@ -84,6 +91,13 @@ cache.populate_removable_rock_cache = nio.create(function()
return
end
_removable_rock_cache = state.query_removable_rocks()
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", {
pattern = "RocksRemovableRocksCachePopulated",
modeline = false,
data = _removable_rock_cache,
})
end)
end)

---Tries to get the cached removable rocks.
Expand All @@ -107,6 +121,13 @@ cache.populate_outdated_rock_cache = nio.create(function()
return
end
_outdated_rock_cache = state.outdated_rocks()
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", {
pattern = "RocksOutdatedRocksCachePopulated",
modeline = false,
data = _outdated_rock_cache,
})
end)
end)

---Populate all rocks state caches
Expand Down
19 changes: 16 additions & 3 deletions lua/rocks/meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@
---@brief [[
---The following |User| |event|s are available:
---
---RocksInstallPost Invoked after installing or updating a rock
--- The `data` is of type
--- |rocks.user-events.data.RocksInstallPost|.
---RocksInstallPost Invoked after installing or updating a rock
--- The `data` is of type
--- |rocks.user-events.data.RocksInstallPost|.
---
---RocksCachePopulated Invoked when the luarocks rocks cache has been populated.
--- The `data` is a reference to the cached rocks,
--- of type `table<rock_name, Rock[]>`
---
---RocksRemovableRocksCachePopulated Invoked when the removable rocks cache has been populated.
--- The `data` is a reference to the rock names,
--- of type `string[]`
---
---RocksOutdatedRocksCachePopulated Invoked when the outdated rocks cache has been populated.
--- The `data` is a reference to the outdated rocks,
--- of type `table<rock_name, OutdatedRock>`.
---
---
---To create an autocommand for an event:
Expand All @@ -27,6 +39,7 @@
--- end,
--- })
---<
---@brief ]]

---@class rocks.user-events.data.RocksInstallPost
---@field spec RockSpec
Expand Down

0 comments on commit 4b56808

Please sign in to comment.