rate limiter
rate limiting is a strategy for limiting an action. it puts a cap on how often someone can repeat an action within a certain timeframe. using rate_limiter we made it easier than ever to apply these strategies on regular dart functions.
installation
add the following to your pubspec.yaml
and replace [version]
with the latest version:
dependencies:
rate_limiter: ^[version]
strategies
debounce
a debounced function will ignore all calls to it until the calls have stopped for a specified time period. only then it will call the original function. for instance, if we specify the time as two seconds, and the debounced function is called 10 times with an interval of one second between each call, the function will not call the original function until two seconds after the last (tenth) call.
usage
it’s fairly simple to create debounced function with rate_limiter
- creating from scratch
final debouncedfunction = debounce((string value) {
print('got value : $value');
return value;
}, const duration(seconds: 2));
- converting an existing function into debounced function
string regularfunction(string value) {
print('got value : $value');
return value;
}
final debouncedfunction = regularfunction.debounced(
const duration(seconds: 2),
);
example
often times, search boxes offer dropdowns that provide autocomplete options for the user’s current input. sometimes the items suggested are fetched from the backend via api (for instance, on google maps). the autocomplete api gets called whenever the search query changes. without debounce, an api call would be made for every letter you type, even if you’re typing very fast. debouncing by one second will ensure that the autocomplete function does nothing until one second after the user is done typing.
final debouncedautocompletesearch = debounce(
(string searchquery) async {
// fetches results from the api
final results = await searchapi.get(searchquery);
// updates suggestion list
updatesearchsuggestions(results);
},
const duration(seconds: 1),
);
textfield(
onchanged: (query) {
debouncedautocompletesearch([query]);
},
);
throttle
to throttle a function means to ensure that the function is called at most once in a specified time period (for instance, once every 10 seconds). this means throttling will prevent a function from running if it has run “recently”. throttling also ensures a function is run regularly at a fixed rate.
usage
creating throttled function is similar to debounce function
- creating from scratch
final throttledfunction = throttle((string value) {
print('got value : $value');
return value;
}, const duration(seconds: 2));
- converting an existing function into throttled function
string regularfunction(string value) {
print('got value : $value');
return value;
}
final throttledfunction = regularfunction.throttled(
const duration(seconds: 2),
);
example
in action games, the user often performs a key action by pushing a button (example: shooting, punching). but, as any gamer knows, users often press the buttons much more than is necessary, probably due to the excitement and intensity of the action. so the user might hit “punch” 10 times in 5 seconds, but the game character can only throw one punch in one second. in such a situation, it makes sense to throttle the action. in this case, throttling the “punch” action to one second would ignore the second button press each second.
final throttledperformpunch = throttle(
() {
print('performed one punch to the opponent');
},
const duration(seconds: 1),
);
raisedbutton(
onpressed: (){
throttledperformpunch();
}
child: text('punch')
);
pending
used to check if the there are functions still remaining to get invoked.
final pending = debouncedfunction.ispending;
final pending = throttledfunction.ispending;
flush
used to immediately invoke all the remaining delayed functions.
final result = debouncedfunction.flush();
final result = throttledfunction.flush();
cancellation
used to cancel all the remaining delayed functions.
debouncedfunction.cancel();
throttledfunction.cancel();
Comments are closed.