Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tpc9000 committed Oct 22, 2022
1 parent 149793a commit c6f0add
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Connection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function Connection:Disconnect()
local SignalRef = self._Signal
local Temp = SignalRef._HeadConnection

if (Temp == nil) then
if (not Temp) then
return
end

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

if (SignalRef._ConnectionCount == 0) then
SignalRef._HeadConnection = nil
SignalRef._HeadConnection = false
SignalRef._OnConnectionsEmpty()
end

Expand Down
19 changes: 15 additions & 4 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,19 @@ type Connection = Connection.Connection;

export type XSignal<T...> = {
WaitNoTimeout: (() -> (T...));
DisconnectAll: (() -> ());
Destroy: (() -> ());
Connect: (((T...) -> ()) -> (Connection));
Once: (((T...) -> ()) -> (Connection));
Fire: ((T...) -> ());
Wait: ((number?, boolean?) -> (T...));

waitNoTimeout: (() -> (T...));
disconnectAll: (() -> ());
connect: (((T...) -> ()) -> (Connection));
once: (((T...) -> ()) -> (Connection));
fire: ((T...) -> ());
wait: ((number?, boolean?) -> (T...));
}
type GenericConnection = RBXScriptConnection | {
Disconnect: (GenericConnection) -> ();
Expand Down Expand Up @@ -213,11 +222,13 @@ XSignal.waitNoTimeout = XSignal.WaitNoTimeout

--- Flushes all connections from the Signal.
function XSignal:Destroy()
self._HeadConnection = false
self._ConnectionCount = 0
self._OnConnectionsEmpty()
local Head = self._HeadConnection

while (Head) do
Head:Disconnect()
Head = Head._Next
end
end
XSignal.destroy = XSignal.Destroy
XSignal.DisconnectAll = XSignal.Destroy
XSignal.disconnectAll = XSignal.Destroy

Expand Down
49 changes: 49 additions & 0 deletions src/init.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,55 @@ return function()
Test:Fire()
expect(RunCount).to.equal(0)
end)

it("should not error on multiple disconnections", function()
local Test = XSignal.new()
local X = Test:Connect(function() end)
Test:DisconnectAll()

expect(function()
X:Disconnect()
end).never.to.throw()
end)

it("should not reconnect the whole chain when Reconnect is called, and should set Connected = false", function()
local Test = XSignal.new()
local Count = 0

local X = Test:Connect(function()
Count += 1
end)

local Y = Test:Connect(function()
Count += 1
end)

local Z = Test:Connect(function()
Count += 1
end)

expect(Count).to.equal(0)
Test:Fire()
expect(Count).to.equal(3)
Test:DisconnectAll()
Test:Fire()
expect(Count).to.equal(3)

Count = 0
X:Reconnect()
Test:Fire()
expect(Count).to.equal(1)

Count = 0
Z:Reconnect()
Test:Fire()
expect(Count).to.equal(2)

Count = 0
Y:Reconnect()
Test:Fire()
expect(Count).to.equal(3)
end)
end)

describe("XSignal.Extend", function()
Expand Down

0 comments on commit c6f0add

Please sign in to comment.