@@ -26,26 +26,28 @@ const filterCatalog = (catalogItems: CatalogItemWithName[], registryItems: { [ke
26
26
27
27
const NEVER_SHOW_AGAIN_KEY = 'registry-sync-never-show-again' ;
28
28
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 ;
33
31
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
+ }
48
49
}
50
+
49
51
export const CatalogGrid : React . FC < CatalogGridProps > = ( {
50
52
registryItems,
51
53
canRegister,
@@ -96,7 +98,7 @@ export const CatalogGrid: React.FC<CatalogGridProps> = ({
96
98
const debouncedAddSecret = debounce ( async ( client : v1 . DockerDesktopClient , name : string , value : string ) => {
97
99
await Secrets . addSecret ( client , { name, value, policies : [ MCP_POLICY_NAME ] } )
98
100
loadSecrets ( ) ;
99
- } , 1000 ) ;
101
+ } , 1000 , false ) ;
100
102
101
103
const registerCatalogItem = async ( item : CatalogItemWithName ) => {
102
104
try {
0 commit comments