fix(mobile): abort uni.request when the platform timeout does not fire
Some uni-app platforms ignore the `timeout` option, leaving the returned promise pending forever and hanging the calling page with no error path. Drive the deadline locally: keep a single-shot `finish` guard so success, failure and timeout can only settle once, and on timeout call `task.abort()` before rejecting so the in-flight request is released instead of leaking. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -118,8 +118,17 @@ export const apiRequest = async <T>(options: RequestOptions): Promise<T> => {
|
|||||||
return requestWithXhr<T>(options);
|
return requestWithXhr<T>(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const timeout = options.timeout || 30000;
|
||||||
const response = await new Promise<any>((resolve, reject) => {
|
const response = await new Promise<any>((resolve, reject) => {
|
||||||
uni.request({
|
let finished = false;
|
||||||
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||||
|
const finish = (callback: () => void) => {
|
||||||
|
if (finished) return;
|
||||||
|
finished = true;
|
||||||
|
if (timer) clearTimeout(timer);
|
||||||
|
callback();
|
||||||
|
};
|
||||||
|
const task = uni.request({
|
||||||
url: normalizeUrl(options.url),
|
url: normalizeUrl(options.url),
|
||||||
method: options.method || 'GET',
|
method: options.method || 'GET',
|
||||||
data: options.data as string | ArrayBuffer | Record<string, unknown> | undefined,
|
data: options.data as string | ArrayBuffer | Record<string, unknown> | undefined,
|
||||||
@@ -127,10 +136,19 @@ export const apiRequest = async <T>(options: RequestOptions): Promise<T> => {
|
|||||||
...authHeaders(options.method !== 'GET', options.auth !== false),
|
...authHeaders(options.method !== 'GET', options.auth !== false),
|
||||||
...options.header
|
...options.header
|
||||||
},
|
},
|
||||||
timeout: options.timeout || 30000,
|
timeout,
|
||||||
success: resolve,
|
success: (result) => finish(() => resolve(result)),
|
||||||
fail: reject
|
fail: (error) => finish(() => reject(error))
|
||||||
});
|
});
|
||||||
|
if (!finished) {
|
||||||
|
timer = setTimeout(() => finish(() => {
|
||||||
|
try {
|
||||||
|
task.abort();
|
||||||
|
} finally {
|
||||||
|
reject(new Error('网络请求超时'));
|
||||||
|
}
|
||||||
|
}), timeout);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return readPayload<T>(response.statusCode, response.data, options.clearAuthOnUnauthorized !== false);
|
return readPayload<T>(response.statusCode, response.data, options.clearAuthOnUnauthorized !== false);
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import { after, test } from 'node:test';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
import { createServer } from 'vite';
|
||||||
|
|
||||||
|
let abortCount = 0;
|
||||||
|
globalThis.plus = {};
|
||||||
|
globalThis.uni = {
|
||||||
|
getStorageSync: () => '',
|
||||||
|
removeStorageSync: () => {},
|
||||||
|
request: () => ({
|
||||||
|
abort: () => {
|
||||||
|
abortCount += 1;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
const vite = await createServer({
|
||||||
|
root: fileURLToPath(new URL('..', import.meta.url)),
|
||||||
|
configFile: false,
|
||||||
|
optimizeDeps: { noDiscovery: true },
|
||||||
|
server: { middlewareMode: true, hmr: false },
|
||||||
|
appType: 'custom'
|
||||||
|
});
|
||||||
|
|
||||||
|
after(async () => {
|
||||||
|
await vite.close();
|
||||||
|
delete globalThis.plus;
|
||||||
|
delete globalThis.uni;
|
||||||
|
});
|
||||||
|
|
||||||
|
const { apiRequest } = await vite.ssrLoadModule('/src/services/api.ts');
|
||||||
|
|
||||||
|
test('App 原生请求遗漏回调时主动超时并中止任务', async () => {
|
||||||
|
await assert.rejects(
|
||||||
|
apiRequest({ url: '/api/test', timeout: 10 }),
|
||||||
|
/网络请求超时/
|
||||||
|
);
|
||||||
|
assert.equal(abortCount, 1);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user