Skip to content

make Layout context management async #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions docs/source/about/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ more info, see the :ref:`Contributor Guide <Creating a Changelog Entry>`.
Unreleased
----------

Nothing yet...
Changed:

- :pull:`730` - Layout context management is not async


0.38.0-a2
Expand Down Expand Up @@ -444,7 +446,7 @@ See :ref:`Custom JavaScript Components` for details on the new interface.
- Make docs section margins larger - :issue:`450`
- Search broken in docs - :issue:`443`
- Move src/idom/client out of Python package - :issue:`429`
- Use composition instead of classes with Layout and LifeCycleHook - :issue:`412`
- Use composition instead of classes async with Layout and LifeCycleHook - :issue:`412`
- Remove Python language extension - :issue:`282`
- Add keys to models so React doesn't complain of child arrays requiring them -
:issue:`255`
Expand Down
6 changes: 3 additions & 3 deletions docs/source/about/contributor-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ might look like:

**Added**

- A really cool new feature - :pull:`123`
- :pull:`123` - A really cool new feature

**Changed**

- The behavior of some existing feature - :pull:`456`
- :pull:`456` - The behavior of some existing feature

**Fixed**

- Some really bad bug - :issue:`789`
- :issue:`789` - Some really bad bug

.. note::

Expand Down
7 changes: 2 additions & 5 deletions src/idom/core/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ class LayoutEvent(NamedTuple):
"""A list of event data passed to the event handler."""


_Self = TypeVar("_Self", bound="Layout")


class Layout:
"""Responsible for "rendering" components. That is, turning them into VDOM."""

Expand All @@ -84,7 +81,7 @@ def __init__(self, root: "ComponentType") -> None:
raise TypeError(f"Expected a ComponentType, not {type(root)!r}.")
self.root = root

def __enter__(self: _Self) -> _Self:
async def __aenter__(self) -> Layout:
# create attributes here to avoid access before entering context manager
self._event_handlers: EventHandlerDict = {}

Expand All @@ -98,7 +95,7 @@ def __enter__(self: _Self) -> _Self:

return self

def __exit__(self, *exc: Any) -> None:
async def __aexit__(self, *exc: Any) -> None:
root_csid = self._root_life_cycle_state_id
root_model_state = self._model_states_by_life_cycle_state_id[root_csid]
self._unmount_model_states([root_model_state])
Expand Down
2 changes: 1 addition & 1 deletion src/idom/core/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def serve_json_patch(
recv: RecvCoroutine,
) -> None:
"""Run a dispatch loop for a single view instance"""
with layout:
async with layout:
try:
async with create_task_group() as task_group:
task_group.start_soon(_single_outgoing_loop, layout, send)
Expand Down
10 changes: 6 additions & 4 deletions src/idom/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def should_render(self: _OwnType, new: _OwnType) -> bool:
"""Whether the new component instance should be rendered."""


_Self = TypeVar("_Self")
_Render = TypeVar("_Render", covariant=True)
_Event = TypeVar("_Event", contravariant=True)

Expand All @@ -66,11 +65,14 @@ async def render(self) -> _Render:
async def deliver(self, event: _Event) -> None:
"""Relay an event to its respective handler"""

def __enter__(self: _Self) -> _Self:
async def __aenter__(self) -> LayoutType[_Render, _Event]:
"""Prepare the layout for its first render"""

def __exit__(
self, exc_type: Type[Exception], exc_value: Exception, traceback: TracebackType
async def __aexit__(
self,
exc_type: Type[Exception],
exc_value: Exception,
traceback: TracebackType,
) -> Optional[bool]:
"""Clean up the view after its final render"""

Expand Down
Loading