Skip to content

Instantly share code, notes, and snippets.

@beppu
Created June 3, 2014 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beppu/48b17ec3c5d553135499 to your computer and use it in GitHub Desktop.
Save beppu/48b17ec3c5d553135499 to your computer and use it in GitHub Desktop.
a debounce that summarizes instead of throwing away information from frequent function calls.
# Think of this as a debounce that remembers intermediate calls and summarizes them instead of throwing them away.
#
# @param {Function} initial-fn function that returns initial-state for use with fold and combiner-fn
# @param {Function} combiner-fn given an array of arrays of function arguments, fold them into a value that can be used by final-fn
# @param {Function} final-fn function to run after nothing has happend for longer than delay milliseconds
# @param {Number} delay milliseconds to wait for inactivity before running final-fn
export burst = (initial-fn, combiner-fn, final-fn, delay) ->
ax = []
accumulating = false
wait = null
finish = ->
initial-value = initial-fn!
final = fold combiner-fn, initial-value, ax
final-fn(final)
ax := []
accumulating := false
wait := null
(...args) ->
if not accumulating
accumulating := true
else
clear-timeout(wait) if wait
wait := set-timeout finish, delay
ax.push args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment