call method

void call(
  1. void action()
)

Execute action with smart delay adaptation

Implementation

void call(void Function() action) {
  final now = DateTime.now();

  // Adaptive delay calculation based on typing speed
  if (_lastAction != null) {
    final timeBetweenActions = now.difference(_lastAction!);

    if (timeBetweenActions <= _fastTypingThreshold) {
      // User is typing fast - use minimal delay for instant feedback
      _actionCount++;
      final adaptedDelay = Duration(
        milliseconds: (_minDelay.inMilliseconds +
                      (_maxDelay.inMilliseconds - _minDelay.inMilliseconds) /
                      (_actionCount.clamp(1, 10))).round()
      );
      _currentDelay = adaptedDelay;
    } else {
      // User is typing slowly - use standard delay and reset counter
      _currentDelay = _maxDelay;
      _actionCount = 0;
    }
  }

  _lastAction = now;

  // Cancel previous timer and start new one
  _timer?.cancel();
  _timer = Timer(_currentDelay, () {
    action();
    _actionCount = 0; // Reset after execution
  });
}