From c69e309472de7aef9e6a8b2c0e085331e2112ad5 Mon Sep 17 00:00:00 2001 From: Rinne Date: Mon, 28 Apr 2025 11:55:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20responsiveGetPages?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E9=A2=91=E7=B9=81=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E8=A2=AB=E9=A3=8E=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/ajax.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/core/ajax.ts b/src/core/ajax.ts index 8b5a1e27c..5466c77b5 100644 --- a/src/core/ajax.ts +++ b/src/core/ajax.ts @@ -1,4 +1,5 @@ import { logError } from './utils/log' +import { list } from 'postcss' type XhrBody = Document | XMLHttpRequestBodyInit type XhrConfig = (xhr: XMLHttpRequest) => { isText?: boolean; body?: XhrBody } @@ -196,7 +197,6 @@ export const responsiveGetPages = (config: { const { api, getList, getTotal } = config let total = Infinity const result: T[] = [] - const tasks: Promise[] = [] const fetchPage = async (p: number): Promise => { const json = await api(p) @@ -209,6 +209,7 @@ export const responsiveGetPages = (config: { } return getList(json) } + // 请求第一次获得total const firstReq = await api(1) result.push(...getList(firstReq)) @@ -221,14 +222,18 @@ export const responsiveGetPages = (config: { resolveTotal(result) return } - console.log(totalPages) - // 收集Promise + // 收集Promise,每5个为一个batch运行 + const batchSize = 5 + let batch: Promise[] = [] for (let i = 2; i <= totalPages; i++) { - tasks.push(fetchPage(i)) + batch.push(fetchPage(i)) + if (batch.length === batchSize || i === totalPages) { + const lists = await Promise.all(batch) + result.push(...lists.flat()) + batch = [] + } } // 等所有并发完成 - const lists = await Promise.all(tasks) - result.push(...lists.flat()) resolveTotal(result) })() })