2022-11-24 22:53:58 +01:00
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
2023-10-20 23:39:32 +02:00
|
|
|
const Task = require('../objects/Task')
|
2022-10-02 21:16:17 +02:00
|
|
|
|
2024-09-21 00:18:29 +02:00
|
|
|
/**
|
|
|
|
* @typedef TaskString
|
|
|
|
* @property {string} text
|
|
|
|
* @property {string} key
|
|
|
|
* @property {string[]} [subs]
|
|
|
|
*/
|
|
|
|
|
2022-11-24 22:53:58 +01:00
|
|
|
class TaskManager {
|
|
|
|
constructor() {
|
2023-10-20 23:39:32 +02:00
|
|
|
/** @type {Task[]} */
|
2022-10-02 21:16:17 +02:00
|
|
|
this.tasks = []
|
|
|
|
}
|
|
|
|
|
2023-10-20 23:39:32 +02:00
|
|
|
/**
|
|
|
|
* Add task and emit socket task_started event
|
2024-07-17 00:05:52 +02:00
|
|
|
*
|
|
|
|
* @param {Task} task
|
2023-10-20 23:39:32 +02:00
|
|
|
*/
|
2022-10-02 21:16:17 +02:00
|
|
|
addTask(task) {
|
|
|
|
this.tasks.push(task)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('task_started', task.toJSON())
|
2022-10-02 21:16:17 +02:00
|
|
|
}
|
|
|
|
|
2023-10-20 23:39:32 +02:00
|
|
|
/**
|
|
|
|
* Remove task and emit task_finished event
|
2024-07-17 00:05:52 +02:00
|
|
|
*
|
|
|
|
* @param {Task} task
|
2023-10-20 23:39:32 +02:00
|
|
|
*/
|
2022-10-02 21:16:17 +02:00
|
|
|
taskFinished(task) {
|
2024-07-17 00:05:52 +02:00
|
|
|
if (this.tasks.some((t) => t.id === task.id)) {
|
|
|
|
this.tasks = this.tasks.filter((t) => t.id !== task.id)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('task_finished', task.toJSON())
|
2022-10-02 21:16:17 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-21 00:46:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create new task and add
|
2024-07-17 00:05:52 +02:00
|
|
|
*
|
|
|
|
* @param {string} action
|
2024-09-21 00:18:29 +02:00
|
|
|
* @param {TaskString} titleString
|
|
|
|
* @param {TaskString|null} descriptionString
|
2024-07-17 00:05:52 +02:00
|
|
|
* @param {boolean} showSuccess
|
|
|
|
* @param {Object} [data]
|
2023-10-21 00:46:18 +02:00
|
|
|
*/
|
2024-09-21 00:18:29 +02:00
|
|
|
createAndAddTask(action, titleString, descriptionString, showSuccess, data = {}) {
|
2023-10-21 00:46:18 +02:00
|
|
|
const task = new Task()
|
2024-09-21 00:18:29 +02:00
|
|
|
task.setData(action, titleString, descriptionString, showSuccess, data)
|
2023-10-21 00:46:18 +02:00
|
|
|
this.addTask(task)
|
|
|
|
return task
|
|
|
|
}
|
2024-07-17 00:05:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create new failed task and add
|
|
|
|
*
|
|
|
|
* @param {string} action
|
2024-09-21 00:18:29 +02:00
|
|
|
* @param {TaskString} titleString
|
|
|
|
* @param {TaskString|null} descriptionString
|
|
|
|
* @param {TaskString} errorMessageString
|
2024-07-17 00:05:52 +02:00
|
|
|
*/
|
2024-09-21 00:18:29 +02:00
|
|
|
createAndEmitFailedTask(action, titleString, descriptionString, errorMessageString) {
|
2024-07-17 00:05:52 +02:00
|
|
|
const task = new Task()
|
2024-09-21 00:18:29 +02:00
|
|
|
task.setData(action, titleString, descriptionString, false)
|
2024-09-21 21:02:57 +02:00
|
|
|
task.setFailed(errorMessageString)
|
2024-07-17 00:05:52 +02:00
|
|
|
SocketAuthority.emitter('task_started', task.toJSON())
|
|
|
|
return task
|
|
|
|
}
|
2022-10-02 21:16:17 +02:00
|
|
|
}
|
2024-07-17 00:05:52 +02:00
|
|
|
module.exports = new TaskManager()
|