react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / node_modules / lodash / function / debounce.js
80742 viewsvar isObject = require('../lang/isObject'),1now = require('../date/now');23/** Used as the `TypeError` message for "Functions" methods. */4var FUNC_ERROR_TEXT = 'Expected a function';56/* Native method references for those with the same name as other `lodash` methods. */7var nativeMax = Math.max;89/**10* Creates a debounced function that delays invoking `func` until after `wait`11* milliseconds have elapsed since the last time the debounced function was12* invoked. The debounced function comes with a `cancel` method to cancel13* delayed invocations. Provide an options object to indicate that `func`14* should be invoked on the leading and/or trailing edge of the `wait` timeout.15* Subsequent calls to the debounced function return the result of the last16* `func` invocation.17*18* **Note:** If `leading` and `trailing` options are `true`, `func` is invoked19* on the trailing edge of the timeout only if the the debounced function is20* invoked more than once during the `wait` timeout.21*22* See [David Corbacho's article](http://drupalmotion.com/article/debounce-and-throttle-visual-explanation)23* for details over the differences between `_.debounce` and `_.throttle`.24*25* @static26* @memberOf _27* @category Function28* @param {Function} func The function to debounce.29* @param {number} [wait=0] The number of milliseconds to delay.30* @param {Object} [options] The options object.31* @param {boolean} [options.leading=false] Specify invoking on the leading32* edge of the timeout.33* @param {number} [options.maxWait] The maximum time `func` is allowed to be34* delayed before it is invoked.35* @param {boolean} [options.trailing=true] Specify invoking on the trailing36* edge of the timeout.37* @returns {Function} Returns the new debounced function.38* @example39*40* // avoid costly calculations while the window size is in flux41* jQuery(window).on('resize', _.debounce(calculateLayout, 150));42*43* // invoke `sendMail` when the click event is fired, debouncing subsequent calls44* jQuery('#postbox').on('click', _.debounce(sendMail, 300, {45* 'leading': true,46* 'trailing': false47* }));48*49* // ensure `batchLog` is invoked once after 1 second of debounced calls50* var source = new EventSource('/stream');51* jQuery(source).on('message', _.debounce(batchLog, 250, {52* 'maxWait': 100053* }));54*55* // cancel a debounced call56* var todoChanges = _.debounce(batchLog, 1000);57* Object.observe(models.todo, todoChanges);58*59* Object.observe(models, function(changes) {60* if (_.find(changes, { 'user': 'todo', 'type': 'delete'})) {61* todoChanges.cancel();62* }63* }, ['delete']);64*65* // ...at some point `models.todo` is changed66* models.todo.completed = true;67*68* // ...before 1 second has passed `models.todo` is deleted69* // which cancels the debounced `todoChanges` call70* delete models.todo;71*/72function debounce(func, wait, options) {73var args,74maxTimeoutId,75result,76stamp,77thisArg,78timeoutId,79trailingCall,80lastCalled = 0,81maxWait = false,82trailing = true;8384if (typeof func != 'function') {85throw new TypeError(FUNC_ERROR_TEXT);86}87wait = wait < 0 ? 0 : (+wait || 0);88if (options === true) {89var leading = true;90trailing = false;91} else if (isObject(options)) {92leading = options.leading;93maxWait = 'maxWait' in options && nativeMax(+options.maxWait || 0, wait);94trailing = 'trailing' in options ? options.trailing : trailing;95}9697function cancel() {98if (timeoutId) {99clearTimeout(timeoutId);100}101if (maxTimeoutId) {102clearTimeout(maxTimeoutId);103}104maxTimeoutId = timeoutId = trailingCall = undefined;105}106107function delayed() {108var remaining = wait - (now() - stamp);109if (remaining <= 0 || remaining > wait) {110if (maxTimeoutId) {111clearTimeout(maxTimeoutId);112}113var isCalled = trailingCall;114maxTimeoutId = timeoutId = trailingCall = undefined;115if (isCalled) {116lastCalled = now();117result = func.apply(thisArg, args);118if (!timeoutId && !maxTimeoutId) {119args = thisArg = null;120}121}122} else {123timeoutId = setTimeout(delayed, remaining);124}125}126127function maxDelayed() {128if (timeoutId) {129clearTimeout(timeoutId);130}131maxTimeoutId = timeoutId = trailingCall = undefined;132if (trailing || (maxWait !== wait)) {133lastCalled = now();134result = func.apply(thisArg, args);135if (!timeoutId && !maxTimeoutId) {136args = thisArg = null;137}138}139}140141function debounced() {142args = arguments;143stamp = now();144thisArg = this;145trailingCall = trailing && (timeoutId || !leading);146147if (maxWait === false) {148var leadingCall = leading && !timeoutId;149} else {150if (!maxTimeoutId && !leading) {151lastCalled = stamp;152}153var remaining = maxWait - (stamp - lastCalled),154isCalled = remaining <= 0 || remaining > maxWait;155156if (isCalled) {157if (maxTimeoutId) {158maxTimeoutId = clearTimeout(maxTimeoutId);159}160lastCalled = stamp;161result = func.apply(thisArg, args);162}163else if (!maxTimeoutId) {164maxTimeoutId = setTimeout(maxDelayed, remaining);165}166}167if (isCalled && timeoutId) {168timeoutId = clearTimeout(timeoutId);169}170else if (!timeoutId && wait !== maxWait) {171timeoutId = setTimeout(delayed, wait);172}173if (leadingCall) {174isCalled = true;175result = func.apply(thisArg, args);176}177if (isCalled && !timeoutId && !maxTimeoutId) {178args = thisArg = null;179}180return result;181}182debounced.cancel = cancel;183return debounced;184}185186module.exports = debounce;187188189