2017-11-20 19:40:09 +01:00
|
|
|
/**
|
|
|
|
* Service Worker
|
|
|
|
*/
|
2020-05-23 06:19:31 +02:00
|
|
|
declare var self: ServiceWorkerGlobalScope;
|
2017-11-20 19:40:09 +01:00
|
|
|
|
2021-02-06 10:55:53 +01:00
|
|
|
import { get, set } from 'idb-keyval';
|
2021-03-23 09:30:14 +01:00
|
|
|
import composeNotification from '@client/sw/compose-notification';
|
2021-02-13 04:28:26 +01:00
|
|
|
import { I18n } from '../../misc/i18n';
|
2017-11-27 14:00:48 +01:00
|
|
|
|
2021-02-06 10:55:53 +01:00
|
|
|
//#region Variables
|
2019-09-26 22:12:56 +02:00
|
|
|
const version = _VERSION_;
|
|
|
|
const cacheName = `mk-cache-${version}`;
|
|
|
|
|
2021-02-06 10:55:53 +01:00
|
|
|
let lang: string;
|
|
|
|
let i18n: I18n<any>;
|
|
|
|
let pushesPool: any[] = [];
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Startup
|
|
|
|
get('lang').then(async prelang => {
|
|
|
|
if (!prelang) return;
|
|
|
|
lang = prelang;
|
|
|
|
return fetchLocale();
|
|
|
|
});
|
|
|
|
//#endregion
|
2017-11-27 14:00:48 +01:00
|
|
|
|
2021-02-06 10:55:53 +01:00
|
|
|
//#region Lifecycle: Install
|
|
|
|
self.addEventListener('install', ev => {
|
2021-02-13 11:53:40 +01:00
|
|
|
self.skipWaiting();
|
2019-09-26 22:12:56 +02:00
|
|
|
});
|
2021-02-06 10:55:53 +01:00
|
|
|
//#endregion
|
2019-09-26 22:12:56 +02:00
|
|
|
|
2021-02-06 10:55:53 +01:00
|
|
|
//#region Lifecycle: Activate
|
2019-09-26 22:12:56 +02:00
|
|
|
self.addEventListener('activate', ev => {
|
|
|
|
ev.waitUntil(
|
|
|
|
caches.keys()
|
|
|
|
.then(cacheNames => Promise.all(
|
|
|
|
cacheNames
|
|
|
|
.filter((v) => v !== cacheName)
|
|
|
|
.map(name => caches.delete(name))
|
|
|
|
))
|
|
|
|
.then(() => self.clients.claim())
|
|
|
|
);
|
|
|
|
});
|
2021-02-06 10:55:53 +01:00
|
|
|
//#endregion
|
2019-09-26 22:12:56 +02:00
|
|
|
|
2021-02-06 10:55:53 +01:00
|
|
|
//#region When: Fetching
|
2019-09-26 22:12:56 +02:00
|
|
|
self.addEventListener('fetch', ev => {
|
2021-02-13 11:53:40 +01:00
|
|
|
// Nothing to do
|
2017-11-27 14:00:48 +01:00
|
|
|
});
|
2021-02-06 10:55:53 +01:00
|
|
|
//#endregion
|
2017-11-27 14:00:48 +01:00
|
|
|
|
2021-02-06 10:55:53 +01:00
|
|
|
//#region When: Caught Notification
|
2017-11-20 19:40:09 +01:00
|
|
|
self.addEventListener('push', ev => {
|
|
|
|
// クライアント取得
|
2017-11-20 23:06:36 +01:00
|
|
|
ev.waitUntil(self.clients.matchAll({
|
2017-11-20 19:40:09 +01:00
|
|
|
includeUncontrolled: true
|
2020-05-23 06:19:31 +02:00
|
|
|
}).then(async clients => {
|
2017-11-20 19:40:09 +01:00
|
|
|
// クライアントがあったらストリームに接続しているということなので通知しない
|
|
|
|
if (clients.length != 0) return;
|
|
|
|
|
2021-02-06 10:55:53 +01:00
|
|
|
const { type, body } = ev.data?.json();
|
|
|
|
|
|
|
|
// localeを読み込めておらずi18nがundefinedだった場合はpushesPoolにためておく
|
|
|
|
if (!i18n) return pushesPool.push({ type, body });
|
2017-11-20 23:06:36 +01:00
|
|
|
|
2021-02-06 10:55:53 +01:00
|
|
|
const n = await composeNotification(type, body, i18n);
|
|
|
|
if (n) return self.registration.showNotification(...n);
|
2017-11-20 23:06:36 +01:00
|
|
|
}));
|
2017-11-20 19:40:09 +01:00
|
|
|
});
|
2021-02-06 10:55:53 +01:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region When: Caught a message from the client
|
|
|
|
self.addEventListener('message', ev => {
|
|
|
|
switch(ev.data) {
|
|
|
|
case 'clear':
|
|
|
|
return; // TODO
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof ev.data === 'object') {
|
|
|
|
// E.g. '[object Array]' → 'array'
|
|
|
|
const otype = Object.prototype.toString.call(ev.data).slice(8, -1).toLowerCase();
|
|
|
|
|
|
|
|
if (otype === 'object') {
|
|
|
|
if (ev.data.msg === 'initialize') {
|
|
|
|
lang = ev.data.lang;
|
|
|
|
set('lang', lang);
|
|
|
|
fetchLocale();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Function: (Re)Load i18n instance
|
|
|
|
async function fetchLocale() {
|
|
|
|
//#region localeファイルの読み込み
|
|
|
|
// Service Workerは何度も起動しそのたびにlocaleを読み込むので、CacheStorageを使う
|
|
|
|
const localeUrl = `/assets/locales/${lang}.${version}.json`;
|
|
|
|
let localeRes = await caches.match(localeUrl);
|
|
|
|
|
|
|
|
if (!localeRes) {
|
|
|
|
localeRes = await fetch(localeUrl);
|
|
|
|
const clone = localeRes?.clone();
|
|
|
|
if (!clone?.clone().ok) return;
|
|
|
|
|
|
|
|
caches.open(cacheName).then(cache => cache.put(localeUrl, clone));
|
|
|
|
}
|
|
|
|
|
|
|
|
i18n = new I18n(await localeRes.json());
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region i18nをきちんと読み込んだ後にやりたい処理
|
|
|
|
for (const { type, body } of pushesPool) {
|
|
|
|
const n = await composeNotification(type, body, i18n);
|
|
|
|
if (n) self.registration.showNotification(...n);
|
|
|
|
}
|
|
|
|
pushesPool = [];
|
|
|
|
//#endregion
|
|
|
|
}
|
|
|
|
//#endregion
|