@launchdarkly/react-native-client-sdk
Version:
React Native LaunchDarkly SDK
41 lines • 1.23 kB
JavaScript
import { AppState } from 'react-native';
import { ApplicationState } from './platform/ConnectionManager';
/**
* @internal
*/
function translateAppState(state) {
switch (state) {
case 'active':
return ApplicationState.Foreground;
case 'inactive':
case 'background':
case 'extension':
default:
return ApplicationState.Background;
}
}
/**
* @internal
*/
export default class RNStateDetector {
constructor() {
AppState.addEventListener('change', (state) => {
var _a;
(_a = this._applicationStateListener) === null || _a === void 0 ? void 0 : _a.call(this, translateAppState(state));
});
}
setApplicationStateListener(fn) {
this._applicationStateListener = fn;
// When you listen provide the current state immediately.
this._applicationStateListener(translateAppState(AppState.currentState));
}
setNetworkStateListener(fn) {
this._networkStateListener = fn;
// Not implemented.
}
stopListening() {
this._applicationStateListener = undefined;
this._networkStateListener = undefined;
}
}
//# sourceMappingURL=RNStateDetector.js.map