Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/taskSingler.ts
13397 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
7
/**
8
* Taken from https://github.com/microsoft/vscode/blob/e8e6e1f69c34232ea2668d3ea2c1c0393303474f/src/vs/workbench/api/common/extHostAuthentication.ts#L122-L136
9
* Given a task with the same key, it will only run one at a time and will return the same promise for all calls with the same key.
10
*/
11
export class TaskSingler<T> {
12
private _inFlightPromises = new Map<string, Promise<T>>();
13
getOrCreate(key: string, promiseFactory: () => Promise<T>) {
14
const inFlight = this._inFlightPromises.get(key);
15
if (inFlight) {
16
return inFlight;
17
}
18
19
const promise = promiseFactory().finally(() => this._inFlightPromises.delete(key));
20
this._inFlightPromises.set(key, promise);
21
22
return promise;
23
}
24
}
25