wip
This commit is contained in:
parent
f95ca164d6
commit
77902e8d80
3 changed files with 28 additions and 30 deletions
|
@ -1,44 +1,43 @@
|
||||||
/*
|
/*
|
||||||
* Language manager for SW
|
* Language manager for SW
|
||||||
*/
|
*/
|
||||||
|
|
||||||
declare var self: ServiceWorkerGlobalScope;
|
declare var self: ServiceWorkerGlobalScope;
|
||||||
|
|
||||||
import { get, set } from 'idb-keyval';
|
import { get, set } from 'idb-keyval';
|
||||||
import { I18n } from '@/scripts/i18n';
|
import { I18n } from '@/scripts/i18n';
|
||||||
|
|
||||||
class SwLang {
|
class SwLang {
|
||||||
public cacheName = `mk-cache-${_VERSION_}`;
|
public cacheName = `mk-cache-${_VERSION_}`;
|
||||||
|
|
||||||
public lang: Promise<string> = get('lang').then(async prelang => {
|
public lang: Promise<string> = get('lang').then(async prelang => {
|
||||||
if (!prelang) return 'en-US';
|
if (!prelang) return 'en-US';
|
||||||
return prelang;
|
return prelang;
|
||||||
});
|
});
|
||||||
|
|
||||||
public i18n: I18n<any> | null = null;
|
public i18n: I18n<any> | null = null;
|
||||||
|
|
||||||
public setLang(newLang: string) {
|
public setLang(newLang: string) {
|
||||||
this.i18n = null;
|
this.i18n = null;
|
||||||
this.lang = Promise.resolve(newLang);
|
this.lang = Promise.resolve(newLang);
|
||||||
set('lang', newLang);
|
set('lang', newLang);
|
||||||
return this.fetchLocale();
|
return this.fetchLocale();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchLocale() {
|
public async fetchLocale() {
|
||||||
// Service Workerは何度も起動しそのたびにlocaleを読み込むので、CacheStorageを使う
|
// Service Workerは何度も起動しそのたびにlocaleを読み込むので、CacheStorageを使う
|
||||||
const localeUrl = `/assets/locales/${await this.lang}.${_VERSION_}.json`;
|
const localeUrl = `/assets/locales/${await this.lang}.${_VERSION_}.json`;
|
||||||
let localeRes = await caches.match(localeUrl);
|
let localeRes = await caches.match(localeUrl);
|
||||||
|
|
||||||
if (!localeRes) {
|
|
||||||
localeRes = await fetch(localeUrl);
|
|
||||||
const clone = localeRes?.clone();
|
|
||||||
if (!clone?.clone().ok) return;
|
|
||||||
|
|
||||||
caches.open(this.cacheName).then(cache => cache.put(localeUrl, clone));
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.i18n = new I18n(await localeRes.json());
|
if (!localeRes) {
|
||||||
}
|
localeRes = await fetch(localeUrl);
|
||||||
|
const clone = localeRes?.clone();
|
||||||
|
if (!clone?.clone().ok) return;
|
||||||
|
|
||||||
|
caches.open(this.cacheName).then(cache => cache.put(localeUrl, clone));
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.i18n = new I18n(await localeRes.json());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const swLang = new SwLang();
|
export const swLang = new SwLang();
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
/*
|
/*
|
||||||
* Notification manager for SW
|
* Notification manager for SW
|
||||||
*/
|
*/
|
||||||
|
|
||||||
declare var self: ServiceWorkerGlobalScope;
|
declare var self: ServiceWorkerGlobalScope;
|
||||||
|
|
||||||
import { getNoteSummary } from '../../misc/get-note-summary';
|
import { getNoteSummary } from '../../misc/get-note-summary';
|
||||||
import getUserName from '../../misc/get-user-name';
|
import getUserName from '../../misc/get-user-name';
|
||||||
import { swLang } from '@/sw/lang'
|
import { swLang } from '@/sw/lang';
|
||||||
|
|
||||||
class SwNotification {
|
class SwNotification {
|
||||||
private queue: any[] = [];
|
private queue: any[] = [];
|
||||||
|
@ -22,11 +21,11 @@ class SwNotification {
|
||||||
if (this.fetching == false) {
|
if (this.fetching == false) {
|
||||||
this.fetching = true;
|
this.fetching = true;
|
||||||
await swLang.fetchLocale();
|
await swLang.fetchLocale();
|
||||||
this.fetching = false;
|
|
||||||
const promises = this.queue.map(this.composeNotification).map(n => {
|
const promises = this.queue.map(this.composeNotification).map(n => {
|
||||||
if (!n) return;
|
if (!n) return;
|
||||||
return self.registration.showNotification(...n);
|
return self.registration.showNotification(...n);
|
||||||
})
|
});
|
||||||
|
this.fetching = false;
|
||||||
this.queue = [];
|
this.queue = [];
|
||||||
return Promise.all(promises);
|
return Promise.all(promises);
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,7 +134,7 @@ self.addEventListener('notificationclose', async ev => {
|
||||||
|
|
||||||
//#region When: Caught a message from the client
|
//#region When: Caught a message from the client
|
||||||
self.addEventListener('message', ev => {
|
self.addEventListener('message', ev => {
|
||||||
switch(ev.data) {
|
switch (ev.data) {
|
||||||
case 'clear':
|
case 'clear':
|
||||||
return; // TODO
|
return; // TODO
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in a new issue