Код IT
← Каталог

Python — диаблоид — `core/event_bus.py`

Фрагмент из «Python — диаблоид»: `core/event_bus.py`.

python spinoffencyclopedia9-04-razrabotka-igr-praktikum-razrabotki-igr-6 embed URL статья в энциклопедии
Python main.py
from collections import defaultdict
from typing import Any, Callable


class EventBus:
    def __init__(self) -> None:
        self._subs: dict[str, list[Callable[..., None]]] = defaultdict(list)

    def subscribe(self, event: str, callback: Callable[..., None]) -> None:
        if callback not in self._subs[event]:
            self._subs[event].append(callback)

    def emit(self, event: str, **payload: Any) -> None:
        for cb in list(self._subs[event]):
            cb(**payload)
from collections import defaultdict
from typing import Any, Callable


class EventBus:
    def __init__(self) -> None:
        self._subs: dict[str, list[Callable[..., None]]] = defaultdict(list)

    def subscribe(self, event: str, callback: Callable[..., None]) -> None:
        if callback not in self._subs[event]:
            self._subs[event].append(callback)

    def emit(self, event: str, **payload: Any) -> None:
        for cb in list(self._subs[event]):
            cb(**payload)