Skip to content

Commit

Permalink
subscribe method now can be used like decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
trabem committed Jul 9, 2023
1 parent 501352c commit b884ae3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,4 +17,4 @@ pre-commit:

.PHONY: format
format:
pre-commit run --all-files
pre-commit run --all-files
34 changes: 24 additions & 10 deletions event_dispatcher/_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def main():

dispatcher = event_dispatcher.SyncEventDispatcher()

@dispatcher.subscribe_decorator(event_name)
@dispatcher.subscribe(event_name)
def _callback(data):
print(data)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <ruslan.saiko.dev@gmail.com>"]
repository = "https://github.com/trabem/event-dispatcher"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b884ae3

Please sign in to comment.