import { resolveTracingPort, runWithTracing, type TraceOperation, type TraceSpan, type TracingPort, } from "./index.js"; type MaybePromise = T | Promise; /** * Resolve a lazy application context inside a span when the runtime tracing * port is available independently from that context. */ export async function runWithResolvedTracingContext(args: { tracing?: TracingPort; ctx: Ctx | (() => MaybePromise); operation: TraceOperation; run(ctx: Ctx, span?: TraceSpan): MaybePromise; }): Promise { const resolveContext = async (): Promise => typeof args.ctx === "function" ? await (args.ctx as () => MaybePromise)() : args.ctx; if (args.tracing) { return await runWithTracing(args.tracing, args.operation, async (span) => args.run(await resolveContext(), span), ); } const ctx = await resolveContext(); const tracing = resolveTracingPort(ctx); return await runWithTracing(tracing, args.operation, (span) => args.run(ctx, span), ); }