Repository Public
1// Suivi d'un envoi fait par /api/v1 (docs/API.md §7) : on relit GET /messages/outbound/{id} jusqu'à un état final.2// Règle de l'interface : « accepté » n'est pas « remis » — seul l'état observé par destinataire est affiché.3import { errorKind } from "../jmap/errors"4import { FINAL_OUTBOUND, v1, type Outbound } from "../mail/v1"56const DELAYS = [600, 1000, 1500, 2000, 3000, 5000, 8000, 15000, 30000, 60000, 60000, 120000]78export const isFinalOutbound = (o: Pick<Outbound, "status">): boolean => (FINAL_OUTBOUND as readonly string[]).includes(o.status)9export const getOutbound = (id: string, signal?: AbortSignal) => v1<Outbound>(`/messages/outbound/${encodeURIComponent(id)}`, { signal })1011/**12 * Relit l'envoi à intervalles croissants et appelle `onUpdate` à chaque lecture ; s'arrête à l'état final, à l'abandon13 * (`signal`) ou au bout d'environ cinq minutes. Rend le dernier état lu (null si aucune lecture n'a abouti).14 */15export async function pollOutbound(id: string, onUpdate: (o: Outbound) => void, signal?: AbortSignal): Promise<Outbound | null> {16 let last: Outbound | null = null17 for (const delay of DELAYS) {18 await new Promise((r) => setTimeout(r, delay))19 if (signal?.aborted) return last20 try {21 last = await getOutbound(id, signal)22 onUpdate(last)23 if (isFinalOutbound(last)) return last24 } catch (e) {25 if (signal?.aborted || errorKind(e) === "unauthorized" || errorKind(e) === "notFound") return last26 }27 }28 return last29}30