audiobookshelf/server/managers/TaskManager.js

32 lines
714 B
JavaScript
Raw Normal View History

const SocketAuthority = require('../SocketAuthority')
2023-10-20 23:39:32 +02:00
const Task = require('../objects/Task')
class TaskManager {
constructor() {
2023-10-20 23:39:32 +02:00
/** @type {Task[]} */
this.tasks = []
}
2023-10-20 23:39:32 +02:00
/**
* Add task and emit socket task_started event
*
* @param {Task} task
*/
addTask(task) {
this.tasks.push(task)
SocketAuthority.emitter('task_started', task.toJSON())
}
2023-10-20 23:39:32 +02:00
/**
* Remove task and emit task_finished event
*
* @param {Task} task
*/
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)
SocketAuthority.emitter('task_finished', task.toJSON())
}
}
}
2023-10-20 23:39:32 +02:00
module.exports = new TaskManager()