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
|
|
|
|
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
|
|
|
|
* @param {string} title
|
|
|
|
* @param {string} description
|
|
|
|
* @param {boolean} showSuccess
|
|
|
|
* @param {Object} [data]
|
2023-10-21 00:46:18 +02:00
|
|
|
*/
|
|
|
|
createAndAddTask(action, title, description, showSuccess, data = {}) {
|
|
|
|
const task = new Task()
|
|
|
|
task.setData(action, title, description, showSuccess, data)
|
|
|
|
this.addTask(task)
|
|
|
|
return task
|
|
|
|
}
|
2024-07-17 00:05:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create new failed task and add
|
|
|
|
*
|
|
|
|
* @param {string} action
|
|
|
|
* @param {string} title
|
|
|
|
* @param {string} description
|
|
|
|
* @param {string} errorMessage
|
|
|
|
*/
|
|
|
|
createAndEmitFailedTask(action, title, description, errorMessage) {
|
|
|
|
const task = new Task()
|
|
|
|
task.setData(action, title, description, false)
|
|
|
|
task.setFailed(errorMessage)
|
|
|
|
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()
|