Skip to content

Commit d490009

Browse files
committed
Refactor and fix type hints
1 parent 833a1c6 commit d490009

File tree

3 files changed

+8
-23
lines changed

3 files changed

+8
-23
lines changed

pyproject.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ flask = ["flask", "markupsafe>=1.1.1,<2.1", "flask-cors", "flask-sock"]
8989
tornado = ["tornado"]
9090
testing = ["playwright"]
9191

92-
9392
#############################
9493
# >>> Hatch Test Runner <<< #
9594
#############################
@@ -134,11 +133,6 @@ xfail_strict = true
134133
asyncio_mode = "auto"
135134
log_cli_level = "INFO"
136135

137-
[tool.hatch.envs.default.scripts]
138-
test-cov = "playwright install && coverage run -m pytest {args:tests}"
139-
cov-report = ["coverage report"]
140-
cov = ["test-cov {args}", "cov-report"]
141-
142136
[tool.hatch.envs.default.env-vars]
143137
REACTPY_DEBUG_MODE = "1"
144138

src/reactpy/core/_life_cycle_hook.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,19 @@ async def __call__(self, stop: Event) -> None: ...
1717

1818

1919
logger = logging.getLogger(__name__)
20-
21-
_hook_state = ContextVar("_hook_state")
22-
23-
24-
def create_hook_state(initial: list | None = None) -> Token[list]:
25-
return _hook_state.set(initial or [])
20+
_HOOK_STATE: ContextVar[list[LifeCycleHook]] = ContextVar("_hook_state")
2621

2722

2823
def clear_hook_state(token: Token[list]) -> None:
29-
hook_stack = _hook_state.get()
24+
hook_stack = _HOOK_STATE.get()
3025
if hook_stack:
3126
logger.warning("clear_hook_state: Hook stack was not empty")
32-
_hook_state.reset(token)
33-
34-
35-
def get_hook_state() -> list[LifeCycleHook]:
36-
return _hook_state.get()
27+
_HOOK_STATE.reset(token)
3728

3829

3930
def current_hook() -> LifeCycleHook:
4031
"""Get the current :class:`LifeCycleHook`"""
41-
hook_stack = _hook_state.get()
32+
hook_stack = _HOOK_STATE.get()
4233
if not hook_stack:
4334
msg = "No life cycle hook is active. Are you rendering in a layout?"
4435
raise RuntimeError(msg)
@@ -247,13 +238,13 @@ def set_current(self) -> None:
247238
This method is called by a layout before entering the render method
248239
of this hook's associated component.
249240
"""
250-
hook_stack = get_hook_state()
241+
hook_stack = _HOOK_STATE.get()
251242
if hook_stack:
252243
parent = hook_stack[-1]
253244
self._context_providers.update(parent._context_providers)
254245
hook_stack.append(self)
255246

256247
def unset_current(self) -> None:
257248
"""Unset this hook as the active hook in this thread"""
258-
if get_hook_state().pop() is not self:
249+
if _HOOK_STATE.get().pop() is not self:
259250
raise RuntimeError("Hook stack is in an invalid state") # nocov

src/reactpy/core/serve.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from anyio.abc import TaskGroup
1010

1111
from reactpy.config import REACTPY_DEBUG_MODE
12-
from reactpy.core._life_cycle_hook import clear_hook_state, create_hook_state
12+
from reactpy.core._life_cycle_hook import _HOOK_STATE, clear_hook_state
1313
from reactpy.core.types import LayoutEventMessage, LayoutType, LayoutUpdateMessage
1414

1515
logger = getLogger(__name__)
@@ -59,7 +59,7 @@ async def _single_outgoing_loop(
5959
layout: LayoutType[LayoutUpdateMessage, LayoutEventMessage], send: SendCoroutine
6060
) -> None:
6161
while True:
62-
token = create_hook_state()
62+
token = _HOOK_STATE.set([])
6363
try:
6464
update = await layout.render()
6565
try:

0 commit comments

Comments
 (0)