Created
June 3, 2014 02:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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