Skip to content

Commit c6f0add

Browse files
committed
More tests
1 parent 149793a commit c6f0add

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

src/Connection.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function Connection:Disconnect()
2626
local SignalRef = self._Signal
2727
local Temp = SignalRef._HeadConnection
2828

29-
if (Temp == nil) then
29+
if (not Temp) then
3030
return
3131
end
3232

@@ -45,7 +45,7 @@ function Connection:Disconnect()
4545
SignalRef._ConnectionCount -= 1
4646

4747
if (SignalRef._ConnectionCount == 0) then
48-
SignalRef._HeadConnection = nil
48+
SignalRef._HeadConnection = false
4949
SignalRef._OnConnectionsEmpty()
5050
end
5151

src/init.lua

+15-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,19 @@ type Connection = Connection.Connection;
3131

3232
export type XSignal<T...> = {
3333
WaitNoTimeout: (() -> (T...));
34+
DisconnectAll: (() -> ());
35+
Destroy: (() -> ());
3436
Connect: (((T...) -> ()) -> (Connection));
3537
Once: (((T...) -> ()) -> (Connection));
3638
Fire: ((T...) -> ());
3739
Wait: ((number?, boolean?) -> (T...));
40+
41+
waitNoTimeout: (() -> (T...));
42+
disconnectAll: (() -> ());
43+
connect: (((T...) -> ()) -> (Connection));
44+
once: (((T...) -> ()) -> (Connection));
45+
fire: ((T...) -> ());
46+
wait: ((number?, boolean?) -> (T...));
3847
}
3948
type GenericConnection = RBXScriptConnection | {
4049
Disconnect: (GenericConnection) -> ();
@@ -213,11 +222,13 @@ XSignal.waitNoTimeout = XSignal.WaitNoTimeout
213222

214223
--- Flushes all connections from the Signal.
215224
function XSignal:Destroy()
216-
self._HeadConnection = false
217-
self._ConnectionCount = 0
218-
self._OnConnectionsEmpty()
225+
local Head = self._HeadConnection
226+
227+
while (Head) do
228+
Head:Disconnect()
229+
Head = Head._Next
230+
end
219231
end
220-
XSignal.destroy = XSignal.Destroy
221232
XSignal.DisconnectAll = XSignal.Destroy
222233
XSignal.disconnectAll = XSignal.Destroy
223234

src/init.spec.lua

+49
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,55 @@ return function()
368368
Test:Fire()
369369
expect(RunCount).to.equal(0)
370370
end)
371+
372+
it("should not error on multiple disconnections", function()
373+
local Test = XSignal.new()
374+
local X = Test:Connect(function() end)
375+
Test:DisconnectAll()
376+
377+
expect(function()
378+
X:Disconnect()
379+
end).never.to.throw()
380+
end)
381+
382+
it("should not reconnect the whole chain when Reconnect is called, and should set Connected = false", function()
383+
local Test = XSignal.new()
384+
local Count = 0
385+
386+
local X = Test:Connect(function()
387+
Count += 1
388+
end)
389+
390+
local Y = Test:Connect(function()
391+
Count += 1
392+
end)
393+
394+
local Z = Test:Connect(function()
395+
Count += 1
396+
end)
397+
398+
expect(Count).to.equal(0)
399+
Test:Fire()
400+
expect(Count).to.equal(3)
401+
Test:DisconnectAll()
402+
Test:Fire()
403+
expect(Count).to.equal(3)
404+
405+
Count = 0
406+
X:Reconnect()
407+
Test:Fire()
408+
expect(Count).to.equal(1)
409+
410+
Count = 0
411+
Z:Reconnect()
412+
Test:Fire()
413+
expect(Count).to.equal(2)
414+
415+
Count = 0
416+
Y:Reconnect()
417+
Test:Fire()
418+
expect(Count).to.equal(3)
419+
end)
371420
end)
372421

373422
describe("XSignal.Extend", function()

0 commit comments

Comments
 (0)