11import { newSessionId } from "./session" ;
22import { EnvironmentInfo , getEnvironmentInfo } from "./env" ;
3- import { Platform } from "react-native" ;
3+ import { AppState , Platform } from "react-native" ;
44import { 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
2727let _sessionId = newSessionId ( ) ;
2828let _lastTouched = new Date ( ) ;
@@ -34,7 +34,7 @@ let _dispatcher: EventDispatcher | undefined;
3434const _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