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