UNPKG

@itwin/core-bentley

Version:

Bentley JavaScript core components

120 lines 4.13 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Utils */ Object.defineProperty(exports, "__esModule", { value: true }); exports.DisposableList = void 0; exports.isIDisposable = isIDisposable; exports.isDisposable = isDisposable; exports.dispose = dispose; exports.disposeArray = disposeArray; exports.using = using; /* eslint-disable @typescript-eslint/no-deprecated */ Symbol.dispose ??= Symbol("Symbol.dispose"); Symbol.asyncDispose ??= Symbol("Symbol.asyncDispose"); /** * A type guard that checks whether the given argument implements `IDisposable` interface * @deprecated in 5.0 - will not be removed until after 2026-06-13. Use isDisposable instead. * @public */ function isIDisposable(obj) { return !!obj && (obj instanceof Object) && !!obj.dispose && (typeof obj.dispose === "function"); } /** * A type guard that checks whether the given argument implements `Disposable` interface * @public */ function isDisposable(obj) { return !!obj && (obj instanceof Object) && !!obj[Symbol.dispose] && (typeof obj[Symbol.dispose] === "function"); } function dispose(disposable) { if (undefined !== disposable) { if (Symbol.dispose in disposable) disposable[Symbol.dispose](); else disposable.dispose(); } return undefined; } function disposeArray(list) { if (undefined === list) return undefined; for (const entry of list) { if (Symbol.dispose in entry) entry[Symbol.dispose](); else entry.dispose(); } list.length = 0; return undefined; } /** A 'using' function which is a substitution for .NET's using statement. It makes sure that 'dispose' * is called on the resource no matter if the func returns or throws. If func returns, the return value * of this function is equal to return value of func. If func throws, this function also throws (after * disposing the resource). * @public * @deprecated in 5.0 - will not be removed until after 2026-06-13. Use `using` declarations instead. */ function using(resources, func) { if (!Array.isArray(resources)) return using([resources], func); const doDispose = () => resources.forEach((disposable) => disposable.dispose()); let shouldDisposeImmediately = true; try { const result = func(...resources); if (result instanceof Promise) { shouldDisposeImmediately = false; result.then(doDispose, doDispose); } return result; } finally { if (shouldDisposeImmediately) doDispose(); } } class FuncDisposable { _disposeFunc; constructor(disposeFunc) { this._disposeFunc = disposeFunc; } dispose() { this._disposeFunc(); } } /** A disposable container of disposable objects. * @public */ class DisposableList { _disposables; /** Creates a disposable list. */ constructor(disposables = []) { this._disposables = []; disposables.forEach((disposable) => { this.add(disposable); }); } isDisposable(x) { return x.dispose !== undefined; } /** Register an object for disposal. */ add(disposable) { if (this.isDisposable(disposable)) this._disposables.push(disposable); else this._disposables.push(new FuncDisposable(disposable)); } /** Unregister disposable object. */ remove(disposable) { const idx = this._disposables.indexOf(disposable); if (-1 !== idx) this._disposables.splice(idx, 1); } /** Disposes all registered objects. */ dispose() { for (const disposable of this._disposables) disposable.dispose(); } } exports.DisposableList = DisposableList; //# sourceMappingURL=Disposable.js.map