@tamsensedev/dataclient 0.1.0 → 0.1.2

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.
@@ -13093,8 +13093,8 @@ var dataclient = (() => {
13093
13093
  }
13094
13094
 
13095
13095
  // src/utils/sender.ts
13096
- var STORAGE_KEY = "sc2_pending";
13097
13096
  var MAX_RETRIES = 2;
13097
+ var BEACON_MAX_SIZE = 6e4;
13098
13098
  var Sender = class {
13099
13099
  constructor(endpoint, apiKey, batchSize, sessionId, deviceId, flushInterval) {
13100
13100
  this.endpoint = endpoint;
@@ -13102,13 +13102,7 @@ var dataclient = (() => {
13102
13102
  this.batchSize = batchSize;
13103
13103
  this.sessionId = sessionId;
13104
13104
  this.deviceId = deviceId;
13105
- this.restoreFromStorage();
13106
13105
  this.timer = setInterval(() => this.flush(), flushInterval);
13107
- document.addEventListener("visibilitychange", () => {
13108
- if (document.visibilityState === "hidden") {
13109
- this.flush();
13110
- }
13111
- });
13112
13106
  }
13113
13107
  endpoint;
13114
13108
  apiKey;
@@ -13117,58 +13111,62 @@ var dataclient = (() => {
13117
13111
  deviceId;
13118
13112
  queue = [];
13119
13113
  timer = null;
13120
- isFlushing = false;
13121
- restoreFromStorage() {
13122
- try {
13123
- localStorage.removeItem(STORAGE_KEY);
13124
- } catch {
13114
+ flushPromise = Promise.resolve();
13115
+ add(event) {
13116
+ this.queue.push(event);
13117
+ const isRrwebSnapshot = event.event === "rrweb" && event.rrwebEvent?.type === 2;
13118
+ if (isRrwebSnapshot || this.queue.length >= this.batchSize) {
13119
+ this.flush();
13125
13120
  }
13126
13121
  }
13127
- saveToStorage() {
13128
- if (this.queue.length === 0) {
13129
- return;
13122
+ flush() {
13123
+ this.flushPromise = this.flushPromise.then(() => this.doFlush());
13124
+ }
13125
+ flushSync() {
13126
+ if (this.queue.length === 0) return;
13127
+ const events = this.queue.splice(0);
13128
+ const url = this.buildUrl();
13129
+ let chunk = [];
13130
+ let chunkSize = 0;
13131
+ for (const event of events) {
13132
+ const eventJson = JSON.stringify(event);
13133
+ if (chunkSize + eventJson.length > BEACON_MAX_SIZE && chunk.length > 0) {
13134
+ this.sendBeacon(chunk, url);
13135
+ chunk = [];
13136
+ chunkSize = 0;
13137
+ }
13138
+ chunk.push(event);
13139
+ chunkSize += eventJson.length;
13130
13140
  }
13131
- try {
13132
- localStorage.setItem(STORAGE_KEY, JSON.stringify(this.queue));
13133
- } catch {
13141
+ if (chunk.length > 0) {
13142
+ this.sendBeacon(chunk, url);
13134
13143
  }
13135
13144
  }
13136
- add(event) {
13137
- this.queue.push(event);
13138
- if (this.queue.length >= this.batchSize) {
13139
- this.flush();
13145
+ destroy() {
13146
+ if (this.timer) {
13147
+ clearInterval(this.timer);
13140
13148
  }
13149
+ this.flushSync();
13141
13150
  }
13142
- async flush() {
13143
- if (this.queue.length === 0 || this.isFlushing) {
13144
- return;
13145
- }
13146
- this.isFlushing = true;
13151
+ async doFlush() {
13152
+ if (this.queue.length === 0) return;
13147
13153
  const events = this.queue.splice(0);
13148
13154
  const batch = this.buildBatch(events);
13149
13155
  const json = JSON.stringify(batch);
13150
- const url = `${this.endpoint}?key=${encodeURIComponent(this.apiKey)}`;
13156
+ const url = this.buildUrl();
13151
13157
  const success = await this.send(json, url);
13152
13158
  if (!success) {
13153
13159
  this.queue.unshift(...events);
13154
- this.saveToStorage();
13155
13160
  }
13156
- this.isFlushing = false;
13157
13161
  }
13158
- flushSync() {
13159
- if (this.queue.length === 0) {
13160
- return;
13161
- }
13162
- const events = this.queue.splice(0);
13162
+ sendBeacon(events, url) {
13163
13163
  const batch = this.buildBatch(events);
13164
13164
  const json = JSON.stringify(batch);
13165
- const url = `${this.endpoint}?key=${encodeURIComponent(this.apiKey)}`;
13166
13165
  const blob2 = new Blob([json], { type: "application/json" });
13167
- const sent = navigator.sendBeacon(url, blob2);
13168
- if (!sent) {
13169
- this.queue.unshift(...events);
13170
- this.saveToStorage();
13171
- }
13166
+ navigator.sendBeacon(url, blob2);
13167
+ }
13168
+ buildUrl() {
13169
+ return `${this.endpoint}?key=${encodeURIComponent(this.apiKey)}`;
13172
13170
  }
13173
13171
  buildBatch(events) {
13174
13172
  return {
@@ -13192,12 +13190,9 @@ var dataclient = (() => {
13192
13190
  const response = await fetch(url, {
13193
13191
  method: "POST",
13194
13192
  headers: { "Content-Type": "application/json" },
13195
- body: json,
13196
- keepalive: true
13193
+ body: json
13197
13194
  });
13198
- if (response.ok) {
13199
- return true;
13200
- }
13195
+ if (response.ok) return true;
13201
13196
  } catch {
13202
13197
  }
13203
13198
  if (attempt < MAX_RETRIES) {
@@ -13206,20 +13201,14 @@ var dataclient = (() => {
13206
13201
  }
13207
13202
  return false;
13208
13203
  }
13209
- destroy() {
13210
- if (this.timer) {
13211
- clearInterval(this.timer);
13212
- }
13213
- this.flushSync();
13214
- }
13215
13204
  };
13216
13205
 
13217
13206
  // src/index.ts
13218
13207
  var defaults = {
13219
13208
  endpoint: "https://my.tamsense.com/api/scenes2",
13220
13209
  debug: false,
13221
- batchSize: 10,
13222
- flushInterval: 1e4,
13210
+ batchSize: 5,
13211
+ flushInterval: 5e3,
13223
13212
  checkpointInterval: 3e4,
13224
13213
  mutationDebounce: 200,
13225
13214
  inputDebounce: 1e3,
@@ -13249,13 +13238,14 @@ var dataclient = (() => {
13249
13238
  const rrwebTracker = new RrwebTracker(this.config, this.sender);
13250
13239
  this.trackers = [snapshotTracker, mutationTracker, actionTracker, rrwebTracker];
13251
13240
  this.trackers.forEach((t) => t.start());
13252
- const onUnload = () => {
13241
+ const onLeave = () => {
13253
13242
  this.trackers.forEach((t) => t.beforeUnload?.());
13254
13243
  this.sender.flushSync();
13255
13244
  };
13256
- window.addEventListener("beforeunload", onUnload);
13257
- window.addEventListener("pagehide", onUnload);
13258
- console.log(`[dataclient] Initialized. Session: ${sessionId}, Device: ${deviceId}`);
13245
+ document.addEventListener("visibilitychange", () => {
13246
+ if (document.visibilityState === "hidden") onLeave();
13247
+ });
13248
+ window.addEventListener("pagehide", onLeave);
13259
13249
  }
13260
13250
  setUser(userId) {
13261
13251
  this.sender.add({ event: "identify", timestamp: (/* @__PURE__ */ new Date()).toISOString(), user_id: userId });