Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

thread-outbox.ts

56 lines2.1 KB

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 * Lands queued outbox mutations before the JS runtime is torn down (app update
15 * restart). An enqueued message is published to the atom immediately but its
16 * durable write waits behind the mutation queue, so draining only the writes
17 * already mid-file would miss it.
18 */
19export async function flushThreadOutbox(): Promise<void> {
20 await threadOutboxManager.serialize(async () => {});
21 await flushThreadOutboxWrites();
23​
24export function ensureThreadOutboxLoaded(): void {
25 void threadOutboxManager.load();
27​
28export function enqueueThreadOutboxMessage(message: QueuedThreadMessage): Promise<void> {
29 return threadOutboxManager.enqueue(message);
31​
32/** Waits for pending writes to settle; false if the message was rolled back. */
33export function confirmThreadOutboxMessageQueued(message: QueuedThreadMessage): Promise<boolean> {
34 return threadOutboxManager.confirmQueued(message);
36​
37/**
38 * Rewrite a queued message; no-op (false) if it was removed in the meantime,
39 * or (with `expectedRevision` from `threadOutboxRevision`) if any other write
40 * was accepted since the revision was read.
41 */
42export function updateThreadOutboxMessage(
43 message: QueuedThreadMessage,
44 expectedRevision?: number,
45): Promise<boolean> {
46 return threadOutboxManager.update(message, expectedRevision);
48​
49/** Snapshot of a queued message's write revision, for update's CAS. */
50export function threadOutboxRevision(messageId: QueuedThreadMessage["messageId"]): number {
51 return threadOutboxManager.revisionOf(messageId);
53​
54// Removal lives in `thread-outbox-removal.ts`: taking a message out of the
55// outbox must also release its local attachment files, and that owner needs
56// the composer draft state this module must not depend on.