Skip to content
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

feat: Redraw once promise returned from event handler is settled #2862

Open
wants to merge 6 commits into
base: next
Choose a base branch
from

Conversation

EtiamNullam
Copy link

@EtiamNullam EtiamNullam commented Aug 18, 2023

Description

In this PR I'm proposing a solution to handle async event handlers.

I've decided to duck-type for finally with fallback to then. I assume we still want to redraw on rejected promise.

NOTE: seems like onbeforeremove lifecycle event handler is also dealing with promises so maybe some common logic can be extracted.

Motivation and Context

I think it's reasonable to redraw after asynchronous action is finished (resulting from user action). I was surprised it doesn't work like that and seems like it can!

More details in the issue here: #2861

How Has This Been Tested?

It seems to work according to my manual testing and it doesn't fail any existing tests.

I'm marking it as a draft as I still need to work out how to make tests work for asynchronous code. Some help would be appreciated. I will hopefully include them soon. Solved.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation change

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated docs/changelog.md

EDIT: Updated docs. I'm not sure how I'm supposed to update changelog, as it seems like it was not updated recently.
EDIT2: Added tests. Let me know if they are not sufficient.

@EtiamNullam EtiamNullam marked this pull request as ready for review August 21, 2023 02:56
render/render.js Outdated
if (this._ && ev.redraw !== false) {
(0, this._)()
if (result != null) {
if (typeof result.finally === "function") result.finally((0, this._))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just use .then. It's redundant to do both that and .finally, because per spec p.finally(f) is roughly just .then(async v => { await f(); return v }, async e => { await f(); throw e }), complete with the .then method call being observable.

render/render.js Outdated
(0, this._)()
if (result != null) {
if (typeof result.finally === "function") result.finally((0, this._))
else if (typeof result.then === "function") result.then((0, this._))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Your (0, expr) is redundant. It's not immediately called, so it does nothing.
  2. Let's do it even on error, similarly to my above finally to ensure errors get reported correctly. (Obviously, you can't use async/await, but it's fairly straightforward.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I was not sure about the use of this (0, expr) so I've tried to keep it consistent. I'm gonna call this._ directly.
  2. Not sure what you mean by "my above finally" but I guess I can just pass this._ to both resolve and reject callbacks of then, like this:
if (result != null && typeof result.then === "function") result.then(this._, this._)

I just need to update it with the tests as it seems to work as expected. Thank you!

@EtiamNullam
Copy link
Author

Let me know if there's anything more I have to do. I've applied the requested changes.

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.

None yet

2 participants