Skip to content

ft: allow async callbacks with Page.on calls #353

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 2 commits into from
Dec 9, 2020
Merged

ft: allow async callbacks with Page.on calls #353

merged 2 commits into from
Dec 9, 2020

Conversation

Mattwmaster58
Copy link
Contributor

This allows for async callback functions as an argument to Page.on. Relevant pyee documentation:

An event emitter class which can run asyncio coroutines in addition to synchronous blocking functions. For example:

@ee.on('event')
async def async_handler(*args, **kwargs):
    await returns_a_future()

On emit, the event emitter will automatically schedule the coroutine using asyncio.ensure_future and the configured event loop (defaults to asyncio.get_event_loop()).

Unlike the case with the BaseEventEmitter, all exceptions raised by event handlers are automatically emitted on the error event. This is important for asyncio coroutines specifically but is also handled for synchronous functions for consistency.

When loop is specified, the supplied event loop will be used when scheduling work with ensure_future. Otherwise, the default asyncio event loop is used.

For asyncio coroutine event handlers, calling emit is non-blocking. In other words, you do not have to await any results from emit, and the coroutine is scheduled in a fire-and-forget fashion.

@Mattwmaster58 Mattwmaster58 changed the title ft: allow async callbacks ft: allow async callbacks with Page.on calls Dec 7, 2020
@pavelfeldman
Copy link
Member

Looks great! Let's update the tests to reflect these changes. For example, pagemost of the .on("dialog", on_dialog) occurrences in the tests would benefit from it.

@ghost
Copy link

ghost commented Dec 8, 2020

CLA assistant check
All CLA requirements met.

@pavelfeldman pavelfeldman merged commit 58f212e into microsoft:master Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants