From b884ae349d9ffafc19f94128b7e94a63f93209d8 Mon Sep 17 00:00:00 2001 From: Ruslan Saiko Date: Sun, 9 Jul 2023 18:46:19 +0300 Subject: [PATCH] subscribe method now can be used like decorator --- Makefile | 13 +++---- event_dispatcher/_dispatcher.py | 34 +++++++++++++------ ...tor.py => using_subscribe_as_decorator.py} | 2 +- pyproject.toml | 2 +- tests/test_dispatcher.py | 2 +- 5 files changed, 32 insertions(+), 21 deletions(-) rename examples/{using_subscribe_decorator.py => using_subscribe_as_decorator.py} (84%) diff --git a/Makefile b/Makefile index 12a2f05..231df61 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,14 @@ -.PHONY: unittests -unittests: - coverage run -m pytest - .PHONY: tests -tests: unittests +tests: + coverage run -m pytest coverage report .PHONY: cov-html -cov-html: unittests +cov-html: tests coverage html .PHONY: cov-xml -cov-xml: unittests +cov-xml: tests coverage xml .PHONY: pre-commit @@ -20,4 +17,4 @@ pre-commit: .PHONY: format format: - pre-commit run --all-files \ No newline at end of file + pre-commit run --all-files diff --git a/event_dispatcher/_dispatcher.py b/event_dispatcher/_dispatcher.py index e58e05f..de9948b 100644 --- a/event_dispatcher/_dispatcher.py +++ b/event_dispatcher/_dispatcher.py @@ -7,7 +7,7 @@ import collections from abc import ABC, abstractmethod -from typing import Callable, Generic, List, Optional, TypeVar +from typing import Callable, Generic, List, Optional, TypeVar, Union, overload from event_dispatcher import types @@ -18,17 +18,31 @@ class BaseEventDispatcher(ABC, Generic[_CallbackT]): def __init__(self): self._subscribers = collections.defaultdict(list) - def subscribe(self, event_name: str, callback: _CallbackT) -> None: - self._subscribers[event_name].append(callback) - - def subscribe_decorator( + @overload + def subscribe( self, event_name: str - ) -> Callable[[_CallbackT], _CallbackT]: - def decorator(callback: _CallbackT) -> _CallbackT: - self.subscribe(event_name, callback) - return callback + ) -> Callable[[_CallbackT], _CallbackT]: # pragma: no cover + """decorated function/method will be added to subscribers for event `event_name`""" + pass + + @overload + def subscribe( + self, event_name: str, callback: _CallbackT + ) -> None: # pragma: no cover + pass + + def subscribe( + self, event_name: str, callback: Optional[_CallbackT] = None + ) -> Union[None, Callable[[_CallbackT], _CallbackT]]: + if callback: # when subscribe used like method + self._subscribers[event_name].append(callback) + else: # when subscribe used like decorator + + def decorator(_callback: _CallbackT) -> _CallbackT: + self.subscribe(event_name, _callback) + return _callback - return decorator + return decorator def subscribers_count(self, event_name: str) -> int: return len(self._subscribers[event_name]) diff --git a/examples/using_subscribe_decorator.py b/examples/using_subscribe_as_decorator.py similarity index 84% rename from examples/using_subscribe_decorator.py rename to examples/using_subscribe_as_decorator.py index 43c6625..9becf50 100644 --- a/examples/using_subscribe_decorator.py +++ b/examples/using_subscribe_as_decorator.py @@ -6,7 +6,7 @@ def main(): dispatcher = event_dispatcher.SyncEventDispatcher() - @dispatcher.subscribe_decorator(event_name) + @dispatcher.subscribe(event_name) def _callback(data): print(data) diff --git a/pyproject.toml b/pyproject.toml index edd0874..2c3e592 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "event-dispatching" -version = "0.1.5" +version = "0.2.0" description = "Pure python implementation of event dispatcher" authors = ["Ruslan Saiko "] repository = "https://github.com/trabem/event-dispatcher" diff --git a/tests/test_dispatcher.py b/tests/test_dispatcher.py index 5fef01a..1a01ab7 100644 --- a/tests/test_dispatcher.py +++ b/tests/test_dispatcher.py @@ -24,7 +24,7 @@ def test_subscribe_to_event_decorator(): sut = SyncEventDispatcher() # act - sut.subscribe_decorator(event_name)(callback_mock) + sut.subscribe(event_name)(callback_mock) # assert assert sut.subscribers_count(event_name) == 1