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.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
  /**