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.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
  /**
@@ -414,10 +435,12 @@ class Reporter {
414
435
  }
415
436
  /**
416
437
  * 发送早期缓存的数据(根据最新配置过滤)
438
+ * 所有数据会先累积到 sender 的缓冲区,最后统一发送一次
417
439
  */
418
440
  flushEarlyBuffer() {
419
441
  const cfg = config.get();
420
442
  let skippedCount = 0;
443
+ let processedCount = 0;
421
444
  // 全局开关检查:如果 enabled 为 false,清空缓存不上报
422
445
  if (cfg.enabled === false) {
423
446
  const totalCount = this.earlyBuffer.length;
@@ -458,6 +481,7 @@ class Reporter {
458
481
  continue;
459
482
  }
460
483
  this.reportError(item.data);
484
+ processedCount++;
461
485
  break;
462
486
  case 'performance':
463
487
  // 检查性能监控是否启用
@@ -466,6 +490,7 @@ class Reporter {
466
490
  continue;
467
491
  }
468
492
  this.reportPerformance(item.data);
493
+ processedCount++;
469
494
  break;
470
495
  case 'behavior':
471
496
  // 检查行为监控是否启用
@@ -484,9 +509,17 @@ class Reporter {
484
509
  continue;
485
510
  }
486
511
  this.reportBehavior(item.data);
512
+ processedCount++;
487
513
  break;
488
514
  }
489
515
  }
516
+ // 所有早期数据已添加到 sender 缓冲区,立即统一发送
517
+ if (processedCount > 0) {
518
+ sender.flush();
519
+ if (cfg.debug) {
520
+ console.log(`[Monitor] Flushed ${processedCount} early buffered items in single batch`);
521
+ }
522
+ }
490
523
  if (skippedCount > 0 && cfg.debug) {
491
524
  console.log(`[Monitor] Filtered out ${skippedCount} early buffered items based on config`);
492
525
  }