1
Fork 0

add debouncing

This commit is contained in:
Conduitry 2018-07-06 18:35:00 -04:00
parent 0e2c432bc9
commit 70eb4df2b9
1 changed files with 26 additions and 19 deletions

13
shim.js
View File

@ -18,9 +18,7 @@ const response = request =>
.on('error', error => reject(error)),
);
const attachWatcher = (source, target) => {
watch(source, { recursive: true }, async (eventType, filename) => {
await new Promise(res => setTimeout(res, 10));
const watchHandler = async (target, filename) => {
const dest = target + '/' + filename.replace(/\\/g, '/');
console.log(`Event: ${dest}`);
const { Id } = JSON.parse(
@ -39,6 +37,15 @@ const attachWatcher = (source, target) => {
path: `/v${apiVersion}/exec/${Id}/start`,
headers: { 'content-type': 'application/json' },
}).end(JSON.stringify({ Detach: true }));
};
const attachWatcher = (source, target) => {
const timeouts = new Map();
watch(source, { recursive: true }, async (eventType, filename) => {
if (timeouts.has(filename)) {
clearTimeout(timeouts.get(filename));
}
timeouts.set(filename, setTimeout(watchHandler, 10, target, filename));
});
console.log(`Watching ${source} => ${target}`);
};