wrappers.py 909 B

123456789101112131415161718192021222324252627
  1. """Wrappers for the state manager."""
  2. from typing import Any
  3. from reflex.istate.proxy import ReadOnlyStateProxy
  4. from reflex.state import _split_substate_key, _substate_key, get_state_manager
  5. async def get_state(token: str, state_cls: Any | None = None) -> ReadOnlyStateProxy:
  6. """Get the instance of a state for a token.
  7. Args:
  8. token: The token for the state.
  9. state_cls: The class of the state.
  10. Returns:
  11. A read-only proxy of the state instance.
  12. """
  13. mng = get_state_manager()
  14. if state_cls is not None:
  15. root_state = await mng.get_state(_substate_key(token, state_cls))
  16. else:
  17. root_state = await mng.get_state(token)
  18. _, state_path = _split_substate_key(token)
  19. state_cls = root_state.get_class_substate(tuple(state_path.split(".")))
  20. instance = await root_state.get_state(state_cls)
  21. return ReadOnlyStateProxy(instance)