audiobookshelf/server/objects/Task.js

92 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-07-05 01:14:44 +02:00
const uuidv4 = require("uuid").v4
class Task {
constructor() {
2023-10-20 23:39:32 +02:00
/** @type {string} */
this.id = null
2023-10-20 23:39:32 +02:00
/** @type {string} */
this.action = null // e.g. embed-metadata, encode-m4b, etc
2023-10-20 23:39:32 +02:00
/** @type {Object} custom data */
this.data = null // additional info for the action like libraryItemId
2023-10-20 23:39:32 +02:00
/** @type {string} */
this.title = null
2023-10-20 23:39:32 +02:00
/** @type {string} */
this.description = null
2023-10-20 23:39:32 +02:00
/** @type {string} */
this.error = null
2023-10-20 23:39:32 +02:00
/** @type {boolean} client should keep the task visible after success */
this.showSuccess = false
2023-10-20 23:39:32 +02:00
/** @type {boolean} */
this.isFailed = false
2023-10-20 23:39:32 +02:00
/** @type {boolean} */
this.isFinished = false
2023-10-20 23:39:32 +02:00
/** @type {number} */
this.startedAt = null
2023-10-20 23:39:32 +02:00
/** @type {number} */
this.finishedAt = null
}
toJSON() {
return {
id: this.id,
action: this.action,
data: this.data ? { ...this.data } : {},
title: this.title,
description: this.description,
error: this.error,
2023-05-27 21:51:03 +02:00
showSuccess: this.showSuccess,
isFailed: this.isFailed,
isFinished: this.isFinished,
startedAt: this.startedAt,
finishedAt: this.finishedAt
}
}
2023-10-20 23:39:32 +02:00
/**
* Set initial task data
*
* @param {string} action
* @param {string} title
* @param {string} description
* @param {boolean} showSuccess
* @param {Object} [data]
*/
2023-05-27 21:51:03 +02:00
setData(action, title, description, showSuccess, data = {}) {
2023-07-05 01:14:44 +02:00
this.id = uuidv4()
this.action = action
this.data = { ...data }
this.title = title
this.description = description
2023-05-27 21:51:03 +02:00
this.showSuccess = showSuccess
this.startedAt = Date.now()
}
2023-10-20 23:39:32 +02:00
/**
* Set task as failed
*
* @param {string} message error message
*/
setFailed(message) {
this.error = message
this.isFailed = true
this.failedAt = Date.now()
this.setFinished()
}
2023-10-20 23:39:32 +02:00
/**
* Set task as finished
*
* @param {string} [newDescription] update description
*/
2023-05-27 21:51:03 +02:00
setFinished(newDescription = null) {
if (newDescription) {
this.description = newDescription
}
this.isFinished = true
this.finishedAt = Date.now()
}
}
module.exports = Task