shouldDebounce static method
Check if a log should be debounced based on time interval
@paramkey: The debounce key to identify the log entry
@paramdebounceMs: Debounce interval in milliseconds
@return: Returns true if the log should be skipped (still in debounce period), false to allow logging
This prevents rapid-fire logging of the same message by enforcing a minimum time interval between consecutive logs with the same key
Memory Management:
- When maxDebounceEntries is exceeded, automatically removes the oldest debounceCleanupCount entries
- This prevents unbounded memory growth in long-running applications
Implementation
static bool shouldDebounce(String key, int debounceMs) {
if (debounceMs <= 0) return false;
final now = DateTime.now();
final lastTime = _debounceTimestamps[key];
if (lastTime == null) {
// Check if we need to cleanup old entries before adding new one
if (maxDebounceEntries > 0 &&
_debounceTimestamps.length >= maxDebounceEntries) {
_cleanupOldestDebounceEntries();
}
_debounceTimestamps[key] = now;
return false;
}
final difference = now.difference(lastTime).inMilliseconds;
if (difference < debounceMs) {
return true; // Still in debounce period, skip this log
}
// Update timestamp and allow log
_debounceTimestamps[key] = now;
return false;
}