index.d.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. export type Options = {
  2. /**
  3. Throw an error when called more than once.
  4. @default false
  5. */
  6. readonly throw?: boolean;
  7. };
  8. declare const onetime: {
  9. /**
  10. Ensure a function is only called once. When called multiple times it will return the return value from the first call.
  11. @param fn - The function that should only be called once.
  12. @returns A function that only calls `fn` once.
  13. @example
  14. ```
  15. import onetime from 'onetime';
  16. let index = 0;
  17. const foo = onetime(() => ++index);
  18. foo(); //=> 1
  19. foo(); //=> 1
  20. foo(); //=> 1
  21. onetime.callCount(foo); //=> 3
  22. ```
  23. */
  24. <ArgumentsType extends unknown[], ReturnType>(
  25. fn: (...arguments_: ArgumentsType) => ReturnType,
  26. options?: Options
  27. ): (...arguments_: ArgumentsType) => ReturnType;
  28. /**
  29. Get the number of times `fn` has been called.
  30. @param fn - The function to get call count from.
  31. @returns A number representing how many times `fn` has been called.
  32. @example
  33. ```
  34. import onetime from 'onetime';
  35. const foo = onetime(() => {});
  36. foo();
  37. foo();
  38. foo();
  39. console.log(onetime.callCount(foo));
  40. //=> 3
  41. ```
  42. */
  43. callCount(fn: (...arguments_: any[]) => unknown): number;
  44. };
  45. export default onetime;