Skip to content

Commit d686e27

Browse files
committed
Debounce watch build script to prevent double compile
1 parent a0ce81b commit d686e27

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

build.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@ function includeIf(predicate, elem) {
2222
return predicate ? [elem] : [];
2323
}
2424

25+
function asyncTimeout(millis) {
26+
return new Promise((resolve) => {
27+
setTimeout(() => resolve(), millis);
28+
});
29+
}
30+
31+
function debounce(func, millis, initial = false) {
32+
let count = 0;
33+
const loop = async (...args) => {
34+
if (!count && initial) {
35+
func(...args);
36+
}
37+
count++;
38+
const id = count;
39+
await asyncTimeout(millis);
40+
if (id === count) {
41+
count = 0;
42+
if (id > 1 || !initial) {
43+
func(...args);
44+
}
45+
}
46+
};
47+
return loop;
48+
}
49+
2550
async function execPrint(command) {
2651
try {
2752
const { stdout, stderr } = await execAsync(command);
@@ -75,20 +100,22 @@ function processArgs() {
75100
}
76101

77102
function startWatch(args) {
103+
const debouncedCompile = debounce(compile, 500);
78104
const watcher = watch(
79105
[
80106
'mangareader/**/*.py',
81107
'mangareader/**/*.ts',
82108
'mangareader/**/*.scss',
83109
'mangareader/**/*.html',
110+
'reader.py',
84111
],
85112
{
86113
persistent: true,
87114
},
88115
);
89116
watcher.on('change', async (path) => {
90117
console.log('File changed:', path);
91-
await compile(args);
118+
await debouncedCompile(args);
92119
});
93120
}
94121

0 commit comments

Comments
 (0)