-
Notifications
You must be signed in to change notification settings - Fork 16
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
Is it possible to optionally "trampoline" subscriptions to be asynchronous? #74
Comments
I just noticed this note: Lines 533 to 536 in 8d43ff4
In this case, I of course am asking for an opt-in mechanism, but it is interesting that this is already somewhat of an existing mechanism. |
Somewhat off-topic but I also found this comment to suggest that it has indeed been common in the observable space generally to follow such a pattern
Though I am not sure if there exists a way to pipe + concatMap + sleep in the current proposal (I guess userland extensions would provide that?) |
After some more reading of RxJS, I believe I am specifically asking for I suspect this is beyond scope of the proposal at the moment, but seems easy enough to polyfill by creating a custom operator (coincidentally, that example already does what I ask for). |
Just to be clear, you are correct that this proposal does not introduce a I've heard some interest in making event handler dispatching optionally asynchronous elsewhere, outside of this proposal, so I'm wondering if this should be something filed more generally against whatwg/dom? Maybe it could be an option passed into Do you know if there's been any appetite for this among folks you work with to pursue a general DOM Standard event listening proposal for this? |
I think that's right. I think I'd be more comfortable with a more generalized event listening + scheduling API proposal instead of trying to narrowly achieve this with the Observable API, off the back of limited microtask infrastructure that we use for the handful of Promise-returning APIs. Given that, I think I will close this, but if nobody ends up filing a DOM issue for the scheduling primitive + event handling ideas, then I might take this upon myself. |
This makes sense to me. I was just curious if Observables will already offer a generic mechanism to, effectively, "yield" before dispatch of callbacks using a simple syntax, rather than relying on the functionality of the
I think there is a strong appetite for this. I've been assuming this would just entail extending the "passive" concept to more/all event types. I think developers should be able to register either non-passive and passive listeners and dispatch from discrete tasks, the latter could be delayed until after-next-paint depending on scheduler.
That would be excellent, and appreciated! I don't myself feel able to carry this task. You likely have more context for some other related projects as well. FWIW, I also think there is appetite for other, slightly related features, in case these interest you also:
|
Hmm I see what you're saying here, but passive event listeners aren't quite the same thing as the "async" we're looking for here. Passive event listeners allow scrolling (associated with the event) to run in the background. So in a way, the scrolling happens "asynchronously" (in a background thread I guess) with respect to the event dispatching, but the event handler doesn't get fired asynchronously with respect to the platform's creation of the event itself. So I think we want something novel here.
Alright I filed whatwg/dom#1308! |
Hmmm, I guess there are two distinct concepts and I wonder if we are both describing the same one?
I think In my 2 cents, the fundamental property of passive events is that the primary / "default action" is handled first, and then later (async) the The main criteria is that you lose the ability to Today, it's true that this is only useful for scrolling specifically, and that is only because that default action is handled by the browser directly from the compositor. But I don't think its that different to say that e.g. a The concept seems fairly equivalent to me-- though perhaps too many developers associate the "passive" concept specifically with scrolling. I'm not sure. |
Also I'm not sure if it helps, but, perhaps observers like |
Reading whatwg/dom/issues/1308, I'll stop discussion on this thread and move conversation there. |
My understanding is that
.subscription
callbacks are always dispatched synchronously as new data arrives, even when the data itself can arrive as a stream, asynchronously.This is described as a feature and the primary use case was specifically for EventTarget support.
However, I also see in this example a (hypothetical?) alternative to
.subscribe
using.then
syntax to compare Promise behaviour which would queue a microtask instead. My read is that.then
isn't actually part of the Observable API proposal and that was just hypothetical-- is that correct?All that makes sense to me-- however, I think there do exist very real use cases (see at bottom) for desiring that the dispatch of subscription calls would be truly asynchronous (i.e. a new macrotask not just microtask), and especially so for EventTarget.
Perhaps one way to accomplish this manually would be to:
Or perhaps there could be a helper to automate this, something like:
However, I feel it may be enough important for EventTarget to warrant a subscription helper that is async-dispatch-by-default. Perhaps something like:
...and perhaps Task priority is also related. EventDispatch is typically very high priority, but perhaps observer callbacks should have the option to change their own priority?
The use case for requesting async dispatch of EventTarget subscription is for decoupling necessary effects which follow important interactions, from unnecessary effects which can be delayed until after next paint.
See the Optimize INP guide for examples of explicitly using yield points to manually to accomplish such patterns.
Today, the web platform does not support native "passive" event listeners which would dispatch callbacks asynchronously (perhaps it could) -- but I wondered if the Observer API already has solutions that would easily address this use case?
The text was updated successfully, but these errors were encountered: