DwebServiceWorker
- Shim
Simulates a ServiceWorker and provides application upgrade controllers and application control functionality. Provides control of apps and interception of APIs. This is a purely web-based mechanism for communication between local apps that can send and receive messages. Inherits from BaseEvent;
import { dwebServiceWorker } from "@plaoc/plugin";
Properties
plugin
The property value is the DwebServiceWorkerPlugin object
updateContoller
The property value is the UpdateController object;
externalFetch
The property value is the DwebServiceWorkerPlugin.externalFetch() method
update
The property value is the UpdateController object;
close
The property value is the DwebServiceWorkerPlugin.close() method
restart
The property value is the DwebServiceWorkerPlugin.restart() method;
Methods
addEventListener
Adds an event listener
removeEventListener
Removes an event listener
Events
Supports any events, the following are predefined events:
This event handles messages sent from others and responds with data via respondWith. Here you can provide different functionality to other apps by matching routes to interconnect them.
"pause"
Triggered when the app is paused; passes an Event to the handler;
"resume"
Triggered when the app resumes foreground; passes an Event to the handler;
"updatefound"
Triggered during updates or restarts; passes an Event to the handler;
Example
import { dwebServiceWorker } from "@plaoc/plugin";
dwebServiceWorker.addEventListener('fetch', async (fetchEvent) => {
const url = new URL(event.request.url);
if (url.pathname.endsWith("/say/hi")) {
// message is the message content
const message = url.searchParams.get("message");
// Send the message back
return event.respondWith(`match`);
}
return event.respondWith("Not match any routes");
})
Platform Compatibility
Properties/Methods/Events | Android | iOS | Desktop-Dev | Desktop |
---|---|---|---|---|
plugin | ✅ | ✅ | ✅ | X |
updateContoller | ✅ | ✅ | ✅ | X |
externalFetch | ✅ | ✅ | ✅ | X |
update | ✅ | ✅ | ✅ | X |
close | ✅ | ✅ | ✅ | X |
restart | ✅ | ✅ | ✅ | X |
addEventListener | ✅ | ✅ | ✅ | X |
removeEventListener | ✅ | ✅ | ✅ | X |
event fetch | ✅ | ✅ | ✅ | X |
event pause | ✅ | ✅ | ✅ | X |
event resume | ✅ | ✅ | ✅ | X |
event updatefound | ✅ | ✅ | ✅ | X |