-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71bd6ba
commit be1c16e
Showing
6 changed files
with
96 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from .events import EVENT_SERVICE, Event, EventService, on_event | ||
from .hooks import HOOK_SERVICE, Hook | ||
|
||
__all__ = [ | ||
"EVENT_SERVICE", | ||
"Event", | ||
"on_event", | ||
"HOOK_SERVICE", | ||
"Hook", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import collections | ||
import enum | ||
import logging | ||
import typing as t | ||
|
||
logger = logging.getLogger("viur.shop").getChild(__name__) | ||
|
||
|
||
class Event(enum.IntEnum): | ||
ORDER_STARTED = enum.auto() | ||
ORDER_ORDERED = enum.auto() | ||
ORDER_PAID = enum.auto() | ||
|
||
|
||
class EventService: | ||
observer: t.Final[dict[[Event, list[t.Callable]]]] = collections.defaultdict(list) | ||
|
||
def register(self, event: Event, func: t.Callable) -> t.Callable: | ||
if not isinstance(event, Event): | ||
raise TypeError(f"event must be of type Event") | ||
EventService.observer[event].append(func) | ||
return func | ||
|
||
def unregister(self, func: t.Callable, event: Event = None) -> None: | ||
for event_, funcs in EventService.observer: | ||
if event is None or event_ == Event: | ||
try: | ||
funcs.pop(func, ) | ||
except ValueError: | ||
pass | ||
|
||
def call( | ||
self, | ||
_event: Event, | ||
_raise_errors: bool = False, | ||
*args, **kwargs | ||
) -> None: | ||
for func in EventService.observer[_event]: | ||
try: | ||
func(*args, **kwargs) | ||
except Exception as e: | ||
logger.exception(f"Error while calling {func} at event {_event}: {e}") | ||
if _raise_errors: | ||
raise e | ||
|
||
|
||
EVENT_SERVICE = EventService() | ||
|
||
|
||
def on_event(event: Event) -> t.Callable: | ||
if not isinstance(event, Event): | ||
raise TypeError | ||
|
||
def outer_wrapper(func: t.Callable) -> t.Callable: | ||
EVENT_SERVICE.register(event, func) | ||
return func | ||
|
||
return outer_wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters