2022-07-20 13:28:49 +00:00
|
|
|
local M = {}
|
|
|
|
local fns = {}
|
|
|
|
|
|
|
|
---Store fn with id in fns so it can be later called
|
|
|
|
---@param id integer component id (for now we are only doing one fn per component)
|
|
|
|
---@param fn function the actual function to store.
|
|
|
|
---@return number same id that was provided.
|
|
|
|
function M.register_fn(id, fn)
|
2022-07-20 13:29:21 +00:00
|
|
|
vim.validate {
|
|
|
|
id = { id, 'n' },
|
|
|
|
fn = { fn, 'f' },
|
|
|
|
}
|
2022-07-20 13:28:49 +00:00
|
|
|
fns[id] = fn
|
|
|
|
return id
|
|
|
|
end
|
|
|
|
|
|
|
|
---Get the function with id
|
2023-03-30 04:12:37 +00:00
|
|
|
---@param id number id of the fn to retrieve
|
2022-07-20 13:28:49 +00:00
|
|
|
---@return function
|
|
|
|
function M.get_fn(id)
|
2022-07-20 13:29:21 +00:00
|
|
|
vim.validate { id = { id, 'n' } }
|
|
|
|
return fns[id] or function() end
|
2022-07-20 13:28:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---Call the function of id with args
|
|
|
|
---@param id number
|
|
|
|
---@param ... any
|
|
|
|
---@return any
|
|
|
|
function M.call_fn(id, ...)
|
|
|
|
return M.get_fn(id)(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
---Clear the fns table
|
|
|
|
function M.clear_fns()
|
|
|
|
fns = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|