From 7fefe6f55c11cbed09ec71b92950f56b27fb58c4 Mon Sep 17 00:00:00 2001 From: alice <166900055+alicesaidhi@users.noreply.github.com> Date: Thu, 22 Aug 2024 15:30:27 +0200 Subject: [PATCH 1/2] fix signals firing after disconnecting --- src/ecr.luau | 11 ++++++----- test/tests.luau | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/ecr.luau b/src/ecr.luau index f8d6d82..7985c71 100644 --- a/src/ecr.luau +++ b/src/ecr.luau @@ -1515,7 +1515,6 @@ local signal_create: () -> (Signal, Array<(T...) -> ()>) do type _Connection = { signal: _Signal, listener: (T...) -> (), - idx: number, connected: boolean, disconnect: (_Connection) -> (), @@ -1524,11 +1523,10 @@ local signal_create: () -> (Signal, Array<(T...) -> ()>) do local Connection = {} - function Connection.new(signal: _Signal, fn: (T...) -> (), idx: number): _Connection + function Connection.new(signal: _Signal, fn: (T...) -> ()): _Connection return { signal = signal, listener = fn, - idx = idx, connected = true, disconnect = function(self: _Connection) @@ -1536,7 +1534,10 @@ local signal_create: () -> (Signal, Array<(T...) -> ()>) do 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 @@ -1577,7 +1578,7 @@ local signal_create: () -> (Signal, Array<(T...) -> ()>) do self.listeners[n + 1] = listener - return Connection.new(self, listener, n + 1) + return Connection.new(self, listener) end } diff --git a/test/tests.luau b/test/tests.luau index 27c6c92..e673c60 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -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 From 597c82d74234eac563ccba9836b422bebe17b8c0 Mon Sep 17 00:00:00 2001 From: alicesaidhi <166900055+alicesaidhi@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:09:30 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef8fcec..d0f065a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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