@canonical/utils 0.9.0-experimental.9 → 0.9.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/dist/esm/debounce.js
CHANGED
@@ -5,22 +5,26 @@
|
|
5
5
|
* @template F The type of the function to debounce
|
6
6
|
* @param fn - The function to debounce.
|
7
7
|
* @param delay - The time in milliseconds to wait before calling the function after the last call.
|
8
|
-
* @returns A debounced version of the function that returns a promise.
|
8
|
+
* @returns A debounced version of the function that returns a promise and has a cancel method.
|
9
9
|
*
|
10
10
|
* @example
|
11
11
|
* // Example usage of the debounce function
|
12
12
|
* const debouncedFetchData = debounce(async (query: string) => {
|
13
|
-
*
|
14
|
-
*
|
13
|
+
* const response = await fetch(`/api/search?q=${query}`);
|
14
|
+
* return response.json();
|
15
15
|
* }, 300);
|
16
16
|
*
|
17
17
|
* // Calling the debounced function multiple times
|
18
18
|
* debouncedFetchData("hello").then(console.log); // Will only call fetch once after 300ms delay
|
19
19
|
* debouncedFetchData("world").then(console.log); // Will cancel the previous call and make a new one
|
20
|
+
*
|
21
|
+
* // To cancel the debounced call before it executes
|
22
|
+
* const promise = debouncedFetchData("to be cancelled");
|
23
|
+
* promise.cancel();
|
20
24
|
*/
|
21
25
|
export default function debounce(fn, delay) {
|
22
26
|
let timeoutId = null;
|
23
|
-
|
27
|
+
const debounceExec = (...args) => {
|
24
28
|
// Timer already exists, clear it. This way we avoid resolving more than once.
|
25
29
|
if (timeoutId) {
|
26
30
|
clearTimeout(timeoutId);
|
@@ -29,7 +33,8 @@ export default function debounce(fn, delay) {
|
|
29
33
|
timeoutId = setTimeout(async () => {
|
30
34
|
// Timer hasn't been cancelled, call the function
|
31
35
|
try {
|
32
|
-
|
36
|
+
const result = await fn(...args);
|
37
|
+
resolve(result);
|
33
38
|
}
|
34
39
|
catch (error) {
|
35
40
|
reject(error);
|
@@ -37,5 +42,13 @@ export default function debounce(fn, delay) {
|
|
37
42
|
}, delay);
|
38
43
|
});
|
39
44
|
};
|
45
|
+
// Clear the timeout and reject the promise
|
46
|
+
debounceExec.cancel = () => {
|
47
|
+
if (timeoutId) {
|
48
|
+
clearTimeout(timeoutId);
|
49
|
+
timeoutId = null;
|
50
|
+
}
|
51
|
+
};
|
52
|
+
return debounceExec;
|
40
53
|
}
|
41
54
|
//# sourceMappingURL=debounce.js.map
|
package/dist/esm/debounce.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"debounce.js","sourceRoot":"","sources":["../../src/debounce.ts"],"names":[],"mappings":"AAAA
|
1
|
+
{"version":3,"file":"debounce.js","sourceRoot":"","sources":["../../src/debounce.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAG9B,EAAK,EACL,KAAa;IAIb,IAAI,SAAS,GAAyC,IAAI,CAAC;IAE3D,MAAM,YAAY,GAAG,CAAC,GAAG,IAAmB,EAA0B,EAAE;QACtE,8EAA8E;QAC9E,IAAI,SAAS,EAAE,CAAC;YACd,YAAY,CAAC,SAAS,CAAC,CAAC;QAC1B,CAAC;QAED,OAAO,IAAI,OAAO,CAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACpD,SAAS,GAAG,UAAU,CAAC,KAAK,IAAI,EAAE;gBAChC,iDAAiD;gBACjD,IAAI,CAAC;oBACH,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;oBACjC,OAAO,CAAC,MAAM,CAAC,CAAC;gBAClB,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,MAAM,CAAC,KAAK,CAAC,CAAC;gBAChB,CAAC;YACH,CAAC,EAAE,KAAK,CAAC,CAAC;QACZ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,2CAA2C;IAC3C,YAAY,CAAC,MAAM,GAAG,GAAG,EAAE;QACzB,IAAI,SAAS,EAAE,CAAC;YACd,YAAY,CAAC,SAAS,CAAC,CAAC;YACxB,SAAS,GAAG,IAAI,CAAC;QACnB,CAAC;IACH,CAAC,CAAC;IAEF,OAAO,YAAY,CAAC;AACtB,CAAC"}
|
package/dist/types/debounce.d.ts
CHANGED
@@ -5,18 +5,24 @@
|
|
5
5
|
* @template F The type of the function to debounce
|
6
6
|
* @param fn - The function to debounce.
|
7
7
|
* @param delay - The time in milliseconds to wait before calling the function after the last call.
|
8
|
-
* @returns A debounced version of the function that returns a promise.
|
8
|
+
* @returns A debounced version of the function that returns a promise and has a cancel method.
|
9
9
|
*
|
10
10
|
* @example
|
11
11
|
* // Example usage of the debounce function
|
12
12
|
* const debouncedFetchData = debounce(async (query: string) => {
|
13
|
-
*
|
14
|
-
*
|
13
|
+
* const response = await fetch(`/api/search?q=${query}`);
|
14
|
+
* return response.json();
|
15
15
|
* }, 300);
|
16
16
|
*
|
17
17
|
* // Calling the debounced function multiple times
|
18
18
|
* debouncedFetchData("hello").then(console.log); // Will only call fetch once after 300ms delay
|
19
19
|
* debouncedFetchData("world").then(console.log); // Will cancel the previous call and make a new one
|
20
|
+
*
|
21
|
+
* // To cancel the debounced call before it executes
|
22
|
+
* const promise = debouncedFetchData("to be cancelled");
|
23
|
+
* promise.cancel();
|
20
24
|
*/
|
21
|
-
export default function debounce<F extends (...args: Parameters<F>) => ReturnType<F>>(fn: F, delay: number): (...args: Parameters<F>) => Promise<ReturnType<F
|
25
|
+
export default function debounce<F extends (...args: Parameters<F>) => ReturnType<F>>(fn: F, delay: number): ((...args: Parameters<F>) => Promise<ReturnType<F>>) & {
|
26
|
+
cancel: () => void;
|
27
|
+
};
|
22
28
|
//# sourceMappingURL=debounce.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"debounce.d.ts","sourceRoot":"","sources":["../../src/debounce.ts"],"names":[],"mappings":"AAAA
|
1
|
+
{"version":3,"file":"debounce.d.ts","sourceRoot":"","sources":["../../src/debounce.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAC9B,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,EAEnD,EAAE,EAAE,CAAC,EACL,KAAK,EAAE,MAAM,GACZ,CAAC,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG;IACxD,MAAM,EAAE,MAAM,IAAI,CAAC;CACpB,CA+BA"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"invariant.d.ts","sourceRoot":"","sources":["../../src/invariant.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;
|
1
|
+
{"version":3,"file":"invariant.d.ts","sourceRoot":"","sources":["../../src/invariant.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;yBAED,WAAW,OAAO,EAClB,UAAU,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,EACjC,eAA8B,KAC7B,QAAQ,SAAS;AAJpB,wBASE"}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@canonical/utils",
|
3
3
|
"description": "Standard utility functions for Canonical's Web Engineering team",
|
4
|
-
"version": "0.9.0
|
4
|
+
"version": "0.9.0",
|
5
5
|
"type": "module",
|
6
6
|
"module": "dist/esm/index.js",
|
7
7
|
"types": "dist/types/index.d.ts",
|
@@ -30,7 +30,8 @@
|
|
30
30
|
"homepage": "https://github.com/canonical/ds25#readme",
|
31
31
|
"scripts": {
|
32
32
|
"build": "tsc -p tsconfig.build.json",
|
33
|
-
"
|
33
|
+
"build:all": "tsc -p tsconfig.build.json",
|
34
|
+
"check": "bun run check:biome",
|
34
35
|
"check:fix": "bun run check:biome:fix && bun run check:ts",
|
35
36
|
"check:biome": "biome check",
|
36
37
|
"check:biome:fix": "biome check --write",
|
@@ -38,9 +39,9 @@
|
|
38
39
|
},
|
39
40
|
"devDependencies": {
|
40
41
|
"@biomejs/biome": "^1.9.4",
|
41
|
-
"@canonical/biome-config": "^0.9.0
|
42
|
-
"@canonical/typescript-config-base": "^0.9.0
|
43
|
-
"typescript": "^5.
|
42
|
+
"@canonical/biome-config": "^0.9.0",
|
43
|
+
"@canonical/typescript-config-base": "^0.9.0",
|
44
|
+
"typescript": "^5.8.3"
|
44
45
|
},
|
45
|
-
"gitHead": "
|
46
|
+
"gitHead": "59f31f7d58a1794d36dae3cf99691b6eeb3f4042"
|
46
47
|
}
|