perf(web): 优化大数据量页面切换与聚合性能
This commit is contained in:
@@ -90,3 +90,85 @@ test('server data saves include the latest loaded AppData version', async () =>
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test('server data shares an in-flight load for the same AppData key', async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
let dataRequests = 0;
|
||||
let releaseDataResponse!: () => void;
|
||||
const dataResponseReady = new Promise<void>((resolve) => {
|
||||
releaseDataResponse = resolve;
|
||||
});
|
||||
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = String(input);
|
||||
if (url.endsWith('/config/ai')) {
|
||||
return new Response(JSON.stringify({}), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
if (url.endsWith('/data/test-cases')) {
|
||||
dataRequests += 1;
|
||||
await dataResponseReady;
|
||||
return new Response(JSON.stringify({
|
||||
key: 'test-cases',
|
||||
value: [{ id: 'tc-1' }],
|
||||
version: 'test-cases-version-1',
|
||||
}), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
throw new Error(`Unexpected fetch ${init?.method ?? 'GET'} ${url}`);
|
||||
}) as typeof fetch;
|
||||
|
||||
try {
|
||||
const first = loadServerData<Array<{ id: string }>>('test-cases');
|
||||
const second = loadServerData<Array<{ id: string }>>('test-cases');
|
||||
releaseDataResponse();
|
||||
|
||||
assert.deepEqual(await first, [{ id: 'tc-1' }]);
|
||||
assert.deepEqual(await second, [{ id: 'tc-1' }]);
|
||||
assert.equal(dataRequests, 1);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test('server data reuses a fresh cached load for repeated AppData reads', async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
let dataRequests = 0;
|
||||
|
||||
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
const url = String(input);
|
||||
if (url.endsWith('/config/ai')) {
|
||||
return new Response(JSON.stringify({}), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
if (url.endsWith('/data/version-plans')) {
|
||||
dataRequests += 1;
|
||||
return new Response(JSON.stringify({
|
||||
key: 'version-plans',
|
||||
value: [{ id: 'plan-1' }],
|
||||
version: 'version-plans-version-1',
|
||||
}), {
|
||||
status: 200,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
throw new Error(`Unexpected fetch ${init?.method ?? 'GET'} ${url}`);
|
||||
}) as typeof fetch;
|
||||
|
||||
try {
|
||||
const first = await loadServerData<Array<{ id: string }>>('version-plans');
|
||||
const second = await loadServerData<Array<{ id: string }>>('version-plans');
|
||||
|
||||
assert.deepEqual(first, [{ id: 'plan-1' }]);
|
||||
assert.deepEqual(second, [{ id: 'plan-1' }]);
|
||||
assert.equal(dataRequests, 1);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user