sobey-monitor-sdk 1.1.10 → 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.umd.js CHANGED
@@ -250,6 +250,12 @@
250
250
  constructor() {
251
251
  this.buffer = [];
252
252
  this.timer = null;
253
+ /** 上次发送时间戳 */
254
+ this.lastSendTime = 0;
255
+ /** 发送节流间隔(毫秒) */
256
+ this.sendThrottleMs = 500;
257
+ /** 是否有待发送的数据(节流期间累积) */
258
+ this.pendingSend = false;
253
259
  }
254
260
  /**
255
261
  * 发送单条数据
@@ -267,14 +273,29 @@
267
273
  this.scheduleFlush();
268
274
  }
269
275
  /**
270
- * 立即发送所有缓冲数据
276
+ * 立即发送所有缓冲数据(带节流)
277
+ * 短时间内的多次 flush 请求会被合并为一次发送
271
278
  */
272
279
  flush() {
273
280
  if (this.buffer.length === 0)
274
281
  return;
282
+ const now = Date.now();
283
+ const elapsed = now - this.lastSendTime;
284
+ // 节流:如果距离上次发送时间太短,延迟发送
285
+ if (elapsed < this.sendThrottleMs) {
286
+ if (!this.pendingSend) {
287
+ this.pendingSend = true;
288
+ setTimeout(() => {
289
+ this.pendingSend = false;
290
+ this.flush();
291
+ }, this.sendThrottleMs - elapsed);
292
+ }
293
+ return;
294
+ }
275
295
  const data = [...this.buffer];
276
296
  this.buffer = [];
277
297
  this.clearTimer();
298
+ this.lastSendTime = now;
278
299
  this.doSend(data);
279
300
  }
280
301
  /**
@@ -420,10 +441,12 @@
420
441
  }
421
442
  /**
422
443
  * 发送早期缓存的数据(根据最新配置过滤)
444
+ * 所有数据会先累积到 sender 的缓冲区,最后统一发送一次
423
445
  */
424
446
  flushEarlyBuffer() {
425
447
  const cfg = config.get();
426
448
  let skippedCount = 0;
449
+ let processedCount = 0;
427
450
  // 全局开关检查:如果 enabled 为 false,清空缓存不上报
428
451
  if (cfg.enabled === false) {
429
452
  const totalCount = this.earlyBuffer.length;
@@ -464,6 +487,7 @@
464
487
  continue;
465
488
  }
466
489
  this.reportError(item.data);
490
+ processedCount++;
467
491
  break;
468
492
  case 'performance':
469
493
  // 检查性能监控是否启用
@@ -472,6 +496,7 @@
472
496
  continue;
473
497
  }
474
498
  this.reportPerformance(item.data);
499
+ processedCount++;
475
500
  break;
476
501
  case 'behavior':
477
502
  // 检查行为监控是否启用
@@ -490,9 +515,17 @@
490
515
  continue;
491
516
  }
492
517
  this.reportBehavior(item.data);
518
+ processedCount++;
493
519
  break;
494
520
  }
495
521
  }
522
+ // 所有早期数据已添加到 sender 缓冲区,立即统一发送
523
+ if (processedCount > 0) {
524
+ sender.flush();
525
+ if (cfg.debug) {
526
+ console.log(`[Monitor] Flushed ${processedCount} early buffered items in single batch`);
527
+ }
528
+ }
496
529
  if (skippedCount > 0 && cfg.debug) {
497
530
  console.log(`[Monitor] Filtered out ${skippedCount} early buffered items based on config`);
498
531
  }