import { type GroupConstraints } from './constraints.js'; import { type DisposableItem } from './disposables-group.js'; export declare const DEFAULT_GROUP = "default"; export declare const DEFAULT_TIMEOUT = 1000; export type DisposableOptions = { /** * disposable name, used in error or when timed out */ name: string; /** * the subject to dispose */ dispose: DisposableItem; /** * @default DEFAULT_TIMEOUT */ timeout?: number; /** * disposal group name * @default DEFAULT_GROUP */ group?: string; }; /** * Disposables allow adding of disposal async functions, * when dispose is called, these functions will be run sequentially * * Disposal groups: You can set disposal groups with constraints (before, after) * to ensure that disposal groups are disposed in the correct order * * within each group disposables are disposed in the reverse order they were added * * @example * ```ts * const disposables = createDisposables('sample'); * disposables.add(() => console.log('disposable 1')); * disposables.add({dispose: () => console.log('disposable 2')}); * disposables.dispose(); * // disposable 2 * // disposable 1 * ``` * * @example disposal groups * ```ts * const disposables = createDisposables('sample'); * disposables.registerGroup('first', {before: DEFAULT_GROUP}); * disposables.registerGroup('last', {after: DEFAULT_GROUP}); * disposables.registerGroup('beforeDefault', {before: DEFAULT_GROUP, after: 'first'}); * disposables.add(() => console.log('first'), 'first'); * disposables.add(() => console.log('beforeDefault'), 'beforeDefault'); * disposables.add(() => console.log('last'), 'last'); * disposables.add(() => console.log('default')); * disposables.dispose(); * // first * // beforeDefault * // default * // last * ``` */ export declare function createDisposables(name: string, initialGroups?: string[]): Disposables; export declare class Disposables { private name; private readonly groups; private readonly constrains; constructor(name: string, initialGroups?: string[]); /** * register a new constrained disposal group * @param constraints - constraints for the group must contain {before: groupName} or {after: groupName} */ registerGroup(groupName: string, constraints: GroupConstraints[] | GroupConstraints): void; /** * register a new constrained disposal group if it doesn't exist ignore otherwise * @param constraints - constraints for the group must contain {before: groupName} or {after: groupName} */ registerGroupIfNotExists(groupName: string, constraints: GroupConstraints[] | GroupConstraints): void; /** * @param name - disposable name for error messages * @param disposable - a function or object with a dispose method * @returns a function to remove the disposable */ add(name: string, disposable: DisposableItem): () => void; /** * @param options must include a [dispose] function or object with a dispose method * @returns a function to remove the disposable */ add(options: DisposableOptions): () => void; /** * removes a disposable from all disposal group */ remove(disposable: DisposableItem): void; /** * Disposes all disposables in all groups one at the time, * order based on constraints */ dispose(): Promise; /** * * @returns a serialized list of groups and their disposables and constraints */ list(): { constrains: ({ before: string; after?: string; } | { after: string; before?: string; })[]; groups: { name: string; disposables: { name: string; timeout: number; }[]; totalTimeout: number; }[]; totalTimeout: number; }; } //# sourceMappingURL=create-disposables.d.ts.map