fix: cancel parse requests when modal is closed

点击取消解析时,清空 PQueue 队列并中断正在进行的请求。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-10 09:11:10 +08:00
parent ad31301e10
commit 63a7cb347b
2 changed files with 28 additions and 3 deletions
+12 -1
View File
@@ -42,6 +42,8 @@ export class ParseModalComp implements VanComponent {
currentController?: AbortController
parseQueue?: PQueue
allPlayInfo: State<{
page: PageInParseResult
info: PlayInfo | null
@@ -105,6 +107,8 @@ export class ParseModalComp implements VanComponent {
controller.abort()
}
this.abortControllers = []
// 清空解析队列
this.parseQueue?.clear()
})
this.element.addEventListener('show.bs.modal', () => {
@@ -116,13 +120,19 @@ export class ParseModalComp implements VanComponent {
async start() {
this.finishCount.val = 0
this.errorList.val = []
const queue = new PQueue({ concurrency: 10 })
this.parseQueue = new PQueue({ concurrency: 10 })
const queue = this.parseQueue
for (const page of this.option.workRoute.selectedPages.val) {
queue.add(async () => {
// 检查队列是否已被清空(取消解析)
if (queue !== this.parseQueue) return
if (this.totalCount.val == this.finishCount.val) return
const controller = new AbortController()
this.abortControllers.push(controller)
const playInfo = await getPlayInfo(page.bvid, page.cid, controller)
// 再次检查是否已取消
if (queue !== this.parseQueue) return
playInfo.accept_quality = [...new Set(playInfo.dash.video.map(video => video.id))].sort((a, b) => b - a)
this.allPlayInfo.val = this.allPlayInfo.val.concat({
page,
@@ -132,6 +142,7 @@ export class ParseModalComp implements VanComponent {
})
this.finishCount.val++
}).catch(() => {
if (queue !== this.parseQueue) return
this.finishCount.val++
const badgeNotNum = !page.badge.match(/^\d+$/)
this.errorList.val = this.errorList.val.concat(`${page.part}${badgeNotNum ? ` - ${page.badge}` : ''}`)