@canonical/utils 0.9.0-experimental.4 → 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.
@@ -5,36 +5,36 @@
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
- * const response = await fetch(`/api/search?q=${query}`);
14
- * return response.json();
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
- let promiseReject = null;
24
- return (...args) => {
25
- // Timer already exists, clear it and reject the promise
27
+ const debounceExec = (...args) => {
28
+ // Timer already exists, clear it. This way we avoid resolving more than once.
26
29
  if (timeoutId) {
27
30
  clearTimeout(timeoutId);
28
- if (promiseReject) {
29
- promiseReject();
30
- }
31
31
  }
32
32
  return new Promise((resolve, reject) => {
33
- promiseReject = reject;
34
33
  timeoutId = setTimeout(async () => {
35
34
  // Timer hasn't been cancelled, call the function
36
35
  try {
37
- resolve(fn(...args));
36
+ const result = await fn(...args);
37
+ resolve(result);
38
38
  }
39
39
  catch (error) {
40
40
  reject(error);
@@ -42,5 +42,13 @@ export default function debounce(fn, delay) {
42
42
  }, delay);
43
43
  });
44
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;
45
53
  }
46
54
  //# sourceMappingURL=debounce.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"debounce.js","sourceRoot":"","sources":["../../src/debounce.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAE9B,EAAK,EAAE,KAAa;IACpB,IAAI,SAAS,GAAyC,IAAI,CAAC;IAC3D,IAAI,aAAa,GAAuC,IAAI,CAAC;IAE7D,OAAO,CAAC,GAAG,IAAmB,EAA0B,EAAE;QACxD,wDAAwD;QACxD,IAAI,SAAS,EAAE,CAAC;YACd,YAAY,CAAC,SAAS,CAAC,CAAC;YACxB,IAAI,aAAa,EAAE,CAAC;gBAClB,aAAa,EAAE,CAAC;YAClB,CAAC;QACH,CAAC;QAED,OAAO,IAAI,OAAO,CAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACpD,aAAa,GAAG,MAAM,CAAC;YACvB,SAAS,GAAG,UAAU,CAAC,KAAK,IAAI,EAAE;gBAChC,iDAAiD;gBACjD,IAAI,CAAC;oBACH,OAAO,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;gBACvB,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;AACJ,CAAC"}
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"}
@@ -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
- * const response = await fetch(`/api/search?q=${query}`);
14
- * return response.json();
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;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAC9B,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,EACnD,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAyB1E"}
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;oCAEU,OAAO,YACR,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,sBAEhC,QAAQ,SAAS;AAJpB,wBASE"}
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-experimental.4",
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
- "check": "bun run check:biome && bun run check:ts",
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-experimental.2",
42
- "@canonical/typescript-config-base": "^0.9.0-experimental.2",
43
- "typescript": "^5.7.2"
42
+ "@canonical/biome-config": "^0.9.0",
43
+ "@canonical/typescript-config-base": "^0.9.0",
44
+ "typescript": "^5.8.3"
44
45
  },
45
- "gitHead": "1d084380afa970ea924d774ced9c2a3f6a6c027e"
46
+ "gitHead": "59f31f7d58a1794d36dae3cf99691b6eeb3f4042"
46
47
  }