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

Redraw on resolve of async event handler #2861

Open
EtiamNullam opened this issue Aug 17, 2023 · 1 comment
Open

Redraw on resolve of async event handler #2861

EtiamNullam opened this issue Aug 17, 2023 · 1 comment
Assignees
Labels
Area: Core For anything dealing with Mithril core itself Type: Enhancement For any feature request or suggestion that isn't a bug fix

Comments

@EtiamNullam
Copy link

Is this something you're interested in implementing yourself? Yes

Description

I believe interacting with async code can be improved if we always redraw on resolve of promise from event handlers. Consider this simple app, where we need to manually call m.redraw() after a (fake) long async operation completes which originated from user action (button click):

let count = 0

const longOperation = () => new Promise(
  (resolve) => setTimeout(resolve, 1000)
)

const CounterComponent = () => {
  const add = async () => {
    await longOperation()

    count++
    console.log('added, count:', count)

    // m.redraw() is needed here, my proposal would be that it will be implied when attached to an event
    m.redraw()
  }

  return {
    view: () => [
      m('button', {
        onclick: add,
      }, 'Add'),
      m('span', count),
    ]
  }
}

m.mount(document.body, CounterComponent)

If Mithril's premise is to quickly update after every user interaction then we should also watch for resolve of that action if given opportunity, so my suggestion is to redraw when promise from event handler is resolved.

Why

I believe that's what everyone expects from an async event handler. It seems convenient to me.

Possible Implementation

We are already checking the result of event handler just to see if event propagation should be skipped in case it returned false. In the same place we can check if returned value from event handler looks like promise and attach m.redraw() once it's resolved.

Note

I'm linking to thread I've started on Zulip: https://mithril.zulipchat.com/#narrow/stream/324076-general/topic/Async.20event.20handlers

@EtiamNullam EtiamNullam added the Type: Enhancement For any feature request or suggestion that isn't a bug fix label Aug 17, 2023
@dead-claudia
Copy link
Member

Highly related: #2681

@dead-claudia dead-claudia added the Area: Core For anything dealing with Mithril core itself label Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Core For anything dealing with Mithril core itself Type: Enhancement For any feature request or suggestion that isn't a bug fix
Projects
None yet
Development

No branches or pull requests

2 participants