idle-timer.js
1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* IdleTimer - 通用空闲超时计时器
*
* 用于检测「长时间无活动」场景:
* - ACP/Claude Stream 流式输出的空闲检测
* - 任何需要 touch() 重置的看门狗场景
*
* 使用方式:
* const timer = new IdleTimer(timeoutMs, onTimeout);
* timer.start();
* // 每次有活动时调用 touch()
* timer.touch();
* // 结束时清理
* timer.cancel();
*/
class IdleTimer {
/**
* @param {number} timeoutMs - 空闲超时时间(毫秒)
* @param {Function} onTimeout - 超时回调
* @param {Object} [options]
* @param {string} [options.label] - 标签(用于日志/错误消息)
*/
constructor(timeoutMs, onTimeout, options = {}) {
this.timeoutMs = timeoutMs;
this.onTimeout = onTimeout;
this.label = options.label || 'idle-timer';
this._timer = null;
this._fired = false;
}
/** 启动计时器 */
start() {
this._reset();
return this;
}
/** 重置计时器(收到活动时调用) */
touch() {
if (!this._fired) {
this._reset();
}
}
/** 取消计时器 */
cancel() {
if (this._timer) {
clearTimeout(this._timer);
this._timer = null;
}
}
/** 计时器是否已触发 */
get fired() {
return this._fired;
}
/** @private */
_reset() {
this.cancel();
this._timer = setTimeout(() => {
this._fired = true;
this._timer = null;
this.onTimeout();
}, this.timeoutMs);
}
}
module.exports = { IdleTimer };