mizzkey/src/client/sw/notification-read.ts

51 lines
1.2 KiB
TypeScript
Raw Normal View History

declare var self: ServiceWorkerGlobalScope;
2021-02-24 19:29:35 +09:00
import { get } from 'idb-keyval';
2021-02-10 22:19:09 +09:00
import { pushNotificationData } from '../../types';
2021-02-15 06:05:18 +09:00
import { api } from './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 SwNotificationRead {
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
// プッシュ通知の既読をサーバーに送信
public async read(data: pushNotificationData) {
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 SwNotificationRead()).construct();