add defer, wake runner by holding resolve handle

This commit is contained in:
joshuaboud 2022-06-15 17:20:46 -03:00
parent ecec0b56cc
commit 85be57d26a
No known key found for this signature in database
GPG Key ID: 17EFB59E2A8BF50E

View File

@ -13,13 +13,15 @@ import '../globalTypedefs';
* @property {FileSystemEventCallback} onAttributeChanged - callback for file AttributeChanged * @property {FileSystemEventCallback} onAttributeChanged - callback for file AttributeChanged
* @property {FileSystemEventCallback} onDoneHint - callback for file DoneHint * @property {FileSystemEventCallback} onDoneHint - callback for file DoneHint
* @property {FileSystemEventCallback} onError - Error callback, defaults to logging to console.error * @property {FileSystemEventCallback} onError - Error callback, defaults to logging to console.error
* @param {Boolean} defer - if true, don't start watching until FileSystemWatchObj.start() called
* @returns {FileSystemWatchObj} {@link FileSystemWatchObj} * @returns {FileSystemWatchObj} {@link FileSystemWatchObj}
*/ */
function FileSystemWatcher(path, options = {}, handlers = {}) { function FileSystemWatcher(path, options = {}, handlers = {}, defer=false) {
let fsWatchChannel = null; let fsWatchChannel = null;
let fsWatchJobQueue = []; let fsWatchJobQueue = [];
let unhandledEventQueue = []; let unhandledEventQueue = [];
let running = true; let running = true;
let wakeRunner = () => null;
const self = { const self = {
host: options.host ?? null, host: options.host ?? null,
path, path,
@ -77,6 +79,7 @@ function FileSystemWatcher(path, options = {}, handlers = {}) {
self.onError(error); self.onError(error);
break; break;
} }
wakeRunner();
} }
/** /**
@ -91,7 +94,10 @@ function FileSystemWatcher(path, options = {}, handlers = {}) {
self.onError(error); self.onError(error);
} }
} }
await new Promise(resolve => setTimeout(resolve, 100)); await new Promise(resolve => {
wakeRunner = resolve;
setTimeout(wakeRunner, 1000); // spurious wakeup in case any missed
});
} }
} }
@ -120,28 +126,36 @@ function FileSystemWatcher(path, options = {}, handlers = {}) {
} }
self.stop = () => { self.stop = () => {
if (fsWatchChannel?.valid) takeDownChannel();
running = false; running = false;
takeDownChannel(); fsWatchJobQueue.length = 0;
unhandledEventQueue.length = 0;
wakeRunner();
}; };
self.start = () => {
running = true;
fsWatchJobRunner(); // start runner fsWatchJobRunner(); // start runner
setUpChannel(); if (!fsWatchChannel?.valid) setUpChannel();
}
if (!defer) self.start();
return new Proxy(self, { return new Proxy(self, {
get: (target, prop) => target[prop], get: (target, prop) => target[prop],
set: (target, prop, value) => { set: (target, prop, value) => {
if (target[prop] === value) if (target[prop] === value)
return true; return true;
let restartChannel = (prop === 'path' || prop === 'host'); const restartChannel = (prop === 'path' || prop === 'host');
if (restartChannel) { if (restartChannel) {
takeDownChannel(); self.stop();
} }
target[prop] = value; target[prop] = value;
if (restartChannel) { if (restartChannel) {
setUpChannel(); self.start();
} }
if ( if (