Files
tk-electron-ai/js/burst-broadcast.js
2025-10-28 19:40:13 +08:00

25 lines
758 B
JavaScript

// burst-broadcast.js (CommonJS)
function createBurstBroadcaster(broadcast, {
event = 'message', // 要广播的事件名
idleMs = 10_000, // 静默阈值
startPayload = 'start', // 静默后先发的内容
startOnFirst = true, // 首条是否也先发 start
} = {}) {
let lastTs = 0;
return function burstSend(data) {
const now = Date.now();
const idleTooLong = (now - lastTs) >= idleMs;
const isFirst = lastTs === 0;
if ((startOnFirst && isFirst) || idleTooLong) {
broadcast(event, startPayload); // 先发“start”
}
broadcast(event, data); // 再发真实数据
lastTs = now;
};
}
module.exports = { createBurstBroadcaster };