Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix signals firing after disconnecting #25

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
# Fixed

- Single type view not excluding properly with multiple exclude types.
- `Connection:disconnect()` disconnecting the wrong listener

## [0.8.0] - 2023-12-05

Expand Down
11 changes: 6 additions & 5 deletions src/ecr.luau
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,6 @@ local signal_create: <T...>() -> (Signal<T...>, Array<(T...) -> ()>) do
type _Connection<T... = ...unknown> = {
signal: _Signal<T...>,
listener: (T...) -> (),
idx: number,
connected: boolean,

disconnect: (_Connection<T...>) -> (),
Expand All @@ -1524,19 +1523,21 @@ local signal_create: <T...>() -> (Signal<T...>, Array<(T...) -> ()>) do

local Connection = {}

function Connection.new<T...>(signal: _Signal<T...>, fn: (T...) -> (), idx: number): _Connection<T...>
function Connection.new<T...>(signal: _Signal<T...>, fn: (T...) -> ()): _Connection<T...>
return {
signal = signal,
listener = fn,
idx = idx,
connected = true,

disconnect = function(self: _Connection<T...>)
if self.connected then
local listeners = self.signal.listeners
local n = #listeners
local last = listeners[n]
listeners[self.idx] = last
local idx = table.find(listeners, fn)
assert(idx, "cannot find listener")

listeners[idx] = last
listeners[n] = nil

self.connected = false
Expand Down Expand Up @@ -1577,7 +1578,7 @@ local signal_create: <T...>() -> (Signal<T...>, Array<(T...) -> ()>) do

self.listeners[n + 1] = listener

return Connection.new(self, listener, n + 1)
return Connection.new(self, listener)
end
}

Expand Down
24 changes: 24 additions & 0 deletions test/tests.luau
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,30 @@ TEST("registry:on_remove()", function()
end
end)

TEST("signals", function()
local reg = ecr.registry()

do CASE "firing after disconnecting"
local run = false
local dont_run = true
local a = reg:on_add(A):connect(function() run = true end) -- idx 1
local b = reg:on_add(A):connect(function() dont_run = false end) -- idx 2
-- {A, B}
a:disconnect()
-- {B}
a:reconnect()
-- {B, A}
local _c = reg:on_add(A):connect(function() end) -- idx 3
-- {B, A, C}
b:disconnect()
-- should be {A, C}, not {B, C}
reg:handle():set(A, true)
CHECK(run)
CHECK(dont_run)
end

end)

TEST("registry:track()", function()
local function flip()
return math.random() > 0.3
Expand Down
Loading