1import type { FileBackedComposerAttachment } from "./composerImages";
2import { retainComposerAttachmentFile } from "./composerAttachmentFiles";
3
4/**
5 * Preview retention for saved composer attachment copies.
6 *
7 * The durable owners of an attachment file (composer drafts, queued outbox
8 * messages) live in state, so this module cannot reach the ownership-cleanup
9 * sweep directly. Composer draft state registers the owner-side cleanup hook
10 * at module load; until then a release has nothing to retry, because no draft
11 * store has loaded yet and there is nothing to clean.
12 */
13type UnusedAttachmentHandler = (attachment: FileBackedComposerAttachment) => void;
14
15let onAttachmentUnused: UnusedAttachmentHandler | null = null;
16
17export function registerComposerAttachmentUnusedHandler(handler: UnusedAttachmentHandler): void {
18 onAttachmentUnused = handler;
19}
20
21/** Keeps a native preview or upload readable until it finishes, then retries ownership cleanup. */
22export function retainComposerAttachmentFileForPreview(
23 attachment: FileBackedComposerAttachment,
24): () => void {
25 return retainComposerAttachmentFile(attachment.fileUri, () => {
26 onAttachmentUnused?.(attachment);
27 });
28}