|
4 | 4 | from htmltools import css
|
5 | 5 |
|
6 | 6 | from .. import _utils, reactive
|
| 7 | +from .._deprecated import warn_deprecated |
7 | 8 | from .._docstring import add_example
|
8 | 9 | from .._namespaces import resolve_id
|
9 | 10 | from .._typing_extensions import TypedDict
|
@@ -87,9 +88,14 @@ def __init__(
|
87 | 88 | self.on_error = on_error
|
88 | 89 |
|
89 | 90 | with session_context(self._session):
|
90 |
| - self._latest_stream: reactive.Value[ |
91 |
| - Union[reactive.ExtendedTask[[], str], None] |
92 |
| - ] = reactive.Value(None) |
| 91 | + |
| 92 | + @reactive.extended_task |
| 93 | + async def _mock_task() -> str: |
| 94 | + return "" |
| 95 | + |
| 96 | + self._latest_stream: reactive.Value[reactive.ExtendedTask[[], str]] = ( |
| 97 | + reactive.Value(_mock_task) |
| 98 | + ) |
93 | 99 |
|
94 | 100 | async def stream(
|
95 | 101 | self,
|
@@ -151,32 +157,46 @@ async def _handle_error():
|
151 | 157 |
|
152 | 158 | return _task
|
153 | 159 |
|
154 |
| - def get_latest_stream_result(self) -> Union[str, None]: |
| 160 | + @property |
| 161 | + def latest_stream(self): |
155 | 162 | """
|
156 |
| - Reactively read the latest stream result. |
| 163 | + React to changes in the latest stream. |
| 164 | +
|
| 165 | + Reactively reads for the :class:`~shiny.reactive.ExtendedTask` behind the |
| 166 | + latest stream. |
157 | 167 |
|
158 |
| - This method reads a reactive value containing the result of the latest |
159 |
| - `.stream()`. Therefore, this method must be called in a reactive context (e.g., |
160 |
| - a render function, a :func:`~shiny.reactive.calc`, or a |
161 |
| - :func:`~shiny.reactive.effect`). |
| 168 | + From the return value (i.e., the extended task), you can then: |
| 169 | +
|
| 170 | + 1. Reactively read for the final `.result()`. |
| 171 | + 2. `.cancel()` the stream. |
| 172 | + 3. Check the `.status()` of the stream. |
162 | 173 |
|
163 | 174 | Returns
|
164 | 175 | -------
|
165 | 176 | :
|
166 |
| - The result of the latest stream (a string). |
| 177 | + An extended task that represents the streaming task. The `.result()` method |
| 178 | + of the task can be called in a reactive context to get the final state of the |
| 179 | + stream. |
167 | 180 |
|
168 |
| - Raises |
169 |
| - ------ |
170 |
| - : |
171 |
| - A silent exception if no stream has completed yet. |
| 181 | + Note |
| 182 | + ---- |
| 183 | + If no stream has yet been started when this method is called, then it returns an |
| 184 | + extended task with `.status()` of `"initial"` and that it status doesn't change |
| 185 | + state until a message is streamed. |
172 | 186 | """
|
173 |
| - stream = self._latest_stream() |
174 |
| - if stream is None: |
175 |
| - from .. import req |
| 187 | + return self._latest_stream() |
176 | 188 |
|
177 |
| - req(False) |
178 |
| - else: |
179 |
| - return stream.result() |
| 189 | + def get_latest_stream_result(self) -> Union[str, None]: |
| 190 | + """ |
| 191 | + Reactively read the latest stream result. |
| 192 | +
|
| 193 | + Deprecated. Use `latest_stream.result()` instead. |
| 194 | + """ |
| 195 | + warn_deprecated( |
| 196 | + "The `.get_latest_stream_result()` method is deprecated and will be removed " |
| 197 | + "in a future release. Use `.latest_stream.result()` instead. " |
| 198 | + ) |
| 199 | + self.latest_stream.result() |
180 | 200 |
|
181 | 201 | async def clear(self):
|
182 | 202 | """
|
|
0 commit comments