New Event Modifier for async Listeners #2509
fjahn
started this conversation in
3. Feature Ideas
Replies: 1 comment 1 reply
-
It's currently possible with something like that: https://codepen.io/SimoTod/pen/MWEmWJR |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
While trying out a couple of things, I noticed that there is a requent problem when having click listeners that are async.
Here are a couple of examples:
Traditional solution
In all these cases, developers would need to implement a flag for detecting if the event handler is currently running. The flag would have to be checked at the top of the event listener, then set, and then reset again at the end of the function in a
finally
block.Using vanilla javascript, I have separated this functionality like this before:
When listening to events via Alpinejs, this is not possible, however.
New solution
This functionality could also be realized using a new event modifier. Using my first example with the button:
While
fetchData
has not fulfiled or rejected the promise, clicking the button does not execute the listener.Handling one listener for multiple elements
To make the modifier more versatile, there should be two variations of this modifier that are relevant when one handler is used for multiple buttons: If another button is clicked that would trigger a listener that is already executing, should the listener be called or not? In other words, should the non-concurrency be bound per listener or per button?
For instance, in my third example with the memory cards, we would like it to be bound to the listener, not the button. There are use cases where it should be the other way around.
Things to discuss
.disabled
class to the element while the listener is running?Beta Was this translation helpful? Give feedback.
All reactions