| 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 |
* Given a function, returns a function that is only calling that function once. |
| 8 |
*/ |
| 9 |
export function createSingleCallFunction<T extends Function>(this: unknown, fn: T, fnDidRunCallback?: () => void): T { |
| 10 |
const _this = this; |
| 11 |
let didCall = false; |
| 12 |
let result: unknown; |
| 13 |
|
| 14 |
return function () { |
| 15 |
if (didCall) { |
| 16 |
return result; |
| 17 |
} |
| 18 |
|
| 19 |
didCall = true; |
| 20 |
if (fnDidRunCallback) { |
| 21 |
try { |
| 22 |
result = fn.apply(_this, arguments); |
| 23 |
} finally { |
| 24 |
fnDidRunCallback(); |
| 25 |
} |
| 26 |
} else { |
| 27 |
result = fn.apply(_this, arguments); |
| 28 |
} |
| 29 |
|
| 30 |
return result; |
| 31 |
} as unknown as T; |
| 32 |
} |