Skip to content

hololinked.client.abstractions.SSE dataclass

Dataclass representing a server sent event and the data used to invoke event callbacks.

Source code in repo/hololinked/hololinked/client/abstractions.py
@dataclass
class SSE:
    """Dataclass representing a server sent event and the data used to invoke event callbacks."""

    event: str
    """event name, defaults to message"""
    data: Any
    """event data, defaults to empty string"""
    id: str | int | None
    """unique event id, defaults to None"""
    retry: int | None
    """reconnection time in milliseconds, defaults to None, currently unused."""

    __slots__ = ("data", "event", "id", "retry")

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

    def clear(self) -> None:
        """Reset to default/empty values."""
        self.event = "message"
        self.data = ""
        self.id = None
        self.retry = None

    def flush(self) -> dict[str, Any] | None:
        """
        Obtain the event payload as dictionary and reset to default values.

        Returns
        -------
        dict[str, Any] | None
            dictionary with keys - `event`, `data`, `id`, `retry` if event has data or id, None otherwise
        """
        if not self.data and self.id is None:
            return None
        payload = {
            "event": self.event,
            "data": self.data,
            "id": self.id,
            "retry": self.retry,
        }
        # reset to default for next event
        self.clear()
        return payload

Attributes

data instance-attribute

data: Any

event data, defaults to empty string

event instance-attribute

event: str

event name, defaults to message

id instance-attribute

id: str | int | None

unique event id, defaults to None

retry instance-attribute

retry: int | None

reconnection time in milliseconds, defaults to None, currently unused.

Functions

clear

clear() -> None

Reset to default/empty values.

Source code in repo/hololinked/hololinked/client/abstractions.py
def clear(self) -> None:
    """Reset to default/empty values."""
    self.event = "message"
    self.data = ""
    self.id = None
    self.retry = None

flush

flush() -> dict[str, Any] | None

Obtain the event payload as dictionary and reset to default values.

Returns:

Type Description
dict[str, Any] | None

dictionary with keys - event, data, id, retry if event has data or id, None otherwise

Source code in repo/hololinked/hololinked/client/abstractions.py
def flush(self) -> dict[str, Any] | None:
    """
    Obtain the event payload as dictionary and reset to default values.

    Returns
    -------
    dict[str, Any] | None
        dictionary with keys - `event`, `data`, `id`, `retry` if event has data or id, None otherwise
    """
    if not self.data and self.id is None:
        return None
    payload = {
        "event": self.event,
        "data": self.data,
        "id": self.id,
        "retry": self.retry,
    }
    # reset to default for next event
    self.clear()
    return payload