Skip to content

Commit 7a36362

Browse files
committed
Support annotations, keep task references
1 parent de0e32b commit 7a36362

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

reacttrs/reactive.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# this is a partial copy/paste from:
22
# https://github.com/Textualize/textual/blob/373fc95fc1a5650fd864053e4153eb2d6b38f087/src/textual/reactive.py
33

4+
from __future__ import annotations
45
import asyncio
56
from inspect import isawaitable
67
from typing import Any, Awaitable, Callable, ClassVar, Generic, TypeVar
@@ -10,6 +11,7 @@
1011

1112
Reactable = Any
1213
ReactiveType = TypeVar("ReactiveType")
14+
background_tasks = set()
1315

1416

1517
class Reactive(Generic[ReactiveType]):
@@ -173,7 +175,9 @@ def invoke_watcher(
173175
watch_result = watch_function()
174176
if isawaitable(watch_result):
175177
# Result is awaitable, so we need to await it within an async context
176-
asyncio.create_task(await_watcher(watch_result))
178+
task = asyncio.create_task(await_watcher(watch_result))
179+
background_tasks.add(task)
180+
task.add_done_callback(background_tasks.discard)
177181

178182
watch_function = getattr(obj, f"watch_{name}", None)
179183
if callable(watch_function):

0 commit comments

Comments
 (0)