summaryrefslogtreecommitdiff
path: root/stowables-dotlocal/share/nvim/site/pack/manual/start/nvim-cmp-v0.0.1/lua/cmp/utils/event.lua
blob: 662d5731e4c71074e95ce9c72ab2f1d4ff583a28 (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
---@class cmp.Event
---@field private events table<string, function[]>
local event = {}

---Create vents
event.new = function()
  local self = setmetatable({}, { __index = event })
  self.events = {}
  return self
end

---Add event listener
---@param name string
---@param callback function
---@return function
event.on = function(self, name, callback)
  if not self.events[name] then
    self.events[name] = {}
  end
  table.insert(self.events[name], callback)
  return function()
    self:off(name, callback)
  end
end

---Remove event listener
---@param name string
---@param callback function
event.off = function(self, name, callback)
  for i, callback_ in ipairs(self.events[name] or {}) do
    if callback_ == callback then
      table.remove(self.events[name], i)
      break
    end
  end
end

---Remove all events
event.clear = function(self)
  self.events = {}
end

---Emit event
---@param name string
event.emit = function(self, name, ...)
  for _, callback in ipairs(self.events[name] or {}) do
    callback(...)
  end
end

return event