sobey-monitor-sdk 1.1.11 → 1.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -248,6 +248,12 @@ class Sender {
248
248
  constructor() {
249
249
  this.buffer = [];
250
250
  this.timer = null;
251
+ /** 上次发送时间戳 */
252
+ this.lastSendTime = 0;
253
+ /** 发送节流间隔(毫秒) */
254
+ this.sendThrottleMs = 500;
255
+ /** 是否有待发送的数据(节流期间累积) */
256
+ this.pendingSend = false;
251
257
  }
252
258
  /**
253
259
  * 发送单条数据
@@ -265,14 +271,29 @@ class Sender {
265
271
  this.scheduleFlush();
266
272
  }
267
273
  /**
268
- * 立即发送所有缓冲数据
274
+ * 立即发送所有缓冲数据(带节流)
275
+ * 短时间内的多次 flush 请求会被合并为一次发送
269
276
  */
270
277
  flush() {
271
278
  if (this.buffer.length === 0)
272
279
  return;
280
+ const now = Date.now();
281
+ const elapsed = now - this.lastSendTime;
282
+ // 节流:如果距离上次发送时间太短,延迟发送
283
+ if (elapsed < this.sendThrottleMs) {
284
+ if (!this.pendingSend) {
285
+ this.pendingSend = true;
286
+ setTimeout(() => {
287
+ this.pendingSend = false;
288
+ this.flush();
289
+ }, this.sendThrottleMs - elapsed);
290
+ }
291
+ return;
292
+ }
273
293
  const data = [...this.buffer];
274
294
  this.buffer = [];
275
295
  this.clearTimer();
296
+ this.lastSendTime = now;
276
297
  this.doSend(data);
277
298
  }
278
299
  /**