| 1 |
local M = {} |
| 2 |
local fns = {} |
| 3 |
|
| 4 |
---Store fn with id in fns so it can be later called |
| 5 |
---@param id integer component id (for now we are only doing one fn per component) |
| 6 |
---@param fn function the actual function to store. |
| 7 |
---@return number same id that was provided. |
| 8 |
function M.register_fn(id, fn) |
| 9 |
vim.validate { |
| 10 |
id = { id, 'n' }, |
| 11 |
fn = { fn, 'f' }, |
| 12 |
} |
| 13 |
fns[id] = fn |
| 14 |
return id |
| 15 |
end |
| 16 |
|
| 17 |
---Get the function with id |
| 18 |
---@param id number id of the fn to retrieve |
| 19 |
---@return function |
| 20 |
function M.get_fn(id) |
| 21 |
vim.validate { id = { id, 'n' } } |
| 22 |
return fns[id] or function() end |
| 23 |
end |
| 24 |
|
| 25 |
---Call the function of id with args |
| 26 |
---@param id number |
| 27 |
---@param ... any |
| 28 |
---@return any |
| 29 |
function M.call_fn(id, ...) |
| 30 |
return M.get_fn(id)(...) |
| 31 |
end |
| 32 |
|
| 33 |
---Clear the fns table |
| 34 |
function M.clear_fns() |
| 35 |
fns = {} |
| 36 |
end |
| 37 |
|
| 38 |
return M |