@@ -17,28 +17,19 @@ async def __call__(self, stop: Event) -> None: ...
1717
1818
1919logger = 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
2823def 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
3930def 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
0 commit comments