sobey-monitor-sdk 1.1.11 → 1.1.13

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
@@ -180,12 +180,30 @@
180
180
  this.breadcrumbs = [];
181
181
  }
182
182
  /**
183
- * 生成会话 ID
183
+ * 生成或获取会话 ID
184
+ * sessionId 保存在 sessionStorage 中,同一标签页会话期间保持不变
184
185
  */
185
186
  generateSessionId() {
187
+ const storageKey = '__sobey_monitor_session_id__';
188
+ try {
189
+ const existing = sessionStorage.getItem(storageKey);
190
+ if (existing) {
191
+ return existing;
192
+ }
193
+ }
194
+ catch {
195
+ // sessionStorage 不可用,继续生成新的
196
+ }
186
197
  const timestamp = Date.now().toString(36);
187
198
  const randomPart = Math.random().toString(36).substring(2, 10);
188
- return `${timestamp}-${randomPart}`;
199
+ const newSessionId = `${timestamp}-${randomPart}`;
200
+ try {
201
+ sessionStorage.setItem(storageKey, newSessionId);
202
+ }
203
+ catch {
204
+ // 忽略存储错误
205
+ }
206
+ return newSessionId;
189
207
  }
190
208
  /**
191
209
  * 收集设备信息
@@ -250,6 +268,12 @@
250
268
  constructor() {
251
269
  this.buffer = [];
252
270
  this.timer = null;
271
+ /** 上次发送时间戳 */
272
+ this.lastSendTime = 0;
273
+ /** 发送节流间隔(毫秒) */
274
+ this.sendThrottleMs = 500;
275
+ /** 是否有待发送的数据(节流期间累积) */
276
+ this.pendingSend = false;
253
277
  }
254
278
  /**
255
279
  * 发送单条数据
@@ -267,14 +291,29 @@
267
291
  this.scheduleFlush();
268
292
  }
269
293
  /**
270
- * 立即发送所有缓冲数据
294
+ * 立即发送所有缓冲数据(带节流)
295
+ * 短时间内的多次 flush 请求会被合并为一次发送
271
296
  */
272
297
  flush() {
273
298
  if (this.buffer.length === 0)
274
299
  return;
300
+ const now = Date.now();
301
+ const elapsed = now - this.lastSendTime;
302
+ // 节流:如果距离上次发送时间太短,延迟发送
303
+ if (elapsed < this.sendThrottleMs) {
304
+ if (!this.pendingSend) {
305
+ this.pendingSend = true;
306
+ setTimeout(() => {
307
+ this.pendingSend = false;
308
+ this.flush();
309
+ }, this.sendThrottleMs - elapsed);
310
+ }
311
+ return;
312
+ }
275
313
  const data = [...this.buffer];
276
314
  this.buffer = [];
277
315
  this.clearTimer();
316
+ this.lastSendTime = now;
278
317
  this.doSend(data);
279
318
  }
280
319
  /**