2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-07 19:42:26 +03:00

refactor: batch updates using requestIdleCallback, use setTimeout as fallback

This commit is contained in:
pimlie
2019-10-03 22:41:23 +02:00
parent f6591217e6
commit 8c75ec1356
+9 -3
View File
@@ -1,4 +1,10 @@
import { rootConfigKey } from '../shared/constants'
import { hasGlobalWindow } from '../utils/window'
const scheduleRefresh = (hasGlobalWindow && window.requestIdleCallback) || function (cb, { timeout }) {
return setTimeout(cb, timeout)
}
const cancelRefresh = (hasGlobalWindow && window.cancelIdleCallback) || clearTimeout
// store an id to keep track of DOM updates
let batchId = null
@@ -32,10 +38,10 @@ export function batchUpdate (callback, timeout) {
return
}
clearTimeout(batchId)
batchId = setTimeout(() => {
cancelRefresh(batchId)
batchId = scheduleRefresh(() => {
callback()
}, timeout)
}, { timeout })
return batchId
}