稳定测试版
This commit is contained in:
24
js/burst-broadcast.js
Normal file
24
js/burst-broadcast.js
Normal file
@@ -0,0 +1,24 @@
|
||||
// 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 };
|
||||
Reference in New Issue
Block a user