!15 perf(shipping-notice): 优化发货任务性能,修复上传流程与终态问题
Merge pull request !15 from 曹惠鹏/hugo-shipping-notice
This commit is contained in:
29 files changed
+1074
-128
No files matched your search
@@ -16,8 +16,63 @@ vi.mock('@/shared/api/client', () => ({
|
||||
import {
|
||||
getWorkspaceShippingOrderExtractionRun,
|
||||
getWorkspaceShippingProcessing,
|
||||
getWorkspaceShippingTask,
|
||||
} from './shipping.api';
|
||||
|
||||
describe('getWorkspaceShippingTask', () => {
|
||||
beforeEach(() => {
|
||||
requestJson.mockReset();
|
||||
});
|
||||
|
||||
it('coalesces concurrent reads of the same task detail into one HTTP request', async () => {
|
||||
let resolveRequest!: (value: { task_no: string }) => void;
|
||||
requestJson.mockImplementationOnce(() => new Promise((resolve) => {
|
||||
resolveRequest = resolve;
|
||||
}));
|
||||
|
||||
const first = getWorkspaceShippingTask('SN-DEDUPE-1');
|
||||
const second = getWorkspaceShippingTask('SN-DEDUPE-1');
|
||||
|
||||
// 关键断言:重型任务详情在途时,同 task_no 的并行消费者(工作台 loadFormalTaskDetail
|
||||
// 与对话侧 syncActiveShippingTaskDetail)必须复用一个网络请求,禁止重复拉取。
|
||||
expect(requestJson).toHaveBeenCalledTimes(1);
|
||||
resolveRequest({ task_no: 'SN-DEDUPE-1' });
|
||||
await expect(Promise.all([first, second])).resolves.toEqual([
|
||||
{ task_no: 'SN-DEDUPE-1' },
|
||||
{ task_no: 'SN-DEDUPE-1' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('does not cache a completed task detail across a later read', async () => {
|
||||
requestJson.mockResolvedValueOnce({ task_no: 'SN-FRESH-1' });
|
||||
requestJson.mockResolvedValueOnce({ task_no: 'SN-FRESH-1' });
|
||||
|
||||
// 单飞去重只合并「在途」请求;完成后不得返回过期快照,保证 Gate 动作后的新鲜重读。
|
||||
await getWorkspaceShippingTask('SN-FRESH-1');
|
||||
await getWorkspaceShippingTask('SN-FRESH-1');
|
||||
|
||||
expect(requestJson).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('keys dedup by view so workbench and full projections do not merge', async () => {
|
||||
requestJson.mockResolvedValue({ task_no: 'SN-VIEW-1' });
|
||||
// 关键断言:workbench(裁剪)与 full 是两个不同响应体,缺省 view 与 view=workbench
|
||||
// 共享同一个 task_no 时不能合并成一个请求,否则会把裁剪版响应交给 full 消费者。
|
||||
const full = getWorkspaceShippingTask('SN-VIEW-1');
|
||||
const wb = getWorkspaceShippingTask('SN-VIEW-1', { view: 'workbench' });
|
||||
const wbAgain = getWorkspaceShippingTask('SN-VIEW-1', { view: 'workbench' });
|
||||
|
||||
// 两个视图各一次、workbench 同视图合并:共 2 次底层请求。
|
||||
expect(requestJson).toHaveBeenCalledTimes(2);
|
||||
expect(requestJson).toHaveBeenNthCalledWith(1, '/api/workspace/shipping-tasks/SN-VIEW-1');
|
||||
expect(requestJson).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'/api/workspace/shipping-tasks/SN-VIEW-1?view=workbench',
|
||||
);
|
||||
await Promise.allSettled([full, wb, wbAgain]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getWorkspaceShippingOrderExtractionRun', () => {
|
||||
beforeEach(() => {
|
||||
requestJson.mockReset();
|
||||
|
||||
@@ -118,9 +118,45 @@ export const listWorkspaceShippingTasks = (params?: {
|
||||
/** 拉取用户可见 Agent 列表,供发货工作台跳转 Chat 时定位目标 Agent。 */
|
||||
export const listShippingChatAgents = () => requestJson<ShippingChatAgentOption[]>('/api/agents');
|
||||
|
||||
/** 获取单个发货任务详情。 */
|
||||
export const getWorkspaceShippingTask = (taskNo: string) =>
|
||||
requestJson<ShippingTaskDetail>(`/api/workspace/shipping-tasks/${encodeURIComponent(taskNo)}`);
|
||||
/** 任务详情单飞去重:同一瞬间并发的同一 task_no 详情只发一次网络请求。
|
||||
* [PERF] 订单生命周期工作台首屏会由 hasFormalOt / confirmedNoticeTaskNo 两个
|
||||
* immediate watcher 同时触发 loadFormalTaskDetail,加上对话侧 syncActiveShippingTaskDetail,
|
||||
* 会在同一时刻并发拉取同一任务详情;单飞将多次并发请求合并为一次,
|
||||
* 显著削减 /shipping-tasks/:task_no 的重型重复请求。
|
||||
* 只对「在途」请求去重、不缓存完成结果:保留 Gate 动作后的新鲜重读,
|
||||
* 避免把写操作后的过期详情误当作最新状态。
|
||||
* 去重键含 view:workbench(裁剪)与 full 是两个不同响应体,不能相互合并。 */
|
||||
export type ShippingTaskDetailView = 'full' | 'workbench';
|
||||
const shippingTaskDetailRequests = new Map<string, Promise<ShippingTaskDetail>>();
|
||||
|
||||
/** 获取单个发货任务详情。@param options.view 「workbench」返回订单生命周期工作台首屏裁剪版。 */
|
||||
export const getWorkspaceShippingTask = (
|
||||
taskNo: string,
|
||||
options?: { view?: ShippingTaskDetailView },
|
||||
) => {
|
||||
const normalized = String(taskNo || '').trim();
|
||||
if (!normalized) {
|
||||
return Promise.reject(new BusinessError({
|
||||
message: 'task_no is required',
|
||||
code: 'SHIPPING.TASK_REQUIRED',
|
||||
status: 400,
|
||||
}));
|
||||
}
|
||||
const view = options?.view === 'workbench' ? 'workbench' : 'full';
|
||||
const cacheKey = `${normalized}|${view}`;
|
||||
const existing = shippingTaskDetailRequests.get(cacheKey);
|
||||
if (existing) return existing;
|
||||
const url = view === 'workbench'
|
||||
? `/api/workspace/shipping-tasks/${encodeURIComponent(normalized)}?view=workbench`
|
||||
: `/api/workspace/shipping-tasks/${encodeURIComponent(normalized)}`;
|
||||
const request = requestJson<ShippingTaskDetail>(url).finally(() => {
|
||||
if (shippingTaskDetailRequests.get(cacheKey) === request) {
|
||||
shippingTaskDetailRequests.delete(cacheKey);
|
||||
}
|
||||
});
|
||||
shippingTaskDetailRequests.set(cacheKey, request);
|
||||
return request;
|
||||
};
|
||||
|
||||
/** 读取当前 PO 下按 business_no 聚合的货代上传 Notice。 */
|
||||
export const getWorkspaceShippingFreightNoticeGroups = (taskNo: string) =>
|
||||
|
||||
Reference in new issue
Block a user