- F11 PopulateRule field_name/collection_name corrections - F12 service adaptor update handles PopulatingRepository - F13 nested-references section; F14 authless /users/me guidance - F15 template ruff-clean; F16 stop vendoring .ruff_cache, ship .gitignore - document --template-style in build_recipe/spec_first_routing; refresh collected openapi-first + standards bundle (12 modules, 43 components) - add trial.md and trial_findings.md (F1-F16, re-trial A/B evidence)
55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
"""# Summary
|
|
|
|
SSEInMemoryBus — in-memory event bus for SSE progress events.
|
|
|
|
---
|
|
|
|
Buffer published progress events so they can be replayed (and streamed) per
|
|
correlation key, such as a job, upload, or fetch-request id.
|
|
"""
|
|
from typing import Generic, TypeVar
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
class SSEInMemoryBus(Generic[T]):
|
|
"""Buffer SSE progress events in memory, keyed by an event attribute.
|
|
|
|
Attributes:
|
|
_events (List[T]): Events published since the last clear.
|
|
_key_attr (str): Event attribute used to correlate a stream.
|
|
"""
|
|
|
|
def __init__(self, key_attr: str = "id"):
|
|
"""Initialize an empty event buffer.
|
|
|
|
Args:
|
|
key_attr (str): Attribute name on each event holding the stream
|
|
correlation key (for example ``"job_id"``).
|
|
"""
|
|
self._events: list[T] = []
|
|
self._key_attr = key_attr
|
|
|
|
async def publish(self, event: T) -> None:
|
|
"""Append an event to the buffer.
|
|
|
|
Args:
|
|
event (T): Event to append.
|
|
"""
|
|
self._events.append(event)
|
|
|
|
def get_events(self, key: str) -> list[T]:
|
|
"""Return buffered events for a correlation key.
|
|
|
|
Args:
|
|
key (str): Value matched against the configured ``key_attr``.
|
|
|
|
Returns:
|
|
List[T]: Events whose ``key_attr`` equals ``key``.
|
|
"""
|
|
return [e for e in self._events if getattr(e, self._key_attr, None) == key]
|
|
|
|
def clear(self) -> None:
|
|
"""Remove all buffered events."""
|
|
self._events.clear()
|