Skip to content

feat: Make redraws when Promises returned by event handlers are completed#3020

Merged
dead-claudia merged 4 commits into
MithrilJS:mainfrom
kfule:async-redraw
May 6, 2025
Merged

feat: Make redraws when Promises returned by event handlers are completed#3020
dead-claudia merged 4 commits into
MithrilJS:mainfrom
kfule:async-redraw

Conversation

@kfule
Copy link
Copy Markdown
Contributor

@kfule kfule commented May 2, 2025

This PR allows redraw on completion of the async event handler.

Description

This PR makes redraws when Promises returned by event handlers are completed. This allows for async event handlers to be redrawn on completion of the function as well as synchronous event handlers.

Motivation and Context

This is like re-opening the #2862 that was accidentally closed.
Unlike #2862 , this PR uses Promise.finally to handle the return value and also adds the condition event.redraw === false. Therefore, I did not cherry-pick #2862 .

How Has This Been Tested?

I added the tests and confirmed that npm run test passes.
I also added eslintrc because the new test code includes the async functions.

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)

Checklist

@kfule kfule requested a review from a team as a code owner May 2, 2025 11:10
Copy link
Copy Markdown
Member

@dead-claudia dead-claudia left a comment

Choose a reason for hiding this comment

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

I like the basic idea, but not the current execution of it.

Doing redraws always for promises and only on success for normal functions will be too surprising to people when errors are involved.

Comment thread render/render.js Outdated
if (eventRedraw && ev.redraw !== false) {
eventRedraw()
if (result != null && typeof result.then === "function") {
Promise.resolve(result).finally(function () {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Promise.resolve(result).finally(function () {
Promise.resolve(result).then(function () {

I'd rather rejections and thrown errors have the same behavior. It's less surprising to users.

Obviously, this will also require changes to the tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, I changed finally to then and fixed the test.

@kfule kfule requested a review from dead-claudia May 2, 2025 23:38
@dead-claudia dead-claudia merged commit 12a056c into MithrilJS:main May 6, 2025
7 checks passed
@JAForbes JAForbes mentioned this pull request May 6, 2025
@JAForbes
Copy link
Copy Markdown
Collaborator

JAForbes commented May 7, 2025

Note: @dead-claudia @kfule I don't think this should be a semver major change. I've started a thread on zulip here for us to discuss.

@dead-claudia
Copy link
Copy Markdown
Member

@JAForbes And responded. We can continue the discussion there.

@dead-claudia dead-claudia added minor and removed major labels May 8, 2025
@kfule kfule deleted the async-redraw branch May 28, 2025 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants