|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import MutableMapping |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from reactpy._warnings import warn |
| 7 | +from reactpy.backend.types import Connection, Location |
| 8 | +from reactpy.core.hooks import ConnectionContext, use_context |
| 9 | + |
| 10 | + |
| 11 | +def use_connection() -> Connection[Any]: |
| 12 | + """Get the current :class:`~reactpy.backend.types.Connection`.""" |
| 13 | + warn("The module reactpy.backend.hooks has been deprecated and will be deleted in the future. ", |
| 14 | + "Call use_connection in reactpy.core.hooks instead.", DeprecationWarning) |
| 15 | + |
| 16 | + conn = use_context(ConnectionContext) |
| 17 | + if conn is None: # nocov |
| 18 | + msg = "No backend established a connection." |
| 19 | + raise RuntimeError(msg) |
| 20 | + return conn |
| 21 | + |
| 22 | + |
| 23 | +def use_scope() -> MutableMapping[str, Any]: |
| 24 | + """Get the current :class:`~reactpy.backend.types.Connection`'s scope.""" |
| 25 | + warn("The module reactpy.backend.hooks has been deprecated and will be deleted in the future. ", |
| 26 | + "Call use_scope in reactpy.core.hooks instead.", DeprecationWarning) |
| 27 | + |
| 28 | + return use_connection().scope |
| 29 | + |
| 30 | + |
| 31 | +def use_location() -> Location: |
| 32 | + """Get the current :class:`~reactpy.backend.types.Connection`'s location.""" |
| 33 | + warn("The module reactpy.backend.hooks has been deprecated and will be deleted in the future. ", |
| 34 | + "Call use_location in reactpy.core.hooks instead.", DeprecationWarning) |
| 35 | + |
| 36 | + return use_connection().location |
0 commit comments