logic-circuits-simulator/lib/utils/future_call_debounce.dart
Dan Cojocaru c2d5d86554
Implemented partial simulation and design*
- Implemented partial (step by step) simulation of visually designed
components
- Implemented moving components in design mode and simulating components
in simulation mode (click inputs to toggle)

TODO:
- add/remove subcomponents, wires via GUI
- add GUI for step by step simulation
2022-07-03 05:17:32 +03:00

22 lines
705 B
Dart

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);
}
}