diff --git a/src/lib/edge-functions/registry.mjs b/src/lib/edge-functions/registry.mjs index a1c27f15f2a..917ea2c3bd8 100644 --- a/src/lib/edge-functions/registry.mjs +++ b/src/lib/edge-functions/registry.mjs @@ -269,12 +269,15 @@ export class EdgeFunctionsRegistry { } /** - * @param {string} path + * @param {string[]} paths * @returns {Promise} */ - async #handleFileChange(path) { + async #handleFileChange(paths) { const matchingFunctions = new Set( - [this.#functionPaths.get(path), ...(this.#dependencyPaths.get(path) || [])].filter(Boolean), + [ + ...paths.map((path) => this.#functionPaths.get(path)), + ...paths.flatMap((path) => this.#dependencyPaths.get(path)), + ].filter(Boolean), ) // If the file is not associated with any function, there's no point in @@ -285,7 +288,7 @@ export class EdgeFunctionsRegistry { return } - const reason = this.#debug ? ` because ${chalk.underline(path)} has changed` : '' + const reason = this.#debug ? ` because ${chalk.underline(paths.join(','))} has changed` : '' log(`${NETLIFYDEVLOG} ${chalk.magenta('Reloading')} edge functions${reason}...`) @@ -548,7 +551,7 @@ export class EdgeFunctionsRegistry { const watcher = await watchDebounced(directory, { ignored, onAdd: () => this.#checkForAddedOrDeletedFunctions(), - onChange: (path) => this.#handleFileChange(path), + onChange: (paths) => this.#handleFileChange(paths), onUnlink: () => this.#checkForAddedOrDeletedFunctions(), }) diff --git a/src/utils/command-helpers.mjs b/src/utils/command-helpers.mjs index 3146b85bda0..5bd68dd71e9 100644 --- a/src/utils/command-helpers.mjs +++ b/src/utils/command-helpers.mjs @@ -234,9 +234,9 @@ const DEBOUNCE_WAIT = 100 * @param {Object} opts * @param {number} [opts.depth] * @param {Array} [opts.ignored] - * @param {() => any} [opts.onAdd] - * @param {() => any} [opts.onChange] - * @param {() => any} [opts.onUnlink] + * @param {(paths: string[]) => any} [opts.onAdd] + * @param {(paths: string[]) => any} [opts.onChange] + * @param {(paths: string[]) => any} [opts.onUnlink] */ export const watchDebounced = async ( target, @@ -247,22 +247,38 @@ export const watchDebounced = async ( await once(watcher, 'ready') - const debouncedOnChange = debounce(onChange, DEBOUNCE_WAIT) - const debouncedOnUnlink = debounce(onUnlink, DEBOUNCE_WAIT) - const debouncedOnAdd = debounce(onAdd, DEBOUNCE_WAIT) + let onChangeQueue = [] + let onAddQueue = [] + let onUnlinkQueue = [] + + const debouncedOnChange = debounce(() => { + onChange(onChangeQueue) + onChangeQueue = [] + }, DEBOUNCE_WAIT) + const debouncedOnAdd = debounce(() => { + onAdd(onAddQueue) + onAddQueue = [] + }, DEBOUNCE_WAIT) + const debouncedOnUnlink = debounce(() => { + onUnlink(onUnlinkQueue) + onUnlinkQueue = [] + }, DEBOUNCE_WAIT) watcher .on('change', (path) => { decache(path) - debouncedOnChange(path) + onChangeQueue.push(path) + debouncedOnChange() }) .on('unlink', (path) => { decache(path) - debouncedOnUnlink(path) + onUnlinkQueue.push(path) + debouncedOnUnlink() }) .on('add', (path) => { decache(path) - debouncedOnAdd(path) + onAddQueue.push(path) + debouncedOnAdd() }) return watcher