import '@hazae41/symbol-dispose-polyfill'; import { Context } from './context'; export declare enum LifecycleState { CLOSED = "CLOSED", OPEN = "OPEN", ERROR = "ERROR" } export interface Lifecycle { open?(ctx?: Context): Promise | any; close?(): Promise | any; } /** * Base class for resources that need to be opened and closed. */ export declare abstract class Resource implements Lifecycle { #private; /** * ```ts * await using resource = new Resource(); * await resource.open(); * ``` * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using */ [Symbol.asyncDispose](): Promise; get isOpen(): boolean; protected get _lifecycleState(): LifecycleState; protected get _ctx(): Context; /** * To be overridden by subclasses. */ protected _open(_ctx: Context): Promise; /** * To be overridden by subclasses. */ protected _close(_ctx: Context): Promise; /** * Error handler for errors that are caught by the context. * By default, errors are bubbled up to the parent context which is passed to the open method. */ protected _catch(err: Error): Promise; /** * Calls the provided function, opening and closing the resource. * NOTE: Consider using `using` instead. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using */ use(fn: (resource: this) => Promise): Promise; /** * Opens the resource. * If the resource is already open, it does nothing. * If the resource is in an error state, it throws an error. * If the resource is closed, it waits for it to close and then opens it. * @param ctx - Context to use for opening the resource. This context will receive errors that are not handled in `_catch`. */ open(ctx?: Context): Promise; /** * Closes the resource. * If the resource is already closed, it does nothing. */ close(ctx?: Context): Promise; /** * Waits until the resource is open. */ waitUntilOpen(): Promise; } export declare const openInContext: (ctx: Context, resource: T) => Promise; //# sourceMappingURL=resource.d.ts.map