Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102import { ok } from "@atcute/client";import { ClientResponseError, type BobbinContext } from "#lib/api/client.ts";import type { RawCid, RawResourceUri } from "#lib/api/syntax.ts";import { sleep } from "#lib/sleep.ts";import { mainSchema as awaitRecordSchema } from "#lib/api/lexicons/types/org/tangled/bobbin/awaitRecord.ts";import type * as AwaitRecord from "#lib/api/lexicons/types/org/tangled/bobbin/awaitRecord.ts";
export interface RecordRevision { uri: RawResourceUri; cid?: RawCid;}
export const awaitRecord = async ( ctx: BobbinContext, revision: RecordRevision): Promise<AwaitRecord.$output> => { return ok( ctx.xrpc.call(awaitRecordSchema, { params: { uri: revision.uri, ...(revision.cid ? { cid: revision.cid } : {}) } }) );};
// This is a bit of a hack. We should assume that as long as the PDS or knot accepts our req, that's as good as written. Shouldn't have to wait on the big ol' round trip.// TODO: Don't force a wait on round trip to display "saved!" or whatever to the users.const WAIT_BUDGET_MS = 30_000;const WAIT_FIRST_RETRY_MS = 250;const WAIT_LAST_RETRY_MS = 2_000;
export const awaitIndexedRecord = async ( ctx: BobbinContext, revision: RecordRevision): Promise<void> => { const resp = await pollBob(ctx, revision, () => true); if (resp.status !== "indexed") { throw new Error( resp.detail || resp.reason || "Our view server rejected this record for some reason." ); }};
export const awaitIndexedOrSaved = async ( ctx: BobbinContext, revision: RecordRevision): Promise<void> => { try { await awaitIndexedRecord(ctx, revision); } catch (cause) { if (cause instanceof ClientResponseError || cause instanceof TypeError) { throw new Error("Saved. It can take a minute to show up.", { cause }); } throw cause; }};
export const awaitDeletedRecord = async ( ctx: BobbinContext, uri: RawResourceUri): Promise<boolean> => { try { await pollBob(ctx, { uri }, (resp) => resp.status !== "indexed"); return true; } catch { return false; }};
const pollBob = async ( ctx: BobbinContext, revision: RecordRevision, outcome: (resp: AwaitRecord.$output) => boolean): Promise<AwaitRecord.$output> => { const deadline = Date.now() + WAIT_BUDGET_MS; let delay = WAIT_FIRST_RETRY_MS; for (;;) { let resp: AwaitRecord.$output; try { resp = await awaitRecord(ctx, revision); } catch (cause) { const askAgain = (cause instanceof ClientResponseError && (cause.error === "NotSettled" || cause.error === "Overloaded")) || cause instanceof TypeError; if (!askAgain || Date.now() + delay >= deadline) throw cause; await sleep(delay); delay = Math.min(delay * 2, WAIT_LAST_RETRY_MS); continue; } if (outcome(resp)) return resp; if (Date.now() + delay >= deadline) { throw new Error( "Our view server took too long to ingest this record. It will appear shortly." ); } await sleep(delay); delay = Math.min(delay * 2, WAIT_LAST_RETRY_MS); }};