-
Notifications
You must be signed in to change notification settings - Fork 68
Proposal: Add once as an option in API addListener #805
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
Comments
Speaking of Manually remove listeners as needed. const controller = new AbortController()
browser.storage.local.onChanged.addListener(myListener, {signal: controller.signal})
// ... later, when listener is no longer needed ...
controller.abort() Automatically remove a listener after a specified duration. browser.storage.local.onChanged.addListener(myListener, {
signal: AbortSignal.timeout(1000)
}) Remove after one use. Not as a pretty as const controller = new AbortController()
browser.storage.local.onChanged.addListener(() => {
controller.abort()
// ... do something once
}, {signal: controller.signal}) Temporary listeners tied to background lifetime (#719) const controller = new AbortController()
browser.storage.local.onChanged.addListener(myListener, {signal: controller.signal})
// Once background suspends, the browser will remove the listener. |
Ideally we would make the switch to addEventListener completely. This way we get these for free. Just like the Take for example, the recently proposed |
I would also prefer we switch to |
The argument against that has been that it's hard to feature-detect, whereas events defined this way can be detected via As for the abort signal, aren't extensions service workers incompatible? I implemented both
The first API could be an official way to offer the full "addEventListener" API without breaking feature detection nor backwards compatibility. |
Background
EventTarget: addEventListener() has the
once
option which would result in its automatic removal when invoked.API
addListener
could also benefit from having such option. It can be specially useful for listeners with confined conditions. Currently, the only option is toremoveListener
as soon as it is invoked.It also appears that browsers might not automatically remove listeners when the conditions no longer match (e.g. tab is removed in the following example).
Benefit
The purpose and benefit of
once
is:browser.***.removeListener()
Proposal
Add
once
as an option to APIaddListener
Example
See also:
The text was updated successfully, but these errors were encountered: