Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import type * as BlueMicrocosmIdentityResolveMiniDoc from "@atcute/microcosm/types/blue/microcosm/identity/resolveMiniDoc";import { cached } from "./cache";import type { BobbinContext, XrpcRequestInit } from "./client";import { jsonGet } from "./_request";
export type MiniDoc = BlueMicrocosmIdentityResolveMiniDoc.$output;
export const INVALID_HANDLE = "handle.invalid";
export const resolveMiniDoc = ( ctx: BobbinContext, identifier: string, init?: XrpcRequestInit): Promise<MiniDoc> => cached(`minidoc:${identifier.toLowerCase()}`, 60_000, () => jsonGet<MiniDoc>(ctx, "blue.microcosm.identity.resolveMiniDoc", { identifier }, init) );// did/handle cache with in-flight de-dupe.export class IdentityCache { readonly #ctx: BobbinContext; readonly #byDid = new Map<string, MiniDoc>(); readonly #byHandle = new Map<string, MiniDoc>(); readonly #inflight = new Map<string, Promise<MiniDoc>>();
constructor(ctx: BobbinContext) { this.#ctx = ctx; }
prime(doc: MiniDoc): void { this.#byDid.set(doc.did, doc); this.#byHandle.set(doc.handle, doc); }
didFor(handle: string): string | undefined { return this.#byHandle.get(handle)?.did; }
handleFor(did: string): string | undefined { return this.#byDid.get(did)?.handle; }
resolve(identifier: string, init?: XrpcRequestInit): Promise<MiniDoc> { const hit = identifier.startsWith("did:") ? this.#byDid.get(identifier) : this.#byHandle.get(identifier); if (hit) return Promise.resolve(hit); const existing = this.#inflight.get(identifier); if (existing) return existing;
const pending = resolveMiniDoc(this.#ctx, identifier, init) .then((doc) => { this.prime(doc); return doc; }) .finally(() => this.#inflight.delete(identifier)); this.#inflight.set(identifier, pending); return pending; }}