Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/about/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Unreleased
fragment to conditionally render an element by writing
``something if condition else html._()``. Now you can simply write
``something if condition else None``.
- :pull:`1210` - Move hooks in `reactpy.backend.core` into `reactpy.core.hooks`.

**Deprecated**

Expand Down
4 changes: 3 additions & 1 deletion src/py/reactpy/reactpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from reactpy import backend, config, html, logging, sample, svg, types, web, widgets
from reactpy.backend.hooks import use_connection, use_location, use_scope
from reactpy.backend.utils import run
from reactpy.core import hooks
from reactpy.core.component import component
from reactpy.core.events import event
from reactpy.core.hooks import (
create_context,
use_callback,
use_connection,
use_context,
use_debug_value,
use_effect,
use_location,
use_memo,
use_reducer,
use_ref,
use_scope,
use_state,
)
from reactpy.core.layout import Layout
Expand Down
15 changes: 9 additions & 6 deletions src/py/reactpy/reactpy/backend/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
safe_client_build_dir_path,
safe_web_modules_dir_path,
)
from reactpy.backend.hooks import ConnectionContext
from reactpy.backend.hooks import use_connection as _use_connection
from reactpy.core.hooks import ConnectionContext
from reactpy.core.hooks import use_connection as _use_connection
from reactpy.backend.types import Connection, Location
from reactpy.core.serve import serve_layout
from reactpy.core.types import ComponentType, RootComponentConstructor
Expand Down Expand Up @@ -70,7 +70,8 @@ def configure(
"""
options = options or Options()

api_bp = Blueprint(f"reactpy_api_{id(app)}", __name__, url_prefix=str(PATH_PREFIX))
api_bp = Blueprint(f"reactpy_api_{id(app)}",
__name__, url_prefix=str(PATH_PREFIX))
spa_bp = Blueprint(
f"reactpy_spa_{id(app)}", __name__, url_prefix=options.url_prefix
)
Expand Down Expand Up @@ -192,14 +193,15 @@ def recv() -> Any:
_dispatch_in_thread(
ws,
# remove any url prefix from path
path[len(options.url_prefix) :],
path[len(options.url_prefix):],
constructor(),
send,
recv,
)

sock.route(STREAM_PATH.name, endpoint="without_path")(model_stream)
sock.route(f"{STREAM_PATH.name}/<path:path>", endpoint="with_path")(model_stream)
sock.route(f"{STREAM_PATH.name}/<path:path>",
endpoint="with_path")(model_stream)


def _dispatch_in_thread(
Expand Down Expand Up @@ -260,7 +262,8 @@ async def main() -> None:
Thread(target=run_dispatcher, daemon=True).start()

dispatch_thread_info_created.wait()
dispatch_thread_info = cast(_DispatcherThreadInfo, dispatch_thread_info_ref.current)
dispatch_thread_info = cast(
_DispatcherThreadInfo, dispatch_thread_info_ref.current)

if dispatch_thread_info is None:
raise RuntimeError("Failed to create dispatcher thread") # nocov
Expand Down
30 changes: 0 additions & 30 deletions src/py/reactpy/reactpy/backend/hooks.py

This file was deleted.

4 changes: 2 additions & 2 deletions src/py/reactpy/reactpy/backend/sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
safe_web_modules_dir_path,
serve_with_uvicorn,
)
from reactpy.backend.hooks import ConnectionContext
from reactpy.backend.hooks import use_connection as _use_connection
from reactpy.core.hooks import ConnectionContext
from reactpy.core.hooks import use_connection as _use_connection
from reactpy.backend.types import Connection, Location
from reactpy.core.layout import Layout
from reactpy.core.serve import RecvCoroutine, SendCoroutine, Stop, serve_layout
Expand Down
4 changes: 2 additions & 2 deletions src/py/reactpy/reactpy/backend/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
read_client_index_html,
serve_with_uvicorn,
)
from reactpy.backend.hooks import ConnectionContext
from reactpy.backend.hooks import use_connection as _use_connection
from reactpy.core.hooks import ConnectionContext
from reactpy.core.hooks import use_connection as _use_connection
from reactpy.backend.types import Connection, Location
from reactpy.config import REACTPY_WEB_MODULES_DIR
from reactpy.core.layout import Layout
Expand Down
4 changes: 2 additions & 2 deletions src/py/reactpy/reactpy/backend/tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
CommonOptions,
read_client_index_html,
)
from reactpy.backend.hooks import ConnectionContext
from reactpy.backend.hooks import use_connection as _use_connection
from reactpy.core.hooks import ConnectionContext
from reactpy.core.hooks import use_connection as _use_connection
from reactpy.backend.types import Connection, Location
from reactpy.config import REACTPY_WEB_MODULES_DIR
from reactpy.core.layout import Layout
Expand Down
25 changes: 25 additions & 0 deletions src/py/reactpy/reactpy/core/hooks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from __future__ import annotations
from collections.abc import MutableMapping
from reactpy.backend.types import Connection, Location

import asyncio
from collections.abc import Coroutine, Sequence
Expand Down Expand Up @@ -227,6 +229,19 @@ def context(
return context


# backend implementations should establish this context at the root of an app
ConnectionContext: Context[Connection[Any] | None] = create_context(None)


def use_connection() -> Connection[Any]:
"""Get the current :class:`~reactpy.backend.types.Connection`."""
conn = use_context(ConnectionContext)
if conn is None: # nocov
msg = "No backend established a connection."
raise RuntimeError(msg)
return conn


def use_context(context: Context[_Type]) -> _Type:
"""Get the current value for the given context type.

Expand All @@ -248,6 +263,16 @@ def use_context(context: Context[_Type]) -> _Type:
return provider.value


def use_scope() -> MutableMapping[str, Any]:
"""Get the current :class:`~reactpy.backend.types.Connection`'s scope."""
return use_connection().scope


def use_location() -> Location:
"""Get the current :class:`~reactpy.backend.types.Connection`'s location."""
return use_connection().location


class _ContextProvider(Generic[_Type]):
def __init__(
self,
Expand Down