/** * Polyfills for DisposableStack and AsyncDisposableStack. * These provide Go-like `defer` cleanup semantics using the TC39 Explicit * Resource Management proposal (TypeScript 5.2+ `using` / `await using`). * * Works in every runtime — no native DisposableStack support required. * Only needs Symbol.dispose / Symbol.asyncDispose to exist (polyfilled here). */ type DisposeMethod = () => void; type AsyncDisposeMethod = () => void | Promise; /** * A stack of cleanup functions that run in LIFO order when disposed. * Go-like `defer` semantics for synchronous resource management. * * @example * import * as errore from 'errore' * * function processFile(path: string) { * using cleanup = new errore.DisposableStack() * * const file = openFileSync(path) * cleanup.defer(() => file.closeSync()) * * const lock = acquireLock(path) * cleanup.defer(() => lock.release()) * * // ... use file and lock ... * // cleanup runs in reverse order when scope exits: * // 1. lock.release() * // 2. file.closeSync() * } */ export declare class DisposableStack implements Disposable { #private; /** * Whether this stack has already been disposed. */ get disposed(): boolean; /** * Schedule a cleanup function to run when this stack is disposed. * Functions run in LIFO (last-in, first-out) order — like Go's defer. */ defer(onDispose: DisposeMethod): void; /** * Register a Disposable resource. Its [Symbol.dispose]() will be called * when this stack is disposed. Returns the resource for convenience. */ use(value: T): T; /** * Register a non-disposable value with a custom cleanup callback. * Returns the value for convenience. */ adopt(value: T, onDispose: (value: T) => void): T; /** * Move all registered disposables to a new stack, leaving this one empty. * The returned stack owns the cleanup responsibilities. */ move(): DisposableStack; /** * Dispose all resources in LIFO order. If multiple disposers throw, * later errors are attached via SuppressedError (or cause chain fallback). */ [Symbol.dispose](): void; dispose(): void; } /** * A stack of async cleanup functions that run in LIFO order when disposed. * Go-like `defer` semantics for async resource management. * * @example * import * as errore from 'errore' * * async function handleRequest(id: string) { * await using cleanup = new errore.AsyncDisposableStack() * * const db = await connectDb() * cleanup.defer(async () => await db.close()) * * const cache = await openCache() * cleanup.defer(async () => await cache.flush()) * * // ... use db and cache ... * // cleanup runs in reverse order when scope exits * } */ export declare class AsyncDisposableStack implements AsyncDisposable { #private; /** * Whether this stack has already been disposed. */ get disposed(): boolean; /** * Schedule an async cleanup function to run when this stack is disposed. * Functions run in LIFO (last-in, first-out) order — like Go's defer. */ defer(onDispose: AsyncDisposeMethod): void; /** * Register a Disposable or AsyncDisposable resource. Its dispose method * will be called when this stack is disposed. Returns the resource. */ use(value: T): T; /** * Register a non-disposable value with a custom async cleanup callback. * Returns the value for convenience. */ adopt(value: T, onDispose: (value: T) => void | Promise): T; /** * Move all registered disposables to a new stack, leaving this one empty. * The returned stack owns the cleanup responsibilities. */ move(): AsyncDisposableStack; /** * Dispose all resources in LIFO order. If multiple disposers throw, * later errors are attached via SuppressedError (or cause chain fallback). */ [Symbol.asyncDispose](): Promise; disposeAsync(): Promise; } export {}; //# sourceMappingURL=disposable.d.ts.map