Skip to content

Commit

Permalink
fix: don't swallow file changes when a lot of them come in (#6075)
Browse files Browse the repository at this point in the history
* fix: don't swallow file changes when a lot of them come in

* fix: reintroduce debounce, but collect paths
  • Loading branch information
Skn0tt authored Oct 20, 2023
1 parent f7518f9 commit 20d6fef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
13 changes: 8 additions & 5 deletions src/lib/edge-functions/registry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,15 @@ export class EdgeFunctionsRegistry {
}

/**
* @param {string} path
* @param {string[]} paths
* @returns {Promise<void>}
*/
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
Expand All @@ -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}...`)

Expand Down Expand Up @@ -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(),
})

Expand Down
34 changes: 25 additions & 9 deletions src/utils/command-helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ const DEBOUNCE_WAIT = 100
* @param {Object} opts
* @param {number} [opts.depth]
* @param {Array<string|RegExp>} [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,
Expand All @@ -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
Expand Down

2 comments on commit 20d6fef

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,655
  • Package size: 480 MB

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,655
  • Package size: 480 MB

Please sign in to comment.