Skip to content

Commit 20d6fef

Browse files
authored
fix: don't swallow file changes when a lot of them come in (#6075)
* fix: don't swallow file changes when a lot of them come in * fix: reintroduce debounce, but collect paths
1 parent f7518f9 commit 20d6fef

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/lib/edge-functions/registry.mjs

+8-5
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,15 @@ export class EdgeFunctionsRegistry {
269269
}
270270

271271
/**
272-
* @param {string} path
272+
* @param {string[]} paths
273273
* @returns {Promise<void>}
274274
*/
275-
async #handleFileChange(path) {
275+
async #handleFileChange(paths) {
276276
const matchingFunctions = new Set(
277-
[this.#functionPaths.get(path), ...(this.#dependencyPaths.get(path) || [])].filter(Boolean),
277+
[
278+
...paths.map((path) => this.#functionPaths.get(path)),
279+
...paths.flatMap((path) => this.#dependencyPaths.get(path)),
280+
].filter(Boolean),
278281
)
279282

280283
// If the file is not associated with any function, there's no point in
@@ -285,7 +288,7 @@ export class EdgeFunctionsRegistry {
285288
return
286289
}
287290

288-
const reason = this.#debug ? ` because ${chalk.underline(path)} has changed` : ''
291+
const reason = this.#debug ? ` because ${chalk.underline(paths.join(','))} has changed` : ''
289292

290293
log(`${NETLIFYDEVLOG} ${chalk.magenta('Reloading')} edge functions${reason}...`)
291294

@@ -548,7 +551,7 @@ export class EdgeFunctionsRegistry {
548551
const watcher = await watchDebounced(directory, {
549552
ignored,
550553
onAdd: () => this.#checkForAddedOrDeletedFunctions(),
551-
onChange: (path) => this.#handleFileChange(path),
554+
onChange: (paths) => this.#handleFileChange(paths),
552555
onUnlink: () => this.#checkForAddedOrDeletedFunctions(),
553556
})
554557

src/utils/command-helpers.mjs

+25-9
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ const DEBOUNCE_WAIT = 100
234234
* @param {Object} opts
235235
* @param {number} [opts.depth]
236236
* @param {Array<string|RegExp>} [opts.ignored]
237-
* @param {() => any} [opts.onAdd]
238-
* @param {() => any} [opts.onChange]
239-
* @param {() => any} [opts.onUnlink]
237+
* @param {(paths: string[]) => any} [opts.onAdd]
238+
* @param {(paths: string[]) => any} [opts.onChange]
239+
* @param {(paths: string[]) => any} [opts.onUnlink]
240240
*/
241241
export const watchDebounced = async (
242242
target,
@@ -247,22 +247,38 @@ export const watchDebounced = async (
247247

248248
await once(watcher, 'ready')
249249

250-
const debouncedOnChange = debounce(onChange, DEBOUNCE_WAIT)
251-
const debouncedOnUnlink = debounce(onUnlink, DEBOUNCE_WAIT)
252-
const debouncedOnAdd = debounce(onAdd, DEBOUNCE_WAIT)
250+
let onChangeQueue = []
251+
let onAddQueue = []
252+
let onUnlinkQueue = []
253+
254+
const debouncedOnChange = debounce(() => {
255+
onChange(onChangeQueue)
256+
onChangeQueue = []
257+
}, DEBOUNCE_WAIT)
258+
const debouncedOnAdd = debounce(() => {
259+
onAdd(onAddQueue)
260+
onAddQueue = []
261+
}, DEBOUNCE_WAIT)
262+
const debouncedOnUnlink = debounce(() => {
263+
onUnlink(onUnlinkQueue)
264+
onUnlinkQueue = []
265+
}, DEBOUNCE_WAIT)
253266

254267
watcher
255268
.on('change', (path) => {
256269
decache(path)
257-
debouncedOnChange(path)
270+
onChangeQueue.push(path)
271+
debouncedOnChange()
258272
})
259273
.on('unlink', (path) => {
260274
decache(path)
261-
debouncedOnUnlink(path)
275+
onUnlinkQueue.push(path)
276+
debouncedOnUnlink()
262277
})
263278
.on('add', (path) => {
264279
decache(path)
265-
debouncedOnAdd(path)
280+
onAddQueue.push(path)
281+
debouncedOnAdd()
266282
})
267283

268284
return watcher

0 commit comments

Comments
 (0)