refactor exportcontext to taskcontext

This commit is contained in:
zadam
2019-10-18 22:27:38 +02:00
parent 157bd3816d
commit 56e4f4f5ac
9 changed files with 37 additions and 93 deletions

View File

@@ -4,64 +4,24 @@ const tarExportService = require('../../services/export/tar');
const singleExportService = require('../../services/export/single');
const opmlExportService = require('../../services/export/opml');
const repository = require("../../services/repository");
const ws = require("../../services/ws.js");
const TaskContext = require("../../services/task_context");
const log = require("../../services/log");
class ExportContext {
constructor(exportId) {
// exportId is to distinguish between different export events - it is possible (though not recommended)
// to have multiple exports going at the same time
this.exportId = exportId;
// count is mean to represent count of exported notes where practical, otherwise it's just some measure of progress
this.progressCount = 0;
this.lastSentCountTs = Date.now();
}
async increaseProgressCount() {
this.progressCount++;
if (Date.now() - this.lastSentCountTs >= 500) {
this.lastSentCountTs = Date.now();
await ws.sendMessageToAllClients({
exportId: this.exportId,
type: 'export-progress-count',
progressCount: this.progressCount
});
}
}
async exportFinished() {
await ws.sendMessageToAllClients({
exportId: this.exportId,
type: 'export-finished'
});
}
// must remaing non-static
async reportError(message) {
await ws.sendMessageToAllClients({
type: 'export-error',
message: message
});
}
}
async function exportBranch(req, res) {
const {branchId, type, format, version, exportId} = req.params;
const {branchId, type, format, version, taskId} = req.params;
const branch = await repository.getBranch(branchId);
const exportContext = new ExportContext(exportId);
const taskContext = new TaskContext(taskId, 'export');
try {
if (type === 'subtree' && (format === 'html' || format === 'markdown')) {
await tarExportService.exportToTar(exportContext, branch, format, res);
await tarExportService.exportToTar(taskContext, branch, format, res);
}
else if (type === 'single') {
await singleExportService.exportSingleNote(exportContext, branch, format, res);
await singleExportService.exportSingleNote(taskContext, branch, format, res);
}
else if (format === 'opml') {
await opmlExportService.exportToOpml(exportContext, branch, version, res);
await opmlExportService.exportToOpml(taskContext, branch, version, res);
}
else {
return [404, "Unrecognized export format " + format];
@@ -69,7 +29,7 @@ async function exportBranch(req, res) {
}
catch (e) {
const message = "Export failed with following error: '" + e.message + "'. More details might be in the logs.";
exportContext.reportError(message);
taskContext.reportError(message);
log.error(message + e.stack);