-
Notifications
You must be signed in to change notification settings - Fork 600
feat: Page.post_frame_callback
#5842
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
Open
ndonkoHenri
wants to merge
3
commits into
main
Choose a base branch
from
post-frame-callback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||
| import asyncio | ||||||
| import inspect | ||||||
| import logging | ||||||
| import sys | ||||||
| import threading | ||||||
|
|
@@ -424,6 +425,18 @@ class Page(BasePage): | |||||
| """ | ||||||
| TBD | ||||||
| """ | ||||||
|
|
||||||
| on_first_frame: Optional[ControlEventHandler["Page"]] = None | ||||||
| """ | ||||||
| Called once after the client renders the very first frame. | ||||||
|
|
||||||
| Useful for starting implicit animations or other work that must wait until | ||||||
| the initial layout completes. | ||||||
|
|
||||||
| Pair with [`Page.post_frame_callback()`][flet.Page.post_frame_callback] to register | ||||||
| callbacks without wiring up an explicit event handler. | ||||||
| """ | ||||||
|
|
||||||
| _services: list[Service] = field(default_factory=list) | ||||||
| _user_services: ServiceRegistry = field(default_factory=lambda: ServiceRegistry()) | ||||||
|
|
||||||
|
|
@@ -447,6 +460,8 @@ def __post_init__( | |||||
| self.__last_route = None | ||||||
| self.__query: QueryString = QueryString(self) | ||||||
| self.__authorization: Optional[Authorization] = None | ||||||
| self.__first_frame_callbacks: list[Callable[[], Any]] = [] | ||||||
| self.__first_frame_fired = False | ||||||
|
|
||||||
| def get_control(self, id: int) -> Optional[BaseControl]: | ||||||
| """ | ||||||
|
|
@@ -523,8 +538,56 @@ def before_event(self, e: ControlEvent): | |||||
| if view_index is not None: | ||||||
| e.view = views[view_index] | ||||||
|
|
||||||
| elif e.name == "first_frame": | ||||||
| self.__handle_first_frame_event() | ||||||
|
|
||||||
| return super().before_event(e) | ||||||
|
|
||||||
| def post_frame_callback(self, callback: Callable[[], Any]): | ||||||
| """ | ||||||
| Schedule a callable to run immediately after the page finishes | ||||||
| rendering its very first frame. | ||||||
|
|
||||||
| Args: | ||||||
| callback: A synchronous function or coroutine function to execute after the | ||||||
| initial layout completes. Already-rendered pages trigger the callback | ||||||
| immediately. | ||||||
|
|
||||||
| Raises: | ||||||
| TypeError: If `callback` is not callable. | ||||||
| """ | ||||||
| if not callable(callback): | ||||||
| raise TypeError("callback must be callable") | ||||||
|
|
||||||
| if self.__first_frame_fired: | ||||||
| self.__run_first_frame_callback(callback) | ||||||
| else: | ||||||
| self.__first_frame_callbacks.append(callback) | ||||||
|
|
||||||
| def __handle_first_frame_event(self): | ||||||
| """Drain and execute callbacks when the Flutter client signals first frame.""" | ||||||
| if self.__first_frame_fired: | ||||||
| return | ||||||
|
|
||||||
| self.__first_frame_fired = True | ||||||
| callbacks = self.__first_frame_callbacks[:] | ||||||
| self.__first_frame_callbacks.clear() | ||||||
| for cb in callbacks: | ||||||
| self.__run_first_frame_callback(cb) | ||||||
|
|
||||||
| def __run_first_frame_callback(self, callback: Callable[[], Any]): | ||||||
| """Execute a queued callback asynchronously, awaiting it if needed.""" | ||||||
|
|
||||||
| async def _runner(): | ||||||
| try: | ||||||
| result = callback() | ||||||
| if inspect.isawaitable(result): | ||||||
| await result | ||||||
| except Exception: | ||||||
| logger.exception("Error running post_frame_callback callback") | ||||||
|
||||||
| logger.exception("Error running post_frame_callback callback") | |
| logger.exception("Error running post-frame callback") |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential race condition: The
__first_frame_callbackslist and__first_frame_firedflag are accessed without synchronization. Ifpost_frame_callback()is called from a different thread while__handle_first_frame_event()is executing, the callback could be:__first_frame_firedbetween lines 569-572, seeing False, then the flag gets set to True before appending (line 565), causing the callback to never executeConsider using a lock (e.g.,
self._lockif available, or a new dedicated lock) to protect access to both__first_frame_firedand__first_frame_callbacks.