Last active
June 13, 2022 13:03
-
-
Save demirdev/2d40166fe5c58cfa9d3399ede56f8520 to your computer and use it in GitHub Desktop.
DeBouncer mixin for Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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