From 8c75ec1356dbf2cdee7a94c5987c8df417538d07 Mon Sep 17 00:00:00 2001 From: pimlie Date: Thu, 3 Oct 2019 22:41:23 +0200 Subject: [PATCH] refactor: batch updates using requestIdleCallback, use setTimeout as fallback --- src/client/update.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/client/update.js b/src/client/update.js index 3e661ee..5c8eca1 100644 --- a/src/client/update.js +++ b/src/client/update.js @@ -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 }