local unpack = unpack or table.unpack
local M = {}
local hooks = {
ready = {},
shutdown = {},
pre_activity = {},
post_activity = {},
idle_enter = {},
idle_leave = {},
workspace_change = {},
}
M.PRIORITY = {
HIGHEST = 100,
HIGH = 75,
NORMAL = 50,
LOW = 25,
LOWEST = 0,
}
function M.register(event, fn, priority)
local hook = hooks[event]
if not hook then
error(string.format('Invalid hook event: %s', event))
return
end
priority = priority or M.PRIORITY.NORMAL
table.insert(hook, {
fn = fn,
priority = priority,
})
table.sort(hook, function(a, b) return a.priority > b.priority end)
end
function M.run(event, ...)
local hook = hooks[event]
if not hook then return end
local args = { ... }
for _, ihook in ipairs(hook) do
ihook.fn(unpack(args))
end
end
return M