Skip to content

Instantly share code, notes, and snippets.

@banksJeremy
Last active January 10, 2016 04:41
Show Gist options
  • Save banksJeremy/8afa255b9714fbd47283 to your computer and use it in GitHub Desktop.
Save banksJeremy/8afa255b9714fbd47283 to your computer and use it in GitHub Desktop.
Throttle decorator parameters:
f - function
interval - number - the minimum duration in miliseconds between calls
keep - FIRST/LAST/ALL - which function calls will be kept if throttling must occur
wait - NONE/INTERVAL/IDLE - how long to wait after getting the first call before making a call
- if NONE the call will be made immediately
- if INTERVAL the call will be made after interval ms have elapsed since the first call
- if IDLE the call will be made after interval ms have elasped without a call
wait-interval - optional number - can be used to override the wait interval, instead of using the standard interval.
must be <= interval
result - OWN/ANY - what the promise result can correspond to
- if OWN, the promise will be resolved when the input call is excecuted, else rejected.
- if ANY, the promise will be resolved with the input call, or the call which was excuted to block the input call
Returns:
a promise that will be either resolved or rejected.
I guess we need an option for how these work
There are different potential throttling modes.
Given a throttle interval of |----| and these input calls:
A B C D E F
The modes will produce the following output calls:
1: A---- C---- D----F----
(drop: ignore calls within interval of latest not ignored)
(this is the only one that can result in latest value being dropped)
( keep: first / delay: none )
2: --------B ----C ----D---F
(debounce: waiting for interval of inactivity, then sending latest)
(keep: last / delay: until-inactive)
3: A----B---- C---- D----E----F----
(throttle: make all calls, but buffer to throttle to interval)
(keep: all / delay: none )
4: ----B ----C ----E----F
(lazy: after first call, wait interval than make latest call and reset)
(keep: last, delay: one-interval)
5: A----B---- C---- D----F----
(smart: send first immediately, then after interval send latest if any)
(keep: last / delay: none )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment