diffjam
Version:
cli for diffjam.com
55 lines (54 loc) • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SingleThreadWorkerPool = void 0;
var workerProcess_1 = require("./workerProcess");
var node_assert_1 = require("node:assert");
// Has the same interface as `WorkerPool`, but operates in the same thread
// This is useful for testing purposes and for actions that modify the config.
var SingleThreadWorkerPool = /** @class */ (function () {
function SingleThreadWorkerPool(config, cwd) {
var _this = this;
this.closed = false;
this.inProgress = new Set();
this.queued = [];
this.filesChecked = [];
this.resultsMap = {};
this.onResults = function () { };
this.worker = (0, workerProcess_1.readyWorker)(config, cwd, function (match, policy) {
_this.resultsMap[policy.name].matches.push(match);
}, function (filePath) {
_this.inProgress.delete(filePath);
if (_this.queued.length) {
_this.checkFile(_this.queued.shift());
}
else if (_this.isWorkDone()) {
_this.onResults();
}
});
}
SingleThreadWorkerPool.prototype.isWorkDone = function () {
return this.closed && !this.inProgress.size && !this.queued.length;
};
SingleThreadWorkerPool.prototype.checkFile = function (filePath) {
this.filesChecked.push(filePath);
this.inProgress.add(filePath);
this.worker.processFile(filePath);
};
SingleThreadWorkerPool.prototype.processFile = function (filePath) {
if (this.inProgress.size >= 3) {
this.queued.push(filePath);
}
else {
this.checkFile(filePath);
}
};
SingleThreadWorkerPool.prototype.onFilesDone = function () {
(0, node_assert_1.equal)(this.closed, false, "onFilesDone called twice");
this.closed = true;
if (this.isWorkDone()) {
this.onResults();
}
};
return SingleThreadWorkerPool;
}());
exports.SingleThreadWorkerPool = SingleThreadWorkerPool;