import type { IJMAPClient } from '@/lib/jmap/client-interface'; type JmapTypeHandler = (client: IJMAPClient, accountChanges: Record) => Promise; const handlers = new Map(); export function registerPushHandler(jmapType: string, handler: JmapTypeHandler): () => void { const list = handlers.get(jmapType) ?? []; list.push(handler); handlers.set(jmapType, list); return () => { const current = handlers.get(jmapType); if (!current) return; const idx = current.indexOf(handler); if (idx >= 0) current.splice(idx, 1); if (current.length === 0) handlers.delete(jmapType); }; } export async function dispatchPushEvent( client: IJMAPClient, changed: Record>, accountId: string, ): Promise { const accountChanges = changed[accountId]; for (const [jmapType, typeHandlers] of handlers) { if (accountChanges?.[jmapType]) { for (const handler of typeHandlers) { try { await handler(client, accountChanges); } catch (error) { console.error(`Push handler for ${jmapType} failed:`, error); } } } } }