all files / src/utils/ debounce.js

93.1% Statements 27/29
71.43% Branches 10/14
100% Functions 3/3
93.1% Lines 27/29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                           
"use strict";
 
exports.__esModule = true;
exports["default"] = debounce;
 
function debounce(func, wait, immediate) {
  var timeout = undefined;
  var args = undefined;
  var context = undefined;
  var timestamp = undefined;
  var result = undefined;
 
  var later = function later() {
    var last = +new Date() - timestamp;
 
    if (last < wait && last >= 0) {
      timeout = setTimeout(later, wait - last);
    } else {
      timeout = null;
      Eif (!immediate) {
        result = func.apply(context, args);
        Eif (!timeout) {
          context = args = null;
        }
      }
    }
  };
 
  return function debounced() {
    context = this;
    args = arguments;
    timestamp = +new Date();
 
    var callNow = immediate && !timeout;
    if (!timeout) {
      timeout = setTimeout(later, wait);
    }
 
    Iif (callNow) {
      result = func.apply(context, args);
      context = args = null;
    }
 
    return result;
  };
}
 
module.exports = exports["default"];