-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.lua
83 lines (74 loc) · 1.76 KB
/
Event.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
local Event = {}
Event.__index = Event
local CreateConnection
local Connection = {}
Connection.__index = Connection
local function find(haystack, needle)
for i = 1,#haystack do
if haystack[i] == needle then
return i
end
end
end
function Event.new()
local NewEvent = {
_Connections = {},
}
setmetatable(NewEvent, Event)
return NewEvent
end
function Event:Fire(...)
local CurrentConnections = {}
for _,Connection in ipairs(self._Connections) do
table.insert(CurrentConnections, Connection)
end
for i = #CurrentConnections,1,-1 do
local Connection = CurrentConnections[i]
if Connection.Connected then
if Connection._Callback then
coroutine.wrap(Connection._Callback)(...)
elseif Connection._Thread then
coroutine.resume(Connection._Thread, ...)
end
end
end
end
function Event:Connect(Callback)
local NewConnection = CreateConnection(self, Callback)
table.insert(self._Connections, NewConnection)
return NewConnection
end
function Event:Wait()
local Thread = coroutine.running()
local NewConnection = CreateConnection(self, nil, Thread)
table.insert(self._Connections, NewConnection)
return coroutine.yield()
end
function CreateConnection(Event, Callback, Thread)
assert(Event)
assert(not (Callback and Thread))
if Callback then
assert(type(Callback) == "function")
elseif Thread then
assert(type(Thread) == "thread")
else
error()
end
local NewConnection = {
Connected = true,
_Event = Event,
_Callback = Callback,
_Thread = Thread,
}
setmetatable(NewConnection, Connection)
return NewConnection
end
function Connection:Disconnect()
self.Connected = false
local EventConnections = self._Event._Connections
local i = find(EventConnections, self)
if i then
table.remove(EventConnections, i)
end
end
return Event