26 lines
525 B
Python
26 lines
525 B
Python
from backend.events.schema import create_event
|
|
from backend.events.clock import GlobalClock
|
|
|
|
|
|
class EventBus:
|
|
|
|
def __init__(self):
|
|
self.subscribers = []
|
|
|
|
def subscribe(self, fn):
|
|
self.subscribers.append(fn)
|
|
|
|
def emit(self, event_type, **kwargs):
|
|
|
|
event = create_event(
|
|
type=event_type,
|
|
timestamp=GlobalClock.tick(),
|
|
**kwargs
|
|
)
|
|
|
|
# fan-out to all listeners
|
|
for sub in self.subscribers:
|
|
sub(event)
|
|
|
|
return event
|