Open History to see the last commit
1import type { EnvironmentId } from "@t3tools/contracts";
2
3import { appAtomRegistry } from "./atom-registry";
4import { createThreadOutboxManager } from "./thread-outbox-manager";
5import type { QueuedThreadMessage } from "./thread-outbox-model";
6import { expoThreadOutboxStorage, flushThreadOutboxWrites } from "./thread-outbox-storage";
7
8export * from "./thread-outbox-model";
9
10export const threadOutboxManager = createThreadOutboxManager({
11 registry: appAtomRegistry,
12 storage: expoThreadOutboxStorage,
13});
14
15/**
16 * Lands queued outbox mutations before the JS runtime is torn down (app update
17 * restart). An enqueued message is published to the atom immediately but its
18 * durable write waits behind the mutation queue, so draining only the writes
19 * already mid-file would miss it.
20 */
21export async function flushThreadOutbox(): Promise<void> {
22 await threadOutboxManager.serialize(async () => {});
23 await flushThreadOutboxWrites();
24}
25
26export function ensureThreadOutboxLoaded(): void {
27 void threadOutboxManager.load();
28}
29
30export function enqueueThreadOutboxMessage(message: QueuedThreadMessage): Promise<void> {
31 return threadOutboxManager.enqueue(message);
32}
33
34/** Waits for pending writes to settle; false if the message was rolled back. */
35export function confirmThreadOutboxMessageQueued(message: QueuedThreadMessage): Promise<boolean> {
36 return threadOutboxManager.confirmQueued(message);
37}
38
39/** Rewrite a queued message; no-op (false) if it was removed in the meantime. */
40export function updateThreadOutboxMessage(message: QueuedThreadMessage): Promise<boolean> {
41 return threadOutboxManager.update(message);
42}
43
44export function removeThreadOutboxMessage(message: QueuedThreadMessage): Promise<void> {
45 return threadOutboxManager.remove(message);
46}
47
48export function clearThreadOutboxEnvironment(environmentId: EnvironmentId): Promise<void> {
49 return threadOutboxManager.clearEnvironment(environmentId);
50}