@base44-preview/sdk 0.8.6-pr.57.0298afb → 0.8.6-pr.57.346f67a

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,4 +1,10 @@
1
- import { getSharedInstance, } from "../utils/singleton";
1
+ import { getSharedInstance } from "../utils/sharedInstance";
2
+ const defaultConfiguration = {
3
+ enabled: true,
4
+ maxQueueSize: 1000,
5
+ throttleTime: 1000,
6
+ batchSize: 30,
7
+ };
2
8
  ///////////////////////////////////////////////
3
9
  //// shared queue for analytics events ////
4
10
  ///////////////////////////////////////////////
@@ -7,21 +13,24 @@ const ANALYTICS_SHARED_STATE_NAME = "analytics";
7
13
  const analyticsSharedState = getSharedInstance(ANALYTICS_SHARED_STATE_NAME, () => ({
8
14
  requestsQueue: [],
9
15
  isProcessing: false,
16
+ sessionContext: null,
17
+ config: {
18
+ ...defaultConfiguration,
19
+ ...getAnalyticsModuleOptionsFromUrlParams(),
20
+ },
10
21
  }));
11
22
  export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthModule, }) => {
12
- var _a;
13
23
  // prevent overflow of events //
14
- const { enabled = true, maxQueueSize = 1000, throttleTime = 1000, batchSize = 30, } = (_a = getAnalyticsModuleOptionsFromUrlParams()) !== null && _a !== void 0 ? _a : {};
24
+ const { enabled, maxQueueSize, throttleTime, batchSize } = analyticsSharedState.config;
15
25
  const trackBatchUrl = `${serverUrl}/api/apps/${appId}/analytics/track/batch`;
16
- let sessionContext = null;
17
26
  const getSessionContext = async () => {
18
- if (sessionContext)
19
- return sessionContext;
20
- const user = await userAuthModule.me();
21
- sessionContext = {
22
- user_id: user.id,
23
- };
24
- return sessionContext;
27
+ if (!analyticsSharedState.sessionContext) {
28
+ const user = await userAuthModule.me();
29
+ analyticsSharedState.sessionContext = {
30
+ user_id: user.id,
31
+ };
32
+ }
33
+ return analyticsSharedState.sessionContext;
25
34
  };
26
35
  const track = (params) => {
27
36
  if (!enabled || analyticsSharedState.requestsQueue.length >= maxQueueSize) {
@@ -41,7 +50,7 @@ export const createAnalyticsModule = ({ axiosClient, serverUrl, appId, userAuthM
41
50
  });
42
51
  };
43
52
  const flush = async (eventsData) => {
44
- const sessionContext_ = sessionContext !== null && sessionContext !== void 0 ? sessionContext : (await getSessionContext());
53
+ const sessionContext_ = await getSessionContext();
45
54
  const events = eventsData.map(transformEventDataToApiRequestData(sessionContext_));
46
55
  const beaconPayload = JSON.stringify({ events });
47
56
  if (typeof navigator === "undefined" ||
@@ -35,3 +35,6 @@ export type AnalyticsModuleOptions = {
35
35
  throttleTime?: number;
36
36
  batchSize?: number;
37
37
  };
38
+ export type AnalyticsModule = {
39
+ track: (params: TrackEventParams) => void;
40
+ };
@@ -1,2 +1 @@
1
1
  export declare function getSharedInstance<T>(name: string, factory: () => T): T;
2
- export declare function getSharedInstanceRefCount<T>(name: string): number;
@@ -9,13 +9,7 @@ export function getSharedInstance(name, factory) {
9
9
  if (!windowObj.base44SharedInstances[name]) {
10
10
  windowObj.base44SharedInstances[name] = {
11
11
  instance: factory(),
12
- _refCount: 0,
13
12
  };
14
13
  }
15
- windowObj.base44SharedInstances[name]._refCount++;
16
14
  return windowObj.base44SharedInstances[name].instance;
17
15
  }
18
- export function getSharedInstanceRefCount(name) {
19
- var _a, _b, _c;
20
- return (_c = (_b = (_a = windowObj.base44SharedInstances) === null || _a === void 0 ? void 0 : _a[name]) === null || _b === void 0 ? void 0 : _b._refCount) !== null && _c !== void 0 ? _c : 0;
21
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.6-pr.57.0298afb",
3
+ "version": "0.8.6-pr.57.346f67a",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",