-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
perf(useEventListener): use AbortController
under the hood instead of cleanup array
#4514
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
base: main
Are you sure you want to change the base?
Conversation
…of cleanup array
What about browser compatibility? |
Correct me if I am wrong, but Source: https://developer.mozilla.org/en-US/docs/Web/API/AbortController/AbortController |
I mean that I think we should have the same browser support as Vue 3, which is exactly ES6. Merging this means that we request more recent browser support than Vue 3 itself. See also vuejs/core#8763 (comment) The question here it's what's the browser support baseline we want for VueUse? I don't see that mentioned everywhere, but as a consumer of this library, I expect it should have the same support as Vue 3 itself in the core composables and return isSupported in the composables that use modern APIs. In my opinion, here it's not necessary to return isSupported, but we should handle a fallback for old browsers. So it's a matter of internally calling useSupported checking the existence of AbortController and testing if the signal option is also valid (which was introduced later in Chrome 90, as seen in MDN In my opinion, not sure how worth all of this is though for the increased complexity. |
Okay, got it.
The question is how to detect if Perhaps we need to check existing composables. |
@@ -136,16 +132,6 @@ describe('useEventListener', () => { | |||
listeners.forEach(listener => expect(listener).toBeCalledTimes(index + 1)) | |||
}) | |||
}) | |||
|
|||
it('should remove all listeners with all events', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe you could have an equivalent test for an abort signal, and just assert signal.aborted
is true
?
i know we already test teardown by asserting calls are 0
, but its probably still worth testing the signal got aborted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that easy. We have to mock AbortSignal
to do this, no?
i've approved but i agree we should decide what our level of support is if we don't care about engines old enough that they don't have if we do care, i'd consider holding off for now rather than having a mixture of cleanup/abort in the repo looks like |
keep in mind it seems like so when we have a slightly different use case here, since we're using it to clean up rather than abort something. we'd end up with a memory leak in browsers which don't support it if we did that. that means in our case, we'd need a fallback ( |
same as above |
@OrbisK see my comment above |
I rather mean that with |
I thought about this a bit more. Of course, the different browser compatibility with vue speaks against this. On the other hand, according to caniuse, With I don't know what's right here. Maybe tomorrow I will try to see how a fallback would feel and work. 👍🏽 |
Some key points:
To sum up my points:
¹ In fact, it's even slower with a single event, JSBenchmark), 5 events JSBenchmark, I've only see it better for 10+ events, which are really low for a single composable usage (a global AbortController that handles all would be great, but not possible in this case) |
I am also concerned with the browser support. This also relies on whether the targets' I agree that in the long term this seems to be a more elegant solution to clean up events. To move forward, I think:
A bit out of topic: I also wonder if we should build a practice around AbortController, like accepting a controller for most of the composable to replace the |
When we do, I was looking for a polyfill to reference in docs like: https://github.com/nuxodin/lazyfill/blob/main/monkeyPatches/addEventListenerSignal.js Maybe we can still get the fallback variant to work if we somehow discover support for all use cases once, like this:
Should I do this within this PR? or create branches from this one? |
Isn't this against the problem described by @ferferga in #4514 (comment). with vues browser comparability? Or maybe because it is optional it should be possible? |
not caniuse.com, but canivueuse.com 😅 |
Before submitting the PR, please make sure you do the following
fixes #123
).Description
This PR removes the cleanup array and replaces it with an abort controller.
Ref: https://kettanaito.com/blog/dont-sleep-on-abort-controller
Additional context