kontroll 1.0.1 → 1.0.2

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 CHANGED
@@ -55,12 +55,13 @@ If a `options.key` is not present, key are taken as `callback.toString()`.
55
55
  <!-- Explaining the basic for sleep derived beginner :> -->
56
56
  If you call something like:
57
57
  ```js
58
- // * Case 1, (arrow) function with unchanged/variable-only body
58
+ // // * Case 1, (arrow) function with unchanged/variable-only body
59
59
  debounce(1000, () => console.log(variable))
60
60
 
61
- // * Case 2, declared function
61
+
62
+ // // * Case 2, declared function
62
63
  debounce(1000, makeDoSum(1, 2))
63
- // For declared function, input could be changed.
64
+ // For declared function, it's parameter could be changed and still share the same key.
64
65
  debounce(1000, makeDoSum(2, 3))
65
66
  ```
66
67
  multiple times,
package/dist/index.d.mts CHANGED
@@ -1,10 +1,11 @@
1
1
  type Fn<T = void> = () => T;
2
2
  type FnWithArgs<T = void> = (...args: any) => T;
3
3
  interface KontrollStore {
4
- [x: keyof any]: {
4
+ [x: PropertyKey]: {
5
5
  timer: ReturnType<typeof setTimeout>;
6
- callback: Fn;
6
+ callback: Fn<any | Promise<any>>;
7
7
  trailing?: [FnWithArgs, ...any];
8
+ finishing?: boolean;
8
9
  };
9
10
  }
10
11
  declare function clear(callback: Fn): void;
@@ -48,7 +49,7 @@ interface KontrollCountdownOptions extends KontrollBaseOptions {
48
49
  * // Result: 2
49
50
  * ```
50
51
  */
51
- declare function countdown(ms: number, callback: Fn, options?: KontrollCountdownOptions): KontrollClearer;
52
+ declare function countdown(ms: number, callback: Fn, { key, replace }?: KontrollCountdownOptions): KontrollClearer;
52
53
  interface KontrollDebounceOptions extends KontrollBaseOptions {
53
54
  /**
54
55
  * Avoiding initial wait for first call
@@ -83,7 +84,7 @@ interface KontrollDebounceOptions extends KontrollBaseOptions {
83
84
  * // Result: 3
84
85
  * ```
85
86
  */
86
- declare function debounce(ms: number, callback: Fn, options?: KontrollDebounceOptions): KontrollClearer;
87
+ declare function debounce(ms: number, callback: Fn, { key, leading }?: KontrollDebounceOptions): KontrollClearer;
87
88
  interface KontrollThrottleOptions extends KontrollBaseOptions {
88
89
  /**
89
90
  * Perform additional execution with last received arguments
@@ -124,6 +125,6 @@ interface KontrollThrottleOptions extends KontrollBaseOptions {
124
125
  * ```
125
126
  *
126
127
  */
127
- declare function throttle(ms: number, callback: Fn, options?: KontrollThrottleOptions): KontrollClearer;
128
+ declare function throttle(ms: number, callback: Fn, { key, trailing }?: KontrollThrottleOptions): KontrollClearer;
128
129
 
129
130
  export { type KontrollBaseOptions, type KontrollClearer, type KontrollCountdownOptions, type KontrollDebounceOptions, type KontrollStore, type KontrollThrottleOptions, clear, countdown, debounce, throttle };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  type Fn<T = void> = () => T;
2
2
  type FnWithArgs<T = void> = (...args: any) => T;
3
3
  interface KontrollStore {
4
- [x: keyof any]: {
4
+ [x: PropertyKey]: {
5
5
  timer: ReturnType<typeof setTimeout>;
6
- callback: Fn;
6
+ callback: Fn<any | Promise<any>>;
7
7
  trailing?: [FnWithArgs, ...any];
8
+ finishing?: boolean;
8
9
  };
9
10
  }
10
11
  declare function clear(callback: Fn): void;
@@ -48,7 +49,7 @@ interface KontrollCountdownOptions extends KontrollBaseOptions {
48
49
  * // Result: 2
49
50
  * ```
50
51
  */
51
- declare function countdown(ms: number, callback: Fn, options?: KontrollCountdownOptions): KontrollClearer;
52
+ declare function countdown(ms: number, callback: Fn, { key, replace }?: KontrollCountdownOptions): KontrollClearer;
52
53
  interface KontrollDebounceOptions extends KontrollBaseOptions {
53
54
  /**
54
55
  * Avoiding initial wait for first call
@@ -83,7 +84,7 @@ interface KontrollDebounceOptions extends KontrollBaseOptions {
83
84
  * // Result: 3
84
85
  * ```
85
86
  */
86
- declare function debounce(ms: number, callback: Fn, options?: KontrollDebounceOptions): KontrollClearer;
87
+ declare function debounce(ms: number, callback: Fn, { key, leading }?: KontrollDebounceOptions): KontrollClearer;
87
88
  interface KontrollThrottleOptions extends KontrollBaseOptions {
88
89
  /**
89
90
  * Perform additional execution with last received arguments
@@ -124,6 +125,6 @@ interface KontrollThrottleOptions extends KontrollBaseOptions {
124
125
  * ```
125
126
  *
126
127
  */
127
- declare function throttle(ms: number, callback: Fn, options?: KontrollThrottleOptions): KontrollClearer;
128
+ declare function throttle(ms: number, callback: Fn, { key, trailing }?: KontrollThrottleOptions): KontrollClearer;
128
129
 
129
130
  export { type KontrollBaseOptions, type KontrollClearer, type KontrollCountdownOptions, type KontrollDebounceOptions, type KontrollStore, type KontrollThrottleOptions, clear, countdown, debounce, throttle };
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- const i={};function o(n){const t=typeof n=="function"?n.toString():n;i[t]&&(clearTimeout(i[t].timer),delete i[t])}async function f(n){if(i[n]){const t=i[n].trailing;await i[n].callback(),o(n),t&&t[0](...t.slice(1))}}function l(n){return()=>{o(n)}}function u(n,t,c){const e=setTimeout(()=>{f(n)},t);return i[n]={timer:e,callback:c},l(n)}function s(n,t,c={}){const{key:e=t.toString(),replace:r=!1}=c;return i[e]?(r&&(i[e].callback=t),l(e)):u(e,n,t)}function g(n,t,c={}){const{key:e=t.toString(),leading:r=!1}=c;if(i[e])o(e);else if(r)return a(n,t,{key:e});return u(e,n,t)}function a(n,t,c={}){const{key:e=t.toString(),trailing:r=!1}=c;return i[e]?(r&&(i[e].trailing=[a,n,t,c]),l(e)):(t(),u(e,n,()=>{}))}export{o as clear,s as countdown,g as debounce,a as throttle};
1
+ const e={};function c(i){const t=typeof i=="function"?i.toString():i;e[t]&&(clearTimeout(e[t].timer),delete e[t])}function u(i){return()=>{c(i)}}async function a(i){if(e[i]){e[i].finishing=!0;const t=e[i].trailing;await e[i].callback(),c(i),t&&t[0](...t.slice(1))}}function l(i,t,n){if(e[n]?.finishing)return u(n);const r=setTimeout(()=>{a(n)},i);return e[n]={timer:r,callback:t},u(n)}function g(i,t,{key:n=t.toString(),replace:r}={}){return e[n]?(r&&(e[n].callback=t),u(n)):l(i,t,n)}function s(i,t,{key:n=t.toString(),leading:r}={}){if(e[n]&&!e[n].finishing)c(n);else if(r)return f(i,t,{key:n});return l(i,t,n)}function f(i,t,{key:n=t.toString(),trailing:r}={}){return e[n]?(r&&(e[n].trailing=[f,i,t,{key:n}]),u(n)):(t(),l(i,()=>{},n))}export{c as clear,g as countdown,s as debounce,f as throttle};
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "kontroll",
3
3
  "type": "module",
4
- "version": "1.0.1",
5
- "packageManager": "pnpm@9.1.4",
4
+ "version": "1.0.2",
5
+ "packageManager": "pnpm@9.6.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>",
8
8
  "license": "MIT",
@@ -58,17 +58,16 @@
58
58
  "prepublishOnly": "pnpm run build"
59
59
  },
60
60
  "devDependencies": {
61
- "@antfu/eslint-config": "^2.19.2",
62
- "@types/node": "^20.13.0",
63
- "@unocss/eslint-plugin": "^0.58.9",
64
- "@vitest/coverage-v8": "^1.6.0",
65
- "eslint": "^8.57.0",
66
- "lint-staged": "^15.2.5",
61
+ "@antfu/eslint-config": "^2.24.0",
62
+ "@types/node": "^20.14.13",
63
+ "@vitest/coverage-v8": "^2.0.5",
64
+ "eslint": "^9.8.0",
65
+ "lint-staged": "^15.2.7",
67
66
  "simple-git-hooks": "^2.11.1",
68
- "tsx": "^4.11.0",
69
- "typescript": "^5.4.5",
67
+ "tsx": "^4.16.3",
68
+ "typescript": "^5.5.4",
70
69
  "unbuild": "^2.0.0",
71
- "vitest": "^1.6.0"
70
+ "vitest": "^2.0.5"
72
71
  },
73
72
  "pnpm": {
74
73
  "overrides": {