diff --git a/sdk/python/src/datastar_py/sse.py b/sdk/python/src/datastar_py/sse.py index 121437304..37b4fe68c 100644 --- a/sdk/python/src/datastar_py/sse.py +++ b/sdk/python/src/datastar_py/sse.py @@ -3,7 +3,7 @@ import json from collections.abc import AsyncIterable, Iterable from itertools import chain -from typing import Protocol, TypeAlias, Union, runtime_checkable +from typing import Literal, Protocol, TypeAlias, Union, overload, runtime_checkable import datastar_py.consts as consts @@ -59,12 +59,34 @@ def _send( return DatastarEvent("\n".join(chain(prefix, data_lines)) + "\n\n") + @overload + @classmethod + def patch_elements( + cls, + *, + selector: str, + mode: Literal[consts.ElementPatchMode.REMOVE], + use_view_transitions: bool | None = None, + event_id: str | None = None, + retry_duration: int | None = None, + ) -> DatastarEvent: ... + @overload @classmethod def patch_elements( cls, elements: str | _HtmlProvider, selector: str | None = None, mode: consts.ElementPatchMode | None = None, + use_view_transitions: bool | None = None, + event_id: str | None = None, + retry_duration: int | None = None, + ) -> DatastarEvent: ... + @classmethod + def patch_elements( + cls, + elements: str | _HtmlProvider | None = None, + selector: str | None = None, + mode: consts.ElementPatchMode | None = None, use_view_transition: bool | None = None, event_id: str | None = None, retry_duration: int | None = None, @@ -84,7 +106,10 @@ def patch_elements( f"{consts.USE_VIEW_TRANSITION_DATALINE_LITERAL} {_js_bool(use_view_transition)}" ) - data_lines.extend(f"{consts.ELEMENTS_DATALINE_LITERAL} {x}" for x in elements.splitlines()) + if elements: + data_lines.extend( + f"{consts.ELEMENTS_DATALINE_LITERAL} {x}" for x in elements.splitlines() + ) return ServerSentEventGenerator._send( consts.EventType.PATCH_ELEMENTS, @@ -93,6 +118,17 @@ def patch_elements( retry_duration, ) + @classmethod + def remove_elements( + cls, selector: str, event_id: str | None = None, retry_duration: int | None = None + ) -> DatastarEvent: + return ServerSentEventGenerator.patch_elements( + selector=selector, + mode=consts.ElementPatchMode.REMOVE, + event_id=event_id, + retry_duration=retry_duration, + ) + @classmethod def patch_signals( cls,