Skip to content

Commit fe8d5be

Browse files
committed
Document add/remove listener
1 parent eced984 commit fe8d5be

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

twitchio/client.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,27 @@ async def save_tokens(self, path: str | None = None, /) -> None:
719719
await self._http.save(path)
720720

721721
def add_listener(self, listener: Callable[..., Coroutine[Any, Any, None]], *, event: str | None = None) -> None:
722-
# TODO: Docs...
722+
"""Method to add an event listener to the client.
723+
724+
See: :meth:`.listen` for more information on event listeners and for a decorator version of this function.
725+
726+
Parameters
727+
----------
728+
listener: Callable[..., Coroutine[Any, Any, None]]
729+
The coroutine to assign as the callback for the listener.
730+
event: str | None
731+
An optional :class:`str` which indicates which event to listen to. This should include the ``event_`` prefix.
732+
Defaults to ``None`` which uses the coroutine function name passed instead.
733+
734+
Raises
735+
------
736+
ValueError
737+
The ``event`` string passed should start with ``event_``.
738+
ValueError
739+
The ``event`` string passed must not == ``event_``.
740+
TypeError
741+
The listener callback must be a coroutine function.
742+
"""
723743
name: str = event or listener.__name__
724744

725745
if not name.startswith("event_"):
@@ -729,15 +749,32 @@ def add_listener(self, listener: Callable[..., Coroutine[Any, Any, None]], *, ev
729749
raise ValueError('Listener and event names cannot be named "event_".')
730750

731751
if not asyncio.iscoroutinefunction(listener):
732-
raise ValueError("Listeners and Events must be coroutines.")
752+
raise TypeError("Listeners and Events must be coroutines.")
733753

734754
self._listeners[name].add(listener)
735755

736-
def remove_listener(self, listener: Callable[..., Coroutine[Any, Any, None]]) -> None:
737-
# TODO: Docs...
756+
def remove_listener(
757+
self,
758+
listener: Callable[..., Coroutine[Any, Any, None]],
759+
) -> Callable[..., Coroutine[Any, Any, None]] | None:
760+
"""Method to remove a currently registered listener from the client.
761+
762+
Parameters
763+
----------
764+
listener: Callable[..., Coroutine[Any, Any, None]]
765+
The coroutine wrapped with :meth:`.listen` or added via :meth:`.add_listener` to remove as a listener.
766+
767+
Returns
768+
-------
769+
Callable[..., Coroutine[Any, Any, None]]
770+
If a listener was removed, the coroutine function will be returned.
771+
None
772+
Returns ``None`` when no listener was removed.
773+
"""
738774
for listeners in self._listeners.values():
739775
if listener in listeners:
740776
listeners.remove(listener)
777+
return listener
741778

742779
def listen(self, name: str | None = None) -> Any:
743780
"""|deco|

0 commit comments

Comments
 (0)