@@ -98,6 +98,11 @@ export function registerDevCommand(cli: CAC) {
9898 isSyncing : false ,
9999 } ;
100100
101+ const reloadState = {
102+ timeouts : new Map < string , ReturnType < typeof setTimeout > > ( ) ,
103+ reloading : new Set < string > ( ) ,
104+ } ;
105+
101106 async function syncCommands ( ) {
102107 if ( syncState . isSyncing ) return ;
103108 syncState . isSyncing = true ;
@@ -206,7 +211,7 @@ export function registerDevCommand(cli: CAC) {
206211 mod as { default : import ( "@djs-core/runtime" ) . Modal }
207212 ) . default ;
208213 if ( ! modal ) return ;
209- if ( ! modal . baseCustomId ) modal . setCustomId ( route ) ;
214+ modal . setCustomId ( route ) ;
210215 client . modalsHandler . add ( modal ) ;
211216 } ,
212217 unload : async ( route ) => client . modalsHandler . delete ( route ) ,
@@ -234,6 +239,23 @@ export function registerDevCommand(cli: CAC) {
234239 const route = config . getRoute ( absPath , config . dir ) ;
235240 if ( ! route ) return ;
236241
242+ // Debounce reloads to prevent multiple rapid reloads
243+ if ( reloadState . timeouts . has ( absPath ) ) {
244+ clearTimeout ( reloadState . timeouts . get ( absPath ) ! ) ;
245+ }
246+ if ( reloadState . reloading . has ( absPath ) ) {
247+ return ;
248+ }
249+
250+ reloadState . reloading . add ( absPath ) ;
251+ reloadState . timeouts . set (
252+ absPath ,
253+ setTimeout ( ( ) => {
254+ reloadState . reloading . delete ( absPath ) ;
255+ reloadState . timeouts . delete ( absPath ) ;
256+ } , 100 ) ,
257+ ) ;
258+
237259 if ( config . sync ) syncState . pendingReloads . add ( absPath ) ;
238260
239261 try {
@@ -265,12 +287,16 @@ export function registerDevCommand(cli: CAC) {
265287 await config . unload ( route ) ;
266288 } else if ( config . label === "route" ) {
267289 await config . unload ( route ) ;
290+ } else if ( config . label === "modal" ) {
291+ await config . unload ( route ) ;
268292 }
269293
270294 await config . load ( mod , route , absPath ) ;
271295 config . map . set ( absPath , route ) ;
272296
273297 if ( config . sync ) requestSync ( ) ;
298+ reloadState . reloading . delete ( absPath ) ;
299+ reloadState . timeouts . delete ( absPath ) ;
274300 } catch ( error : unknown ) {
275301 if ( retries > 0 ) {
276302 await sleep ( 150 ) ;
@@ -288,6 +314,8 @@ export function registerDevCommand(cli: CAC) {
288314 ) ;
289315 }
290316 if ( config . sync ) syncState . pendingReloads . delete ( absPath ) ;
317+ reloadState . reloading . delete ( absPath ) ;
318+ reloadState . timeouts . delete ( absPath ) ;
291319 }
292320 }
293321
0 commit comments