mizzkey/src/client/sw/sw.ts

138 lines
3.3 KiB
TypeScript
Raw Normal View History

2017-11-21 03:40:09 +09:00
/**
* Service Worker
*/
declare var self: ServiceWorkerGlobalScope;
2017-11-21 03:40:09 +09:00
2021-02-10 22:19:09 +09:00
import { createNotification } from '@/sw/create-notification';
2021-02-10 01:54:07 +09:00
import { swLang } from '@/sw/lang';
2021-02-10 22:19:09 +09:00
import { swNotificationRead } from '@/sw/notification-read';
import { pushNotificationData } from '../../types';
2021-01-21 21:17:22 +09:00
2021-01-23 00:56:06 +09:00
//#region Lifecycle: Install
2017-11-27 22:00:48 +09:00
self.addEventListener('install', ev => {
2021-02-13 18:52:20 +09:00
self.skipWaiting();
});
2021-01-23 00:56:06 +09:00
//#endregion
2021-01-23 00:56:06 +09:00
//#region Lifecycle: Activate
self.addEventListener('activate', ev => {
ev.waitUntil(
caches.keys()
.then(cacheNames => Promise.all(
cacheNames
2021-02-10 01:54:07 +09:00
.filter((v) => v !== swLang.cacheName)
.map(name => caches.delete(name))
))
.then(() => self.clients.claim())
);
});
2021-01-23 00:56:06 +09:00
//#endregion
2021-01-23 00:56:06 +09:00
//#region When: Fetching
self.addEventListener('fetch', ev => {
2021-02-13 18:34:19 +09:00
// Nothing to do
2017-11-27 22:00:48 +09:00
});
2021-01-23 00:56:06 +09:00
//#endregion
2017-11-27 22:00:48 +09:00
2021-01-23 00:56:06 +09:00
//#region When: Caught Notification
2017-11-21 03:40:09 +09:00
self.addEventListener('push', ev => {
// クライアント取得
2017-11-21 07:06:36 +09:00
ev.waitUntil(self.clients.matchAll({
2017-11-21 03:40:09 +09:00
includeUncontrolled: true
}).then(async clients => {
2021-01-28 03:24:32 +09:00
// // クライアントがあったらストリームに接続しているということなので通知しない
// if (clients.length != 0) return;
2017-11-21 03:40:09 +09:00
2021-02-10 22:19:09 +09:00
const data: pushNotificationData = ev.data?.json();
2017-11-21 07:06:36 +09:00
2021-02-13 01:28:20 +09:00
console.log('push', data)
2021-02-10 01:54:07 +09:00
switch (data.type) {
2021-02-10 22:19:09 +09:00
// case 'driveFileCreated':
2021-02-13 01:28:20 +09:00
case 'notification':
2021-02-10 01:54:07 +09:00
case 'unreadMessagingMessage':
2021-02-10 22:19:09 +09:00
return createNotification(data);
2021-02-10 01:54:07 +09:00
case 'readAllNotifications':
for (const n of await self.registration.getNotifications()) {
n.close();
}
break;
case 'readNotifications':
2021-02-10 22:19:09 +09:00
for (const notification of await self.registration.getNotifications()) {
if (data.body.notificationIds.includes(notification.data.body.id)) {
2021-02-10 22:30:02 +09:00
notification.close();
}
2021-02-10 01:54:07 +09:00
}
break;
}
2017-11-21 07:06:36 +09:00
}));
2017-11-21 03:40:09 +09:00
});
2021-01-23 00:56:06 +09:00
//#endregion
2021-01-21 21:17:22 +09:00
2021-01-28 03:24:32 +09:00
//#region Notification
self.addEventListener('notificationclick', ev => {
const { action, notification } = ev;
2021-02-13 01:28:20 +09:00
console.log('click', action, notification)
2021-02-10 22:19:09 +09:00
const data: pushNotificationData = notification.data;
2021-01-28 03:24:32 +09:00
const { origin } = location;
2021-02-10 01:54:07 +09:00
const suffix = `?loginId=${data.userId}`;
2021-01-28 03:24:32 +09:00
switch (action) {
case 'showUser':
switch (data.body.type) {
case 'reaction':
2021-02-10 01:54:07 +09:00
self.clients.openWindow(`${origin}/users/${data.body.user.id}${suffix}`);
2021-01-28 03:24:32 +09:00
break;
default:
if ('note' in data.body) {
2021-02-10 01:54:07 +09:00
self.clients.openWindow(`${origin}/users/${data.body.note.user.id}${suffix}`);
2021-01-28 03:24:32 +09:00
}
}
break;
default:
}
notification.close();
});
2021-02-10 22:19:09 +09:00
self.addEventListener('notificationclose', ev => {
2021-01-28 03:24:32 +09:00
const { notification } = ev;
2021-02-10 22:19:09 +09:00
2021-02-13 01:28:20 +09:00
console.log('close', notification)
2021-02-10 01:54:07 +09:00
if (notification.title !== 'notificationclose') {
2021-02-11 17:14:16 +09:00
self.registration.showNotification('notificationclose', { body: `${notification?.data?.body?.id}` });
2021-02-10 01:54:07 +09:00
}
2021-02-10 22:19:09 +09:00
const data: pushNotificationData = notification.data;
2021-01-28 03:24:32 +09:00
2021-02-10 22:19:09 +09:00
if (data.type === 'notification') {
console.log('close', data);
swNotificationRead.then(that => that.read(data));
2021-01-28 03:24:32 +09:00
}
});
//#endregion
2021-01-23 00:56:06 +09:00
//#region When: Caught a message from the client
2021-01-21 21:17:22 +09:00
self.addEventListener('message', ev => {
2021-02-10 02:03:05 +09:00
switch (ev.data) {
2021-01-21 21:17:22 +09:00
case 'clear':
return; // TODO
default:
break;
}
if (typeof ev.data === 'object') {
2021-01-24 18:17:42 +09:00
// E.g. '[object Array]' → 'array'
2021-01-21 21:17:22 +09:00
const otype = Object.prototype.toString.call(ev.data).slice(8, -1).toLowerCase();
if (otype === 'object') {
if (ev.data.msg === 'initialize') {
2021-02-10 01:54:07 +09:00
swLang.setLang(ev.data.lang);
2021-01-21 21:17:22 +09:00
}
}
}
});
2021-01-23 00:56:06 +09:00
//#endregion