Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
goenning committed Aug 18, 2023
1 parent 018f938 commit 5910178
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
46 changes: 27 additions & 19 deletions src/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,35 @@ export class EventDispatcher {
}

private async _sendEvents(events: Event[]): Promise<void> {
const res = await fetch(this.apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"App-Key": this.appKey,
},
credentials: "omit",
body: JSON.stringify(events),
});
try {
const res = await fetch(this.apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"App-Key": this.appKey,
},
credentials: "omit",
body: JSON.stringify(events),
});

if (res.status >= 300) {
const reason = await res.text();
console.warn(
`Aptabase: Failed to send ${events.length} events. Reason: ${res.status} ${reason}`
);
}
if (res.status < 300) {
return Promise.resolve();
}

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

return Promise.resolve();
throw new Error(reason);
} catch (e) {
console.error(
`Aptabase: Failed to send ${events.length} events. Reason: ${e}`
);
throw e;
}
}
}
37 changes: 25 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { newSessionId } from "./session";
import { EnvironmentInfo, getEnvironmentInfo } from "./env";
import { Platform } from "react-native";
import { AppState, Platform } from "react-native";
import { EventDispatcher } from "./dispatcher";

/**
Expand All @@ -15,14 +15,14 @@ export type AptabaseOptions = {
appVersion?: string;

// Override the default flush interval (in milliseconds)
flushInterval?: string;
flushInterval?: number;
};

// Session expires after 1 hour of inactivity
const SESSION_TIMEOUT = 1 * 60 * 60;
const SESSION_TIMEOUT = 3600;

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

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

Expand All @@ -55,6 +55,23 @@ function getBaseUrl(
return _hosts[region];
}

function startPolling(flushInterval: number) {
const flush = () => _dispatcher?.flush();

let interval = setInterval(flush, flushInterval);

if (AppState.isAvailable) {
AppState.addEventListener("change", (next) => {
clearInterval(interval);
if (next === "background") {
flush();
} else if (next === "active") {
interval = setInterval(flush, flushInterval);
}
});
}
}

/**
* Initializes the SDK with given App Key
* @param {string} appKey - Aptabase App Key
Expand Down Expand Up @@ -88,12 +105,8 @@ export function init(appKey: string, options?: AptabaseOptions) {

_dispatcher = new EventDispatcher(_apiUrl, _appKey);

const flushInterval =
options?.flushInterval ?? _env.isDebug
? DEBUG_FLUSH_INTERVAL
: RELEASE_FLUSH_INTERVAL;

setInterval(_dispatcher.flush.bind(_dispatcher), flushInterval);
const flushInterval = options?.flushInterval ?? FLUSH_INTERVAL;
startPolling(flushInterval);
}

/**
Expand Down

0 comments on commit 5910178

Please sign in to comment.