kontroll 0.1.1 → 0.1.3
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 +2 -2
- package/dist/index.d.ts +129 -0
- package/package.json +1 -5
package/README.md
CHANGED
|
@@ -87,5 +87,5 @@ In you wish them to have the same timer, you can manually set `options.key` like
|
|
|
87
87
|
[codecov-href]: https://codecov.io/gh/namesmt/kontroll
|
|
88
88
|
[license-src]: https://img.shields.io/github/license/namesmt/kontroll.svg?style=flat&colorA=18181B&colorB=F0DB4F
|
|
89
89
|
[license-href]: https://github.com/namesmt/kontroll/blob/main/LICENSE
|
|
90
|
-
[bundlejs-src]: https://deno.bundlejs.com/badge?q=kontroll
|
|
91
|
-
[bundlejs-href]: https://bundlejs.com/?q=
|
|
90
|
+
[bundlejs-src]: https://deno.bundlejs.com/badge?q=kontroll&colorA=18181B&colorB=F0DB4F
|
|
91
|
+
[bundlejs-href]: https://bundlejs.com/?q=kontroll
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
type Fn<T = void> = () => T;
|
|
2
|
+
type FnWithArgs<T = void> = (...args: any) => T;
|
|
3
|
+
interface KontrollStore {
|
|
4
|
+
[x: keyof any]: {
|
|
5
|
+
timer: ReturnType<typeof setTimeout>;
|
|
6
|
+
callback: Fn;
|
|
7
|
+
trailing?: [FnWithArgs, ...any];
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
declare function clear(callback: Fn): void;
|
|
11
|
+
declare function clear(key: keyof KontrollStore): void;
|
|
12
|
+
type KontrollClearer = Fn;
|
|
13
|
+
interface KontrollBaseOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Specify a known key if needed
|
|
16
|
+
* @default string // .toString() of the inputted callback
|
|
17
|
+
*/
|
|
18
|
+
key?: keyof KontrollStore;
|
|
19
|
+
}
|
|
20
|
+
interface KontrollCountdownOptions extends KontrollBaseOptions {
|
|
21
|
+
/**
|
|
22
|
+
* Replaces the current timed callback
|
|
23
|
+
* @default false
|
|
24
|
+
*/
|
|
25
|
+
replace?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Countdown for a period of ms then execute the first/replaced callback, based on `options.replace`.
|
|
29
|
+
* Calls while the timer haven't finished are dropped.
|
|
30
|
+
* ---
|
|
31
|
+
*
|
|
32
|
+
* Example
|
|
33
|
+
* ```
|
|
34
|
+
* countdown(1000, doSum(1), { key: 'eg' })
|
|
35
|
+
* // 500ms passed
|
|
36
|
+
* countdown(1000, doSum(2))
|
|
37
|
+
* // 500ms passed
|
|
38
|
+
* // Result: 1
|
|
39
|
+
* ```
|
|
40
|
+
* ---
|
|
41
|
+
*
|
|
42
|
+
* Example with `options.replace=true`:
|
|
43
|
+
* ```
|
|
44
|
+
* countdown(1000, doSum(1))
|
|
45
|
+
* // 500ms passed
|
|
46
|
+
* countdown(1000, doSum(2), { replace: true })
|
|
47
|
+
* // 500ms passed
|
|
48
|
+
* // Result: 2
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function countdown(ms: number, callback: Fn, options?: KontrollCountdownOptions): KontrollClearer;
|
|
52
|
+
interface KontrollDebounceOptions extends KontrollBaseOptions {
|
|
53
|
+
/**
|
|
54
|
+
* Avoiding initial wait for first call
|
|
55
|
+
* @default false
|
|
56
|
+
*/
|
|
57
|
+
leading?: boolean;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Creates a timer that will execute the callback upon finish, calls while the timer haven't finished recreates the timer.
|
|
61
|
+
* If `options.leading`, execute callback immediately for initial call.
|
|
62
|
+
* ---
|
|
63
|
+
*
|
|
64
|
+
* Example:
|
|
65
|
+
* ```
|
|
66
|
+
* debounce(1000, doSum(1))
|
|
67
|
+
* // 500ms passed
|
|
68
|
+
* debounce(1000, doSum(2))
|
|
69
|
+
* // 1000ms passed
|
|
70
|
+
* // Result: 2
|
|
71
|
+
* ```
|
|
72
|
+
* ---
|
|
73
|
+
*
|
|
74
|
+
* Example with `options.leading`:
|
|
75
|
+
* ```
|
|
76
|
+
* debounce(1000, doSum(1), { leading: true })
|
|
77
|
+
* // Result: 1
|
|
78
|
+
* // 500ms passed
|
|
79
|
+
* debounce(1000, doSum(2), { leading: true }) // leading doesn't matter anymore in this timer scope
|
|
80
|
+
* // 500ms passed
|
|
81
|
+
* debounce(1000, doSum(3))
|
|
82
|
+
* // 1000ms passed
|
|
83
|
+
* // Result: 3
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
declare function debounce(ms: number, callback: Fn, options?: KontrollDebounceOptions): KontrollClearer;
|
|
87
|
+
interface KontrollThrottleOptions extends KontrollBaseOptions {
|
|
88
|
+
/**
|
|
89
|
+
* Perform additional execution with last received arguments
|
|
90
|
+
* @default false
|
|
91
|
+
*/
|
|
92
|
+
trailing?: boolean;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Executes the callback, and bypass any subsequent calls for a period of ms.
|
|
96
|
+
* Calls while the timer haven't finished are dropped.
|
|
97
|
+
* If `options.trailing` and calls while the timer haven't finished are received,
|
|
98
|
+
* an additional execution with last received arguments is performed.
|
|
99
|
+
* ---
|
|
100
|
+
*
|
|
101
|
+
* Example:
|
|
102
|
+
* ```
|
|
103
|
+
* throttle(1000, doSum(1))
|
|
104
|
+
* // Result: 1
|
|
105
|
+
* // 500ms passed
|
|
106
|
+
* throttle(1000, doSum(2))
|
|
107
|
+
* // 9999ms passed
|
|
108
|
+
* // (Nothing)
|
|
109
|
+
* ```
|
|
110
|
+
* ---
|
|
111
|
+
*
|
|
112
|
+
* Example with `options.trailing`:
|
|
113
|
+
* ```
|
|
114
|
+
* throttle(1000, doSum(1))
|
|
115
|
+
* // Result: 1
|
|
116
|
+
* // 500ms passed
|
|
117
|
+
* throttle(1000, doSum(2), { trailing: true })
|
|
118
|
+
* // 500ms passed
|
|
119
|
+
* // Result: 2
|
|
120
|
+
* // A timer is also created, so calls after that are still dropped:
|
|
121
|
+
* throttle(1000, doSum(3))
|
|
122
|
+
* // 9999ms passed
|
|
123
|
+
* // (Nothing)
|
|
124
|
+
* ```
|
|
125
|
+
*
|
|
126
|
+
*/
|
|
127
|
+
declare function throttle(ms: number, callback: Fn, options?: KontrollThrottleOptions): KontrollClearer;
|
|
128
|
+
|
|
129
|
+
export { type KontrollBaseOptions, type KontrollClearer, type KontrollCountdownOptions, type KontrollDebounceOptions, type KontrollStore, type KontrollThrottleOptions, clear, countdown, debounce, throttle };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kontroll",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.3",
|
|
5
5
|
"packageManager": "pnpm@8.12.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>",
|
|
@@ -47,10 +47,6 @@
|
|
|
47
47
|
"prepare": "simple-git-hooks",
|
|
48
48
|
"prepublishOnly": "pnpm run build"
|
|
49
49
|
},
|
|
50
|
-
"dependencies": {
|
|
51
|
-
"consola": "^3.2.3",
|
|
52
|
-
"std-env": "^3.6.0"
|
|
53
|
-
},
|
|
54
50
|
"devDependencies": {
|
|
55
51
|
"@antfu/eslint-config": "^2.4.4",
|
|
56
52
|
"@types/node": "^20.10.4",
|