2019-10-17 21:11:35 +02:00
|
|
|
"use strict";
|
|
|
|
|
2021-06-29 22:15:57 +02:00
|
|
|
const ws = require('./ws');
|
2019-10-17 21:11:35 +02:00
|
|
|
|
|
|
|
// taskId => TaskContext
|
|
|
|
const taskContexts = {};
|
|
|
|
|
|
|
|
class TaskContext {
|
2022-12-25 23:36:56 +01:00
|
|
|
constructor(taskId, taskType = null, data = null) {
|
2019-10-17 21:11:35 +02:00
|
|
|
this.taskId = taskId;
|
|
|
|
this.taskType = taskType;
|
|
|
|
this.data = data;
|
2022-06-05 14:58:19 +02:00
|
|
|
this.noteDeletionHandlerTriggered = false;
|
2019-10-17 21:11:35 +02:00
|
|
|
|
|
|
|
// progressCount is meant to represent just some progress - to indicate the task is not stuck
|
2020-01-04 21:59:28 +01:00
|
|
|
this.progressCount = -1; // we're incrementing immediatelly
|
|
|
|
this.lastSentCountTs = 0; // 0 will guarantee first message will be sent
|
2020-01-04 21:24:39 +01:00
|
|
|
|
|
|
|
// just the fact this has been initialized is a progress which should be sent to clients
|
|
|
|
// this is esp. important when importing big files/images which take long time to upload/process
|
|
|
|
// which means that first "real" increaseProgressCount() will be called quite late and user is without
|
|
|
|
// feedback until then
|
|
|
|
this.increaseProgressCount();
|
2019-10-17 21:11:35 +02:00
|
|
|
}
|
|
|
|
|
2021-10-24 14:53:45 +02:00
|
|
|
/** @returns {TaskContext} */
|
2022-12-09 16:04:13 +01:00
|
|
|
static getInstance(taskId, taskType, data = null) {
|
2019-10-17 21:11:35 +02:00
|
|
|
if (!taskContexts[taskId]) {
|
2019-10-18 23:19:16 +02:00
|
|
|
taskContexts[taskId] = new TaskContext(taskId, taskType, data);
|
2019-10-17 21:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return taskContexts[taskId];
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:42:22 +01:00
|
|
|
increaseProgressCount() {
|
2019-10-17 21:11:35 +02:00
|
|
|
this.progressCount++;
|
|
|
|
|
2021-12-21 16:12:59 +01:00
|
|
|
if (Date.now() - this.lastSentCountTs >= 300 && this.taskId !== 'no-progress-reporting') {
|
2019-10-17 21:11:35 +02:00
|
|
|
this.lastSentCountTs = Date.now();
|
|
|
|
|
2019-10-28 18:42:22 +01:00
|
|
|
ws.sendMessageToAllClients({
|
2021-04-24 11:39:44 +02:00
|
|
|
type: 'taskProgressCount',
|
2019-10-17 21:11:35 +02:00
|
|
|
taskId: this.taskId,
|
|
|
|
taskType: this.taskType,
|
2019-10-19 09:58:18 +02:00
|
|
|
data: this.data,
|
2019-10-17 21:11:35 +02:00
|
|
|
progressCount: this.progressCount
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:42:22 +01:00
|
|
|
reportError(message) {
|
|
|
|
ws.sendMessageToAllClients({
|
2021-04-24 11:39:44 +02:00
|
|
|
type: 'taskError',
|
2019-10-17 21:11:35 +02:00
|
|
|
taskId: this.taskId,
|
|
|
|
taskType: this.taskType,
|
2019-10-19 09:58:18 +02:00
|
|
|
data: this.data,
|
2019-10-17 21:11:35 +02:00
|
|
|
message: message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:42:22 +01:00
|
|
|
taskSucceeded(result) {
|
|
|
|
ws.sendMessageToAllClients({
|
2021-04-24 11:39:44 +02:00
|
|
|
type: 'taskSucceeded',
|
2019-10-17 21:11:35 +02:00
|
|
|
taskId: this.taskId,
|
|
|
|
taskType: this.taskType,
|
2019-10-19 09:58:18 +02:00
|
|
|
data: this.data,
|
2019-10-19 00:11:07 +02:00
|
|
|
result: result
|
2019-10-17 21:11:35 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 21:08:18 +02:00
|
|
|
module.exports = TaskContext;
|