From 627b6446a19781c330ef1f19af9b5804312d93cd Mon Sep 17 00:00:00 2001 From: Max Korbel Date: Mon, 25 Nov 2024 09:02:19 -0800 Subject: [PATCH] clear up glitch listeners not needed anymore --- lib/src/signals/wire_net.dart | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/src/signals/wire_net.dart b/lib/src/signals/wire_net.dart index 4ff752b31..0f99f36be 100644 --- a/lib/src/signals/wire_net.dart +++ b/lib/src/signals/wire_net.dart @@ -97,16 +97,27 @@ class _WireNet extends _Wire { ..forEach((p) => p._replaceWire(other, this)) ..clear(); + other._glitchListeners + ..forEach((listener) => listener.cancel()) + ..clear(); + return this; } + /// A list of listeners of glitches that this wire initiated. + /// + /// If this wire is adopted/replaced/destroyed, these listeners should be + /// cancelled and the list cleared. + late final List> _glitchListeners = + []; + void _addDriver(_WireNetDriver driver) { if (_drivers.add(driver)) { - //TODO: eliminiate glitch listeners after adoption!? (in all wires) - // maybe already taken care of via adoption? - driver.signal.glitch.listen((args) { - _evaluateNewValue(signalName: driver.signal.name); - }); + _glitchListeners.add( + driver.signal.glitch.listen((args) { + _evaluateNewValue(signalName: driver.signal.name); + }), + ); } } @@ -239,4 +250,8 @@ class _WireNetBlasted extends _Wire implements _WireNet { @override String toString() => '${super.toString()} (net blasted)'; + + @override + List> get _glitchListeners => + throw UnimplementedError('Not needed for blasted wires.'); }