Skip to content

Commit ba4d1b9

Browse files
authored
Merge pull request #6791 from Shopify/add-debounce-on-theme-file-watcher
add debounce for theme filewatcher to stop unwanted file deletes
2 parents f8da51a + bb6a1cf commit ba4d1b9

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

.changeset/few-buses-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/theme': patch
3+
---
4+
5+
Add a 250ms debounce on our filewatcher for themes to stop potential file deletes

packages/theme/src/cli/utilities/theme-fs.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import type {
2525
ThemeFSEventPayload,
2626
} from '@shopify/cli-kit/node/themes/types'
2727

28+
const FILE_EVENT_DEBOUNCE_TIME_IN_MS = 250
29+
2830
const THEME_DIRECTORY_PATTERNS = [
2931
'assets/**/*.*',
3032
'config/**/*.json',
@@ -321,10 +323,30 @@ export function mountThemeFileSystem(root: string, options?: ThemeFileSystemOpti
321323
ignoreInitial: true,
322324
})
323325

326+
// Debounce file events per-file and per-event-type
327+
const pendingEvents = new Map<string, NodeJS.Timeout>()
328+
329+
const queueFsEvent = (eventName: 'add' | 'change' | 'unlink', filePath: string) => {
330+
const fileKey = getKey(filePath)
331+
const eventKey = `${fileKey}:${eventName}`
332+
333+
const pending = pendingEvents.get(eventKey)
334+
if (pending) {
335+
clearTimeout(pending)
336+
}
337+
338+
const timeout = setTimeout(() => {
339+
pendingEvents.delete(eventKey)
340+
handleFsEvent(eventName, themeId, adminSession, filePath)
341+
}, FILE_EVENT_DEBOUNCE_TIME_IN_MS)
342+
343+
pendingEvents.set(eventKey, timeout)
344+
}
345+
324346
watcher
325-
.on('add', handleFsEvent.bind(null, 'add', themeId, adminSession))
326-
.on('change', handleFsEvent.bind(null, 'change', themeId, adminSession))
327-
.on('unlink', handleFsEvent.bind(null, 'unlink', themeId, adminSession))
347+
.on('add', queueFsEvent.bind(null, 'add'))
348+
.on('change', queueFsEvent.bind(null, 'change'))
349+
.on('unlink', queueFsEvent.bind(null, 'unlink'))
328350
},
329351
}
330352
}

0 commit comments

Comments
 (0)