Skip to content

Commit

Permalink
Merge pull request #35 from Hundemeier/feature/remove_listener
Browse files Browse the repository at this point in the history
added remove_listener function as described in #34
  • Loading branch information
Hundemeier authored Jul 5, 2021
2 parents 1fa1bc2 + a955632 commit 4f0616d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ Functions:
You can also use the decorator `@listen_on('universe', universe=<universe>)`.
The callback should have one argument: `callback(packet)`
* `packet: DataPacket`: the received DataPacket with all information
* `remove_listener(<callback>)`: removes a previously registered listener regardless of the trigger.
This means a listener can only be removed completely, even if it was listening to multiple universes.
If the function never was registered, nothing happens. Note: if a function was registered multiple times, this remove function needs to be called only once.

### DataPacket
This is an abstract representation of an sACN Data packet that carries the DMX data. This class is used internally by
Expand Down
14 changes: 14 additions & 0 deletions sacn/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ def register_listener(self, trigger: str, func: callable, **kwargs) -> None:
else:
raise TypeError(f'The given trigger "{trigger}" is not a valid one!')

def remove_listener(self, func: callable) -> None:
"""
Removes the given function from all listening options (see LISTEN_ON_OPTIONS).
If the function never was registered, nothing happens. Note: if a function was registered multiple times,
this remove function needs to be called only once.
:param func: the callback
"""
for _trigger, listeners in self._callbacks.items():
while True:
try:
listeners.remove(func)
except ValueError:
break

def join_multicast(self, universe: int) -> None:
"""
Joins the multicast address that is used for the given universe. Note: If you are on Windows you must have given
Expand Down
34 changes: 34 additions & 0 deletions sacn/receiver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,40 @@ def callback_packet(packet):
assert called


def test_remove_listener():
receiver, socket = get_receiver()

packetSend = DataPacket(
cid=tuple(range(0, 16)),
sourceName='Test',
universe=1,
dmxData=tuple(range(0, 16))
)

called = 0

def callback_packet(packet):
assert packetSend.__dict__ == packet.__dict__
nonlocal called
called += 1

# register listener multiple times
receiver.register_listener('universe', callback_packet, universe=packetSend.universe)
receiver.register_listener('universe', callback_packet, universe=packetSend.universe)

socket.call_on_data(bytes(packetSend.getBytes()), 0)
assert called == 2

# change DMX data to trigger a change
packetSend.dmxData = tuple(range(16, 32))
packetSend.sequence_increase()

receiver.remove_listener(callback_packet)

socket.call_on_data(bytes(packetSend.getBytes()), 0)
assert called == 2


def test_invalid_listener():
receiver, socket = get_receiver()

Expand Down

0 comments on commit 4f0616d

Please sign in to comment.