Skip to content

Commit 5b3edf0

Browse files
committed
Add unmap method to dispatcher
1 parent 5c30a50 commit 5b3edf0

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

pythonosc/dispatcher.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ def map(self, address, handler, *args, needs_reply_address=False):
5454
# regarding multiple mappings
5555
self._map[address].append(Handler(handler, list(args), needs_reply_address))
5656

57+
def unmap(self, address, handler, *args, needs_reply_address=False):
58+
"""Remove an already mapped handler from an address
59+
60+
Args:
61+
- address: An explicit endpoint.
62+
- handler: A function that will be run when the address matches with
63+
the OscMessage passed as parameter.
64+
- args: Any additional arguments that will be always passed to the
65+
handlers after the osc messages arguments if any.
66+
- needs_reply_address: True if the handler function needs the
67+
originating client address passed (as the first argument)."""
68+
self._map[address].remove(Handler(handler, list(args), needs_reply_address))
69+
5770
def handlers_for_address(self, address_pattern):
5871
"""yields Handler namedtuples matching the given OSC pattern."""
5972
# First convert the address_pattern into a matchable regexp.

pythonosc/test/test_dispatcher.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,14 @@ def test_multiple_handlers_with_wildcard_map(self):
121121
self.sortAndAssertSequenceEqual(
122122
[Handler(1, []), Handler(2, [])], self.dispatcher.handlers_for_address("/foo/bar"))
123123

124+
def test_unmap(self):
125+
def dummyhandler():
126+
pass
127+
128+
self.dispatcher.map("/map/me", dummyhandler)
129+
self.sortAndAssertSequenceEqual([Handler(dummyhandler, [])], self.dispatcher.handlers_for_address("/map/me"))
130+
self.dispatcher.unmap("/map/me", dummyhandler)
131+
self.sortAndAssertSequenceEqual([], self.dispatcher.handlers_for_address("/map/me"))
132+
124133
if __name__ == "__main__":
125134
unittest.main()

0 commit comments

Comments
 (0)