X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils.ts;h=6f9d1226bb71210b020d987b32d169bbd593b5b8;hb=refs%2Ftags%2Fv2.6.32;hp=efd1f124084776667bd60495f202cec32c7e04a9;hpb=7daf8b1ee6ee28ba8c39c63067cf7071f8638177;p=poolifier.git diff --git a/src/utils.ts b/src/utils.ts index efd1f124..6f9d1226 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -182,3 +182,27 @@ export const updateMeasurementStatistics = ( } } } + +/** + * Executes a function once at a time. + * + * @param fn - The function to execute. + * @param context - The context to bind the function to. + * @returns The function to execute. + */ +export const once = ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + fn: (...args: any[]) => void, + context: unknown + // eslint-disable-next-line @typescript-eslint/no-explicit-any +): ((...args: any[]) => void) => { + let called = false + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return function (...args: any[]): void { + if (!called) { + called = true + fn.apply(context, args) + called = false + } + } +}