- From: Jackie(Guokai) Han <notifications@github.com>
- Date: Fri, 22 Dec 2023 05:24:38 -0800
- To: w3c/ServiceWorker <ServiceWorker@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <w3c/ServiceWorker/issues/1698@github.com>
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](https://github.com/w3c/webextensions). 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. -- Reply to this email directly or view it on GitHub: https://github.com/w3c/ServiceWorker/issues/1698 You are receiving this because you are subscribed to this thread. Message ID: <w3c/ServiceWorker/issues/1698@github.com>
Received on Friday, 22 December 2023 13:24:44 UTC