@eventra_dev/eventra-sdk 2.0.13 → 2.0.14

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.mjs CHANGED
@@ -167,7 +167,7 @@ var Diagnostics = class {
167
167
  // src/client.ts
168
168
  var SDK_NAME = "@eventra_dev/eventra-sdk";
169
169
  var DEFAULT_ENDPOINT = "https://api.eventra.dev/api/v1/ingest/batch";
170
- var SDK_VERSION = "2.0.13";
170
+ var SDK_VERSION = "2.0.14";
171
171
  var LEADER_KEY = "__eventra_leader__";
172
172
  var LEADER_TTL_MS = 4e3;
173
173
  var QUEUE_KEY = "__eventra_q__";
@@ -190,6 +190,8 @@ var DEFAULT_SHUTDOWN_TIMEOUT_MS = 1e4;
190
190
  var PERSISTED_QUEUE_MAX_CHARS = 1e6;
191
191
  var KEEPALIVE_MAX_BYTES = 6e4;
192
192
  var DELIVERED_KEYS_MAX = 1e4;
193
+ var LIVE_TAB_GRACE_MS = 15e3;
194
+ var LIVE_TAB_KEYS_MAX = 1e4;
193
195
  var activeInstances = 0;
194
196
  function detectRuntime() {
195
197
  const g = globalThis;
@@ -454,6 +456,12 @@ var RetryableDeliveryError = class extends Error {
454
456
  this.name = "RetryableDeliveryError";
455
457
  }
456
458
  };
459
+ var DeliveryStopped = class extends Error {
460
+ constructor() {
461
+ super("delivery stopped by destroy()");
462
+ this.name = "DeliveryStopped";
463
+ }
464
+ };
457
465
  var Storage = class {
458
466
  constructor() {
459
467
  this.enabled = false;
@@ -466,6 +474,10 @@ var Storage = class {
466
474
  } catch {
467
475
  }
468
476
  }
477
+ // localStorage works here (not in Node, a blocked or private-mode browser)
478
+ get isEnabled() {
479
+ return this.enabled;
480
+ }
469
481
  get(key) {
470
482
  if (!this.enabled) return null;
471
483
  try {
@@ -637,6 +649,8 @@ var Eventra = class {
637
649
  constructor(options) {
638
650
  this.queue = [];
639
651
  this.sending = null;
652
+ // events other open tabs announced on the channel, by key, and until when they are theirs to send
653
+ this.liveTabEvents = /* @__PURE__ */ new Map();
640
654
  this.inFlight = false;
641
655
  this.destroyed = false;
642
656
  this.shuttingDown = false;
@@ -925,6 +939,12 @@ var Eventra = class {
925
939
  activeInstances = Math.max(0, activeInstances - 1);
926
940
  if (this.timer) clearInterval(this.timer);
927
941
  this.persistNow();
942
+ const kept = this.runtime === "browser" && this.persistQueue && this.storage.isEnabled;
943
+ if (!kept) {
944
+ const inFlight = new Set(this.sending ?? []);
945
+ const lost = this.queue.filter((event) => !inFlight.has(event)).length;
946
+ if (lost > 0) callSafely(this.options.onEventsDropped, lost);
947
+ }
928
948
  this.leader?.destroy();
929
949
  if (this.channel) {
930
950
  try {
@@ -955,6 +975,10 @@ var Eventra = class {
955
975
  let index = 0;
956
976
  while (index < this.queue.length && events.length < this.maxBatch) {
957
977
  const event = this.queue[index];
978
+ if (this.leftToItsTab(event)) {
979
+ index++;
980
+ continue;
981
+ }
958
982
  let eventJson;
959
983
  try {
960
984
  eventJson = JSON.stringify(event);
@@ -1094,6 +1118,7 @@ var Eventra = class {
1094
1118
  throw err instanceof RetryableDeliveryError ? err : new RetryableDeliveryError("retryable delivery error", void 0, "network");
1095
1119
  }
1096
1120
  await sleep(wait);
1121
+ if (this.destroyed) throw new DeliveryStopped();
1097
1122
  } finally {
1098
1123
  clearTimeout(timeout);
1099
1124
  }
@@ -1181,11 +1206,34 @@ var Eventra = class {
1181
1206
  }
1182
1207
  trimQueue(queue = this.queue) {
1183
1208
  if (queue.length <= this.maxQueue) return queue;
1184
- const dropped = queue.length - this.maxQueue;
1185
- this.recordDiagnostic("queue_full", dropped);
1186
- callSafely(this.options.onEventsDropped, dropped);
1209
+ const removed = queue.slice(0, queue.length - this.maxQueue);
1210
+ const dropped = removed.filter((e) => !this.liveTabEvents.has(e?.idempotencyKey)).length;
1211
+ if (dropped > 0) {
1212
+ this.recordDiagnostic("queue_full", dropped);
1213
+ callSafely(this.options.onEventsDropped, dropped);
1214
+ }
1187
1215
  return queue.slice(-this.maxQueue);
1188
1216
  }
1217
+ // events another open tab just announced on the channel, with the time until which that tab sends them
1218
+ rememberLiveTabEvents(events) {
1219
+ const until = Date.now() + LIVE_TAB_GRACE_MS;
1220
+ for (const event of events) {
1221
+ const key = event?.idempotencyKey;
1222
+ if (typeof key === "string" && !this.liveTabEvents.has(key)) this.liveTabEvents.set(key, until);
1223
+ }
1224
+ for (const key of this.liveTabEvents.keys()) {
1225
+ if (this.liveTabEvents.size <= LIVE_TAB_KEYS_MAX) break;
1226
+ this.liveTabEvents.delete(key);
1227
+ }
1228
+ }
1229
+ // In `independent` mode, an event another open tab announced less than LIVE_TAB_GRACE_MS ago is that
1230
+ // tab's to send. After that (the tab was closed, or it cannot reach the server either) anyone sends it.
1231
+ // The leader in `leader` mode sends everything, so it never waits.
1232
+ leftToItsTab(event) {
1233
+ if (this.leader) return false;
1234
+ const until = this.liveTabEvents.get(event?.idempotencyKey);
1235
+ return until !== void 0 && Date.now() < until;
1236
+ }
1189
1237
  // Sends only what changed (a new event, or the keys just delivered) instead
1190
1238
  // of structured-cloning the whole queue on every call.
1191
1239
  broadcast(message) {
@@ -1208,13 +1256,16 @@ var Eventra = class {
1208
1256
  if (data?.type === "delivered" && Array.isArray(data.keys)) {
1209
1257
  const keys = new Set(data.keys);
1210
1258
  this.rememberDelivered(keys);
1259
+ for (const key of keys) this.liveTabEvents.delete(key);
1211
1260
  this.queue = this.queue.filter((ev) => !keys.has(ev.idempotencyKey));
1212
1261
  return;
1213
1262
  }
1214
1263
  if (data?.type !== "queue-add" && data?.type !== "queue-sync" || !Array.isArray(data.events)) {
1215
1264
  return;
1216
1265
  }
1217
- this.queue = mergeQueues(this.queue, this.withoutDelivered(data.events));
1266
+ const announced = this.withoutDelivered(data.events);
1267
+ this.rememberLiveTabEvents(announced);
1268
+ this.queue = mergeQueues(this.queue, announced);
1218
1269
  this.queue = this.trimQueue();
1219
1270
  };
1220
1271
  }