Open History to see the last commit
1import { appAtomRegistry } from "./atom-registry";
2import { createThreadOutboxManager } from "./thread-outbox-manager";
3import type { QueuedThreadMessage } from "./thread-outbox-model";
4import { expoThreadOutboxStorage, flushThreadOutboxWrites } from "./thread-outbox-storage";
5
6export * from "./thread-outbox-model";
7
8export const threadOutboxManager = createThreadOutboxManager({
9 registry: appAtomRegistry,
10 storage: expoThreadOutboxStorage,
11});
12
13/**
14 * An enqueued message is published to the atom immediately but its durable
15 * write waits behind the mutation queue, so draining only the writes already
16 * mid-file would miss it.
17 */
18export async function flushThreadOutbox(): Promise<void> {
19 await threadOutboxManager.serialize(async () => {});
20 await flushThreadOutboxWrites();
21}
22
23export function enqueueThreadOutboxMessage(message: QueuedThreadMessage): Promise<void> {
24 return threadOutboxManager.enqueue(message);
25}
26
27export function confirmThreadOutboxMessageQueued(message: QueuedThreadMessage): Promise<boolean> {
28 return threadOutboxManager.confirmQueued(message);
29}
30
31/**
32 * Rewrite a queued message; no-op (false) if it was removed in the meantime,
33 * or (with `expectedRevision` from `threadOutboxRevision`) if any other write
34 * was accepted since the revision was read.
35 */
36export function updateThreadOutboxMessage(
37 message: QueuedThreadMessage,
38 expectedRevision?: number,
39): Promise<boolean> {
40 return threadOutboxManager.update(message, expectedRevision);
41}
42
43export function threadOutboxRevision(messageId: QueuedThreadMessage["messageId"]): number {
44 return threadOutboxManager.revisionOf(messageId);
45}
46
47// Removal lives in `thread-outbox-removal.ts`: taking a message out of the
48// outbox must also release its local attachment files, and that owner needs
49// the composer draft state this module must not depend on.