mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
ui: add a useless service worker
This commit is contained in:
1 parent
023a13b066
commit
ccb380852f
6 files changed
+43
-7
No files matched your search
@@ -46,12 +46,18 @@ const initWorkerMode = () => {
|
||||
console.log('Messages: using SharedWorker');
|
||||
const worker = new SharedWorker(new URL('./worker', import.meta.url), { name: 'Hydro Messages Worker' });
|
||||
worker.port.start();
|
||||
window.addEventListener('beforeunload', () => {
|
||||
worker.port.postMessage({ type: 'unload' });
|
||||
});
|
||||
worker.port.postMessage({ type: 'conn', path: endpoint, cookie: document.cookie });
|
||||
worker.port.onmessage = async (message) => {
|
||||
if (process.env.NODE_ENV !== 'production') console.log('onmessage: ', message);
|
||||
const { payload, type } = message.data;
|
||||
if (type === 'message') {
|
||||
if (onmessage(payload)) worker.port.postMessage({ type: 'ack', id: payload.mdoc._id });
|
||||
} else if (type === 'open-page') {
|
||||
console.log('opening page');
|
||||
window.open('/home/messages');
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@ console.log('SharedWorker init');
|
||||
|
||||
let conn: ReconnectingWebsocket;
|
||||
let lcookie: string;
|
||||
const ports: MessagePort[] = [];
|
||||
let ports: MessagePort[] = [];
|
||||
interface RequestInitSharedConnPayload {
|
||||
type: 'conn';
|
||||
cookie: string;
|
||||
@@ -16,7 +16,10 @@ interface RequestAckPayload {
|
||||
type: 'ack';
|
||||
id: string;
|
||||
}
|
||||
type RequestPayload = RequestInitSharedConnPayload | RequestAckPayload;
|
||||
interface RequestUnloadPayload {
|
||||
type: 'unload';
|
||||
}
|
||||
type RequestPayload = RequestInitSharedConnPayload | RequestAckPayload | RequestUnloadPayload;
|
||||
const ack = {};
|
||||
|
||||
function broadcastMsg(message: any) {
|
||||
@@ -54,14 +57,15 @@ function initConn(path: string, port: MessagePort, cookie: any) {
|
||||
console.log('Notification permission denied');
|
||||
return;
|
||||
}
|
||||
const notification = new Notification(
|
||||
// eslint-disable-next-line no-new
|
||||
new Notification(
|
||||
payload.udoc.uname || 'Hydro Notification',
|
||||
{
|
||||
tag: `message-${payload.mdoc._id}`,
|
||||
icon: payload.udoc.avatarUrl || '/android-chrome-192x192.png',
|
||||
body: payload.mdoc.content,
|
||||
},
|
||||
);
|
||||
notification.onclick = () => window.open('/home/messages');
|
||||
}, 5000);
|
||||
}
|
||||
};
|
||||
@@ -74,6 +78,7 @@ onconnect = function (e) {
|
||||
port.addEventListener('message', (msg: { data: RequestPayload }) => {
|
||||
if (msg.data.type === 'conn') initConn(msg.data.path, port, msg.data.cookie);
|
||||
if (msg.data.type === 'ack') ack[msg.data.id]();
|
||||
if (msg.data.type === 'unload') ports = ports.filter((i) => i !== port);
|
||||
});
|
||||
|
||||
port.start();
|
||||
|
||||
Reference in new issue
Block a user