tabRefreshService.js
1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Tab 子页刷新 in-flight 合并(主包共享,供各场景 TabBar 子页使用)。
*
* 设计原则:
* - **每次** `show=true` 都会执行刷新(与 Tab0 对齐)
* - 仅合并同一 key 下同一 tick 的并发调用(如 observer + 其它入口叠加)
* - **不做 TTL**,切走再切回应重新请求
*
* @module utils/tabRefreshService
*/
/** @type {Map<string, Promise<void>>} key → 进行中的刷新 */
const inFlightByKey = new Map()
/**
* 清空单测/调试用的 in-flight 状态。
*/
export function clearTabRefreshState() {
inFlightByKey.clear()
}
/**
* 在 in-flight 去重下执行子 Tab 刷新。
*
* @param {'quotation'|'equityList'|'materials'|'mine'} key - Tab 业务隔离键
* @param {() => Promise<void>|void} fn - 实际刷新函数(如 refreshQuotationTab)
* @returns {Promise<void>}
*/
export async function withTabRefreshInFlight(key, fn) {
if (inFlightByKey.has(key)) {
return inFlightByKey.get(key)
}
const promise = Promise.resolve()
.then(fn)
.finally(() => {
if (inFlightByKey.get(key) === promise) {
inFlightByKey.delete(key)
}
})
inFlightByKey.set(key, promise)
return promise
}