DebounceUtils class
- @Author: chuxiong
- @Created at: 2026/11/25 DebounceUtils —— 防抖节流工具(防连点门 + 输入防抖,双件)。
- canRun 防连点门(throttle,前沿触发):本次可执行则返回 true
并锁定
duration冷却窗口;窗口内同 key 后续调用返回 false; 窗口到期自动解锁。适合按钮防连点(首点立即生效)。 - debounce 输入防抖(debounce,尾沿触发):静默满
duration才 执行最后一次传入的回调;窗口内重复调用重置计时。 适合搜索框输入(停手才执行)。
实现为 Timer-map:状态即 Timer(到期自移除),无时间戳常驻表、
无容量清扫;不同 key 互不影响。key 为全局命名空间(同一实例内
跨场景共享),取名建议带场景前缀(如 'login-submit')防互斥。
防抖与节流状态相互独立(各自一张 Timer 表):canRun 的冷却不挡 同 key 的 debounce;cancel 是唯一同时清除两者的入口(该 key 的 待执行防抖 + 防连点冷却一并清除)。
依赖:无第三方(仅 SDK dart:async,纯 Dart——测试可经 fakeAsync 确定性推进)。
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
cancel(
String key) → void - 清除该 key 的全部限制状态(待执行防抖 + 防连点冷却),立即可再触发。 对不存在的 key 无害。
-
canRun(
String key, {Duration duration = const Duration(milliseconds: 1000)}) → bool - 防连点门(throttle,前沿触发)——询问即锁窗。
-
clear(
) → void - 清空全部状态(取消所有待执行 Timer);登出 / 测试隔离用。 对空状态无害。
-
debounce(
String key, void callback(), {Duration duration = const Duration(milliseconds: 500)}) → void - 输入防抖(debounce,尾沿触发)。
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited