dynamically registered event listeners for extension service worker
Nobody has claimed this yet.
- Dominant language
- Bikeshed
- Stars
- 3.6k
- Forks
- 324
- Avg merge
- 14d 22h
- Merged PRs (30d)
- 1
Description
Note that I'm talking about browser extension service worker, not Web service worker. But I would like to seek advice from Web service worker experts here.
Unlike the web environment, browser extensions usually provide users with some functional options in the extension settings interface. Ideally, the extension only registers relevant events in extension service worker when the user turns on a feature. So some developers are proposing to dynamically register events at WECG.
Here is my proposal:
At present, an extension Event object has these method:
addListener()
removeListener()
hasListener()
hasListeners()
addRules()
getRules()
removeRules()
For dynamically registered events for service worker, I proposal these new methods instead of changing existing methods:
subscribe(function-name: String)
unsubscribe(function-name: String)
hasSubscribed(function-name: String)
For example (there are lots of different events in browser extensions):
browser.webNavigation.onCommitted.subscribe("myFunctionName");
browser.webNavigation.onCommitted.unsubscribe("myFunctionName");
"FunctionName" is a global function name that declared in service worker, rather than a function object, and doesn't need to be registered in the first event loop when service worker wakes up. This allows the browser to remember both the event and this specific function independently of service worker's lifecycle, not just remember the event like addListener() does.
These new methods can be called in both service-worker context and non-service-worker context (e.g. a user setting page) dynamically, but only trigger events in service worker context, not in other contexts.
After calling some_event.subscribe(function-name), the browser triggers related events in service worker. If the service worker is inactive, wake up it first. Then looks for the function (by name) to execute.
I'd love to hear from the browser implementation perspective, such as whether it's possible or what problems it will encounter.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no repository files, tests, or implementation entry points; start by reviewing the discussion and the linked WECG proposal. Done would require an agreed design for dynamic subscriptions across service-worker lifecycles, including event delivery and function lookup.
Written by the indexing model from the issue text.
Assessment
- Domain
- web-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100