summaryrefslogtreecommitdiff
path: root/runtime/lua/core/hooks.lua
blob: 9623e9c9afd580f2ba8359122b5a88770098cd8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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