Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/vs/base/common/functional.ts
13405 views
1
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'
2
3
/*---------------------------------------------------------------------------------------------
4
* Copyright (c) Microsoft Corporation. All rights reserved.
5
* Licensed under the MIT License. See License.txt in the project root for license information.
6
*--------------------------------------------------------------------------------------------*/
7
8
/**
9
* Given a function, returns a function that is only calling that function once.
10
*/
11
export function createSingleCallFunction<T extends Function>(this: unknown, fn: T, fnDidRunCallback?: () => void): T {
12
const _this = this;
13
let didCall = false;
14
let result: unknown;
15
16
return function () {
17
if (didCall) {
18
return result;
19
}
20
21
didCall = true;
22
if (fnDidRunCallback) {
23
try {
24
result = fn.apply(_this, arguments);
25
} finally {
26
fnDidRunCallback();
27
}
28
} else {
29
result = fn.apply(_this, arguments);
30
}
31
32
return result;
33
} as unknown as T;
34
}
35
36