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 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) const task = globalThis.relic_core.tasks.find((task) => task.id === pkg_id)
if (task) { if (task) {
BaseLog.warn(`Task not found [${pkg_id}]`) BaseLog.warn(`Task not found [${pkg_id}]`)
await task.abortController.abort() 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) await UninstallHandler(pkg_id)

View File

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