-
-
Notifications
You must be signed in to change notification settings - Fork 324
Fix issue from #1081 #1085
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
Fix issue from #1081 #1085
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
from collections.abc import AsyncIterator | ||
from contextlib import asynccontextmanager | ||
from typing import Any | ||
|
||
from jsonpointer import set_pointer | ||
|
||
from reactpy.core.layout import Layout | ||
from reactpy.core.types import VdomJson | ||
from tests.tooling.common import event_message | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@asynccontextmanager | ||
async def layout_runner(layout: Layout) -> AsyncIterator[LayoutRunner]: | ||
async with layout: | ||
yield LayoutRunner(layout) | ||
|
||
|
||
class LayoutRunner: | ||
def __init__(self, layout: Layout) -> None: | ||
self.layout = layout | ||
self.model = {} | ||
|
||
async def render(self) -> VdomJson: | ||
update = await self.layout.render() | ||
logger.info(f"Rendering element at {update['path'] or '/'!r}") | ||
if not update["path"]: | ||
self.model = update["model"] | ||
else: | ||
self.model = set_pointer( | ||
self.model, update["path"], update["model"], inplace=False | ||
) | ||
return self.model | ||
|
||
async def trigger(self, element: VdomJson, event_name: str, *data: Any) -> None: | ||
event_handler = element.get("eventHandlers", {}).get(event_name, {}) | ||
logger.info(f"Triggering {event_name!r} with target {event_handler['target']}") | ||
if not event_handler: | ||
raise ValueError(f"Element has no event handler for {event_name}") | ||
await self.layout.deliver(event_message(event_handler["target"], *data)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Iterator, Sequence | ||
from dataclasses import dataclass | ||
from typing import Callable | ||
|
||
from reactpy.core.types import VdomJson | ||
|
||
Selector = Callable[[VdomJson, "ElementInfo"], bool] | ||
|
||
|
||
def id_equals(id: str) -> Selector: | ||
return lambda element, _: element.get("attributes", {}).get("id") == id | ||
|
||
|
||
def class_equals(class_name: str) -> Selector: | ||
return ( | ||
lambda element, _: class_name | ||
in element.get("attributes", {}).get("class", "").split() | ||
) | ||
|
||
|
||
def text_equals(text: str) -> Selector: | ||
return lambda element, _: _element_text(element) == text | ||
|
||
|
||
def _element_text(element: VdomJson) -> str: | ||
if isinstance(element, str): | ||
return element | ||
return "".join(_element_text(child) for child in element.get("children", [])) | ||
|
||
|
||
def element_exists(element: VdomJson, selector: Selector) -> bool: | ||
return next(find_elements(element, selector), None) is not None | ||
|
||
|
||
def find_element( | ||
element: VdomJson, | ||
selector: Selector, | ||
*, | ||
first: bool = False, | ||
) -> tuple[VdomJson, ElementInfo]: | ||
"""Find an element by a selector. | ||
|
||
Parameters: | ||
element: | ||
The tree to search. | ||
selector: | ||
A function that returns True if the element matches. | ||
first: | ||
If True, return the first element found. If False, raise an error if | ||
multiple elements are found. | ||
|
||
Returns: | ||
Element info, or None if not found. | ||
""" | ||
find_iter = find_elements(element, selector) | ||
found = next(find_iter, None) | ||
if found is None: | ||
raise ValueError("Element not found") | ||
if not first: | ||
try: | ||
next(find_iter) | ||
raise ValueError("Multiple elements found") | ||
except StopIteration: | ||
pass | ||
return found | ||
|
||
|
||
def find_elements( | ||
element: VdomJson, selector: Selector | ||
) -> Iterator[tuple[VdomJson, ElementInfo]]: | ||
"""Find an element by a selector. | ||
|
||
Parameters: | ||
element: | ||
The tree to search. | ||
selector: | ||
A function that returns True if the element matches. | ||
|
||
Returns: | ||
Element info, or None if not found. | ||
""" | ||
return _find_elements(element, selector, (), ()) | ||
|
||
|
||
def _find_elements( | ||
element: VdomJson, | ||
selector: Selector, | ||
parents: Sequence[VdomJson], | ||
path: Sequence[int], | ||
) -> tuple[VdomJson, ElementInfo] | None: | ||
info = ElementInfo(parents, path) | ||
if selector(element, info): | ||
yield element, info | ||
|
||
for index, child in enumerate(element.get("children", [])): | ||
if isinstance(child, dict): | ||
yield from _find_elements( | ||
child, selector, (*parents, element), (*path, index) | ||
) | ||
|
||
|
||
@dataclass | ||
class ElementInfo: | ||
parents: Sequence[VdomJson] | ||
path: Sequence[int] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.