export function isDisposable(value) {
    return typeof value === "object" && value !== null && "dispose" in value && typeof value.dispose === "function";
}
export class DisposableStack {
    entries = [];
    disposed = false;
    use(resource) {
        if (this.disposed) {
            throw new Error("Cannot add a disposable to an already disposed stack.");
        }
        this.entries.push(resource);
        return resource;
    }
    get size() {
        return this.entries.length;
    }
    async dispose() {
        if (this.disposed)
            return;
        this.disposed = true;
        const errors = [];
        while (this.entries.length > 0) {
            const entry = this.entries.pop();
            if (!entry)
                continue;
            try {
                await entry.dispose();
            }
            catch (error) {
                errors.push(error);
            }
        }
        if (errors.length > 0)
            throw new AggregateError(errors, "One or more disposables failed.");
    }
}
//# sourceMappingURL=Disposable.js.map