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