@levo-so/core 0.1.29 → 0.1.30

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.
@@ -6,6 +6,7 @@ export declare class LevoAudience extends LevoPlugin {
6
6
  instance: AnalyticsInstance | null;
7
7
  request<ResponseBody = unknown, RequestBody = unknown>(url: string, data: RequestBody): Promise<Awaited<ResponseBody>>;
8
8
  initiate(properties: ILevoAudience.Properties): Promise<any>;
9
+ identify(): Promise<void>;
9
10
  getReferrer(): {
10
11
  type?: undefined;
11
12
  referrer?: undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"LevoAudience.d.ts","sourceRoot":"","sources":["../../src/plugins/LevoAudience.ts"],"names":[],"mappings":"AAEA,OAAO,EAAa,KAAK,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAG9D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAKvD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,qBAAa,YAAa,SAAQ,UAAU;;IAC1C,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAQ;IAmB1C,OAAO,CAAC,YAAY,GAAG,OAAO,EAAE,WAAW,GAAG,OAAO,EACnD,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,WAAW;IA8Bb,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC,UAAU;IAkInD,WAAW;;;;;;;;;;;IAmEX,qBAAqB;IAqBf,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,CAAC,UAAU;IAkC/D,eAAe;IAaf,WAAW;CAYZ"}
1
+ {"version":3,"file":"LevoAudience.d.ts","sourceRoot":"","sources":["../../src/plugins/LevoAudience.ts"],"names":[],"mappings":"AAEA,OAAO,EAAa,KAAK,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAG9D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAMvD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,qBAAa,YAAa,SAAQ,UAAU;;IAC1C,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAQ;IAmB1C,OAAO,CAAC,YAAY,GAAG,OAAO,EAAE,WAAW,GAAG,OAAO,EACnD,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,WAAW;IA8Bb,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC,UAAU;IAsI7C,QAAQ;IAad,WAAW;;;;;;;;;;;IAmEX,qBAAqB;IAqBf,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,CAAC,UAAU;IAkC/D,eAAe;IAaf,WAAW;CAYZ"}
@@ -206,6 +206,35 @@ var getUserProperties = async () => {
206
206
  };
207
207
  };
208
208
 
209
+ // src/utils/lock.ts
210
+ var LocalStorageLock = class {
211
+ key;
212
+ constructor(lockKey) {
213
+ this.key = `lock_${lockKey}`;
214
+ }
215
+ acquireLock(expiryMs = 5e3) {
216
+ const now = Date.now();
217
+ const lockValue = localStorage.getItem(this.key);
218
+ if (lockValue) {
219
+ const timestamp = Number(lockValue);
220
+ if (now - timestamp < expiryMs) {
221
+ return false;
222
+ }
223
+ }
224
+ localStorage.setItem(this.key, now.toString());
225
+ return true;
226
+ }
227
+ releaseLock() {
228
+ localStorage.removeItem(this.key);
229
+ }
230
+ isLocked(expiryMs = 5e3) {
231
+ const lockValue = localStorage.getItem(this.key);
232
+ if (!lockValue) return false;
233
+ const timestamp = Number(lockValue);
234
+ return Date.now() - timestamp < expiryMs;
235
+ }
236
+ };
237
+
209
238
  // src/plugins/LevoAudience.ts
210
239
  var LevoAudience = class extends LevoPlugin {
211
240
  instance = null;
@@ -317,35 +346,48 @@ var LevoAudience = class extends LevoPlugin {
317
346
  tab_id: this.getCurrentTabId(),
318
347
  tab_count: this.getTabCount()
319
348
  };
320
- this.request(
321
- `${this?.client?.insightsUrl}/v1/insights/event/welcome`,
322
- welcomeInput
323
- ).then((data) => {
324
- this.#is_identified = true;
325
- this.#session = data.content.data.session;
326
- this.#device = data.content.data.device;
327
- navigator.sendBeacon(
328
- `${this?.client?.insightsUrl}/v1/insights/event/bulk?workspace=${this?.client?.workspace}`,
329
- JSON.stringify(this.#batch)
330
- );
331
- }).catch((e) => getLevoError(e));
349
+ const identifyLock = new LocalStorageLock("lv_aud_identify");
350
+ if (identifyLock.acquireLock()) {
351
+ this.request(
352
+ `${this?.client?.insightsUrl}/v1/insights/event/welcome`,
353
+ welcomeInput
354
+ ).then((data) => {
355
+ this.#is_identified = true;
356
+ this.#session = data.content.data.session;
357
+ this.#device = data.content.data.device;
358
+ navigator.sendBeacon(
359
+ `${this?.client?.insightsUrl}/v1/insights/event/bulk?workspace=${this?.client?.workspace}`,
360
+ JSON.stringify(this.#batch)
361
+ );
362
+ identifyLock.releaseLock();
363
+ }).catch((e) => {
364
+ identifyLock.releaseLock();
365
+ return getLevoError(e);
366
+ });
367
+ }
332
368
  }
333
369
  };
334
370
  this.instance = Analytics({
335
371
  app: this.client.workspace || "",
336
372
  plugins: [plugin]
337
373
  });
338
- const referrer = this.getReferrer();
339
- await this.instance.identify("", {
340
- ...await getUserProperties(),
341
- referrer
342
- });
374
+ await this.identify();
343
375
  this.startActivityListener();
344
376
  return this.instance.page({
345
377
  resource: "page",
346
378
  identifier: properties.id
347
379
  });
348
380
  }
381
+ async identify() {
382
+ if (!this.instance) {
383
+ return;
384
+ }
385
+ const referrer = this.getReferrer();
386
+ await this.instance.identify("", {
387
+ ...await getUserProperties(),
388
+ referrer
389
+ });
390
+ }
349
391
  getReferrer() {
350
392
  if (typeof document === "undefined") {
351
393
  return {};
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @example
3
+ *
4
+ * const lock = new LocalStorageLock("myResource");
5
+ * if (lock.acquireLock()) {
6
+ * console.log("Lock acquired");
7
+ * // Do some work...
8
+ * lock.releaseLock();
9
+ * } else {
10
+ * console.log("Lock is already held by another instance");
11
+ * }
12
+ *
13
+ */
14
+ export declare class LocalStorageLock {
15
+ private key;
16
+ constructor(lockKey: string);
17
+ acquireLock(expiryMs?: number): boolean;
18
+ releaseLock(): void;
19
+ isLocked(expiryMs?: number): boolean;
20
+ }
21
+ //# sourceMappingURL=lock.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lock.d.ts","sourceRoot":"","sources":["../../src/utils/lock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,GAAG,CAAS;gBAER,OAAO,EAAE,MAAM;IAI3B,WAAW,CAAC,QAAQ,GAAE,MAAa,GAAG,OAAO;IAe7C,WAAW,IAAI,IAAI;IAInB,QAAQ,CAAC,QAAQ,GAAE,MAAa,GAAG,OAAO;CAO3C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@levo-so/core",
3
- "version": "0.1.29",
3
+ "version": "0.1.30",
4
4
  "author": "Levo Engineering <devs@theinternetfolks.com>",
5
5
  "description": "Levo common utilities",
6
6
  "type": "module",
@@ -24,8 +24,8 @@
24
24
  "devDependencies": {
25
25
  "tsup": "8.2.4",
26
26
  "typescript": "5.5.4",
27
- "@levo/eslint-config": "0.0.0",
28
- "@levo/ts-config": "0.0.0"
27
+ "@levo/ts-config": "0.0.0",
28
+ "@levo/eslint-config": "0.0.0"
29
29
  },
30
30
  "main": "./dist/index.js",
31
31
  "module": "./dist/index.js",