Last change
on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | declare 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 |
|
---|
| 12 | declare 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 | @example
|
---|
| 20 | ```
|
---|
| 21 | import onetime = require('onetime');
|
---|
| 22 |
|
---|
| 23 | let i = 0;
|
---|
| 24 |
|
---|
| 25 | const foo = onetime(() => ++i);
|
---|
| 26 |
|
---|
| 27 | foo(); //=> 1
|
---|
| 28 | foo(); //=> 1
|
---|
| 29 | foo(); //=> 1
|
---|
| 30 |
|
---|
| 31 | onetime.callCount(foo); //=> 3
|
---|
| 32 | ```
|
---|
| 33 | */
|
---|
| 34 | <ArgumentsType extends unknown[], ReturnType>(
|
---|
| 35 | fn: (...arguments: ArgumentsType) => ReturnType,
|
---|
| 36 | options?: onetime.Options
|
---|
| 37 | ): (...arguments: ArgumentsType) => ReturnType;
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | Get the number of times `fn` has been called.
|
---|
| 41 |
|
---|
| 42 | @param fn - Function to get call count from.
|
---|
| 43 | @returns A number representing how many times `fn` has been called.
|
---|
| 44 |
|
---|
| 45 | @example
|
---|
| 46 | ```
|
---|
| 47 | import onetime = require('onetime');
|
---|
| 48 |
|
---|
| 49 | const foo = onetime(() => {});
|
---|
| 50 | foo();
|
---|
| 51 | foo();
|
---|
| 52 | foo();
|
---|
| 53 |
|
---|
| 54 | console.log(onetime.callCount(foo));
|
---|
| 55 | //=> 3
|
---|
| 56 | ```
|
---|
| 57 | */
|
---|
| 58 | callCount(fn: (...arguments: any[]) => unknown): number;
|
---|
| 59 |
|
---|
| 60 | // TODO: Remove this for the next major release
|
---|
| 61 | default: typeof onetime;
|
---|
| 62 | };
|
---|
| 63 |
|
---|
| 64 | export = onetime;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.