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
|
|
|
|
*
|
|
|
|
* @param {Task} task
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
* @param {Task} task
|
|
|
|
*/
|
2022-10-02 21:16:17 +02:00
|
|
|
taskFinished(task) {
|
|
|
|
if (this.tasks.some(t => t.id === task.id)) {
|
2022-10-02 21:31:04 +02:00
|
|
|
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-20 23:39:32 +02:00
|
|
|
module.exports = new TaskManager()
|