typedash
Version:
modern, type-safe collection of utility functions
1 lines • 1.04 kB
Source Map (JSON)
{"version":3,"file":"once-Dmdw8JVT.cjs","names":[],"sources":["../src/functions/once/once.ts"],"sourcesContent":["import type { AnyFunction } from '../../types/_internal';\n\n/**\n * Invokes the given function only once, no matter how many times it's called.\n * If it was already invoked before, returns the result from the first invocation.\n * @template TFunction The type of the function to be invoked.\n * @param fn The function to be invoked.\n * @returns The result of the first invocation of the function.\n */\nexport function once<TFunction extends AnyFunction>(fn: TFunction): TFunction {\n let result: ReturnType<TFunction> | undefined;\n let hasBeenCalled = false;\n\n return ((...args: Parameters<TFunction>) => {\n if (!hasBeenCalled) {\n result = fn(...args);\n hasBeenCalled = true;\n }\n\n return result;\n }) as TFunction;\n}\n"],"mappings":";;;;;;;;;AASA,SAAgB,KAAoC,IAA0B;CAC5E,IAAI;CACJ,IAAI,gBAAgB;AAEpB,UAAS,GAAG,SAAgC;AAC1C,MAAI,CAAC,eAAe;AAClB,YAAS,GAAG,GAAG,KAAK;AACpB,mBAAgB;;AAGlB,SAAO"}