import { Disposables } from './create-disposables.js'; import type { IDisposable } from './types.js'; declare const DISPOSAL_GUARD_DEFAULTS: { name: string; timeout: number; usedWhileDisposing: boolean; }; type OPTIONS = Partial; type GUARDED_FN_ASYNC = () => Promise; type GUARDED_FN_SYNC = () => T; /** * Adds dispose-safe methods to Disposables: * * - setInterval/setTimeout * - guard * @example * ```ts * export class MyDisposable implements IDisposable { * private disposables = new SafeDisposable(MyDisposable.name) * dispose: () => Promise; * * constructor() { * this.disposables.add('log', () => console.log('disposed')); * this.disposables.setTimeout(() => console.log('will be canceled upon disposal'), 1000); * this.dispose = () => this.disposables.dispose() * } * * async doSomething() { * // will throw if disposed, delays disposal until done is called * return await this.disposables.guard(async () =>{ * // do something * return await somePromise // if dispose is called while the code awaits, new guards will throw, but actual disposal will not begin * }) * // disposal may begin * } * } * ``` */ export declare class SafeDisposable extends Disposables implements IDisposable { private _isDisposed; private _isDisposing?; private timeouts; private intervals; constructor(name: string); /** * Starts instance disposal: * * **phase 1: disposing** * - isDisposed === true * - guard() // will throw * - guard({usedWhileDisposing:true}) // will not throw (for methods that are used in the disposal process) * - all guards are awaited * - disposable.dispose is awaited * * **phase 2: disposed done** * - guard({usedWhileDisposing:true}) // will throw */ dispose(): Promise; /** * returns true if the disposal process started */ isDisposed: () => boolean; /** * After disposal starts, it's necessary to avoid executing some code. `guard` is used for those cases. * * for example: after fileRemover.dispose(), fileRemover.remove() should throw. * * `guard` will: * * - throws if disposal started/finished * - delays disposal actual until the current flow is done * * @example * ```ts * // this will throw if disposed * this.guard(()=> { * // do something * // if dispose is called while the code executes, * // new guards will throw, but actual disposal will not begin * }, {timeout: 1000, name:'something'}); * // disposal may begin * ``` */ guard(fn: GUARDED_FN_ASYNC, options?: OPTIONS): Promise; guard(fn: GUARDED_FN_SYNC, options?: OPTIONS): T; guard<_T>(options?: OPTIONS): void; /** * a disposal safe setTimeout * checks disposal before execution and clears the timeout when the instance is disposed */ setTimeout(fn: () => void, timeout: number): ReturnType; /** * a disposal safe setInterval * checks disposal before execution and clears the interval when the instance is disposed */ setInterval(fn: () => void, interval: number): ReturnType; } export {}; //# sourceMappingURL=safe-disposable.d.ts.map