mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core&ui: contest balloon notice (#1071)
This commit is contained in:
1 parent
6d2442c6a3
commit
053978d9d4
12 files changed
+135
-63
No files matched your search
@@ -1,7 +1,7 @@
|
||||
/// <reference no-default-lib="true" />
|
||||
/// <reference lib="webworker" />
|
||||
import ReconnectingWebsocket from 'reconnecting-websocket';
|
||||
import { FLAG_INFO } from 'vj/constant/message';
|
||||
import { FLAG_I18N, FLAG_INFO } from 'vj/constant/message';
|
||||
declare const self: SharedWorkerGlobalScope;
|
||||
|
||||
console.log('SharedWorker init');
|
||||
@@ -17,6 +17,7 @@ interface RequestInitSharedConnPayload {
|
||||
interface RequestAckPayload {
|
||||
type: 'ack';
|
||||
id: string;
|
||||
payload?: any;
|
||||
}
|
||||
interface RequestUnloadPayload {
|
||||
type: 'unload';
|
||||
@@ -28,28 +29,37 @@ function broadcastMsg(message: any) {
|
||||
for (const p of ports) p.postMessage(message);
|
||||
}
|
||||
|
||||
function onMessage(payload: any) {
|
||||
broadcastMsg({ type: 'message', payload });
|
||||
function onMessage(message: any) {
|
||||
broadcastMsg({ type: 'message', payload: message });
|
||||
let acked = false;
|
||||
ack[payload.mdoc._id] = () => { acked = true; };
|
||||
let payload = message;
|
||||
ack[message.mdoc._id] = () => { acked = true; };
|
||||
ack[`${message.mdoc._id}-i18n`] = (v) => { payload = v; };
|
||||
if (message.mdoc.flag & FLAG_I18N) broadcastMsg({ type: 'i18n', payload: message });
|
||||
setTimeout(() => {
|
||||
delete ack[payload.mdoc._id];
|
||||
delete ack[message.mdoc._id];
|
||||
delete ack[`${message.mdoc._id}-i18n`];
|
||||
if (acked) return;
|
||||
if (payload.mdoc.flag & FLAG_INFO) return;
|
||||
if (message.mdoc.flag & FLAG_INFO) return;
|
||||
if (Notification?.permission !== 'granted') {
|
||||
console.log('Notification permission denied');
|
||||
return;
|
||||
}
|
||||
console.log('Sending as system notification');
|
||||
// eslint-disable-next-line no-new
|
||||
new Notification(
|
||||
payload.udoc.uname || 'Hydro Notification',
|
||||
{
|
||||
payload.udoc._id === 1 ? payload.mdoc.content.split('\n')[0] : payload.udoc.uname || 'Hydro Notification',
|
||||
payload.udoc._id === 1 ? {
|
||||
tag: `notification-${payload.mdoc._id}`,
|
||||
icon: payload.mdoc.avatar || '/android-chrome-192x192.png',
|
||||
body: payload.mdoc.content.split('\n').slice(1).join('\n'),
|
||||
} : {
|
||||
tag: `message-${payload.mdoc._id}`,
|
||||
icon: payload.udoc.avatarUrl || '/android-chrome-192x192.png',
|
||||
body: payload.mdoc.content,
|
||||
},
|
||||
);
|
||||
}, 5000);
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
function initConn(path: string, port: MessagePort, cookie: any) {
|
||||
@@ -92,7 +102,7 @@ self.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 === 'ack') ack[msg.data.id]?.(msg.data.payload);
|
||||
if (msg.data.type === 'unload') ports.delete(port);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user