-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathevents.lua
51 lines (40 loc) · 1.09 KB
/
events.lua
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
local EventHandler = Class(function(self, event, fn, processor)
self.event = event
self.fn = fn
self.processor = processor
end)
function EventHandler:Remove()
self.processor:RemoveHandler(self)
end
------------------------
EventProcessor = Class(function(self)
self.events = {}
end)
function EventProcessor:AddEventHandler(event, fn)
local handler = EventHandler(event, fn, self)
if self.events[event] == nil then
self.events[event] = {}
end
self.events[event][handler] = true
return handler
end
function EventProcessor:RemoveHandler(handler)
if handler then
local ev = self.events[handler.event]
if ev then
ev[handler] = nil
end
end
end
function EventProcessor:GetHandlersForEvent(event)
return self.events[event] or {}
end
function EventProcessor:HandleEvent(event, ...)
local handlers = self.events[event]
if handlers then
for k,v in pairs(handlers) do
k.fn(...)
end
end
end
---------------------------------------------