Skip to content

Commit 0d5b7fb

Browse files
pierreluctgPierre-Luc Tessier Gagne
andauthored
refactor(notifier): change listeners from list to set for uniqueness (#2066)
This prevents adding the same listener multiple times Co-authored-by: Pierre-Luc Tessier Gagne <ptessie3@ford.com>
1 parent 491a691 commit 0d5b7fb

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

can/notifier.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def __init__(
133133
:raises ValueError:
134134
If a passed in *bus* is already assigned to an active :class:`~can.Notifier`.
135135
"""
136-
self.listeners: list[MessageRecipient] = list(listeners)
136+
self.listeners: set[MessageRecipient] = set(listeners)
137137
self._bus_list: list[BusABC] = []
138138
self.timeout = timeout
139139
self._loop = loop
@@ -278,20 +278,18 @@ def _on_error(self, exc: Exception) -> bool:
278278
return was_handled
279279

280280
def add_listener(self, listener: MessageRecipient) -> None:
281-
"""Add new Listener to the notification list.
282-
If it is already present, it will be called two times
283-
each time a message arrives.
281+
"""Add new Listener to the notification set.
284282
285283
:param listener: Listener to be added to the list to be notified
286284
"""
287-
self.listeners.append(listener)
285+
self.listeners.add(listener)
288286

289287
def remove_listener(self, listener: MessageRecipient) -> None:
290-
"""Remove a listener from the notification list. This method
288+
"""Remove a listener from the notification set. This method
291289
throws an exception if the given listener is not part of the
292290
stored listeners.
293291
294-
:param listener: Listener to be removed from the list to be notified
292+
:param listener: Listener to be removed from the set to be notified
295293
:raises ValueError: if `listener` was never added to this notifier
296294
"""
297295
self.listeners.remove(listener)

0 commit comments

Comments
 (0)