Path: blob/main/src/vs/workbench/contrib/chat/browser/chatContentParts/chatCollections.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { IDisposable, Disposable } from '../../../../../base/common/lifecycle.js';67export class ResourcePool<T extends IDisposable> extends Disposable {8private readonly pool: T[] = [];910private _inUse = new Set<T>;11public get inUse(): ReadonlySet<T> {12return this._inUse;13}1415constructor(16private readonly _itemFactory: () => T,17) {18super();19}2021get(): T {22if (this.pool.length > 0) {23const item = this.pool.pop()!;24this._inUse.add(item);25return item;26}2728const item = this._register(this._itemFactory());29this._inUse.add(item);30return item;31}3233release(item: T): void {34this._inUse.delete(item);35this.pool.push(item);36}37}3839export interface IDisposableReference<T> extends IDisposable {40object: T;41isStale: () => boolean;42}434445