@@ -371,10 +371,17 @@ const parseJsonPayload = <T>(payload: unknown, requestUrl: string, contentType:
|
||||
return payload as T
|
||||
}
|
||||
|
||||
const inFlightRequests = new Map<string, Promise<unknown>>()
|
||||
|
||||
const requestJson = <T>(
|
||||
path: string,
|
||||
options: { method?: 'GET' | 'POST'; data?: unknown } = {}
|
||||
): Promise<T> => new Promise((resolve, reject) => {
|
||||
): Promise<T> => {
|
||||
const requestKey = `${options.method || 'GET'}:${path}:${JSON.stringify(options.data || {})}`
|
||||
const existing = inFlightRequests.get(requestKey)
|
||||
if (existing) return existing as Promise<T>
|
||||
|
||||
const request = new Promise<T>((resolve, reject) => {
|
||||
const baseUrl = resolveAppApiBaseUrl()
|
||||
const requestUrl = `${baseUrl}${path}`
|
||||
|
||||
@@ -412,7 +419,11 @@ const requestJson = <T>(
|
||||
reject(new Error(`SGS 数据接口网络失败: ${path} ${JSON.stringify(error)}`))
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
inFlightRequests.set(requestKey, request)
|
||||
void request.finally(() => inFlightRequests.delete(requestKey)).catch(() => undefined)
|
||||
return request
|
||||
}
|
||||
|
||||
const buildQueryString = (params: object) => {
|
||||
const query = new URLSearchParams()
|
||||
|
||||
Reference in New Issue
Block a user