@base44-preview/sdk 0.8.6-pr.57.aa9ac21 → 0.8.6-pr.57.faeb046

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.
@@ -1,8 +1,12 @@
1
- import { getSharedInstance, getSharedInstanceRefCount, } from "../utils/singleton";
1
+ import { getSharedInstance, } from "../utils/singleton";
2
+ ///////////////////////////////////////////////
3
+ //// shared queue for analytics events ////
4
+ ///////////////////////////////////////////////
2
5
  const ANALYTICS_SHARED_STATE_NAME = "analytics";
3
6
  // shared state//
4
7
  const analyticsSharedState = getSharedInstance(ANALYTICS_SHARED_STATE_NAME, () => ({
5
8
  requestsQueue: [],
9
+ isProcessing: false,
6
10
  }));
7
11
  export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthModule, }) => {
8
12
  var _a;
@@ -47,15 +51,24 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
47
51
  await batchRequestFallback(events);
48
52
  }
49
53
  };
50
- if (typeof window !== "undefined") {
54
+ if (typeof window !== "undefined" && enabled) {
51
55
  window.addEventListener("visibilitychange", () => {
52
- // flush entire queue on visibility change and hope for the best //
53
- const eventsData = analyticsSharedState.requestsQueue.splice(0);
54
- flush(eventsData);
56
+ if (document.visibilityState === "hidden") {
57
+ analyticsSharedState.isProcessing = false;
58
+ // flush entire queue on visibility change and hope for the best //
59
+ const eventsData = analyticsSharedState.requestsQueue.splice(0);
60
+ flush(eventsData);
61
+ }
62
+ else if (document.visibilityState === "visible") {
63
+ startAnalyticsProcessor(flush, {
64
+ throttleTime,
65
+ batchSize,
66
+ });
67
+ }
55
68
  });
56
69
  }
57
70
  // start analytics processor only if it's the first instance and analytics is enabled //
58
- if (getSharedInstanceRefCount(ANALYTICS_SHARED_STATE_NAME) <= 1 && enabled) {
71
+ if (enabled) {
59
72
  startAnalyticsProcessor(flush, {
60
73
  throttleTime,
61
74
  batchSize,
@@ -66,9 +79,12 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
66
79
  };
67
80
  };
68
81
  async function startAnalyticsProcessor(handleTrack, options) {
82
+ if (analyticsSharedState.isProcessing) {
83
+ return;
84
+ }
85
+ analyticsSharedState.isProcessing = true;
69
86
  const { throttleTime = 1000, batchSize = 30 } = options !== null && options !== void 0 ? options : {};
70
- while (true) {
71
- await new Promise((resolve) => setTimeout(resolve, throttleTime));
87
+ while (analyticsSharedState.isProcessing) {
72
88
  const requests = analyticsSharedState.requestsQueue.splice(0, batchSize);
73
89
  if (requests.length > 0) {
74
90
  try {
@@ -79,12 +95,13 @@ async function startAnalyticsProcessor(handleTrack, options) {
79
95
  console.error("Error processing analytics request:", error);
80
96
  }
81
97
  }
98
+ await new Promise((resolve) => setTimeout(resolve, throttleTime));
82
99
  }
83
100
  }
84
101
  function getEventIntrinsicData() {
85
102
  return {
86
103
  timestamp: new Date().toISOString(),
87
- pageUrl: typeof window !== "undefined" ? window.location.href : null,
104
+ pageUrl: typeof window !== "undefined" ? window.location.pathname : null,
88
105
  };
89
106
  }
90
107
  function transformEventDataToApiRequestData(sessionContext) {
@@ -5,8 +5,9 @@ export function getSharedInstance(name, factory) {
5
5
  windowObj.base44 = {};
6
6
  }
7
7
  if (!windowObj.base44[name]) {
8
- windowObj.base44[name] = { instance: factory(), _refCount: 1 };
8
+ windowObj.base44[name] = { instance: factory(), _refCount: 0 };
9
9
  }
10
+ windowObj.base44[name]._refCount++;
10
11
  return windowObj.base44[name].instance;
11
12
  }
12
13
  export function getSharedInstanceRefCount(name) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.6-pr.57.aa9ac21",
3
+ "version": "0.8.6-pr.57.faeb046",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",