Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/browser/chatContentParts/chatCollections.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IDisposable, Disposable } from '../../../../../base/common/lifecycle.js';
7
8
export class ResourcePool<T extends IDisposable> extends Disposable {
9
private readonly pool: T[] = [];
10
11
private _inUse = new Set<T>;
12
public get inUse(): ReadonlySet<T> {
13
return this._inUse;
14
}
15
16
constructor(
17
private readonly _itemFactory: () => T,
18
) {
19
super();
20
}
21
22
get(): T {
23
if (this.pool.length > 0) {
24
const item = this.pool.pop()!;
25
this._inUse.add(item);
26
return item;
27
}
28
29
const item = this._register(this._itemFactory());
30
this._inUse.add(item);
31
return item;
32
}
33
34
release(item: T): void {
35
this._inUse.delete(item);
36
this.pool.push(item);
37
}
38
}
39
40
export interface IDisposableReference<T> extends IDisposable {
41
object: T;
42
isStale: () => boolean;
43
}
44
45