import type { Cipher, Folder, Send } from '../types'; import { parseJson, type AuthedFetch } from './shared'; interface VaultSyncResponse { ciphers?: Cipher[]; folders?: Folder[]; sends?: Send[]; } const pendingSyncRequests = new WeakMap>(); export async function loadVaultSyncSnapshot(authedFetch: AuthedFetch): Promise { const existing = pendingSyncRequests.get(authedFetch); if (existing) return existing; const request = (async () => { const resp = await authedFetch('/api/sync', { cache: 'no-store', headers: { 'Cache-Control': 'no-cache', Pragma: 'no-cache', }, }); if (!resp.ok) throw new Error('Failed to load vault'); const body = await parseJson(resp); return body || {}; })(); pendingSyncRequests.set(authedFetch, request); try { return await request; } finally { if (pendingSyncRequests.get(authedFetch) === request) { pendingSyncRequests.delete(authedFetch); } } }