chuangte_bike_newxcx/pages/index/utils/tapGuard.js
2026-05-21 09:43:14 +08:00

46 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** 与首页 guardPage 使用的全页互斥 key 一致 */
export const PAGE_ACTION_LOCK_KEY = '__page__'
/**
* 全页「同一时间只响应一次点击」:任务进行中再次点击直接忽略(无提示)。
* timeoutMs避免底层 Promise 永不结束导致永久锁死。
*/
export function createTapGuard() {
const busy = Object.create(null)
const tokenByKey = Object.create(null)
return {
run(key, fn, timeoutMs = 25000) {
if (busy[key]) {
return Promise.resolve()
}
busy[key] = true
const token = (tokenByKey[key] = (tokenByKey[key] || 0) + 1)
let timeoutId
if (timeoutMs > 0) {
timeoutId = setTimeout(() => {
if (busy[key] && tokenByKey[key] === token) {
busy[key] = false
try {
uni.showToast({ title: '操作超时,请重试', icon: 'none', duration: 2000 })
} catch (e) {}
}
}, timeoutMs)
}
let p
try {
p = Promise.resolve().then(fn)
} catch (e) {
if (timeoutId) clearTimeout(timeoutId)
busy[key] = false
return Promise.reject(e)
}
return p.finally(() => {
if (timeoutId) clearTimeout(timeoutId)
if (tokenByKey[key] === token) {
busy[key] = false
}
})
}
}
}