diff options
| author | Collin Williams <96917990+bluedragon1221@users.noreply.github.com> | 2026-05-11 15:07:55 -0500 |
|---|---|---|
| committer | Collin Williams <96917990+bluedragon1221@users.noreply.github.com> | 2026-05-11 15:07:55 -0500 |
| commit | cebb280fe546190e55e071b6adbf37cd47950f34 (patch) | |
| tree | 6e7a7605f8832b1c1bd7e467884a8792a5e1fab4 /runtime/lua/core/hooks.lua | |
initial commit
Diffstat (limited to 'runtime/lua/core/hooks.lua')
| -rw-r--r-- | runtime/lua/core/hooks.lua | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/runtime/lua/core/hooks.lua b/runtime/lua/core/hooks.lua new file mode 100644 index 0000000..9623e9c --- /dev/null +++ b/runtime/lua/core/hooks.lua @@ -0,0 +1,61 @@ +local M = {} + +function M.new_store() + return {} +end + +function M.add(store, event_name, fn) + if type(event_name) ~= "string" or event_name == "" then + return false, "event name must be a non-empty string" + end + if type(fn) ~= "function" then + return false, "hook must be a function" + end + + local bucket = store[event_name] + if not bucket then + bucket = {} + store[event_name] = bucket + end + + bucket[#bucket + 1] = fn + return true +end + +function M.run(store, event_name, ...) + local bucket = store[event_name] + if not bucket then + return true + end + + local ok_all = true + for i = 1, #bucket do + local ok, err = pcall(bucket[i], ...) + if not ok then + ok_all = false + print(string.format("lemacs hook error [%s]: %s", event_name, tostring(err))) + end + end + + return ok_all +end + +function M.run_first_truthy(store, event_name, ...) + local bucket = store[event_name] + if not bucket then + return false + end + + for i = 1, #bucket do + local ok, result = pcall(bucket[i], ...) + if not ok then + print(string.format("lemacs hook error [%s]: %s", event_name, tostring(result))) + elseif result then + return true + end + end + + return false +end + +return M |
