Skip to content

Commit 8c68d4a

Browse files
committed
Pyscript now supports ContextVars!
1 parent a13d21a commit 8c68d4a

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/reactpy/core/_life_cycle_hook.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import logging
4-
import sys
54
from asyncio import Event, Task, create_task, gather
65
from collections.abc import Callable
76
from contextvars import ContextVar, Token
@@ -28,9 +27,7 @@ class _HookStack(Singleton): # nocov
2827
Life cycle hooks can be stored in a thread local or context variable depending
2928
on the platform."""
3029

31-
_state: ThreadLocal[list[LifeCycleHook]] | ContextVar[list[LifeCycleHook]] = (
32-
ThreadLocal(list) if sys.platform == "emscripten" else ContextVar("hook_state")
33-
)
30+
_state: ContextVar[list[LifeCycleHook]] = ContextVar("hook_state")
3431

3532
def get(self) -> list[LifeCycleHook]:
3633
try:
@@ -268,5 +265,14 @@ def set_current(self) -> None:
268265

269266
def unset_current(self) -> None:
270267
"""Unset this hook as the active hook in this thread"""
271-
if HOOK_STACK.get().pop() is not self:
272-
raise RuntimeError("Hook stack is in an invalid state") # nocov
268+
hook_stack = HOOK_STACK.get()
269+
if not hook_stack:
270+
raise RuntimeError( # nocov
271+
"Attempting to unset current life cycle hook but it no longer exists!\n"
272+
"A separate process or thread may have deleted this component's hook stack!"
273+
)
274+
if hook_stack and hook_stack.pop() is not self:
275+
raise RuntimeError( # nocov
276+
"Hook stack is in an invalid state\n"
277+
"A separate process or thread may have modified this component's hook stack!"
278+
)

0 commit comments

Comments
 (0)