Skip to content

Commit

Permalink
feat(builder): watch mode add debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Sep 5, 2023
1 parent 7f9393e commit 51e5b74
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 8 additions & 2 deletions libs/cfworker-builder/lib/gen-toml.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { readFileSync, writeFileSync, watch as _watch } from 'fs';

import MagicString from 'magic-string';

import { debounce } from './utils/debounce.mjs';

const tplFilePath = './wrangler.tpl.toml';
const targetFilePath = './wrangler.toml';

Expand Down Expand Up @@ -34,11 +36,15 @@ export function run() {
writeFileSync(targetFilePath, magic.toString());
}

const debouncedRun = debounce(run, 300);

export function watch() {
console.log('watching template file changes...');

_watch(tplFilePath, {}, () => {
console.log(`${tplFilePath} changed.`);
run();
console.log(
`- ${new Date().toLocaleString('zh-CN')}: ${tplFilePath} changed.`,
);
debouncedRun();
});
}
12 changes: 12 additions & 0 deletions libs/cfworker-builder/lib/utils/debounce.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function debounce(func, wait) {
let timer = null;
return (...args) => {
if (timer) {
clearTimeout(timer);
}

timer = setTimeout(() => {
func.apply(this, args);
}, wait);
};
}

0 comments on commit 51e5b74

Please sign in to comment.