feat(async/unstable): allow setting dynamic timeframe for throttle#7002
feat(async/unstable): allow setting dynamic timeframe for throttle#7002lionel-rowe wants to merge 6 commits intodenoland:mainfrom
throttle#7002Conversation
9f08109 to
81792a1
Compare
throttlethrottle
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7002 +/- ##
==========================================
+ Coverage 94.21% 94.22% +0.01%
==========================================
Files 615 617 +2
Lines 47840 48591 +751
Branches 8320 8539 +219
==========================================
+ Hits 45074 45787 +713
- Misses 2699 2736 +37
- Partials 67 68 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| */ | ||
| // deno-lint-ignore no-explicit-any | ||
| export function throttle<T extends Array<any>>( | ||
| fn: (this: ThrottledFunction<T>, ...args: T) => void, |
There was a problem hiding this comment.
The return type of the function should be updated to match the new async handling, something like void | PromiseLike<void>
There was a problem hiding this comment.
I thought about that but actually returning a promise doesn't really make sense, as the wrapped function doesn't always run and so the return value should be treated as meaningless and discarded, i.e. should be void/undefined.
There was a problem hiding this comment.
True, the return value is discarded. So I guess the benefit of adding PromiseLike is to signal/document fn?
There was a problem hiding this comment.
True, the return value is discarded. So I guess the benefit of adding
PromiseLikeis to signal/document fn?
Sorry, I don't follow. PromiseLike is not part of the signature, but promise-likes do now have special handling to enable lastExecution to track their time-to-resolve rather than time-to-sync-return.
There was a problem hiding this comment.
Sorry, I don't follow.
PromiseLikeis not part of the signature, but promise-likes do now have special handling to enablelastExecutionto track their time-to-resolve rather than time-to-sync-return.
It was more of a cosmetic thing. Totally fine to keep it like this
|
@lionel-rowe Can you also add an example with comment about when to use the new option? |
Closes #6796.
If
fnreturns a promise,lastExecutionis now based on the asynchronous end time of the previous completed call. For example with this function:If the throttled function is called once at time 0 and we observe
lastExecution:This will also affect how throttling works with asynchronous functions — the current implementation basically doesn't throttle at all for typical async functions, whereas the new one does.