| name | asyncredux-throttle-mixin |
| description | Add the Throttle mixin to prevent actions from running too frequently. Covers setting the throttle duration in milliseconds, use cases like price refresh, and how freshness/staleness works. |
Throttle Mixin
The Throttle mixin limits action execution to at most once per throttle period. When an action is dispatched multiple times within the defined window, only the first execution runs while subsequent calls abort silently. After the period expires, the next dispatch is permitted.
Basic Usage
class LoadPrices extends AppAction with Throttle {
// Throttle period in milliseconds (default is 1000ms)
int get throttle => 5000; // 5 seconds
Future<AppState?> reduce() async {
var prices = await fetchCurrentPrices();
return state.copy(prices: prices);
}
}
The default throttle duration is 1000 milliseconds (1 second). Override the throttle getter to set a custom duration.
How Throttle Works (Freshness/Staleness)
Throttle uses a "freshness window" concept:
- First dispatch: Action runs immediately, data becomes "fresh"
- During throttle period: Data is considered fresh, subsequent dispatches are aborted
- After throttle period expires: Data becomes "stale", next dispatch is allowed to run
This ensures that frequently triggered actions (like a "Refresh Prices" button) don't overwhelm your server while still allowing updates after a reasonable interval.
// User taps "Refresh" rapidly 5 times in 2 seconds
// With a 5-second throttle:
// - 1st tap: Action runs, prices update
// - 2nd-5th taps: Silently aborted (data still "fresh")
// - Tap after 5 seconds: Action runs again (data now "stale")
Throttle vs Debounce