版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

60 wiersze
1.1 KiB

  1. export interface 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 - 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 - 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;