修复防抖可以全部执行完成

This commit is contained in:
DESKTOP-I3JPKHK\wy 2025-12-12 16:00:36 +08:00
parent c184721f06
commit 825aa8017d
1 changed files with 45 additions and 4 deletions

View File

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:easy_debounce/easy_debounce.dart'; import 'package:easy_debounce/easy_debounce.dart';
import 'package:easy_debounce/easy_throttle.dart'; import 'package:easy_debounce/easy_throttle.dart';
@ -15,6 +17,9 @@ class AsyncThrottle {
// //
final Map<String, bool> _asyncLocks = {}; final Map<String, bool> _asyncLocks = {};
// Completer await
final Map<String, Completer<void>> _debounceCompleters = {};
/// - ( Loading UI) /// - ( Loading UI)
/// ///
/// / /// /
@ -40,18 +45,37 @@ class AsyncThrottle {
if (enableDebounce) { if (enableDebounce) {
// === (Debounce) === // === (Debounce) ===
// duration
//
// EasyDebounce Timer
// await
if (_debounceCompleters.containsKey(tagId)) {
_completeDebounce(tagId);
}
final completer = Completer<void>();
_debounceCompleters[tagId] = completer;
EasyDebounce.debounce(tagId, duration, () async { EasyDebounce.debounce(tagId, duration, () async {
// // Completer
if (isExecuting(tagId)) return; if (!_debounceCompleters.containsKey(tagId)) return;
//
if (isExecuting(tagId)) {
_completeDebounce(tagId);
return;
}
_asyncLocks[tagId] = true; _asyncLocks[tagId] = true;
try { try {
await onExecute(); await onExecute();
} finally { } finally {
_asyncLocks.remove(tagId); _asyncLocks.remove(tagId);
_completeDebounce(tagId); // await
} }
}); });
await completer.future;
} else { } else {
// === (Throttle) - === // === (Throttle) - ===
// duration // duration
@ -71,9 +95,26 @@ class AsyncThrottle {
} }
} }
/// Completer
void _completeDebounce(String tagId) {
if (_debounceCompleters.containsKey(tagId)) {
final completer = _debounceCompleters[tagId];
if (completer != null && !completer.isCompleted) {
completer.complete();
}
_debounceCompleters.remove(tagId);
}
}
/// ///
bool isExecuting(String tagId) => _asyncLocks[tagId] ?? false; bool isExecuting(String tagId) => _asyncLocks[tagId] ?? false;
/// ///
void clearAllLocks() => _asyncLocks.clear(); void clearAllLocks() {
_asyncLocks.clear();
// Future await
for (var key in _debounceCompleters.keys.toList()) {
_completeDebounce(key);
}
}
} }