mizzkey/packages/sw/src/scripts/notification-read.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

declare var self: ServiceWorkerGlobalScope;
2021-02-24 19:29:35 +09:00
import { get } from 'idb-keyval';
2021-11-22 19:21:58 +09:00
import { pushNotificationDataMap } from '@/types';
2021-11-22 19:17:28 +09:00
import { api } from '@/scripts/operations';
2021-02-10 22:19:09 +09:00
type Accounts = {
2021-02-10 22:30:02 +09:00
[x: string]: {
queue: string[],
2021-02-15 06:05:18 +09:00
timeout: number | null
2021-02-10 22:30:02 +09:00
}
2021-02-10 22:19:09 +09:00
};
class SwNotificationReadManager {
2021-02-10 22:19:09 +09:00
private accounts: Accounts = {};
2021-02-10 22:30:02 +09:00
public async construct() {
2021-02-15 06:05:18 +09:00
const accounts = await get('accounts');
if (!accounts) Error('Accounts are not recorded');
2021-02-10 22:19:09 +09:00
this.accounts = accounts.reduce((acc, e) => {
2021-02-10 22:30:02 +09:00
acc[e.id] = {
queue: [],
2021-02-15 06:05:18 +09:00
timeout: null
2021-02-10 22:30:02 +09:00
};
return acc;
}, {} as Accounts);
return this;
}
2021-02-10 22:19:09 +09:00
// プッシュ通知の既読をサーバーに送信
2021-09-21 01:55:36 +09:00
public async read<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]) {
2021-02-10 22:30:02 +09:00
if (data.type !== 'notification' || !(data.userId in this.accounts)) return;
2021-02-10 22:19:09 +09:00
2021-02-10 22:30:02 +09:00
const account = this.accounts[data.userId];
2021-02-10 22:19:09 +09:00
2021-02-15 06:05:18 +09:00
account.queue.push(data.body.id as string);
2021-02-10 22:19:09 +09:00
2021-02-14 23:06:47 +09:00
// 最後の呼び出しから200ms待ってまとめて処理する
2021-02-10 22:19:09 +09:00
if (account.timeout) clearTimeout(account.timeout);
account.timeout = setTimeout(() => {
account.timeout = null;
2021-02-15 06:05:18 +09:00
api('notifications/read', data.userId, { notificationIds: account.queue });
2021-02-14 23:06:47 +09:00
}, 200);
2021-02-10 22:19:09 +09:00
}
}
export const swNotificationRead = (new SwNotificationReadManager()).construct();