typedash
Version:
modern, type-safe collection of utility functions
29 lines (27 loc) • 720 B
JavaScript
//#region src/functions/once/once.ts
/**
* Invokes the given function only once, no matter how many times it's called.
* If it was already invoked before, returns the result from the first invocation.
* @template TFunction The type of the function to be invoked.
* @param fn The function to be invoked.
* @returns The result of the first invocation of the function.
*/
function once(fn) {
let result;
let hasBeenCalled = false;
return ((...args) => {
if (!hasBeenCalled) {
result = fn(...args);
hasBeenCalled = true;
}
return result;
});
}
//#endregion
Object.defineProperty(exports, 'once', {
enumerable: true,
get: function () {
return once;
}
});
//# sourceMappingURL=once-Dmdw8JVT.cjs.map