Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 'requestIdleCallback' when available for debounce/throttle #1352

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions util/misc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
define(function () {
define([
'dojo/has'
], function (has) {
// summary:
// This module defines miscellaneous utility methods for purposes of
// adding styles, and throttling/debouncing function calls.
Expand All @@ -9,7 +11,30 @@ define(function () {
extraSheet,
removeMethod,
rulesProperty,
invalidCssChars = /([^A-Za-z0-9_\u00A0-\uFFFF-])/g;
invalidCssChars = /([^A-Za-z0-9_\u00A0-\uFFFF-])/g,
delayCallback = setTimeout,
cancelDelay = clearTimeout;

has.add('requestidlecallback', function (global) {
return typeof global.requestIdleCallback === 'function';
});

// The presence of the 'requestIdleCallback' method indicates a browser that might
// performance optimize code by delaying execution of the callback passed to
// 'setTimeout', so use 'requestIdleCallback' to improve the likelihood of the
// callback being executed in a timely manner.
// This is not a perfect solution, but has worked well in testing.
// requestIdleCallback is designed to be called successively, performing progressive chunks
// of computation each time until the task is complete.
// setTimeout executes its callback when delay has transpired, *or later*
// requestIdleCallback executes its callback when delay has transpired, *or sooner*
// Fixes https://github.com/SitePen/dgrid/issues/1351
if (has('requestidlecallback')) {
delayCallback = function (callback, delay) {
return requestIdleCallback(callback, { timeout: delay });
};
cancelDelay = cancelIdleCallback;
}

function removeRule(index) {
// Function called by the remove method on objects returned by addCssRule.
Expand Down Expand Up @@ -52,7 +77,7 @@ define(function () {
}
ran = true;
cb.apply(context, arguments);
setTimeout(function () {
delayCallback(function () {
ran = false;
}, delay);
};
Expand All @@ -69,7 +94,7 @@ define(function () {
}
ran = true;
var a = arguments;
setTimeout(function () {
delayCallback(function () {
ran = false;
cb.apply(context, a);
}, delay);
Expand All @@ -83,11 +108,12 @@ define(function () {
delay = delay || util.defaultDelay;
return function () {
if (timer) {
clearTimeout(timer);
cancelDelay(timer);
timer = null;
}
var a = arguments;
timer = setTimeout(function () {
timer = delayCallback(function () {
timer = null;
cb.apply(context, a);
}, delay);
};
Expand Down