mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 09:02:03 +00:00
fix(nuxt): use max length + iterations for useCookie
timeout (#24253)
This commit is contained in:
parent
7863981762
commit
a10e33c009
@ -140,22 +140,43 @@ function writeServerCookie (event: H3Event, name: string, value: any, opts: Cook
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum value allowed on a timeout delay.
|
||||||
|
*
|
||||||
|
* Reference: https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value
|
||||||
|
*/
|
||||||
|
const MAX_TIMEOUT_DELAY = 2_147_483_647
|
||||||
|
|
||||||
// custom ref that will update the value to undefined if the cookie expires
|
// custom ref that will update the value to undefined if the cookie expires
|
||||||
function cookieRef<T> (value: T | undefined, delay: number) {
|
function cookieRef<T> (value: T | undefined, delay: number) {
|
||||||
let timeout: NodeJS.Timeout
|
let timeout: NodeJS.Timeout
|
||||||
|
let elapsed = 0
|
||||||
|
if (getCurrentScope()) {
|
||||||
onScopeDispose(() => { clearTimeout(timeout) })
|
onScopeDispose(() => { clearTimeout(timeout) })
|
||||||
|
}
|
||||||
|
|
||||||
return customRef((track, trigger) => {
|
return customRef((track, trigger) => {
|
||||||
|
function createExpirationTimeout () {
|
||||||
|
clearTimeout(timeout)
|
||||||
|
const timeRemaining = delay - elapsed
|
||||||
|
const timeoutLength = timeRemaining < MAX_TIMEOUT_DELAY ? timeRemaining : MAX_TIMEOUT_DELAY
|
||||||
|
timeout = setTimeout(() => {
|
||||||
|
elapsed += timeoutLength
|
||||||
|
if (elapsed < delay) { return createExpirationTimeout() }
|
||||||
|
|
||||||
|
value = undefined
|
||||||
|
trigger()
|
||||||
|
}, timeoutLength)
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
get () {
|
get () {
|
||||||
track()
|
track()
|
||||||
return value
|
return value
|
||||||
},
|
},
|
||||||
set (newValue) {
|
set (newValue) {
|
||||||
clearTimeout(timeout)
|
createExpirationTimeout()
|
||||||
timeout = setTimeout(() => {
|
|
||||||
value = undefined
|
|
||||||
trigger()
|
|
||||||
}, delay)
|
|
||||||
value = newValue
|
value = newValue
|
||||||
trigger()
|
trigger()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user