Bug 980511 - Uplift Add-on SDK to Firefox

This commit is contained in:
Erik Vold
2014-03-06 14:21:49 -08:00
parent 88162368ec
commit 02b270ec51
6 changed files with 141 additions and 17 deletions

View File

@@ -13,7 +13,7 @@ module.metadata = {
};
const { deprecateFunction } = require("../util/deprecate");
const { setImmediate, setTimeout } = require("../timers");
const { setImmediate, setTimeout, clearTimeout } = require("../timers");
const arity = f => f.arity || f.length;
@@ -361,3 +361,40 @@ const debounce = function debounce (fn, wait) {
};
};
exports.debounce = debounce;
/**
* From underscore's `_.throttle`
* http://underscorejs.org
* (c) 2009-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
* Underscore may be freely distributed under the MIT license.
*/
const throttle = function throttle (func, wait, options) {
let context, args, result;
let timeout = null;
let previous = 0;
options || (options = {});
let later = function() {
previous = options.leading === false ? 0 : Date.now();
timeout = null;
result = func.apply(context, args);
context = args = null;
};
return function() {
let now = Date.now();
if (!previous && options.leading === false) previous = now;
let remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
exports.throttle = throttle;