-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvent.lua
More file actions
34 lines (29 loc) · 733 Bytes
/
Event.lua
File metadata and controls
34 lines (29 loc) · 733 Bytes
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
local class = require "class"
---@class Event: class
---@type fun(): Event
---@type fun(...)
local Event = class("Event")
---Appends a new event handler.
---@param handler any
function Event:add(handler)
table.insert(self, handler)
end
---Removes an existing event handler, raising an error if it doesn't actually exist.
---@param handler any
function Event:remove(handler)
for i = #self, 1, -1 do
if self[i] == handler then
table.remove(self, i)
return
end
end
error("event handler not found", 2)
end
---Calls all event handlers in the order they were added with the given parameters.
---@vararg any
function Event.metatable:__call(...)
for i = 1, #self do
self[i](...)
end
end
return Event