import type { Session } from './api'; export type AlertKind = 'finished' | 'attention' | 'mail'; export type Mail = { mail_id: string; sender_session_id: string | null; receiver_session_id: string; kind: string; }; export class Attention { constructor( readonly unread = new Set(), readonly flashing = new Set(), ) {} #mail = new Set(); read(id: string) { this.unread.delete(id); this.flashing.delete(id); } changed( previous: Session | undefined, next: Session, watched: boolean, away: boolean, quiet = false, ): AlertKind | null { if (!previous || !watched || !away) return null; const blocked = next.status.blocking_reason; if ( blocked && JSON.stringify(blocked) !== JSON.stringify(previous.status.blocking_reason) ) return this.#mark(next.id, 'attention', quiet); const answerChanged = next.activity.latest_answer?.message_id && next.activity.latest_answer.message_id !== previous.activity.latest_answer?.message_id; if ( next.status.phase === 'idle' && (previous.status.phase !== 'idle' || answerChanged) ) return this.#mark(next.id, 'finished', quiet); return null; } mail( mail: Mail, watched: boolean, away: boolean, quiet = false, ): AlertKind | null { if (this.#mail.has(mail.mail_id)) return null; this.#mail.add(mail.mail_id); if (this.#mail.size > 256) this.#mail.delete(this.#mail.values().next().value!); if ( !watched || !away || !mail.sender_session_id || !['message', 'result', 'unreviewed'].includes(mail.kind) ) return null; return this.#mark(mail.receiver_session_id, 'mail', quiet); } prune(ids: Set) { for (const id of this.unread) if (!ids.has(id)) this.read(id); } #mark(id: string, kind: AlertKind, quiet: boolean): AlertKind | null { if (this.unread.has(id)) return null; this.unread.add(id); if (!quiet) this.flashing.add(id); return kind; } }