Skip to content

Commit 5910178

Browse files
committed
refactor
1 parent 018f938 commit 5910178

2 files changed

Lines changed: 52 additions & 31 deletions

File tree

src/dispatcher.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,35 @@ export class EventDispatcher {
4949
}
5050

5151
private async _sendEvents(events: Event[]): Promise<void> {
52-
const res = await fetch(this.apiUrl, {
53-
method: "POST",
54-
headers: {
55-
"Content-Type": "application/json",
56-
"App-Key": this.appKey,
57-
},
58-
credentials: "omit",
59-
body: JSON.stringify(events),
60-
});
52+
try {
53+
const res = await fetch(this.apiUrl, {
54+
method: "POST",
55+
headers: {
56+
"Content-Type": "application/json",
57+
"App-Key": this.appKey,
58+
},
59+
credentials: "omit",
60+
body: JSON.stringify(events),
61+
});
6162

62-
if (res.status >= 300) {
63-
const reason = await res.text();
64-
console.warn(
65-
`Aptabase: Failed to send ${events.length} events. Reason: ${res.status} ${reason}`
66-
);
67-
}
63+
if (res.status < 300) {
64+
return Promise.resolve();
65+
}
6866

69-
if (res.status >= 500) {
70-
throw new Error("Failed to send events");
71-
}
67+
const reason = `${res.status} ${await res.text()}`;
68+
if (res.status < 500) {
69+
console.warn(
70+
`Aptabase: Failed to send ${events.length} events because of ${reason}. Will not retry.`
71+
);
72+
return Promise.resolve();
73+
}
7274

73-
return Promise.resolve();
75+
throw new Error(reason);
76+
} catch (e) {
77+
console.error(
78+
`Aptabase: Failed to send ${events.length} events. Reason: ${e}`
79+
);
80+
throw e;
81+
}
7482
}
7583
}

src/index.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { newSessionId } from "./session";
22
import { EnvironmentInfo, getEnvironmentInfo } from "./env";
3-
import { Platform } from "react-native";
3+
import { AppState, Platform } from "react-native";
44
import { EventDispatcher } from "./dispatcher";
55

66
/**
@@ -15,14 +15,14 @@ export type AptabaseOptions = {
1515
appVersion?: string;
1616

1717
// Override the default flush interval (in milliseconds)
18-
flushInterval?: string;
18+
flushInterval?: number;
1919
};
2020

2121
// Session expires after 1 hour of inactivity
22-
const SESSION_TIMEOUT = 1 * 60 * 60;
22+
const SESSION_TIMEOUT = 3600;
2323

24-
const RELEASE_FLUSH_INTERVAL = 60 * 1000;
25-
const DEBUG_FLUSH_INTERVAL = 2 * 1000;
24+
// Flush events every 60 seconds in production, or 2 seconds in development
25+
const FLUSH_INTERVAL = __DEV__ ? 2000 : 60000;
2626

2727
let _sessionId = newSessionId();
2828
let _lastTouched = new Date();
@@ -34,7 +34,7 @@ let _dispatcher: EventDispatcher | undefined;
3434
const _hosts: { [region: string]: string } = {
3535
US: "https://us.aptabase.com",
3636
EU: "https://eu.aptabase.com",
37-
DEV: "http://localhost:3000",
37+
DEV: "http://192.168.0.248:3000",
3838
SH: "",
3939
};
4040

@@ -55,6 +55,23 @@ function getBaseUrl(
5555
return _hosts[region];
5656
}
5757

58+
function startPolling(flushInterval: number) {
59+
const flush = () => _dispatcher?.flush();
60+
61+
let interval = setInterval(flush, flushInterval);
62+
63+
if (AppState.isAvailable) {
64+
AppState.addEventListener("change", (next) => {
65+
clearInterval(interval);
66+
if (next === "background") {
67+
flush();
68+
} else if (next === "active") {
69+
interval = setInterval(flush, flushInterval);
70+
}
71+
});
72+
}
73+
}
74+
5875
/**
5976
* Initializes the SDK with given App Key
6077
* @param {string} appKey - Aptabase App Key
@@ -88,12 +105,8 @@ export function init(appKey: string, options?: AptabaseOptions) {
88105

89106
_dispatcher = new EventDispatcher(_apiUrl, _appKey);
90107

91-
const flushInterval =
92-
options?.flushInterval ?? _env.isDebug
93-
? DEBUG_FLUSH_INTERVAL
94-
: RELEASE_FLUSH_INTERVAL;
95-
96-
setInterval(_dispatcher.flush.bind(_dispatcher), flushInterval);
108+
const flushInterval = options?.flushInterval ?? FLUSH_INTERVAL;
109+
startPolling(flushInterval);
97110
}
98111

99112
/**

0 commit comments

Comments
 (0)