Skip to content

Instantly share code, notes, and snippets.

@demirdev
Last active June 13, 2022 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demirdev/2d40166fe5c58cfa9d3399ede56f8520 to your computer and use it in GitHub Desktop.
Save demirdev/2d40166fe5c58cfa9d3399ede56f8520 to your computer and use it in GitHub Desktop.
DeBouncer mixin for Dart
import 'dart:async';
mixin DeBouncer {
Timer? _debounce;
deBouncer(Function callback) {
if (_debounce?.isActive ?? false) _debounce?.cancel();
_debounce = Timer(Duration(milliseconds: 300), () {
callback();
});
}
}
// Usage
class SearchController extends Controller with DeBouncer {
List<Product>? results;
void search(String query) async {
deBouncer(() async {
results?.clear();
results = await API.instance.searchProduct(query);
update();
});
}
}
//
//
//
//
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment