Skeleton ready
This commit is contained in:
0
kernel/event/__init__.py
Normal file
0
kernel/event/__init__.py
Normal file
16
kernel/event/listener.py
Normal file
16
kernel/event/listener.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from typing import Generic, TypeVar
|
||||
from abc import ABC, abstractmethod
|
||||
from punq import Container
|
||||
|
||||
T = TypeVar('T')
|
||||
|
||||
|
||||
class Listener(ABC, Generic[T]):
|
||||
_container: Container
|
||||
|
||||
def __init__(self, container: Container) -> None:
|
||||
self._container = container
|
||||
|
||||
@abstractmethod
|
||||
async def handle(self, event: str, payload: T) -> None:
|
||||
pass
|
||||
25
kernel/event/registrar.py
Normal file
25
kernel/event/registrar.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from punq import Container
|
||||
from typing import TypeVar
|
||||
from pyee import EventEmitter
|
||||
|
||||
from kernel.event.listener import Listener
|
||||
|
||||
T = TypeVar('T')
|
||||
|
||||
|
||||
class Registrar:
|
||||
_container: Container
|
||||
|
||||
def __init__(self, container: Container) -> None:
|
||||
self._container = container
|
||||
|
||||
def add_event_listener(
|
||||
self,
|
||||
event: str,
|
||||
listener: type[Listener[T]]
|
||||
) -> None:
|
||||
async def callback(payload: T) -> None:
|
||||
listener_instance = listener(self._container)
|
||||
await listener_instance.handle(event, payload)
|
||||
|
||||
self._container.resolve(EventEmitter).on(event, callback)
|
||||
Reference in New Issue
Block a user