/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
export class MultiDisposeError extends Error {
    errors;
    constructor(errors) {
        super(`Encountered errors while disposing of store. Errors: [${errors.join(', ')}]`);
        this.errors = errors;
    }
}
export function disposeAll(disposables) {
    const errors = [];
    for (const disposable of disposables) {
        try {
            disposable.dispose();
        }
        catch (e) {
            errors.push(e);
        }
    }
    if (errors.length === 1) {
        throw errors[0];
    }
    else if (errors.length > 1) {
        throw new MultiDisposeError(errors);
    }
}
export class Disposable {
    #isDisposed = false;
    _disposables = [];
    dispose() {
        if (this.#isDisposed) {
            return;
        }
        this.#isDisposed = true;
        disposeAll(this._disposables);
    }
    _register(value) {
        if (this.#isDisposed) {
            value.dispose();
        }
        else {
            this._disposables.push(value);
        }
        return value;
    }
    get isDisposed() {
        return this.#isDisposed;
    }
}
//# sourceMappingURL=dispose.js.map