Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import isObject from './isObject'
  2. function debounce(func, wait, options) {
  3. let lastArgs,
  4. lastThis,
  5. maxWait,
  6. result,
  7. timerId,
  8. lastCallTime
  9. let lastInvokeTime = 0
  10. let leading = false
  11. let maxing = false
  12. let trailing = true
  13. // Bypass `requestAnimationFrame` by explicitly setting `wait=0`.
  14. const useRAF = false
  15. if (typeof func !== 'function') {
  16. throw new TypeError('Expected a function')
  17. }
  18. wait = +wait || 0
  19. if (isObject(options)) {
  20. leading = !!options.leading
  21. maxing = 'maxWait' in options
  22. maxWait = maxing ? Math.max(+options.maxWait || 0, wait) : maxWait
  23. trailing = 'trailing' in options ? !!options.trailing : trailing
  24. }
  25. function invokeFunc(time) {
  26. const args = lastArgs
  27. const thisArg = lastThis
  28. lastArgs = lastThis = undefined
  29. lastInvokeTime = time
  30. result = func.apply(thisArg, args)
  31. return result
  32. }
  33. function startTimer(pendingFunc, wait) {
  34. return setTimeout(pendingFunc, wait)
  35. }
  36. function cancelTimer(id) {
  37. clearTimeout(id)
  38. }
  39. function leadingEdge(time) {
  40. // Reset any `maxWait` timer.
  41. lastInvokeTime = time
  42. // Start the timer for the trailing edge.
  43. timerId = startTimer(timerExpired, wait)
  44. // Invoke the leading edge.
  45. return leading ? invokeFunc(time) : result
  46. }
  47. function remainingWait(time) {
  48. const timeSinceLastCall = time - lastCallTime
  49. const timeSinceLastInvoke = time - lastInvokeTime
  50. const timeWaiting = wait - timeSinceLastCall
  51. return maxing
  52. ? Math.min(timeWaiting, maxWait - timeSinceLastInvoke)
  53. : timeWaiting
  54. }
  55. function shouldInvoke(time) {
  56. const timeSinceLastCall = time - lastCallTime
  57. const timeSinceLastInvoke = time - lastInvokeTime
  58. // Either this is the first call, activity has stopped and we're at the
  59. // trailing edge, the system time has gone backwards and we're treating
  60. // it as the trailing edge, or we've hit the `maxWait` limit.
  61. return (lastCallTime === undefined || (timeSinceLastCall >= wait) ||
  62. (timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait))
  63. }
  64. function timerExpired() {
  65. const time = Date.now()
  66. if (shouldInvoke(time)) {
  67. return trailingEdge(time)
  68. }
  69. // Restart the timer.
  70. timerId = startTimer(timerExpired, remainingWait(time))
  71. }
  72. function trailingEdge(time) {
  73. timerId = undefined
  74. // Only invoke if we have `lastArgs` which means `func` has been
  75. // debounced at least once.
  76. if (trailing && lastArgs) {
  77. return invokeFunc(time)
  78. }
  79. lastArgs = lastThis = undefined
  80. return result
  81. }
  82. function cancel() {
  83. if (timerId !== undefined) {
  84. cancelTimer(timerId)
  85. }
  86. lastInvokeTime = 0
  87. lastArgs = lastCallTime = lastThis = timerId = undefined
  88. }
  89. function flush() {
  90. return timerId === undefined ? result : trailingEdge(Date.now())
  91. }
  92. function pending() {
  93. return timerId !== undefined
  94. }
  95. function debounced(...args) {
  96. const time = Date.now()
  97. const isInvoking = shouldInvoke(time)
  98. lastArgs = args
  99. lastThis = this
  100. lastCallTime = time
  101. if (isInvoking) {
  102. if (timerId === undefined) {
  103. return leadingEdge(lastCallTime)
  104. }
  105. if (maxing) {
  106. // Handle invocations in a tight loop.
  107. timerId = startTimer(timerExpired, wait)
  108. return invokeFunc(lastCallTime)
  109. }
  110. }
  111. if (timerId === undefined) {
  112. timerId = startTimer(timerExpired, wait)
  113. }
  114. return result
  115. }
  116. debounced.cancel = cancel
  117. debounced.flush = flush
  118. debounced.pending = pending
  119. return debounced
  120. }
  121. export default debounce