47
47
48
48
Properties:
49
49
50
- Connection .Connected
50
+ .Connected
51
51
52
52
Functions:
53
53
54
- Connection :Disconnect()
54
+ :Disconnect()
55
55
Description:
56
56
\\ Disconnects a connection.
57
57
78
78
79
79
]]
80
80
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
+
81
86
local ERROR_ON_ALREADY_DISCONNECTED = false
82
87
83
88
local Signal = {}
@@ -86,14 +91,6 @@ Signal.__index = Signal
86
91
local Connection = {}
87
92
Connection .__index = Connection
88
93
89
- function Signal :__call (_ , ...)
90
- if not self :IsActive () then
91
- return
92
- end
93
-
94
- return self :Connect (... )
95
- end
96
-
97
94
function Signal .new ()
98
95
local self = setmetatable ({
99
96
_active = true ,
@@ -107,27 +104,24 @@ function Signal:IsActive()
107
104
return self ._active == true
108
105
end
109
106
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 )
116
108
if not self :IsActive () then
117
109
return setmetatable ({
118
110
Connected = false
119
111
}, Connection )
120
112
end
121
113
114
+ local _head = self ._head
115
+
122
116
local connection = setmetatable ({
123
117
Connected = true ,
124
118
_func = func ,
125
119
_signal = self ,
126
- _next = nil ,
127
- _prev = nil
120
+ _next = _head ,
121
+ _prev = nil ,
122
+ _is_wait = is_wait
128
123
}, Connection )
129
124
130
- local _head = self ._head
131
125
if _head ~= nil then
132
126
_head ._prev = connection
133
127
connection ._next = _head
@@ -138,14 +132,23 @@ function Signal:Connect(func)
138
132
return connection
139
133
end
140
134
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
+
141
144
function Signal :ConnectParallel (func )
142
145
assert (
143
146
typeof (func ) == ' function' ,
144
147
" :ConnectParallel must be called with a function"
145
148
)
146
149
147
- return self : Connect (function (...)
148
- task . desynchronize ()
150
+ return Connect (self , function (...)
151
+ t_desynchronize ()
149
152
func (... )
150
153
end )
151
154
end
@@ -185,24 +188,13 @@ function Connection:Disconnect()
185
188
end
186
189
187
190
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
+ )
204
196
205
- return coroutine.yield ()
197
+ return c_yield ()
206
198
end
207
199
208
200
function Signal :Fire (...)
@@ -213,10 +205,14 @@ function Signal:Fire(...)
213
205
214
206
local connection = self ._head
215
207
while connection ~= nil do
216
- task . defer (
208
+ t_defer (
217
209
connection ._func ,
218
210
...
219
211
)
212
+
213
+ if connection ._is_wait then
214
+ connection :Disconnect ()
215
+ end
220
216
221
217
connection = connection ._next
222
218
end
225
221
function Signal :DisconnectAll ()
226
222
local connection = self ._head
227
223
while connection ~= nil do
228
- -- connection:Disconnect()
229
-
230
224
connection .Connected = false
231
- connection ._prev = nil
232
225
connection ._signal = nil
226
+ connection ._prev = nil
233
227
234
228
connection = connection ._next
235
229
end
@@ -245,4 +239,17 @@ function Signal:Destroy()
245
239
self :DisconnectAll ()
246
240
end
247
241
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
+
248
255
return Signal
0 commit comments