Skip to content

Commit 180cf98

Browse files
author
colinmcneil
committed
Fix issue with debounce causing 2+ secrets to not be set
1 parent cf30bb0 commit 180cf98

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/extension/ui/src/components/CatalogGrid.tsx

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,28 @@ const filterCatalog = (catalogItems: CatalogItemWithName[], registryItems: { [ke
2626

2727
const NEVER_SHOW_AGAIN_KEY = 'registry-sync-never-show-again';
2828

29-
const debounce = (inner: (...args: any[]) => Promise<void>, ms = 0) => {
30-
let timer: NodeJS.Timeout | null = null;
31-
let resolves: ((value: void | PromiseLike<void>) => void)[] = [];
32-
29+
const debounce = (func: (...args: any[]) => Promise<void>, wait: number, immediate: boolean) => {
30+
let timeout: NodeJS.Timeout | null = null;
3331
return function (...args: any[]) {
34-
// Run the function after a certain amount of time
35-
if (timer) {
36-
clearTimeout(timer);
37-
}
38-
timer = setTimeout(() => {
39-
// Get the result of the inner function, then apply it to the resolve function of
40-
// each promise that has been created since the last time the inner function was run
41-
let result = inner(...args);
42-
resolves.forEach(r => r(result));
43-
resolves = [];
44-
}, ms);
45-
46-
return new Promise(r => resolves.push(r));
47-
};
32+
return new Promise((resolve) => {
33+
if (timeout) {
34+
clearTimeout(timeout);
35+
}
36+
timeout = setTimeout(() => {
37+
timeout = null
38+
if (!immediate) {
39+
// @ts-expect-error
40+
Promise.resolve(func.apply(this as any, [...args])).then(resolve)
41+
}
42+
}, wait)
43+
if (immediate && !timeout) {
44+
// @ts-expect-error
45+
Promise.resolve(func.apply(this as any, [...args])).then(resolve)
46+
}
47+
})
48+
}
4849
}
50+
4951
export const CatalogGrid: React.FC<CatalogGridProps> = ({
5052
registryItems,
5153
canRegister,
@@ -96,7 +98,7 @@ export const CatalogGrid: React.FC<CatalogGridProps> = ({
9698
const debouncedAddSecret = debounce(async (client: v1.DockerDesktopClient, name: string, value: string) => {
9799
await Secrets.addSecret(client, { name, value, policies: [MCP_POLICY_NAME] })
98100
loadSecrets();
99-
}, 1000);
101+
}, 1000, false);
100102

101103
const registerCatalogItem = async (item: CatalogItemWithName) => {
102104
try {

0 commit comments

Comments
 (0)