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

thread-outbox.ts

47 lines1.9 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/** Drain queued mutations too; optimistic publication precedes the durable file write. */
14export async function flushThreadOutbox(): Promise<void> {
15 await threadOutboxManager.serialize(async () => {});
16 await flushThreadOutboxWrites();
18​
19export function enqueueThreadOutboxMessage(message: QueuedThreadMessage): Promise<void> {
20 return threadOutboxManager.enqueue(message);
22​
23export function confirmThreadOutboxMessageQueued(message: QueuedThreadMessage): Promise<boolean> {
24 return threadOutboxManager.confirmQueued(message);
26​
27/** Returns false if removed or if expectedRevision differs from threadOutboxRevision; otherwise rewrites the message. */
28export function updateThreadOutboxMessage(
29 message: QueuedThreadMessage,
30 expectedRevision?: number,
31): Promise<boolean> {
32 return threadOutboxManager.update(message, expectedRevision);
34​
35/** Changes the message as it is when its turn comes; false when it is gone or `change` returns null. */
36export function modifyThreadOutboxMessage(
37 messageId: QueuedThreadMessage["messageId"],
38 change: (message: QueuedThreadMessage) => QueuedThreadMessage | null,
39): Promise<boolean> {
40 return threadOutboxManager.modify(messageId, change);
42​
43export function threadOutboxRevision(messageId: QueuedThreadMessage["messageId"]): number {
44 return threadOutboxManager.revisionOf(messageId);
46​
47// Removal needs draft-state ownership to release files; keep that dependency outside this module.