refactor(offline-booking): 优化离线预约缓存刷新逻辑
重构刷新逻辑,明确 force 参数的作用并添加注释说明 过滤已完成状态的记录以提高缓存效率
Showing
2 changed files
with
13 additions
and
2 deletions
| ... | @@ -149,14 +149,25 @@ export const build_offline_qr_list = (bill) => { | ... | @@ -149,14 +149,25 @@ export const build_offline_qr_list = (bill) => { |
| 149 | * 刷新离线预约记录缓存 | 149 | * 刷新离线预约记录缓存 |
| 150 | * - 仅在有授权且网络可用时调用 | 150 | * - 仅在有授权且网络可用时调用 |
| 151 | * - 成功后将数据存储到本地缓存(key: OFFLINE_BOOKING_DATA) | 151 | * - 成功后将数据存储到本地缓存(key: OFFLINE_BOOKING_DATA) |
| 152 | - * @param {boolean} force - 是否强制刷新,默认为 false | 152 | + * @param {boolean} force - 是否强制刷新,默认为 false. force 参数的核心作用是控制是否忽略 “正在进行的缓存请求”, 管的是 “是否允许重复发起请求”,不管 “请求能不能成功执行缓存”。 |
| 153 | * @returns {Promise<void>} | 153 | * @returns {Promise<void>} |
| 154 | */ | 154 | */ |
| 155 | export const refresh_offline_booking_cache = async ({ force = false } = {}) => { | 155 | export const refresh_offline_booking_cache = async ({ force = false } = {}) => { |
| 156 | + // 1. 检查是否有正在进行的刷新请求 | ||
| 157 | + // 2. 如果有,且 force 为 false,则直接返回该 Promise | ||
| 158 | + // 3. 如果没有,或 force 为 true,则继续执行刷新逻辑 | ||
| 159 | + // 4. 刷新完成后,将结果存储到本地缓存(key: OFFLINE_BOOKING_CACHE_KEY) | ||
| 160 | + // 5. 返回刷新结果 Promise | ||
| 161 | + | ||
| 156 | if (!hasAuth()) return { code: 0, data: null, msg: '未授权' } | 162 | if (!hasAuth()) return { code: 0, data: null, msg: '未授权' } |
| 157 | 163 | ||
| 158 | if (refresh_promise && !force) return refresh_promise | 164 | if (refresh_promise && !force) return refresh_promise |
| 159 | 165 | ||
| 166 | + // 核心逻辑: | ||
| 167 | + // 1. 立刻触发异步逻辑,同时捕获 Promise 状态 | ||
| 168 | + // 2. 保证 refresh_promise 始终是 Promise 类型,适配 await | ||
| 169 | + // 3. 隔离作用域,避免变量污染 | ||
| 170 | + // 加 () 是为了 “让异步逻辑立刻跑起来”,并把 “跑的结果(Promise)” 存起来,供后续复用和等待。 | ||
| 160 | refresh_promise = (async () => { | 171 | refresh_promise = (async () => { |
| 161 | const network_type = await get_network_type() | 172 | const network_type = await get_network_type() |
| 162 | if (!is_usable_network(network_type)) { | 173 | if (!is_usable_network(network_type)) { |
| ... | @@ -165,6 +176,7 @@ export const refresh_offline_booking_cache = async ({ force = false } = {}) => { | ... | @@ -165,6 +176,7 @@ export const refresh_offline_booking_cache = async ({ force = false } = {}) => { |
| 165 | 176 | ||
| 166 | const { code, data, msg } = await billOfflineAllAPI() | 177 | const { code, data, msg } = await billOfflineAllAPI() |
| 167 | if (code && Array.isArray(data)) { | 178 | if (code && Array.isArray(data)) { |
| 179 | + // 过滤出状态为3(已完成)的记录 | ||
| 168 | const normalized = data.map(normalize_bill_item).filter((item) => item && item.pay_id && item.status == 3) | 180 | const normalized = data.map(normalize_bill_item).filter((item) => item && item.pay_id && item.status == 3) |
| 169 | if (normalized.length > 0) { | 181 | if (normalized.length > 0) { |
| 170 | Taro.setStorageSync(OFFLINE_BOOKING_CACHE_KEY, normalized) | 182 | Taro.setStorageSync(OFFLINE_BOOKING_CACHE_KEY, normalized) | ... | ... |
| ... | @@ -57,7 +57,6 @@ const normalize_options = (options) => { | ... | @@ -57,7 +57,6 @@ const normalize_options = (options) => { |
| 57 | * @param {Object} options 选项 | 57 | * @param {Object} options 选项 |
| 58 | * @param {Boolean} options.force 是否强制刷新 | 58 | * @param {Boolean} options.force 是否强制刷新 |
| 59 | */ | 59 | */ |
| 60 | - | ||
| 61 | const run_refresh_once = async (options) => { | 60 | const run_refresh_once = async (options) => { |
| 62 | if (polling_state.in_flight) return // 核心防重复——如果正在刷新,直接返回 | 61 | if (polling_state.in_flight) return // 核心防重复——如果正在刷新,直接返回 |
| 63 | // 标记为“正在刷新” | 62 | // 标记为“正在刷新” | ... | ... |
-
Please register or login to post a comment