index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* eslint-disable no-undefined,no-param-reassign,no-shadow */
  2. /**
  3. * Throttle execution of a function. Especially useful for rate limiting
  4. * execution of handlers on events like resize and scroll.
  5. *
  6. * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher)
  7. * are most useful.
  8. * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through,
  9. * as-is, to `callback` when the throttled-function is executed.
  10. * @param {object} [options] - An object to configure options.
  11. * @param {boolean} [options.noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds
  12. * while the throttled-function is being called. If noTrailing is false or unspecified, callback will be executed
  13. * one final time after the last throttled-function call. (After the throttled-function has not been called for
  14. * `delay` milliseconds, the internal counter is reset).
  15. * @param {boolean} [options.noLeading] - Optional, defaults to false. If noLeading is false, the first throttled-function call will execute callback
  16. * immediately. If noLeading is true, the first the callback execution will be skipped. It should be noted that
  17. * callback will never executed if both noLeading = true and noTrailing = true.
  18. * @param {boolean} [options.debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is
  19. * false (at end), schedule `callback` to execute after `delay` ms.
  20. *
  21. * @returns {Function} A new, throttled, function.
  22. */
  23. function throttle (delay, callback, options) {
  24. var _ref = options || {},
  25. _ref$noTrailing = _ref.noTrailing,
  26. noTrailing = _ref$noTrailing === void 0 ? false : _ref$noTrailing,
  27. _ref$noLeading = _ref.noLeading,
  28. noLeading = _ref$noLeading === void 0 ? false : _ref$noLeading,
  29. _ref$debounceMode = _ref.debounceMode,
  30. debounceMode = _ref$debounceMode === void 0 ? undefined : _ref$debounceMode;
  31. /*
  32. * After wrapper has stopped being called, this timeout ensures that
  33. * `callback` is executed at the proper times in `throttle` and `end`
  34. * debounce modes.
  35. */
  36. var timeoutID;
  37. var cancelled = false;
  38. // Keep track of the last time `callback` was executed.
  39. var lastExec = 0;
  40. // Function to clear existing timeout
  41. function clearExistingTimeout() {
  42. if (timeoutID) {
  43. clearTimeout(timeoutID);
  44. }
  45. }
  46. // Function to cancel next exec
  47. function cancel(options) {
  48. var _ref2 = options || {},
  49. _ref2$upcomingOnly = _ref2.upcomingOnly,
  50. upcomingOnly = _ref2$upcomingOnly === void 0 ? false : _ref2$upcomingOnly;
  51. clearExistingTimeout();
  52. cancelled = !upcomingOnly;
  53. }
  54. /*
  55. * The `wrapper` function encapsulates all of the throttling / debouncing
  56. * functionality and when executed will limit the rate at which `callback`
  57. * is executed.
  58. */
  59. function wrapper() {
  60. for (var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++) {
  61. arguments_[_key] = arguments[_key];
  62. }
  63. var self = this;
  64. var elapsed = Date.now() - lastExec;
  65. if (cancelled) {
  66. return;
  67. }
  68. // Execute `callback` and update the `lastExec` timestamp.
  69. function exec() {
  70. lastExec = Date.now();
  71. callback.apply(self, arguments_);
  72. }
  73. /*
  74. * If `debounceMode` is true (at begin) this is used to clear the flag
  75. * to allow future `callback` executions.
  76. */
  77. function clear() {
  78. timeoutID = undefined;
  79. }
  80. if (!noLeading && debounceMode && !timeoutID) {
  81. /*
  82. * Since `wrapper` is being called for the first time and
  83. * `debounceMode` is true (at begin), execute `callback`
  84. * and noLeading != true.
  85. */
  86. exec();
  87. }
  88. clearExistingTimeout();
  89. if (debounceMode === undefined && elapsed > delay) {
  90. if (noLeading) {
  91. /*
  92. * In throttle mode with noLeading, if `delay` time has
  93. * been exceeded, update `lastExec` and schedule `callback`
  94. * to execute after `delay` ms.
  95. */
  96. lastExec = Date.now();
  97. if (!noTrailing) {
  98. timeoutID = setTimeout(debounceMode ? clear : exec, delay);
  99. }
  100. } else {
  101. /*
  102. * In throttle mode without noLeading, if `delay` time has been exceeded, execute
  103. * `callback`.
  104. */
  105. exec();
  106. }
  107. } else if (noTrailing !== true) {
  108. /*
  109. * In trailing throttle mode, since `delay` time has not been
  110. * exceeded, schedule `callback` to execute `delay` ms after most
  111. * recent execution.
  112. *
  113. * If `debounceMode` is true (at begin), schedule `clear` to execute
  114. * after `delay` ms.
  115. *
  116. * If `debounceMode` is false (at end), schedule `callback` to
  117. * execute after `delay` ms.
  118. */
  119. timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);
  120. }
  121. }
  122. wrapper.cancel = cancel;
  123. // Return the wrapper function.
  124. return wrapper;
  125. }
  126. /* eslint-disable no-undefined */
  127. /**
  128. * Debounce execution of a function. Debouncing, unlike throttling,
  129. * guarantees that a function is only executed a single time, either at the
  130. * very beginning of a series of calls, or at the very end.
  131. *
  132. * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
  133. * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
  134. * to `callback` when the debounced-function is executed.
  135. * @param {object} [options] - An object to configure options.
  136. * @param {boolean} [options.atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds
  137. * after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.
  138. * (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).
  139. *
  140. * @returns {Function} A new, debounced function.
  141. */
  142. function debounce (delay, callback, options) {
  143. var _ref = options || {},
  144. _ref$atBegin = _ref.atBegin,
  145. atBegin = _ref$atBegin === void 0 ? false : _ref$atBegin;
  146. return throttle(delay, callback, {
  147. debounceMode: atBegin !== false
  148. });
  149. }
  150. export { debounce, throttle };
  151. //# sourceMappingURL=index.js.map