mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-14 19:34:57 +02:00
Fix:Include Watcher as lib with no dependencies and fix tiny-readdir bug #610
This commit is contained in:
parent
160dac109d
commit
ec6e70725c
34 changed files with 2187 additions and 281 deletions
41
server/libs/watcher/promise-concurrency-limiter.js
Normal file
41
server/libs/watcher/promise-concurrency-limiter.js
Normal 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 });
|
Loading…
Add table
Add a link
Reference in a new issue