advplyr.audiobookshelf/server/objects/Task.js

139 lines
3.5 KiB
JavaScript
Raw Normal View History

const uuidv4 = require('uuid').v4
/**
* @typedef TaskString
* @property {string} text
* @property {string} key
* @property {string[]} [subs]
*/
class Task {
constructor() {
2023-10-20 16:39:32 -05:00
/** @type {string} */
this.id = null
2023-10-20 16:39:32 -05:00
/** @type {string} */
this.action = null // e.g. embed-metadata, encode-m4b, etc
2023-10-20 16:39:32 -05:00
/** @type {Object} custom data */
this.data = null // additional info for the action like libraryItemId
2023-10-20 16:39:32 -05:00
/** @type {string} */
this.title = null
/** @type {string} - Used for translation */
this.titleKey = null
/** @type {string[]} - Used for translation */
this.titleSubs = null
2023-10-20 16:39:32 -05:00
/** @type {string} */
this.description = null
/** @type {string} - Used for translation */
this.descriptionKey = null
/** @type {string[]} - Used for translation */
this.descriptionSubs = null
2023-10-20 16:39:32 -05:00
/** @type {string} */
this.error = null
/** @type {string} - Used for translation */
this.errorKey = null
/** @type {string[]} - Used for translation */
this.errorSubs = null
2023-10-20 16:39:32 -05:00
/** @type {boolean} client should keep the task visible after success */
this.showSuccess = false
2023-10-20 16:39:32 -05:00
/** @type {boolean} */
this.isFailed = false
2023-10-20 16:39:32 -05:00
/** @type {boolean} */
this.isFinished = false
2023-10-20 16:39:32 -05:00
/** @type {number} */
this.startedAt = null
2023-10-20 16:39:32 -05: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 14:51:03 -05:00
showSuccess: this.showSuccess,
isFailed: this.isFailed,
isFinished: this.isFinished,
startedAt: this.startedAt,
finishedAt: this.finishedAt
}
}
2023-10-20 16:39:32 -05:00
/**
* Set initial task data
*
* @param {string} action
* @param {TaskString} titleString
* @param {TaskString|null} descriptionString
* @param {boolean} showSuccess
* @param {Object} [data]
2023-10-20 16:39:32 -05:00
*/
setData(action, titleString, descriptionString, showSuccess, data = {}) {
2023-07-04 18:14:44 -05:00
this.id = uuidv4()
this.action = action
this.data = { ...data }
this.title = titleString.text
this.titleKey = titleString.key || null
this.titleSubs = titleString.subs || null
this.description = descriptionString?.text || null
this.descriptionKey = descriptionString?.key || null
this.descriptionSubs = descriptionString?.subs || null
2023-05-27 14:51:03 -05:00
this.showSuccess = showSuccess
this.startedAt = Date.now()
}
2023-10-20 16:39:32 -05:00
/**
* Set task as failed
*
* @param {TaskString} messageString
*/
setFailed(messageString) {
this.error = messageString.text
this.errorKey = messageString.key || null
this.errorSubs = messageString.subs || null
this.isFailed = true
this.failedAt = Date.now()
this.setFinished()
}
/**
* Set task as failed without translation key
* TODO: Remove this method after all tasks are using translation keys
*
* @param {string} message
2023-10-20 16:39:32 -05:00
*/
setFailedText(message) {
this.error = message
this.errorKey = null
this.errorSubs = null
this.isFailed = true
this.failedAt = Date.now()
this.setFinished()
}
2023-10-20 16:39:32 -05:00
/**
* Set task as finished
* TODO: Update to use translation keys
*
2023-10-20 16:39:32 -05:00
* @param {string} [newDescription] update description
*/
2023-05-27 14:51:03 -05:00
setFinished(newDescription = null) {
if (newDescription) {
this.description = newDescription
this.descriptionKey = null
this.descriptionSubs = null
2023-05-27 14:51:03 -05:00
}
this.isFinished = true
this.finishedAt = Date.now()
}
}
module.exports = Task