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

thread-outbox.ts

50 lines1.9 KB

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();
25​
26export function ensureThreadOutboxLoaded(): void {
27 void threadOutboxManager.load();
29​
30export function enqueueThreadOutboxMessage(message: QueuedThreadMessage): Promise<void> {
31 return threadOutboxManager.enqueue(message);
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);
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);
43​
44export function removeThreadOutboxMessage(message: QueuedThreadMessage): Promise<void> {
45 return threadOutboxManager.remove(message);
47​
48export function clearThreadOutboxEnvironment(environmentId: EnvironmentId): Promise<void> {
49 return threadOutboxManager.clearEnvironment(environmentId);