kontroll 1.0.4 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -16
- package/dist/index.d.mts +25 -8
- package/dist/index.d.ts +25 -8
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
**kontroll** ("control") is a tiny, dead-simple package for function behavior controls like debounce, countdown, throttle (limit).
|
|
10
10
|
|
|
11
11
|
## Features
|
|
12
|
+
|
|
12
13
|
- **100% coverage!**
|
|
13
14
|
- **Self-explained**: for real, every functions and options have TSDoc comments to explain their behavior plus examples at a **hover** *(based on your IDE)*, apart from the already intuitive logic path.
|
|
14
15
|
- [![jsDocs.io][jsDocs-src]][jsDocs-href]
|
|
@@ -16,6 +17,7 @@
|
|
|
16
17
|
- **Promise aware**: avoid duplicated call if your promise haven't settled.
|
|
17
18
|
|
|
18
19
|
## Usage
|
|
20
|
+
|
|
19
21
|
### Install package:
|
|
20
22
|
```sh
|
|
21
23
|
# npm
|
|
@@ -28,52 +30,61 @@ yarn add kontroll
|
|
|
28
30
|
pnpm install kontroll
|
|
29
31
|
```
|
|
30
32
|
|
|
31
|
-
### Import:
|
|
33
|
+
### Import and use:
|
|
34
|
+
|
|
32
35
|
```js
|
|
33
36
|
// This package exports ESM only.
|
|
34
37
|
import {
|
|
35
38
|
clear,
|
|
36
39
|
countdown, // Can be understand as throttle no leading (execute at end of throttle instead of lead)
|
|
37
40
|
debounce,
|
|
41
|
+
getInstance,
|
|
38
42
|
throttle,
|
|
39
43
|
} from 'kontroll'
|
|
40
44
|
|
|
41
45
|
const doSum = (...numbers) => console.log(numbers.reduce((acc, cur) => acc + cur, 0))
|
|
42
|
-
const
|
|
46
|
+
const createDoSum = (...numbers) => () => doSum(numbers)
|
|
43
47
|
|
|
44
|
-
const clearCountdown = countdown(1000,
|
|
45
|
-
const clearDebounce = debounce(1000,
|
|
46
|
-
const resetThrottle = throttle(1000,
|
|
48
|
+
const clearCountdown = countdown(1000, createDoSum(1, 2), { key: 'defined', replace: false })
|
|
49
|
+
const clearDebounce = debounce(1000, createDoSum(3, 4), { key: 34, leading: false })
|
|
50
|
+
const resetThrottle = throttle(1000, createDoSum(5, 6), { trailing: false })
|
|
51
|
+
|
|
52
|
+
const debounceInstance = getInstance(34) // { timer: Timeout, callback: <fn>, finishing: false }
|
|
47
53
|
```
|
|
48
54
|
|
|
49
55
|
## **Notice**
|
|
56
|
+
|
|
50
57
|
### `key` behavior
|
|
58
|
+
|
|
51
59
|
Kontroll follows a key-first strategy, as long as things share the same key, they share the same timer.
|
|
52
60
|
|
|
53
61
|
If a `options.key` is not present, key are taken as `callback.toString()`.
|
|
54
62
|
|
|
55
|
-
|
|
56
|
-
|
|
63
|
+
*While `kontroll` supports a key-less usage, its recommended to set your key for production code, for better performance and expected behavior.*
|
|
64
|
+
|
|
65
|
+
The following cases are debounced as their callback body is the same:
|
|
57
66
|
```js
|
|
58
|
-
//
|
|
67
|
+
// * Case 1, (arrow) function with unchanged/variable-only body
|
|
59
68
|
debounce(1000, () => console.log(variable))
|
|
60
69
|
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
debounce(1000,
|
|
70
|
+
// * Case 2, callback / function returned by a function
|
|
71
|
+
const sumOneTwo = createDoSum(1, 2)
|
|
72
|
+
debounce(1000, sumOneTwo) // All this
|
|
73
|
+
debounce(1000, createDoSum(3, 4)) // 3 lines are
|
|
74
|
+
debounce(1000, createDoSum(5, 6)) // same key
|
|
65
75
|
```
|
|
66
|
-
multiple times,
|
|
67
|
-
The callback are properly debounced, because they have the same callback body.
|
|
68
76
|
|
|
69
|
-
|
|
77
|
+
Be notice, for something like
|
|
70
78
|
```js
|
|
71
79
|
debounce(1000, () => console.log('hi'))
|
|
72
80
|
debounce(1000, () => console.log('hello'))
|
|
73
81
|
```
|
|
74
|
-
The automated key are different for the two calls (because they have different callback body), so they are timed separately.
|
|
82
|
+
The automated key are different for the two calls (because they have different callback body), so they are timed separately.
|
|
83
|
+
|
|
75
84
|
In you wish them to have the same timer, you can manually set `options.key` like: `debounce(1000, () => {}, { key: 'KEY' })`
|
|
76
85
|
|
|
86
|
+
Note: the storage to check the key is set globally, if you use `kontroll` in your library, you should prefix the key with your package name.
|
|
87
|
+
|
|
77
88
|
## License
|
|
78
89
|
|
|
79
90
|
[MIT](./LICENSE) License © 2024 [NamesMT](https://github.com/NamesMT)
|
package/dist/index.d.mts
CHANGED
|
@@ -1,15 +1,32 @@
|
|
|
1
1
|
type Fn<T = void> = () => T;
|
|
2
2
|
type FnWithArgs<T = void> = (...args: any) => T;
|
|
3
3
|
interface KontrollStore {
|
|
4
|
-
[x: PropertyKey]:
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
[x: PropertyKey]: KontrollInstance;
|
|
5
|
+
}
|
|
6
|
+
interface KontrollInstance {
|
|
7
|
+
timer: ReturnType<typeof setTimeout>;
|
|
8
|
+
callback: Fn<any | Promise<any>>;
|
|
9
|
+
trailing?: [FnWithArgs, ...any];
|
|
10
|
+
finishing?: boolean;
|
|
10
11
|
}
|
|
11
12
|
declare function clear(callback: Fn): void;
|
|
12
13
|
declare function clear(key: keyof KontrollStore): void;
|
|
14
|
+
/**
|
|
15
|
+
* Returns the {@link KontrollInstance} for the given key, if exists.
|
|
16
|
+
*
|
|
17
|
+
* Could be useful to check if a promise is executing and not settled.
|
|
18
|
+
*
|
|
19
|
+
* ---
|
|
20
|
+
*
|
|
21
|
+
* Example
|
|
22
|
+
* ```
|
|
23
|
+
* debounce(1, async => await sleep(1000), { key: '1sec' })
|
|
24
|
+
* // 500ms passed
|
|
25
|
+
* getInstance('1sec')
|
|
26
|
+
* // Result: `KontrollInstance` ({ timer: Timeout, callback: <fn>, finishing: true })
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
declare function getInstance(key: keyof KontrollStore): KontrollInstance | undefined;
|
|
13
30
|
type KontrollClearer = Fn;
|
|
14
31
|
interface KontrollBaseOptions {
|
|
15
32
|
/**
|
|
@@ -138,5 +155,5 @@ interface KontrollThrottleOptions extends KontrollBaseOptions {
|
|
|
138
155
|
*/
|
|
139
156
|
declare function throttle(ms: number, callback: Fn, { key, trailing }?: KontrollThrottleOptions): KontrollClearer;
|
|
140
157
|
|
|
141
|
-
export { clear, countdown, debounce, throttle };
|
|
142
|
-
export type { KontrollBaseOptions, KontrollClearer, KontrollCountdownOptions, KontrollDebounceOptions, KontrollStore, KontrollThrottleOptions };
|
|
158
|
+
export { clear, countdown, debounce, getInstance, throttle };
|
|
159
|
+
export type { KontrollBaseOptions, KontrollClearer, KontrollCountdownOptions, KontrollDebounceOptions, KontrollInstance, KontrollStore, KontrollThrottleOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,32 @@
|
|
|
1
1
|
type Fn<T = void> = () => T;
|
|
2
2
|
type FnWithArgs<T = void> = (...args: any) => T;
|
|
3
3
|
interface KontrollStore {
|
|
4
|
-
[x: PropertyKey]:
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
[x: PropertyKey]: KontrollInstance;
|
|
5
|
+
}
|
|
6
|
+
interface KontrollInstance {
|
|
7
|
+
timer: ReturnType<typeof setTimeout>;
|
|
8
|
+
callback: Fn<any | Promise<any>>;
|
|
9
|
+
trailing?: [FnWithArgs, ...any];
|
|
10
|
+
finishing?: boolean;
|
|
10
11
|
}
|
|
11
12
|
declare function clear(callback: Fn): void;
|
|
12
13
|
declare function clear(key: keyof KontrollStore): void;
|
|
14
|
+
/**
|
|
15
|
+
* Returns the {@link KontrollInstance} for the given key, if exists.
|
|
16
|
+
*
|
|
17
|
+
* Could be useful to check if a promise is executing and not settled.
|
|
18
|
+
*
|
|
19
|
+
* ---
|
|
20
|
+
*
|
|
21
|
+
* Example
|
|
22
|
+
* ```
|
|
23
|
+
* debounce(1, async => await sleep(1000), { key: '1sec' })
|
|
24
|
+
* // 500ms passed
|
|
25
|
+
* getInstance('1sec')
|
|
26
|
+
* // Result: `KontrollInstance` ({ timer: Timeout, callback: <fn>, finishing: true })
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
declare function getInstance(key: keyof KontrollStore): KontrollInstance | undefined;
|
|
13
30
|
type KontrollClearer = Fn;
|
|
14
31
|
interface KontrollBaseOptions {
|
|
15
32
|
/**
|
|
@@ -138,5 +155,5 @@ interface KontrollThrottleOptions extends KontrollBaseOptions {
|
|
|
138
155
|
*/
|
|
139
156
|
declare function throttle(ms: number, callback: Fn, { key, trailing }?: KontrollThrottleOptions): KontrollClearer;
|
|
140
157
|
|
|
141
|
-
export { clear, countdown, debounce, throttle };
|
|
142
|
-
export type { KontrollBaseOptions, KontrollClearer, KontrollCountdownOptions, KontrollDebounceOptions, KontrollStore, KontrollThrottleOptions };
|
|
158
|
+
export { clear, countdown, debounce, getInstance, throttle };
|
|
159
|
+
export type { KontrollBaseOptions, KontrollClearer, KontrollCountdownOptions, KontrollDebounceOptions, KontrollInstance, KontrollStore, KontrollThrottleOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const
|
|
1
|
+
const t={};function c(n){const e=typeof n=="function"?n.toString():n;t[e]&&(clearTimeout(t[e].timer),delete t[e])}function a(n){return t[n]}function u(n){return()=>{c(n)}}async function s(n){if(t[n]){t[n].finishing=!0;const e=t[n].trailing;await t[n].callback(),c(n),e&&e[0](...e.slice(1))}}function o(n,e,i){if(t[i]?.finishing)return u(i);const r=setTimeout(()=>{s(i)},n);return t[i]={timer:r,callback:e},u(i)}function g(n,e,{key:i=e.toString(),replace:r}={}){return t[i]?(r&&(t[i].callback=e),u(i)):o(n,e,i)}function k(n,e,{key:i=e.toString(),leading:r}={}){if(t[i]&&!t[i].finishing)c(i);else if(r)return f(n,e,{key:i});return o(n,e,i)}function f(n,e,{key:i=e.toString(),trailing:r}={}){if(t[i])return r&&(t[i].trailing=[f,n,e,{key:i}]),u(i);const l=e();return o(n,()=>l,i)}export{c as clear,g as countdown,k as debounce,a as getInstance,f as throttle};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kontroll",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"packageManager": "pnpm@10.8.0",
|
|
6
6
|
"description": "kontroll (\"control\") is a tiny, dead-simple package for function behavior controls like debounce, countdown, throttle (limit).",
|
|
7
7
|
"author": "NamesMT <dangquoctrung123@gmail.com>",
|