Skip to content

Commit 1488ccc

Browse files
authored
Update Signal.lua
1 parent eeef255 commit 1488ccc

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

src/ReplicatedStorage/Signal.lua

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
4848
Properties:
4949
50-
Connection.Connected
50+
.Connected
5151
5252
Functions:
5353
54-
Connection:Disconnect()
54+
:Disconnect()
5555
Description:
5656
\\ Disconnects a connection.
5757
@@ -78,6 +78,11 @@
7878
7979
]]
8080

81+
local c_running = coroutine.running
82+
local c_yield = coroutine.yield
83+
local t_defer = task.defer
84+
local t_desynchronize = task.desynchronize
85+
8186
local ERROR_ON_ALREADY_DISCONNECTED = false
8287

8388
local Signal = {}
@@ -86,14 +91,6 @@ Signal.__index = Signal
8691
local Connection = {}
8792
Connection.__index = Connection
8893

89-
function Signal:__call(_, ...)
90-
if not self:IsActive() then
91-
return
92-
end
93-
94-
return self:Connect(...)
95-
end
96-
9794
function Signal.new()
9895
local self = setmetatable({
9996
_active = true,
@@ -107,27 +104,24 @@ function Signal:IsActive()
107104
return self._active == true
108105
end
109106

110-
function Signal:Connect(func)
111-
assert(
112-
typeof(func) == 'function',
113-
":Connect must be called with a function"
114-
)
115-
107+
local function Connect(self, func, is_wait)
116108
if not self:IsActive() then
117109
return setmetatable({
118110
Connected = false
119111
}, Connection)
120112
end
121113

114+
local _head = self._head
115+
122116
local connection = setmetatable({
123117
Connected = true,
124118
_func = func,
125119
_signal = self,
126-
_next = nil,
127-
_prev = nil
120+
_next = _head,
121+
_prev = nil,
122+
_is_wait = is_wait
128123
}, Connection)
129124

130-
local _head = self._head
131125
if _head ~= nil then
132126
_head._prev = connection
133127
connection._next = _head
@@ -138,14 +132,23 @@ function Signal:Connect(func)
138132
return connection
139133
end
140134

135+
function Signal:Connect(func)
136+
assert(
137+
typeof(func) == 'function',
138+
":Connect must be called with a function"
139+
)
140+
141+
return Connect(self, func)
142+
end
143+
141144
function Signal:ConnectParallel(func)
142145
assert(
143146
typeof(func) == 'function',
144147
":ConnectParallel must be called with a function"
145148
)
146149

147-
return self:Connect(function(...)
148-
task.desynchronize()
150+
return Connect(self, function(...)
151+
t_desynchronize()
149152
func(...)
150153
end)
151154
end
@@ -185,24 +188,13 @@ function Connection:Disconnect()
185188
end
186189

187190
function Signal:Wait()
188-
if not self:IsActive() then
189-
warn("Tried to :Wait on destroyed signal")
190-
return
191-
end
192-
193-
local thread = coroutine.running()
194-
195-
local connection
196-
connection = self:Connect(function(...)
197-
connection:Disconnect()
198-
199-
task.spawn(
200-
thread,
201-
...
202-
)
203-
end)
191+
Connect(
192+
self,
193+
c_running(),
194+
true
195+
)
204196

205-
return coroutine.yield()
197+
return c_yield()
206198
end
207199

208200
function Signal:Fire(...)
@@ -213,10 +205,14 @@ function Signal:Fire(...)
213205

214206
local connection = self._head
215207
while connection ~= nil do
216-
task.defer(
208+
t_defer(
217209
connection._func,
218210
...
219211
)
212+
213+
if connection._is_wait then
214+
connection:Disconnect()
215+
end
220216

221217
connection = connection._next
222218
end
@@ -225,11 +221,9 @@ end
225221
function Signal:DisconnectAll()
226222
local connection = self._head
227223
while connection ~= nil do
228-
--connection:Disconnect()
229-
230224
connection.Connected = false
231-
connection._prev = nil
232225
connection._signal = nil
226+
connection._prev = nil
233227

234228
connection = connection._next
235229
end
@@ -245,4 +239,17 @@ function Signal:Destroy()
245239
self:DisconnectAll()
246240
end
247241

242+
function Signal:__call(_, func)
243+
if not self:IsActive() then
244+
return
245+
end
246+
247+
assert(
248+
typeof(func) == 'function',
249+
":Connect must be called with a function"
250+
)
251+
252+
return Connect(self, func)
253+
end
254+
248255
return Signal

0 commit comments

Comments
 (0)