Path: blob/main/extensions/copilot/src/util/common/taskSingler.ts
13397 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*--------------------------------------------------------------------------------------------*/456/**7* Taken from https://github.com/microsoft/vscode/blob/e8e6e1f69c34232ea2668d3ea2c1c0393303474f/src/vs/workbench/api/common/extHostAuthentication.ts#L122-L1368* 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.9*/10export class TaskSingler<T> {11private _inFlightPromises = new Map<string, Promise<T>>();12getOrCreate(key: string, promiseFactory: () => Promise<T>) {13const inFlight = this._inFlightPromises.get(key);14if (inFlight) {15return inFlight;16}1718const promise = promiseFactory().finally(() => this._inFlightPromises.delete(key));19this._inFlightPromises.set(key, promise);2021return promise;22}23}2425