Skip to content

Commit fc48301

Browse files
committed
bot comments
1 parent 05bee93 commit fc48301

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

src/modules/analytics.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,31 @@ export const createAnalyticsModule = ({
9999
}
100100
};
101101

102-
if (typeof window !== "undefined" && enabled) {
103-
window.addEventListener("visibilitychange", () => {
104-
if (document.visibilityState === "hidden") {
105-
analyticsSharedState.isProcessing = false;
106-
// flush entire queue on visibility change and hope for the best //
107-
const eventsData = analyticsSharedState.requestsQueue.splice(0);
108-
flush(eventsData);
109-
} else if (document.visibilityState === "visible") {
110-
startAnalyticsProcessor(flush, {
111-
throttleTime,
112-
batchSize,
113-
});
114-
}
102+
const onDocHidden = () => {
103+
analyticsSharedState.isProcessing = false;
104+
// flush entire queue on visibility change and hope for the best //
105+
const eventsData = analyticsSharedState.requestsQueue.splice(0);
106+
flush(eventsData);
107+
};
108+
109+
const onDocVisible = () => {
110+
startAnalyticsProcessor(flush, {
111+
throttleTime,
112+
batchSize,
115113
});
114+
};
115+
116+
const onVisibilityChange = () => {
117+
if (typeof window === "undefined") return;
118+
if (document.visibilityState === "hidden") {
119+
onDocHidden();
120+
} else if (document.visibilityState === "visible") {
121+
onDocVisible();
122+
}
123+
};
124+
125+
if (typeof window !== "undefined" && enabled) {
126+
window.addEventListener("visibilitychange", onVisibilityChange);
116127
}
117128

118129
// start analytics processor only if it's the first instance and analytics is enabled //
@@ -174,6 +185,7 @@ function transformEventDataToApiRequestData(sessionContext: SessionContext) {
174185
export function getAnalyticsModuleOptionsFromUrlParams():
175186
| AnalyticsModuleOptions
176187
| undefined {
188+
if (typeof window === "undefined") return undefined;
177189
const urlParams = new URLSearchParams(window.location.search);
178190
const jsonString = urlParams.get("analytics");
179191
if (!jsonString) return undefined;

src/utils/singleton.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
2-
1+
const windowObj: {
2+
base44SharedInstances?: {
3+
[key: string]: { instance: any; _refCount: number };
4+
};
5+
} =
6+
typeof window !== "undefined"
7+
? (window as any)
8+
: { base44SharedInstances: {} };
39

410
// Singleton (shared between sdk instances)//
511
export function getSharedInstance<T>(name: string, factory: () => T): T {
6-
const windowObj: Window & {
7-
base44?: { [key: string]: { instance: T; _refCount: number } };
8-
} = typeof window !== "undefined" ? (window as any) : { base44: {} };
9-
10-
if (!windowObj.base44) {
11-
windowObj.base44 = {};
12+
if (!windowObj.base44SharedInstances) {
13+
windowObj.base44SharedInstances = {};
1214
}
13-
if (!windowObj.base44[name]) {
14-
windowObj.base44[name] = { instance: factory(), _refCount: 0 };
15+
if (!windowObj.base44SharedInstances[name]) {
16+
windowObj.base44SharedInstances[name] = {
17+
instance: factory(),
18+
_refCount: 0,
19+
};
1520
}
16-
windowObj.base44[name]._refCount++;
17-
return windowObj.base44[name].instance;
21+
windowObj.base44SharedInstances[name]._refCount++;
22+
return windowObj.base44SharedInstances[name].instance;
1823
}
1924

2025
export function getSharedInstanceRefCount<T>(name: string): number {
21-
const windowObj: Window & {
22-
base44?: { [key: string]: { instance: T; _refCount: number } };
23-
} = typeof window !== "undefined" ? (window as any) : { base44: {} };
24-
25-
return windowObj.base44?.[name]?._refCount ?? 0;
26+
return windowObj.base44SharedInstances?.[name]?._refCount ?? 0;
2627
}

0 commit comments

Comments
 (0)