1
1
local runService = game :GetService (" RunService" )
2
2
3
3
local Signal = {}
4
- local signalFuncs = {}
5
- local connectionFuncs = {}
4
+ Signal .__index = Signal
6
5
7
6
local function doesYield (func , ...)
8
7
local packed = table.pack (... )
@@ -15,25 +14,24 @@ local function doesYield(func, ...)
15
14
return not completed
16
15
end
17
16
18
-
19
17
function Signal .new ()
20
- return setmetatable ({
21
- _connections = {};
22
- }, {
23
- __index = signalFuncs
24
- })
18
+
19
+ local self = setmetatable ({
20
+ _connections = {}
21
+ }, Signal )
22
+
23
+ return self
25
24
end
26
25
27
- function signalFuncs . Destroy (self )
26
+ function Signal : Destroy ()
28
27
for i , connection in ipairs (self ._connections ) do
29
28
connection :Disconnect ()
30
29
self ._connections [i ] = nil
31
30
end
32
- setmetatable (self , nil )
33
- table .clear (self )
31
+ self = nil
34
32
end
35
33
36
- function signalFuncs . Fire (self , ...)
34
+ function Signal : Fire (...)
37
35
for _ , connection in pairs (self ._connections ) do
38
36
if connection .Function then
39
37
local thread = coroutine.create (connection .Function )
@@ -42,7 +40,7 @@ function signalFuncs.Fire(self, ...)
42
40
end
43
41
end
44
42
45
- function signalFuncs . FireNoYield (self , ...)
43
+ function Signal : FireNoYield (...)
46
44
for _ , connection in pairs (self ._connections ) do
47
45
if connection .Function then
48
46
local yields = doesYield (connection .Function , ... )
@@ -53,14 +51,14 @@ function signalFuncs.FireNoYield(self, ...)
53
51
end
54
52
end
55
53
56
- function signalFuncs . Wait (self )
57
- local fired = false ;
54
+ function Signal : Wait ()
55
+ local fired = false
58
56
59
57
local connection = self :Connect (function ()
60
58
fired = true
61
59
end )
62
- local startTime = os.clock ();
63
-
60
+
61
+ local startTime = os.clock ()
64
62
repeat
65
63
runService .Stepped :Wait ()
66
64
until fired or not connection .Connected
@@ -69,23 +67,20 @@ function signalFuncs.Wait(self)
69
67
return os.clock () - startTime
70
68
end
71
69
72
- function signalFuncs . Connect (self , givenFunction )
70
+ function Signal : Connect (givenFunction )
73
71
assert (typeof (givenFunction ) == " function" , " You need to give a function." )
74
-
75
- local connection = setmetatable ({
76
- Function = givenFunction ;
77
- Connected = true ;
78
- }, {
79
- __index = connectionFuncs ;
80
- })
81
- table.insert (self ._connections , connection )
72
+
73
+ local connection = {
74
+ Function = givenFunction ,
75
+ Connected = true
76
+ }
77
+ table.insert (self , # self + 1 , connection )
78
+
82
79
return connection
83
80
end
84
81
85
- function connectionFuncs .Disconnect (self )
86
- self .Function = nil ;
87
- self .Connected = false
88
- setmetatable (self , nil )
82
+ function Signal :Disconnect ()
83
+ table .clear (self ._connections )
89
84
end
90
85
91
86
0 commit comments