fix torrent cancelation

This commit is contained in:
SrGooglo 2024-06-30 20:28:44 +02:00
parent 1e74f49e4e
commit e1d833a5f5
2 changed files with 26 additions and 22 deletions

View File

@ -15,15 +15,15 @@ export default async function reinstall(pkg_id) {
return null
}
global._relic_eventBus.emit(`pkg:install:cancel`, pkg_id)
global._relic_eventBus.emit(`pkg:install:cancel:${pkg_id}`, pkg_id)
global._relic_eventBus.emit(`task:cancel:${pkg_id}`, pkg_id)
const task = globalThis.relic_core.tasks.find((task) => task.id === pkg_id)
if (task) {
BaseLog.warn(`Task not found [${pkg_id}]`)
await task.abortController.abort()
global._relic_eventBus.emit(`pkg:install:cancel`, pkg_id)
global._relic_eventBus.emit(`pkg:install:cancel:${pkg_id}`, pkg_id)
global._relic_eventBus.emit(`task:cancel:${pkg_id}`, pkg_id)
}
await UninstallHandler(pkg_id)

View File

@ -30,11 +30,11 @@ export default async function downloadTorrent(
}
const client = new aria2({
host: 'localhost',
host: "localhost",
port: 6800,
secure: false,
secret: '',
path: '/jsonrpc'
secret: "",
path: "/jsonrpc"
})
await client.open()
@ -52,13 +52,14 @@ export default async function downloadTorrent(
onStart()
}
if (typeof taskId === "string") {
// TODO: Unregister me when task is cancelled or finished
global._relic_eventBus.once(`task:cancel:${taskId}`, () => {
client.call("remove", [downloadId])
async function stopDownload() {
await client.call("remove", downloadId)
clearInterval(progressInterval)
reject()
})
}
if (taskId) {
// TODO: Unregister me when download finish
global._relic_eventBus.once(`task:cancel:${taskId}`, stopDownload)
}
progressInterval = setInterval(async () => {
@ -104,17 +105,15 @@ export default async function downloadTorrent(
return false
}
clearInterval(progressInterval)
if (typeof onDone === "function") {
onDone()
}
resolve({
stopDownload()
return resolve({
downloadId,
})
return null
})
client.on("onDownloadError", ([{ gid }]) => {
@ -122,17 +121,22 @@ export default async function downloadTorrent(
return false
}
clearInterval(progressInterval)
stopDownload()
if (typeof onError === "function") {
onError()
} else {
reject()
}
return reject()
})
})
await client.call("remove", downloadId)
if (taskId) {
global._relic_eventBus.off(`task:cancel:${taskId}`, stopDownload)
}
return downloadId
}