import type { EnvironmentId } from "@t3tools/contracts"; import { appAtomRegistry } from "./atom-registry"; import { createThreadOutboxManager } from "./thread-outbox-manager"; import type { QueuedThreadMessage } from "./thread-outbox-model"; import { expoThreadOutboxStorage, flushThreadOutboxWrites } from "./thread-outbox-storage"; export * from "./thread-outbox-model"; export const threadOutboxManager = createThreadOutboxManager({ registry: appAtomRegistry, storage: expoThreadOutboxStorage, }); /** * Lands queued outbox mutations before the JS runtime is torn down (app update * restart). An enqueued message is published to the atom immediately but its * durable write waits behind the mutation queue, so draining only the writes * already mid-file would miss it. */ export async function flushThreadOutbox(): Promise { await threadOutboxManager.serialize(async () => {}); await flushThreadOutboxWrites(); } export function ensureThreadOutboxLoaded(): void { void threadOutboxManager.load(); } export function enqueueThreadOutboxMessage(message: QueuedThreadMessage): Promise { return threadOutboxManager.enqueue(message); } /** Waits for pending writes to settle; false if the message was rolled back. */ export function confirmThreadOutboxMessageQueued(message: QueuedThreadMessage): Promise { return threadOutboxManager.confirmQueued(message); } /** Rewrite a queued message; no-op (false) if it was removed in the meantime. */ export function updateThreadOutboxMessage(message: QueuedThreadMessage): Promise { return threadOutboxManager.update(message); } export function removeThreadOutboxMessage(message: QueuedThreadMessage): Promise { return threadOutboxManager.remove(message); } export function clearThreadOutboxEnvironment(environmentId: EnvironmentId): Promise { return threadOutboxManager.clearEnvironment(environmentId); }