Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger authored Oct 29, 2024
2 parents bf7236f + f70bccd commit b10be18
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 66 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/.hatch-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ jobs:
runs-on: ${{ fromJson(inputs.runs-on-array) }}
runs-on: ${{ matrix.runs-on }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "23.x"
registry-url: ${{ inputs.node-registry-url }}
- name: Pin NPM Version
run: npm install -g [email protected]
- name: Use Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install Python Dependencies
Expand Down
44 changes: 22 additions & 22 deletions .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
name: deploy-docs

on:
push:
branches:
- "main"
tags:
- "*"
push:
branches:
- "main"
tags:
- "*"

jobs:
deploy-documentation:
runs-on: ubuntu-latest
steps:
- name: Check out src from Git
uses: actions/checkout@v2
- name: Get history and tags for SCM versioning to work
run: |
git fetch --prune --unshallow
git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- name: Login to Heroku Container Registry
run: echo ${{ secrets.HEROKU_API_KEY }} | docker login -u ${{ secrets.HEROKU_EMAIL }} --password-stdin registry.heroku.com
- name: Build Docker Image
run: docker build . --file docs/Dockerfile --tag registry.heroku.com/${{ secrets.HEROKU_APP_NAME }}/web
- name: Push Docker Image
run: docker push registry.heroku.com/${{ secrets.HEROKU_APP_NAME }}/web
- name: Deploy
run: HEROKU_API_KEY=${{ secrets.HEROKU_API_KEY }} heroku container:release web --app ${{ secrets.HEROKU_APP_NAME }}
deploy-documentation:
runs-on: ubuntu-latest
steps:
- name: Check out src from Git
uses: actions/checkout@v4
- name: Get history and tags for SCM versioning to work
run: |
git fetch --prune --unshallow
git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- name: Login to Heroku Container Registry
run: echo ${{ secrets.HEROKU_API_KEY }} | docker login -u ${{ secrets.HEROKU_EMAIL }} --password-stdin registry.heroku.com
- name: Build Docker Image
run: docker build . --file docs/Dockerfile --tag registry.heroku.com/${{ secrets.HEROKU_APP_NAME }}/web
- name: Push Docker Image
run: docker push registry.heroku.com/${{ secrets.HEROKU_APP_NAME }}/web
- name: Deploy
run: HEROKU_API_KEY=${{ secrets.HEROKU_API_KEY }} heroku container:release web --app ${{ secrets.HEROKU_APP_NAME }}
24 changes: 12 additions & 12 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
name: publish

on:
release:
types: [published]
release:
types: [published]

jobs:
publish:
uses: ./.github/workflows/.hatch-run.yml
with:
job-name: "publish"
hatch-run: "publish"
node-registry-url: "https://registry.npmjs.org"
secrets:
node-auth-token: ${{ secrets.NODE_AUTH_TOKEN }}
pypi-username: ${{ secrets.PYPI_USERNAME }}
pypi-password: ${{ secrets.PYPI_PASSWORD }}
publish:
uses: ./.github/workflows/.hatch-run.yml
with:
job-name: "publish"
hatch-run: "publish"
node-registry-url: "https://registry.npmjs.org"
secrets:
node-auth-token: ${{ secrets.NODE_AUTH_TOKEN }}
pypi-username: ${{ secrets.PYPI_USERNAME }}
pypi-password: ${{ secrets.PYPI_PASSWORD }}
3 changes: 3 additions & 0 deletions docs/source/about/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ 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 from ``reactpy.backend.hooks`` into ``reactpy.core.hooks``.

**Deprecated**

- :pull:`1171` - The ``Stop`` exception. Recent releases of ``anyio`` have made this
exception difficult to use since it now raises an ``ExceptionGroup``. This exception
was primarily used for internal testing purposes and so is now deprecated.
- :pull:`1210` - Deprecate ``reactpy.backend.hooks`` since the hooks have been moved into
``reactpy.core.hooks``.


v1.0.2
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
4 changes: 2 additions & 2 deletions src/py/reactpy/reactpy/backend/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
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.backend.types import Connection, Location
from reactpy.core.hooks import ConnectionContext
from reactpy.core.hooks import use_connection as _use_connection
from reactpy.core.serve import serve_layout
from reactpy.core.types import ComponentType, RootComponentConstructor
from reactpy.utils import Ref
Expand Down
41 changes: 28 additions & 13 deletions src/py/reactpy/reactpy/backend/hooks.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
from __future__ import annotations
from __future__ import annotations # nocov

from collections.abc import MutableMapping
from typing import Any
from collections.abc import MutableMapping # nocov
from typing import Any # nocov

from reactpy.backend.types import Connection, Location
from reactpy.core.hooks import create_context, use_context
from reactpy.core.types import Context
from reactpy._warnings import warn # nocov
from reactpy.backend.types import Connection, Location # nocov
from reactpy.core.hooks import ConnectionContext, use_context # nocov

# 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]:
def use_connection() -> Connection[Any]: # nocov
"""Get the current :class:`~reactpy.backend.types.Connection`."""
warn(
"The module reactpy.backend.hooks has been deprecated and will be deleted in the future. ",
"Call reactpy.use_connection instead.",
DeprecationWarning,
)

conn = use_context(ConnectionContext)
if conn is None: # nocov
if conn is None:
msg = "No backend established a connection."
raise RuntimeError(msg)
return conn


def use_scope() -> MutableMapping[str, Any]:
def use_scope() -> MutableMapping[str, Any]: # nocov
"""Get the current :class:`~reactpy.backend.types.Connection`'s scope."""
warn(
"The module reactpy.backend.hooks has been deprecated and will be deleted in the future. ",
"Call reactpy.use_scope instead.",
DeprecationWarning,
)

return use_connection().scope


def use_location() -> Location:
def use_location() -> Location: # nocov
"""Get the current :class:`~reactpy.backend.types.Connection`'s location."""
warn(
"The module reactpy.backend.hooks has been deprecated and will be deleted in the future. ",
"Call reactpy.use_location instead.",
DeprecationWarning,
)

return use_connection().location
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,9 +24,9 @@
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.backend.types import Connection, Location
from reactpy.core.hooks import ConnectionContext
from reactpy.core.hooks import use_connection as _use_connection
from reactpy.core.layout import Layout
from reactpy.core.serve import RecvCoroutine, SendCoroutine, Stop, serve_layout
from reactpy.core.types import RootComponentConstructor
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,10 +24,10 @@
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.backend.types import Connection, Location
from reactpy.config import REACTPY_WEB_MODULES_DIR
from reactpy.core.hooks import ConnectionContext
from reactpy.core.hooks import use_connection as _use_connection
from reactpy.core.layout import Layout
from reactpy.core.serve import RecvCoroutine, SendCoroutine, serve_layout
from reactpy.core.types import RootComponentConstructor
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,10 +24,10 @@
CommonOptions,
read_client_index_html,
)
from reactpy.backend.hooks import ConnectionContext
from reactpy.backend.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.hooks import ConnectionContext
from reactpy.core.hooks import use_connection as _use_connection
from reactpy.core.layout import Layout
from reactpy.core.serve import serve_layout
from reactpy.core.types import ComponentConstructor
Expand Down
26 changes: 25 additions & 1 deletion src/py/reactpy/reactpy/core/hooks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import asyncio
from collections.abc import Coroutine, Sequence
from collections.abc import Coroutine, MutableMapping, Sequence
from logging import getLogger
from types import FunctionType
from typing import (
Expand All @@ -17,6 +17,7 @@

from typing_extensions import TypeAlias

from reactpy.backend.types import Connection, Location
from reactpy.config import REACTPY_DEBUG_MODE
from reactpy.core._life_cycle_hook import current_hook
from reactpy.core.types import Context, Key, State, VdomDict
Expand Down Expand Up @@ -248,6 +249,29 @@ def use_context(context: Context[_Type]) -> _Type:
return provider.value


# 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_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
7 changes: 1 addition & 6 deletions src/py/reactpy/tests/test_backend/test_all.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
from collections.abc import MutableMapping

import pytest
Expand Down Expand Up @@ -107,17 +106,13 @@ def ShowScope():
assert isinstance(scope.current, MutableMapping)


@pytest.mark.skipIf(
sys.platform == "darwin",
reason="Tornado and Flask backends are currently buggy on MacOS.",
)
async def test_use_location(display: DisplayFixture):
location = reactpy.Ref()

@poll
async def poll_location():
"""This needs to be async to allow the server to respond"""
return location.current
return getattr(location, "current", None)

@reactpy.component
def ShowRoute():
Expand Down

0 comments on commit b10be18

Please sign in to comment.