@descope-ui/common 0.0.2 → 0.0.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/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
4
 
5
+ ## [0.0.3](https://github.com/descope/web-components-ui/compare/@descope-ui/common-0.0.2...@descope-ui/common-0.0.3) (2025-02-04)
6
+
5
7
  ## [0.0.2](https://github.com/descope/web-components-ui/compare/@descope-ui/common-0.0.1...@descope-ui/common-0.0.2) (2025-01-28)
6
8
 
7
9
  ## 0.0.1 (2025-01-28)
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@descope-ui/common",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "dependencies": {
5
5
  "element-internals-polyfill": "^1.3.9",
6
6
  "color": "^4.2.3",
7
+ "lodash.debounce": "4.0.8",
7
8
  "lodash.merge": "4.6.2"
8
9
  },
9
10
  "exports": {
@@ -0,0 +1,58 @@
1
+ import debounce from 'lodash.debounce';
2
+
3
+ /**
4
+ * Creates a debounced version of an async function that manages multiple pending promises and ensures proper error handling.
5
+ * When multiple calls are made within the wait period, all pending promises will be resolved/rejected with the result/error
6
+ * of the most recent call.
7
+ *
8
+ * @param {Function} func - The async function to debounce.
9
+ * @param {number} wait - The number of milliseconds to delay before calling the function.
10
+ * @returns {Function} A debounced version of the input function that returns a promise which will resolve/reject based on
11
+ * the most recent call's result.
12
+ *
13
+ * @example
14
+ * const debouncedSearch = asyncDebounce(async (query) => {
15
+ * const results = await api.search(query);
16
+ * return results;
17
+ * }, 300);
18
+ *
19
+ * // Multiple calls within 300ms will all resolve with the results from the last call
20
+ * const promise1 = debouncedSearch('search');
21
+ * const promise2 = debouncedSearch('search term');
22
+ * // Both promises will resolve with results from 'search term'
23
+ *
24
+ * // If the last call throws an error, all pending promises will be rejected
25
+ * try {
26
+ * await debouncedSearch('error term');
27
+ * } catch (error) {
28
+ * // Handle error
29
+ * }
30
+ */
31
+ export const asyncDebounce = (func, wait) => {
32
+ let currentId = 0;
33
+ const resolveSet = new Set();
34
+ const rejectSet = new Set();
35
+
36
+ const debounced = debounce((args, requestId) => {
37
+ func(...args)
38
+ .then((result) => {
39
+ if (requestId === currentId) {
40
+ resolveSet.forEach((resolve) => resolve(result));
41
+ resolveSet.clear();
42
+ }
43
+ })
44
+ .catch((error) => {
45
+ rejectSet.forEach((reject) => reject(error));
46
+ rejectSet.clear();
47
+ });
48
+ }, wait);
49
+
50
+ return (...args) => {
51
+ currentId++; // Increment for new request
52
+ return new Promise((resolve, reject) => {
53
+ resolveSet.add(resolve);
54
+ rejectSet.add(reject);
55
+ debounced(args, currentId);
56
+ });
57
+ };
58
+ };
@@ -1,10 +1,13 @@
1
+ export { asyncDebounce } from './asyncDebounce';
2
+
1
3
  export const kebabCase = (str) =>
2
4
  str
3
5
  .replace(/([a-z])([A-Z])/g, '$1-$2')
4
6
  .replace(/[\s_.]+/g, '-')
5
7
  .toLowerCase();
6
8
 
7
- export const kebabCaseJoin = (...args) => kebabCase(args.filter((arg) => !!arg).join('-'));
9
+ export const kebabCaseJoin = (...args) =>
10
+ kebabCase(args.filter((arg) => !!arg).join('-'));
8
11
 
9
12
  export const upperFirst = (str) => str.charAt(0).toUpperCase() + str.slice(1);
10
13