/** * `POST /customer/get_list@v1` 的**模块级共享快照**(缺口清单 C-8)。 * * 此前同一份客户列表有三个各自独立的读源——第 2 册台账 `useCustomerDirectory()`、 * 第 1 册组织 store `fetchAll()` 里为合伙人/团队反查而做的 `pagedAll(customerGetList)`、 * 概览页 `useOverview()` 的客户状态分桶——同一页面进入即打 2 次同一接口 * (`useLoad` 的飞行中去重按 `run` 身份合并,跨调用点合不了)。 * 本模块把该列表收成**一份缓存 + 一次请求**:并发调用复用同一次在飞请求, * 热态直接回缓存,写操作后由 `invalidateCustomerList()` 显式作废。 * * 失效口径(⛔ TTL、⛔ "看起来够新"):只有两种情况会重新打接口—— * ① 本模块内任何客户写操作后 `invalidateCustomerList()`/`reload()`;② 换登录身份 * (可见范围由后端按角色下发,⛔ 把上一个身份的快照留给下一个)。 * * 取数失败⛔ 进缓存(下次调用真重取),并把 `ApiError` 如实回给每个读源—— * 「本次取数失败」与「后端确实没有客户」必须能区分(总则§16.5)。 */ import { useEffect, useSyncExternalStore } from "react"; import { customerGetList } from "./api/bos.ts"; import type { ApiError } from "./api/http.ts"; import type { CustomerListResult, CustomerListRow } from "./api/types.ts"; import type { Load } from "./useLoad.ts"; /** ⚠️ `page` 为 **0 基**(缺口清单 B-8),`page:1` 实测回空集。 */ const PAGE_SIZE = 200; type State = { data: CustomerListResult | null; loading: boolean; error: ApiError | null }; /** 首帧即「取数中」:冷快照下页面⛔ 闪一下空列表(看起来像「0 条客户」)。 */ let state: State = { data: null, loading: true, error: null }; let fresh = false; /** 在飞请求的发起代数:其间作废过 ⇒ 结果⛔ 回写快照(否则写前的数据会顶掉写后的)。 */ let epoch = 0; let inflight: Promise | null = null; const listeners = new Set<() => void>(); function publish(next: State) { state = next; listeners.forEach((listener) => listener()); } /** 分页取全量(`total` 可能是字符串,见缺口清单 B-7)。 */ async function fetchAllPages(): Promise { const acc: CustomerListRow[] = []; let total = 0; let page = 0; for (;;) { const res = await customerGetList({ page, page_size: PAGE_SIZE }); const rows = res?.items ?? []; acc.push(...rows); total = Number(res?.total ?? acc.length); if (!rows.length || acc.length >= total || page >= 40) return { items: acc, total }; page += 1; } } /** * 读客户列表:热缓存直接返回、在飞请求直接复用,冷态才发一次。 * `force`=绕开缓存(页面「重试/刷新」用)。失败不进缓存。 */ export function loadCustomerList(force = false): Promise { if (inflight) return inflight; if (!force && fresh && state.data) return Promise.resolve(state.data); const myEpoch = epoch; const run = fetchAllPages().then( (data) => { if (inflight === run) inflight = null; if (myEpoch !== epoch) { // 其间有写操作作废过:把结果交回调用方(它要的是一个可用应答),但⛔ 进快照。 if (!inflight) publish({ ...state, loading: false }); return data; } fresh = true; publish({ data, loading: false, error: null }); return data; }, (e) => { if (inflight === run) inflight = null; if (myEpoch === epoch) publish({ ...state, loading: false, error: e as ApiError }); else if (!inflight) publish({ ...state, loading: false }); throw e; }, ); inflight = run; publish({ ...state, loading: true, error: null }); return run; } /** * 作废缓存(客户建档/编辑/归属变更/移动区域/删除/批量导入建档/分账号数变化后调用)。 * 在飞的旧请求一并作废:完成后⛔ 回写快照。已挂载的读源由调用方随后 `reload()` 触发重取。 */ export function invalidateCustomerList() { epoch += 1; fresh = false; inflight = null; } /** 换登录身份:整块快照连同失败态一起清,⛔ 把上一个身份的可见范围留给下一个。 */ export function resetCustomerListCache() { invalidateCustomerList(); publish({ data: null, loading: true, error: null }); } export function peekCustomerList(): CustomerListResult | null { return state.data; } const subscribe = (listener: () => void) => { listeners.add(listener); return () => { listeners.delete(listener); }; }; /** * 与 `useLoad` 同形状的读源(页面⛔ 感知缓存的存在): * 多处同时读 ⇒ 仍只发一次请求;`reload()`=作废 + 强制重取。 */ export function useCustomerList(): Load { const snapshot = useSyncExternalStore(subscribe, () => state); useEffect(() => { loadCustomerList().catch(() => {}); }, []); return { data: snapshot.data, loading: snapshot.loading, error: snapshot.error, reload: () => { invalidateCustomerList(); loadCustomerList(true).catch(() => {}); }, }; }