This repository has been archived on 2022-03-12. You can view files and clone it, but cannot push or open issues or pull requests.
reValuate/node_modules/fbjs/lib/debounceCore.js

65 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2021-04-02 02:24:13 +03:00
"use strict";
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @typechecks
*/
/**
* Invokes the given callback after a specified number of milliseconds have
* elapsed, ignoring subsequent calls.
*
* For example, if you wanted to update a preview after the user stops typing
* you could do the following:
*
* elem.addEventListener('keyup', debounce(this.updatePreview, 250), false);
*
* The returned function has a reset method which can be called to cancel a
* pending invocation.
*
* var debouncedUpdatePreview = debounce(this.updatePreview, 250);
* elem.addEventListener('keyup', debouncedUpdatePreview, false);
*
* // later, to cancel pending calls
* debouncedUpdatePreview.reset();
*
* @param {function} func - the function to debounce
* @param {number} wait - how long to wait in milliseconds
* @param {*} context - optional context to invoke the function in
* @param {?function} setTimeoutFunc - an implementation of setTimeout
* if nothing is passed in the default setTimeout function is used
* @param {?function} clearTimeoutFunc - an implementation of clearTimeout
* if nothing is passed in the default clearTimeout function is used
*/
function debounce(func, wait, context, setTimeoutFunc, clearTimeoutFunc) {
setTimeoutFunc = setTimeoutFunc || setTimeout;
clearTimeoutFunc = clearTimeoutFunc || clearTimeout;
var timeout;
function debouncer() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
debouncer.reset();
var callback = function callback() {
func.apply(context, args);
};
callback.__SMmeta = func.__SMmeta;
timeout = setTimeoutFunc(callback, wait);
}
debouncer.reset = function () {
clearTimeoutFunc(timeout);
};
return debouncer;
}
module.exports = debounce;