You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
705 B
23 lines
705 B
2 years ago
|
class FutureCallDebounce<TParams extends Object> {
|
||
|
TParams? _params;
|
||
|
Future? _awaited;
|
||
|
final Future Function(TParams) futureCall;
|
||
|
final TParams Function(TParams oldParams, TParams newParams) combiner;
|
||
|
|
||
|
static TParams _defaultCombiner<TParams>(TParams _, TParams newParams) => newParams;
|
||
|
|
||
|
FutureCallDebounce({required this.futureCall, required this.combiner});
|
||
|
FutureCallDebounce.replaceCombiner({required this.futureCall}) : combiner = _defaultCombiner;
|
||
|
|
||
|
void call(TParams newParams) {
|
||
|
if (_params != null) {
|
||
|
_params = combiner(_params!, newParams);
|
||
|
}
|
||
|
else {
|
||
|
_params = newParams;
|
||
|
}
|
||
|
|
||
|
_awaited ??= futureCall(_params!).then((value) => _awaited = null);
|
||
|
}
|
||
|
}
|