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.esm.js CHANGED
@@ -174,12 +174,30 @@ class ContextManager {
174
174
  this.breadcrumbs = [];
175
175
  }
176
176
  /**
177
- * 生成会话 ID
177
+ * 生成或获取会话 ID
178
+ * sessionId 保存在 sessionStorage 中,同一标签页会话期间保持不变
178
179
  */
179
180
  generateSessionId() {
181
+ const storageKey = '__sobey_monitor_session_id__';
182
+ try {
183
+ const existing = sessionStorage.getItem(storageKey);
184
+ if (existing) {
185
+ return existing;
186
+ }
187
+ }
188
+ catch {
189
+ // sessionStorage 不可用,继续生成新的
190
+ }
180
191
  const timestamp = Date.now().toString(36);
181
192
  const randomPart = Math.random().toString(36).substring(2, 10);
182
- return `${timestamp}-${randomPart}`;
193
+ const newSessionId = `${timestamp}-${randomPart}`;
194
+ try {
195
+ sessionStorage.setItem(storageKey, newSessionId);
196
+ }
197
+ catch {
198
+ // 忽略存储错误
199
+ }
200
+ return newSessionId;
183
201
  }
184
202
  /**
185
203
  * 收集设备信息
@@ -244,6 +262,12 @@ class Sender {
244
262
  constructor() {
245
263
  this.buffer = [];
246
264
  this.timer = null;
265
+ /** 上次发送时间戳 */
266
+ this.lastSendTime = 0;
267
+ /** 发送节流间隔(毫秒) */
268
+ this.sendThrottleMs = 500;
269
+ /** 是否有待发送的数据(节流期间累积) */
270
+ this.pendingSend = false;
247
271
  }
248
272
  /**
249
273
  * 发送单条数据
@@ -261,14 +285,29 @@ class Sender {
261
285
  this.scheduleFlush();
262
286
  }
263
287
  /**
264
- * 立即发送所有缓冲数据
288
+ * 立即发送所有缓冲数据(带节流)
289
+ * 短时间内的多次 flush 请求会被合并为一次发送
265
290
  */
266
291
  flush() {
267
292
  if (this.buffer.length === 0)
268
293
  return;
294
+ const now = Date.now();
295
+ const elapsed = now - this.lastSendTime;
296
+ // 节流:如果距离上次发送时间太短,延迟发送
297
+ if (elapsed < this.sendThrottleMs) {
298
+ if (!this.pendingSend) {
299
+ this.pendingSend = true;
300
+ setTimeout(() => {
301
+ this.pendingSend = false;
302
+ this.flush();
303
+ }, this.sendThrottleMs - elapsed);
304
+ }
305
+ return;
306
+ }
269
307
  const data = [...this.buffer];
270
308
  this.buffer = [];
271
309
  this.clearTimer();
310
+ this.lastSendTime = now;
272
311
  this.doSend(data);
273
312
  }
274
313
  /**