This repository was archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsSignal.modules.lua
More file actions
89 lines (65 loc) · 1.73 KB
/
Copy pathsSignal.modules.lua
File metadata and controls
89 lines (65 loc) · 1.73 KB
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
84
85
86
87
88
89
--// Initialization
local Signal = {}
Signal.__index = Signal
local Connection = {}
Connection.__index = Connection
--// Functions
function Connection:Disconnect()
for Index, Connection in next, self.Signal.Connections do
if Connection == self then
table.remove(self.Signal.Connections, Index)
break
end
end
self.Function = nil
self.Signal = nil
end
function Connection:Fire(...)
self.Function(...)
end
function Connection.new(BoundSignal, Function)
local self = setmetatable({}, Connection)
self.Function = Function
self.Signal = BoundSignal
table.insert(self.Signal.Connections, self)
return self
end
function Signal:Connect(Function)
local NewConnection = Connection.new(self, Function)
if self.StoreFireNew then
for _, Arguments in next, self.FireStore do
task.spawn(Function, unpack(Arguments))
end
end
return NewConnection
end
function Signal:Wait()
table.insert(self.YieldedThreads, (coroutine.running()))
return coroutine.yield()
end
function Signal:Fire(...)
if self.StoreFireNew then
table.insert(self.FireStore, {...})
end
for ThreadId = #self.YieldedThreads, 1, -1 do
task.spawn(self.YieldedThreads[ThreadId], ...)
table.remove(self.YieldedThreads, ThreadId)
end
for _, BoundConnection in next, self.Connections do
local Success, Error = pcall(Connection.Fire, BoundConnection, ...)
if not Success then
warn("Error detected in function bound to sSignal. Errata follows.")
warn(Error)
print(debug.traceback())
end
end
end
function Signal.new(StoreFireNew)
local self = setmetatable({}, Signal)
self.Connections = {}
self.YieldedThreads = {}
self.StoreFireNew = StoreFireNew
self.FireStore = (self.StoreFireNew and {}) or nil
return self
end
return Signal