@@ -122,6 +122,10 @@ export async function runPreviewServer({
122
122
let project = initialProject ;
123
123
let docsDefinition : DocsV1Read . DocsDefinition | undefined ;
124
124
125
+ let reloadTimer : NodeJS . Timeout | null = null ;
126
+ let isReloading = false ;
127
+ const RELOAD_DEBOUNCE_MS = 1000 ;
128
+
125
129
const reloadDocsDefinition = async ( ) => {
126
130
context . logger . info ( "Reloading docs..." ) ;
127
131
const startTime = Date . now ( ) ;
@@ -161,25 +165,45 @@ export async function runPreviewServer({
161
165
const watcher = new Watcher ( [ absoluteFilePathToFern , ...additionalFilepaths ] , {
162
166
recursive : true ,
163
167
ignoreInitial : true ,
164
- debounce : 1000 ,
168
+ debounce : 100 ,
165
169
renameDetection : true
166
170
} ) ;
171
+
167
172
// eslint-disable-next-line @typescript-eslint/no-misused-promises
168
173
watcher . on ( "all" , async ( event : string , targetPath : string , _targetPathNext : string ) => {
169
174
context . logger . info ( chalk . dim ( `[${ event } ] ${ targetPath } ` ) ) ;
170
- sendData ( {
171
- version : 1 ,
172
- type : "startReload"
173
- } ) ;
174
- // after the docsDefinition is reloaded, send a message to all connected clients to reload the page
175
- const reloadedDocsDefinition = await reloadDocsDefinition ( ) ;
176
- if ( reloadedDocsDefinition != null ) {
177
- docsDefinition = reloadedDocsDefinition ;
175
+
176
+ // Don't schedule another reload if one is in progress
177
+ if ( isReloading ) {
178
+ return ;
178
179
}
179
- sendData ( {
180
- version : 1 ,
181
- type : "finishReload"
182
- } ) ;
180
+
181
+ // Clear any existing timer
182
+ if ( reloadTimer != null ) {
183
+ clearTimeout ( reloadTimer ) ;
184
+ }
185
+
186
+ // Set up new timer
187
+ reloadTimer = setTimeout ( ( ) => {
188
+ void ( async ( ) => {
189
+ isReloading = true ;
190
+ sendData ( {
191
+ version : 1 ,
192
+ type : "startReload"
193
+ } ) ;
194
+
195
+ const reloadedDocsDefinition = await reloadDocsDefinition ( ) ;
196
+ if ( reloadedDocsDefinition != null ) {
197
+ docsDefinition = reloadedDocsDefinition ;
198
+ }
199
+
200
+ sendData ( {
201
+ version : 1 ,
202
+ type : "finishReload"
203
+ } ) ;
204
+ isReloading = false ;
205
+ } ) ( ) ;
206
+ } , RELOAD_DEBOUNCE_MS ) ;
183
207
} ) ;
184
208
185
209
// eslint-disable-next-line @typescript-eslint/no-misused-promises
0 commit comments