-- Add a hook
function add_hook(object, name, fn, priority)
local h = {name = name, fn = fn, priority = priority}
object.hooks = object.hooks or {}
local n = #object.hooks + 1
for i, v in ipairs(object.hooks) do
if v.priority <= priority then n = i break end
end
table.insert(object.hooks, n, h);
end
-- Remove hooks by name
function remove_hooks(object, name)
if object.hooks then
for i, v in ipairs(object.hooks) do
if v.name == name then table.remove(object.hooks, i) end
end
end
end
-- Remove a hook by reference
function remove_hook(object, hook)
if object.hooks then
for i, v in ipairs(object.hooks) do
if v == hook then table.remove(object.hooks, i) end
end
end
end
-- Run hooks by name
function run_hooks(object, name, params)
for i, v in ipairs(object.hooks) do
if (v.name == name) then
v.fn(object, params)
if params.quit then break end
end
end
end