fix(wechat): 协议页切后台暂停超时计时并全量映射技术文案
This commit is contained in:
@@ -129,17 +129,58 @@ function createBlockedAgreementRequest(options, resolve, reject) {
|
||||
options: cloneRequestOptions(options),
|
||||
settled: false,
|
||||
retrying: false,
|
||||
timer: null
|
||||
timer: null,
|
||||
startedAt: null,
|
||||
remainingMs: AGREEMENT_PENDING_TIMEOUT_MS
|
||||
}
|
||||
const cleanup = () => {
|
||||
if (entry.timer) {
|
||||
clearTimeout(entry.timer)
|
||||
entry.timer = null
|
||||
}
|
||||
entry.startedAt = null
|
||||
if (agreementBlockedRequest === entry) {
|
||||
agreementBlockedRequest = null
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 启动(或恢复)超时兜底计时;已结算、已开始重放或计时已在运行时为 no-op,
|
||||
* 保证同一请求不会被重复计时或二次结算。
|
||||
*/
|
||||
entry.startTimer = delay => {
|
||||
if (entry.settled || entry.retrying || entry.timer) {
|
||||
return
|
||||
}
|
||||
entry.remainingMs = delay
|
||||
entry.startedAt = Date.now()
|
||||
entry.timer = setTimeout(() => {
|
||||
entry.timer = null
|
||||
entry.startedAt = null
|
||||
entry.rejectNotAccepted()
|
||||
}, delay)
|
||||
}
|
||||
/**
|
||||
* 切后台时暂停计时:清除定时器并记下剩余可见时长,后台期间不会触发结算。
|
||||
*/
|
||||
entry.pauseTimer = () => {
|
||||
if (!entry.timer) {
|
||||
return
|
||||
}
|
||||
const elapsed = Date.now() - entry.startedAt
|
||||
entry.remainingMs = Math.max(0, entry.remainingMs - elapsed)
|
||||
clearTimeout(entry.timer)
|
||||
entry.timer = null
|
||||
entry.startedAt = null
|
||||
}
|
||||
/**
|
||||
* 回到前台按剩余时间恢复计时;挂起请求已被结算/已取走重放时为 no-op。
|
||||
*/
|
||||
entry.resumeTimer = () => {
|
||||
if (entry.settled || entry.retrying || entry.timer) {
|
||||
return
|
||||
}
|
||||
entry.startTimer(entry.remainingMs)
|
||||
}
|
||||
entry.resolveOnce = value => {
|
||||
if (entry.settled) {
|
||||
return
|
||||
@@ -172,9 +213,7 @@ function createBlockedAgreementRequest(options, resolve, reject) {
|
||||
cleanup()
|
||||
return Object.assign(cloneRequestOptions(entry.options), { _agreementRetry: 1 })
|
||||
}
|
||||
entry.timer = setTimeout(() => {
|
||||
entry.rejectNotAccepted()
|
||||
}, AGREEMENT_PENDING_TIMEOUT_MS)
|
||||
entry.startTimer(AGREEMENT_PENDING_TIMEOUT_MS)
|
||||
return entry
|
||||
}
|
||||
|
||||
@@ -238,6 +277,31 @@ function discardPendingAgreement() {
|
||||
blocked.rejectNotAccepted()
|
||||
}
|
||||
|
||||
/**
|
||||
* 协议页切后台时调用:暂停挂起请求的超时计时,后台期间不会触发结算。
|
||||
* 挂起请求已不存在(已结算/已取走重放)时为 no-op。
|
||||
*/
|
||||
function pausePendingAgreementTimeout() {
|
||||
const blocked = agreementBlockedRequest
|
||||
if (!blocked) {
|
||||
return
|
||||
}
|
||||
blocked.pauseTimer()
|
||||
}
|
||||
|
||||
/**
|
||||
* 协议页回到前台时调用:按剩余可见时长恢复计时。
|
||||
* 挂起请求已不存在(例如后台期间已在别处完成同意、或被新的 4510 结算)时为 no-op,
|
||||
* 不会重复计时或二次结算。
|
||||
*/
|
||||
function resumePendingAgreementTimeout() {
|
||||
const blocked = agreementBlockedRequest
|
||||
if (!blocked) {
|
||||
return
|
||||
}
|
||||
blocked.resumeTimer()
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户在协议页同意成功后调用:重放首个被 4510 拦下的原请求一次。
|
||||
* 取走待重放请求时即清理定时器与挂起引用,后续返回/超时不会打断已开始的成功重放;
|
||||
@@ -390,6 +454,8 @@ request.toBackendRequestPath = toBackendRequestPath
|
||||
request.handleUnauthorized = handleUnauthorized
|
||||
request.retryPendingAfterAgreement = retryPendingAfterAgreement
|
||||
request.discardPendingAgreement = discardPendingAgreement
|
||||
request.pausePendingAgreementTimeout = pausePendingAgreementTimeout
|
||||
request.resumePendingAgreementTimeout = resumePendingAgreementTimeout
|
||||
request.resolveUserMessage = resolveUserMessage
|
||||
request.AGREEMENT_NOT_ACCEPTED_CODE = AGREEMENT_NOT_ACCEPTED_CODE
|
||||
|
||||
|
||||
Reference in New Issue
Block a user