Open
Description
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 to removeListener
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:
- avoid additional asynchronous request to remove the listener e.g.
browser.***.removeListener()
- avoid unwanted events being processed by the browser during the time it takes to perform the above asynchronous removal (which can be many in case of net requests)
Proposal
Add once
as an option to API addListener
Example
browser.webRequest.onBeforeRequest.addListener(listener, {
urls: ['<all_urls>'],
tabId: tab.id,
once: true
})