reduce calls to offlinestorage while uploading attachments using debounce

This commit is contained in:
Salvatore Giordano
2021-03-01 11:52:32 +01:00
parent 578c4efade
commit ecb559f687
4 changed files with 37 additions and 1 deletions
@@ -0,0 +1,23 @@
import 'dart:async';
import 'package:meta/meta.dart';
/// Map of timeouts being debounced
Map timeouts = {};
/// Runs a function avoiding calling it too many times in a [timeoutMS] window
void debounce({
@required Duration timeout,
@required Function target,
@required List arguments,
}) {
if (timeouts.containsKey(target)) {
timeouts[target].cancel();
}
final timer = Timer(timeout, () {
Function.apply(target, arguments);
});
timeouts[target] = timer;
}