semantic-analysis
Version:
Extensible semantic Structured Data analysis library for Microdata and JSON-LD
138 lines • 6.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BatchAnalyzer = void 0;
var tslib_1 = require("tslib");
var analysis_1 = tslib_1.__importDefault(require("../../structured-data/analysis"));
/**
* General batch analysis class
*/
var BatchAnalyzer = /** @class */ (function () {
/* Initialization */
function BatchAnalyzer(options) {
var _this = this;
var _a, _b;
this.queued = [];
this.running = [];
this.done = [];
this.failed = [];
this.disposed = false;
this.state = function () { return ({
queued: _this.queued.map(archive),
running: _this.running.map(archive),
done: _this.done,
failed: _this.failed,
}); };
this.taskUpdate = function () { var _a, _b; return (_b = (_a = _this.options).onTaskUpdate) === null || _b === void 0 ? void 0 : _b.call(_a, _this.state()); };
this.options = tslib_1.__assign(tslib_1.__assign({}, (options !== null && options !== void 0 ? options : {})), { rateLimit: (_a = options === null || options === void 0 ? void 0 : options.rateLimit) !== null && _a !== void 0 ? _a : 0, maxRunning: (_b = options === null || options === void 0 ? void 0 : options.maxRunning) !== null && _b !== void 0 ? _b : 1 });
}
/* Interface */
BatchAnalyzer.prototype.addTask = function (url, source) {
var _this = this;
if (this.disposed) {
throw 'The analyzer has already been disposed, get a new one.';
}
return new Promise(function (resolve, reject) {
var task = {
url: url, source: source,
resolver: { resolve: resolve, reject: reject },
queued: new Date(),
};
_this.queued.push(task);
_this.taskUpdate();
_this.trigger();
})
.then(function (done) {
var _a, _b, _c, _d;
try {
(_b = (_a = _this.options).doneCallback) === null || _b === void 0 ? void 0 : _b.call(_a, done);
(_d = (_c = _this.options).callback) === null || _d === void 0 ? void 0 : _d.call(_c, done);
}
catch (e) {
console.error('error in callback', e);
}
return done;
})
.catch(function (failed) {
var _a, _b, _c, _d;
try {
(_b = (_a = _this.options).failedCallback) === null || _b === void 0 ? void 0 : _b.call(_a, failed);
(_d = (_c = _this.options).callback) === null || _d === void 0 ? void 0 : _d.call(_c, failed);
}
catch (e) {
console.error('error in callback', e);
}
return failed;
});
};
BatchAnalyzer.prototype.trigger = function () {
if (this.coolingDown === undefined && this.running.length < this.options.maxRunning) {
var task = this.queued.shift();
if (task) {
this.runTask(task);
}
}
};
BatchAnalyzer.prototype.runTask = function (task) {
var _this = this;
var taskRunning = tslib_1.__assign({ handler: analysis_1.default(task.url, task.source), run: new Date() }, task);
this.running.push(taskRunning);
this.taskUpdate();
taskRunning.handler
.then(function (results) {
var taskDone = tslib_1.__assign({ results: results, done: new Date() }, taskRunning);
if (!_this.options.exporter) {
_this.done.push(archive(taskDone));
taskRunning.resolver.resolve(taskDone);
return Promise.resolve();
}
else {
return _this.options.exporter.export(taskDone.results)
.then(function (exported) {
var taskExported = tslib_1.__assign(tslib_1.__assign({}, taskDone), { exportInfo: exported.paths });
_this.done.push(archive(taskExported));
taskRunning.resolver.resolve(taskExported);
})
.catch(function (error) {
console.error("Failed to export [" + taskDone.url + "]", error);
var taskFailed = tslib_1.__assign(tslib_1.__assign({}, taskDone), { failed: new Date(), error: taskDone.error ? [taskDone.error, error] : error });
_this.failed.push(archive(taskFailed));
taskRunning.resolver.reject(taskFailed);
});
}
})
.catch(function (error) {
console.error("Task could not completed [" + taskRunning.url + "]", error);
var taskFailed = tslib_1.__assign(tslib_1.__assign({}, taskRunning), { failed: new Date(), error: taskRunning.error ? [taskRunning.error, error] : error });
_this.failed.push(archive(taskFailed));
taskRunning.resolver.reject(taskFailed);
})
.finally(function () {
_this.running.splice(_this.running.indexOf(taskRunning), 1);
_this.taskUpdate();
_this.coolDown();
});
};
BatchAnalyzer.prototype.coolDown = function () {
var _this = this;
var _a;
this.coolingDown = (_a = this.coolingDown) !== null && _a !== void 0 ? _a : setTimeout(function () {
_this.coolingDown = undefined;
_this.trigger();
}, this.options.rateLimit);
};
/* Disposable */
BatchAnalyzer.prototype.dispose = function () {
if (this.coolingDown !== undefined) {
clearTimeout(this.coolingDown);
this.coolingDown = undefined;
}
this.disposed = true;
};
return BatchAnalyzer;
}());
exports.BatchAnalyzer = BatchAnalyzer;
var archive = function (_a) {
var url = _a.url, queued = _a.queued, run = _a.run, done = _a.done, failed = _a.failed, error = _a.error, exportInfo = _a.exportInfo;
return ({ url: url, queued: queued, run: run, done: done, failed: failed, error: error, exportInfo: exportInfo });
};
//# sourceMappingURL=BatchAnalyzer.js.map