@segment/analytics-react-native
Version:
The hassle-free way to add Segment analytics to your React-Native app.
39 lines (34 loc) • 947 B
text/typescript
import {
AppState,
AppStateStatus,
NativeEventSubscription,
} from 'react-native';
import type { SegmentEvent } from '../types';
import { FlushPolicyBase } from './types';
/**
* StatupFlushPolicy triggers a flush right away on client startup
*/
export class BackgroundFlushPolicy extends FlushPolicyBase {
private appStateSubscription?: NativeEventSubscription;
private appState: AppStateStatus = AppState.currentState;
start() {
this.appStateSubscription = AppState.addEventListener(
'change',
(nextAppState) => {
if (
this.appState === 'active' &&
['inactive', 'background'].includes(nextAppState)
) {
// When the app goes into the background we will trigger a flush
this.shouldFlush.value = true;
}
}
);
}
onEvent(_event: SegmentEvent): void {
// Nothing to do
}
end(): void {
this.appStateSubscription?.remove();
}
}