/* * [ Caution ] * Since this module is being used by a service worker, be very careful about adding imports or using functions for browsers. */ /* * A notice is kept once in the inbox however many copies of it arrive. * * Every open tab subscribes to the notices and all of them write the same IndexedDB store, so with three tabs a * single notice was kept three times (seen on :4700, 2026-10-26). A notice also reaches a person by web push, which * the service worker keeps in the same store, so one approval was kept twice (2026-11-10). The tabs and the worker * take turns on one lock, and a copy is kept only when no copy of that notice is kept yet. */ /** * Whether two copies are one notice - the server stamps one moment for all its copies (deliver-notice.ts). * * The moment and the words, nothing that differs by path: the push has no subject, opens the full address, and its * moment comes as text. */ export function sameNotice(a: any, b: any): boolean { return ( !!a && !!b && Number(a.timestamp) === Number(b.timestamp) && a.type === b.type && a.title === b.title && a.body === b.body ) } /** Keeping only - the open screen keeps each tab of the inbox to its own limit (notification client/inbox.ts). */ export type NoticeStore = { getAll(): Promise add(notice: any): Promise } /** The Web Locks API, or what a test hands in. Without it (an old browser) the copy is kept as before. */ export type NoticeLocks = { request(name: string, callback: () => Promise): Promise } export const NOTICE_STORE_LOCK = 'notification-store' /** Keeps `notice` unless a copy of it is kept already. Answers whether this one was kept. */ export async function storeOnce( store: NoticeStore, notice: any, locks: NoticeLocks | undefined = (globalThis as any).navigator?.locks ): Promise { const keep = async () => { const kept = await store.getAll() if (kept.some(one => sameNotice(one, notice))) return false await store.add(notice) return true } return locks ? await locks.request(NOTICE_STORE_LOCK, keep) : await keep() }