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.esm.js CHANGED
@@ -244,6 +244,12 @@ class Sender {
244
244
  constructor() {
245
245
  this.buffer = [];
246
246
  this.timer = null;
247
+ /** 上次发送时间戳 */
248
+ this.lastSendTime = 0;
249
+ /** 发送节流间隔(毫秒) */
250
+ this.sendThrottleMs = 500;
251
+ /** 是否有待发送的数据(节流期间累积) */
252
+ this.pendingSend = false;
247
253
  }
248
254
  /**
249
255
  * 发送单条数据
@@ -261,14 +267,29 @@ class Sender {
261
267
  this.scheduleFlush();
262
268
  }
263
269
  /**
264
- * 立即发送所有缓冲数据
270
+ * 立即发送所有缓冲数据(带节流)
271
+ * 短时间内的多次 flush 请求会被合并为一次发送
265
272
  */
266
273
  flush() {
267
274
  if (this.buffer.length === 0)
268
275
  return;
276
+ const now = Date.now();
277
+ const elapsed = now - this.lastSendTime;
278
+ // 节流:如果距离上次发送时间太短,延迟发送
279
+ if (elapsed < this.sendThrottleMs) {
280
+ if (!this.pendingSend) {
281
+ this.pendingSend = true;
282
+ setTimeout(() => {
283
+ this.pendingSend = false;
284
+ this.flush();
285
+ }, this.sendThrottleMs - elapsed);
286
+ }
287
+ return;
288
+ }
269
289
  const data = [...this.buffer];
270
290
  this.buffer = [];
271
291
  this.clearTimer();
292
+ this.lastSendTime = now;
272
293
  this.doSend(data);
273
294
  }
274
295
  /**