2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-20 09:10:34 +03:00
Files
vue-meta/src/client/batchUpdate.js
T
2019-02-11 10:37:56 +01:00

24 lines
845 B
JavaScript

import { isUndefined } from '../shared/typeof'
// fallback to timers if rAF not present
const stopUpdate = (!isUndefined(window) ? window.cancelAnimationFrame : null) || clearTimeout
const startUpdate = (!isUndefined(window) ? window.requestAnimationFrame : null) || (cb => setTimeout(cb, 0))
/**
* Performs a batched update. Uses requestAnimationFrame to prevent
* calling a function too many times in quick succession.
* You need to pass it an ID (which can initially be `null`),
* but be sure to overwrite that ID with the return value of batchUpdate.
*
* @param {(null|Number)} id - the ID of this update
* @param {Function} callback - the update to perform
* @return {Number} id - a new ID
*/
export default function batchUpdate(id, callback) {
stopUpdate(id)
return startUpdate(() => {
id = null
callback()
})
}