UNPKG

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