2024-09-21 00:18:29 +02:00
|
|
|
const uuidv4 = require('uuid').v4
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef TaskString
|
|
|
|
* @property {string} text
|
|
|
|
* @property {string} key
|
|
|
|
* @property {string[]} [subs]
|
|
|
|
*/
|
2022-10-02 21:16:17 +02:00
|
|
|
|
|
|
|
class Task {
|
|
|
|
constructor() {
|
2023-10-20 23:39:32 +02:00
|
|
|
/** @type {string} */
|
2022-10-02 21:16:17 +02:00
|
|
|
this.id = null
|
2023-10-20 23:39:32 +02:00
|
|
|
/** @type {string} */
|
2022-10-02 21:16:17 +02:00
|
|
|
this.action = null // e.g. embed-metadata, encode-m4b, etc
|
2023-10-20 23:39:32 +02:00
|
|
|
/** @type {Object} custom data */
|
2022-10-02 21:16:17 +02:00
|
|
|
this.data = null // additional info for the action like libraryItemId
|
|
|
|
|
2023-10-20 23:39:32 +02:00
|
|
|
/** @type {string} */
|
2022-10-02 21:16:17 +02:00
|
|
|
this.title = null
|
2024-09-21 00:18:29 +02:00
|
|
|
/** @type {string} - Used for translation */
|
|
|
|
this.titleKey = null
|
|
|
|
/** @type {string[]} - Used for translation */
|
|
|
|
this.titleSubs = null
|
|
|
|
|
2023-10-20 23:39:32 +02:00
|
|
|
/** @type {string} */
|
2022-10-02 21:16:17 +02:00
|
|
|
this.description = null
|
2024-09-21 00:18:29 +02:00
|
|
|
/** @type {string} - Used for translation */
|
|
|
|
this.descriptionKey = null
|
|
|
|
/** @type {string[]} - Used for translation */
|
|
|
|
this.descriptionSubs = null
|
|
|
|
|
2023-10-20 23:39:32 +02:00
|
|
|
/** @type {string} */
|
2022-10-02 21:16:17 +02:00
|
|
|
this.error = null
|
2024-09-21 00:18:29 +02:00
|
|
|
/** @type {string} - Used for translation */
|
|
|
|
this.errorKey = null
|
|
|
|
/** @type {string[]} - Used for translation */
|
|
|
|
this.errorSubs = null
|
|
|
|
|
2023-10-20 23:39:32 +02:00
|
|
|
/** @type {boolean} client should keep the task visible after success */
|
|
|
|
this.showSuccess = false
|
2022-10-02 21:16:17 +02:00
|
|
|
|
2023-10-20 23:39:32 +02:00
|
|
|
/** @type {boolean} */
|
2022-10-02 21:16:17 +02:00
|
|
|
this.isFailed = false
|
2023-10-20 23:39:32 +02:00
|
|
|
/** @type {boolean} */
|
2022-10-02 21:16:17 +02:00
|
|
|
this.isFinished = false
|
|
|
|
|
2023-10-20 23:39:32 +02:00
|
|
|
/** @type {number} */
|
2022-10-02 21:16:17 +02:00
|
|
|
this.startedAt = null
|
2023-10-20 23:39:32 +02:00
|
|
|
/** @type {number} */
|
2022-10-02 21:16:17 +02:00
|
|
|
this.finishedAt = null
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
action: this.action,
|
|
|
|
data: this.data ? { ...this.data } : {},
|
|
|
|
title: this.title,
|
2024-09-21 21:02:57 +02:00
|
|
|
titleKey: this.titleKey,
|
|
|
|
titleSubs: this.titleSubs,
|
2022-10-02 21:16:17 +02:00
|
|
|
description: this.description,
|
2024-09-21 21:02:57 +02:00
|
|
|
descriptionKey: this.descriptionKey,
|
|
|
|
descriptionSubs: this.descriptionSubs,
|
2022-10-02 21:16:17 +02:00
|
|
|
error: this.error,
|
2024-09-21 21:02:57 +02:00
|
|
|
errorKey: this.errorKey,
|
|
|
|
errorSubs: this.errorSubs,
|
2023-05-27 21:51:03 +02:00
|
|
|
showSuccess: this.showSuccess,
|
2022-10-02 21:16:17 +02:00
|
|
|
isFailed: this.isFailed,
|
|
|
|
isFinished: this.isFinished,
|
|
|
|
startedAt: this.startedAt,
|
|
|
|
finishedAt: this.finishedAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 23:39:32 +02:00
|
|
|
/**
|
|
|
|
* Set initial task data
|
2024-09-21 00:18:29 +02:00
|
|
|
*
|
|
|
|
* @param {string} action
|
|
|
|
* @param {TaskString} titleString
|
|
|
|
* @param {TaskString|null} descriptionString
|
|
|
|
* @param {boolean} showSuccess
|
|
|
|
* @param {Object} [data]
|
2023-10-20 23:39:32 +02:00
|
|
|
*/
|
2024-09-21 00:18:29 +02:00
|
|
|
setData(action, titleString, descriptionString, showSuccess, data = {}) {
|
2023-07-05 01:14:44 +02:00
|
|
|
this.id = uuidv4()
|
2022-10-02 21:16:17 +02:00
|
|
|
this.action = action
|
|
|
|
this.data = { ...data }
|
2024-09-21 00:18:29 +02:00
|
|
|
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 21:51:03 +02:00
|
|
|
this.showSuccess = showSuccess
|
2022-10-02 21:16:17 +02:00
|
|
|
this.startedAt = Date.now()
|
|
|
|
}
|
|
|
|
|
2023-10-20 23:39:32 +02:00
|
|
|
/**
|
|
|
|
* Set task as failed
|
2024-09-21 00:18:29 +02:00
|
|
|
*
|
|
|
|
* @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()
|
|
|
|
}
|
|
|
|
|
2023-10-20 23:39:32 +02:00
|
|
|
/**
|
|
|
|
* Set task as finished
|
2024-09-21 00:18:29 +02:00
|
|
|
*
|
2024-09-21 21:02:57 +02:00
|
|
|
* @param {TaskString} [newDescriptionString] update description
|
|
|
|
* @param {boolean} [clearDescription] clear description
|
2023-10-20 23:39:32 +02:00
|
|
|
*/
|
2024-09-21 21:02:57 +02:00
|
|
|
setFinished(newDescriptionString = null, clearDescription = false) {
|
|
|
|
if (newDescriptionString) {
|
|
|
|
this.description = newDescriptionString.text
|
|
|
|
this.descriptionKey = newDescriptionString.key || null
|
|
|
|
this.descriptionSubs = newDescriptionString.subs || null
|
|
|
|
} else if (clearDescription) {
|
|
|
|
this.description = null
|
2024-09-21 00:18:29 +02:00
|
|
|
this.descriptionKey = null
|
|
|
|
this.descriptionSubs = null
|
2023-05-27 21:51:03 +02:00
|
|
|
}
|
2022-10-02 21:16:17 +02:00
|
|
|
this.isFinished = true
|
|
|
|
this.finishedAt = Date.now()
|
|
|
|
}
|
|
|
|
}
|
2024-09-21 00:18:29 +02:00
|
|
|
module.exports = Task
|