simple-git
Version:
Simple GIT interface for node.js
47 lines • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const promise_deferred_1 = require("@kwsites/promise-deferred");
const git_logger_1 = require("../git-logger");
const logger = git_logger_1.createLogger('', 'scheduler');
const createScheduledTask = (() => {
let id = 0;
return () => {
id++;
const { promise, done } = promise_deferred_1.createDeferred();
return {
promise,
done,
id,
};
};
})();
class Scheduler {
constructor(concurrency = 2) {
this.concurrency = concurrency;
this.pending = [];
this.running = [];
logger(`Constructed, concurrency=%s`, concurrency);
}
schedule() {
if (!this.pending.length || this.running.length >= this.concurrency) {
logger(`Schedule attempt ignored, pending=%s running=%s concurrency=%s`, this.pending.length, this.running.length, this.concurrency);
return;
}
const task = utils_1.append(this.running, this.pending.shift());
logger(`Attempting id=%s`, task.id);
task.done(() => {
logger(`Completing id=`, task.id);
utils_1.remove(this.running, task);
this.schedule();
});
}
next() {
const { promise, id } = utils_1.append(this.pending, createScheduledTask());
logger(`Scheduling id=%s`, id);
this.schedule();
return promise;
}
}
exports.Scheduler = Scheduler;
//# sourceMappingURL=scheduler.js.map