Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't swallow file changes when a lot of them come in #6075

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading