2023-07-05 01:14:44 +02:00
|
|
|
const uuidv4 = require("uuid").v4
|
2022-10-02 21:16:17 +02:00
|
|
|
|
|
|
|
class Task {
|
|
|
|
constructor() {
|
|
|
|
this.id = null
|
|
|
|
this.action = null // e.g. embed-metadata, encode-m4b, etc
|
|
|
|
this.data = null // additional info for the action like libraryItemId
|
|
|
|
|
|
|
|
this.title = null
|
|
|
|
this.description = null
|
|
|
|
this.error = null
|
2023-05-27 21:51:03 +02:00
|
|
|
this.showSuccess = false // If true client side should keep the task visible after success
|
2022-10-02 21:16:17 +02:00
|
|
|
|
|
|
|
this.isFailed = false
|
|
|
|
this.isFinished = false
|
|
|
|
|
|
|
|
this.startedAt = null
|
|
|
|
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,
|
2022-10-02 21:16:17 +02:00
|
|
|
isFailed: this.isFailed,
|
|
|
|
isFinished: this.isFinished,
|
|
|
|
startedAt: this.startedAt,
|
|
|
|
finishedAt: this.finishedAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
2022-10-02 21:16:17 +02:00
|
|
|
this.action = action
|
|
|
|
this.data = { ...data }
|
|
|
|
this.title = title
|
|
|
|
this.description = description
|
2023-05-27 21:51:03 +02:00
|
|
|
this.showSuccess = showSuccess
|
2022-10-02 21:16:17 +02:00
|
|
|
this.startedAt = Date.now()
|
|
|
|
}
|
|
|
|
|
|
|
|
setFailed(message) {
|
|
|
|
this.error = message
|
|
|
|
this.isFailed = true
|
|
|
|
this.failedAt = Date.now()
|
|
|
|
this.setFinished()
|
|
|
|
}
|
|
|
|
|
2023-05-27 21:51:03 +02:00
|
|
|
setFinished(newDescription = null) {
|
|
|
|
if (newDescription) {
|
|
|
|
this.description = newDescription
|
|
|
|
}
|
2022-10-02 21:16:17 +02:00
|
|
|
this.isFinished = true
|
|
|
|
this.finishedAt = Date.now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = Task
|