Skip to content

Commit 94de9cf

Browse files
committed
Overload unmap to accept handlers object or function
1 parent 037dd4e commit 94de9cf

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

pythonosc/dispatcher.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import re
55
import time
66
from pythonosc import osc_packet
7+
from typing import overload
8+
from types import FunctionType
79

810
class Handler(object):
911
def __init__(self, _callback, _args, _needs_reply_address=False):
@@ -58,7 +60,18 @@ def map(self, address, handler, *args, needs_reply_address=False):
5860
self._map[address].append(handlerobj)
5961
return handlerobj
6062

61-
def unmap(self, address, handler, *args, needs_reply_address=False):
63+
@overload
64+
def unmap(self, address: str, handler: Handler):
65+
"""Remove an already mapped handler from an address
66+
67+
Args:
68+
- address: An explicit endpoint.
69+
- handler: A Handler object as returned from map().
70+
"""
71+
pass
72+
73+
@overload
74+
def unmap(self, address: str, handler: FunctionType, *args, needs_reply_address: bool=False):
6275
"""Remove an already mapped handler from an address
6376
6477
Args:
@@ -68,8 +81,15 @@ def unmap(self, address, handler, *args, needs_reply_address=False):
6881
- args: Any additional arguments that will be always passed to the
6982
handlers after the osc messages arguments if any.
7083
- needs_reply_address: True if the handler function needs the
71-
originating client address passed (as the first argument)."""
72-
self._map[address].remove(Handler(handler, list(args), needs_reply_address))
84+
originating client address passed (as the first argument).
85+
"""
86+
pass
87+
88+
def unmap(self, address, handler, *args, needs_reply_address=False):
89+
if isinstance(handler, Handler):
90+
self._map[address].remove(handler)
91+
else:
92+
self._map[address].remove(Handler(handler, list(args), needs_reply_address))
7393

7494
def handlers_for_address(self, address_pattern):
7595
"""yields Handler namedtuples matching the given OSC pattern."""

pythonosc/test/test_dispatcher.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,17 @@ def test_unmap(self):
125125
def dummyhandler():
126126
pass
127127

128-
self.dispatcher.map("/map/me", dummyhandler)
128+
# Test with handler returned by map
129+
returnedhandler = self.dispatcher.map("/map/me", dummyhandler)
129130
self.sortAndAssertSequenceEqual([Handler(dummyhandler, [])], self.dispatcher.handlers_for_address("/map/me"))
130-
self.dispatcher.unmap("/map/me", dummyhandler)
131+
self.dispatcher.unmap("/map/me", returnedhandler)
131132
self.sortAndAssertSequenceEqual([], self.dispatcher.handlers_for_address("/map/me"))
132133

134+
# Test with reconstructing handler
135+
self.dispatcher.map("/map/me/too", dummyhandler)
136+
self.sortAndAssertSequenceEqual([Handler(dummyhandler, [])], self.dispatcher.handlers_for_address("/map/me/too"))
137+
self.dispatcher.unmap("/map/me/too", dummyhandler)
138+
self.sortAndAssertSequenceEqual([], self.dispatcher.handlers_for_address("/map/me/too"))
139+
133140
if __name__ == "__main__":
134141
unittest.main()

0 commit comments

Comments
 (0)