Fix:Include Watcher as lib with no dependencies and fix tiny-readdir bug #610

This commit is contained in:
advplyr 2022-05-28 20:01:20 -05:00
parent 160dac109d
commit ec6e70725c
34 changed files with 2187 additions and 281 deletions

View file

@ -0,0 +1,41 @@
"use strict";
/* IMPORT */
/* PROMISE CONCURRENCY LIMITER */
class Limiter {
/* CONSTRUCTOR */
constructor(options) {
this.concurrency = options.concurrency;
this.count = 0;
this.queue = new Set();
}
/* API */
add(fn) {
if (this.count < this.concurrency)
return this.run(fn);
return new Promise(resolve => {
const callback = () => resolve(this.run(fn));
this.queue.add(callback);
});
}
flush() {
for (const callback of this.queue) {
if (this.count >= this.concurrency)
break;
this.queue.delete(callback);
callback();
}
}
run(fn) {
this.count += 1;
const promise = fn();
const cleanup = () => {
this.count -= 1;
this.flush();
};
promise.then(cleanup, cleanup);
return promise;
}
}
module.exports = Limiter;
module.exports.default = Limiter;
Object.defineProperty(module.exports, "__esModule", { value: true });