import type { OAuthSession } from "@atproto/oauth-client-browser"; import { invalidateUnreadCountRequests } from "@/lib/appViewUnreadCountRequests"; type MutationQueue = { active: boolean; generation: number; tail: Promise }; const queues = new WeakMap(); function queueFor(oauth: OAuthSession): MutationQueue { let queue = queues.get(oauth); if (!queue) { queue = { active: true, generation: 0, tail: Promise.resolve() }; queues.set(oauth, queue); } return queue; } // Reserve before authority lookup or a legacy request: a delayed 409 transition // must not let an older bulk action overtake a newer single-entry action. export function queueReadStateMutation(oauth: OAuthSession, operation: (assertCurrent: () => void) => Promise): Promise { const queue = queueFor(oauth), generation = queue.generation; const assertCurrent = () => { if (!queue.active || generation !== queue.generation) throw new DOMException("Read-state session stopped", "AbortError"); }; try { assertCurrent(); } catch (error) { return Promise.reject(error); } const result = queue.tail.then(async () => { assertCurrent(); invalidateUnreadCountRequests(oauth); try { const value = await operation(assertCurrent); assertCurrent(); return value; } finally { invalidateUnreadCountRequests(oauth); } }); queue.tail = result.then(() => {}, () => {}); return result; } export function stopReadStateMutations(oauth: OAuthSession): void { const queue = queueFor(oauth); invalidateUnreadCountRequests(oauth); queue.active = false; queue.generation++; queue.tail = Promise.resolve(); } export function resumeReadStateMutations(oauth: OAuthSession): void { queueFor(oauth).active = true; }