Skip to content

Commit 3724c3b

Browse files
authored
Add sugar for remove_elements (#971)
Allow calling patch_elements without elements in remove mode
1 parent a68eff6 commit 3724c3b

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/datastar_py/sse.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44
from collections.abc import AsyncIterable, Iterable
55
from itertools import chain
6-
from typing import Protocol, TypeAlias, Union, runtime_checkable
6+
from typing import Literal, Protocol, TypeAlias, Union, overload, runtime_checkable
77

88
import datastar_py.consts as consts
99

@@ -59,12 +59,34 @@ def _send(
5959

6060
return DatastarEvent("\n".join(chain(prefix, data_lines)) + "\n\n")
6161

62+
@overload
63+
@classmethod
64+
def patch_elements(
65+
cls,
66+
*,
67+
selector: str,
68+
mode: Literal[consts.ElementPatchMode.REMOVE],
69+
use_view_transitions: bool | None = None,
70+
event_id: str | None = None,
71+
retry_duration: int | None = None,
72+
) -> DatastarEvent: ...
73+
@overload
6274
@classmethod
6375
def patch_elements(
6476
cls,
6577
elements: str | _HtmlProvider,
6678
selector: str | None = None,
6779
mode: consts.ElementPatchMode | None = None,
80+
use_view_transitions: bool | None = None,
81+
event_id: str | None = None,
82+
retry_duration: int | None = None,
83+
) -> DatastarEvent: ...
84+
@classmethod
85+
def patch_elements(
86+
cls,
87+
elements: str | _HtmlProvider | None = None,
88+
selector: str | None = None,
89+
mode: consts.ElementPatchMode | None = None,
6890
use_view_transition: bool | None = None,
6991
event_id: str | None = None,
7092
retry_duration: int | None = None,
@@ -84,7 +106,10 @@ def patch_elements(
84106
f"{consts.USE_VIEW_TRANSITION_DATALINE_LITERAL} {_js_bool(use_view_transition)}"
85107
)
86108

87-
data_lines.extend(f"{consts.ELEMENTS_DATALINE_LITERAL} {x}" for x in elements.splitlines())
109+
if elements:
110+
data_lines.extend(
111+
f"{consts.ELEMENTS_DATALINE_LITERAL} {x}" for x in elements.splitlines()
112+
)
88113

89114
return ServerSentEventGenerator._send(
90115
consts.EventType.PATCH_ELEMENTS,
@@ -93,6 +118,17 @@ def patch_elements(
93118
retry_duration,
94119
)
95120

121+
@classmethod
122+
def remove_elements(
123+
cls, selector: str, event_id: str | None = None, retry_duration: int | None = None
124+
) -> DatastarEvent:
125+
return ServerSentEventGenerator.patch_elements(
126+
selector=selector,
127+
mode=consts.ElementPatchMode.REMOVE,
128+
event_id=event_id,
129+
retry_duration=retry_duration,
130+
)
131+
96132
@classmethod
97133
def patch_signals(
98134
cls,

0 commit comments

Comments
 (0)