from __future__ import annotations

from collections import defaultdict
from typing import Any, Callable


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

    def on(self, event_name: str, handler: Callable[..., Any]) -> None:
        self._handlers[event_name].append(handler)

    def off(self, event_name: str, handler: Callable[..., Any]) -> None:
        if handler in self._handlers.get(event_name, []):
            self._handlers[event_name].remove(handler)

    def emit(self, event_name: str, *args: Any, **kwargs: Any) -> list[Any]:
        return [handler(*args, **kwargs) for handler in list(self._handlers.get(event_name, []))]

    def clear(self) -> None:
        self._handlers.clear()
