@launchdarkly/react-native-client-sdk
Version:
React Native LaunchDarkly SDK
110 lines • 4.06 kB
JavaScript
/**
* @internal
*/
export var ApplicationState;
(function (ApplicationState) {
/// The application is in the foreground.
ApplicationState["Foreground"] = "foreground";
/// The application is in the background.
///
/// Note, the application will not be active while in the background, but
/// it will track when it is entering or exiting a background state.
ApplicationState["Background"] = "background";
})(ApplicationState || (ApplicationState = {}));
/**
* @internal
*/
export var NetworkState;
(function (NetworkState) {
/// There is no network available for the SDK to use.
NetworkState["Unavailable"] = "unavailable";
/// The network is available. Note that network requests may still fail
/// for other reasons.
NetworkState["Available"] = "available";
})(NetworkState || (NetworkState = {}));
/**
* @internal
*/
export class ConnectionManager {
constructor(_logger, _config, _destination, _detector) {
this._logger = _logger;
this._config = _config;
this._destination = _destination;
this._detector = _detector;
this._applicationState = ApplicationState.Foreground;
this._networkState = NetworkState.Available;
this._offline = false;
this._currentConnectionMode = _config.initialConnectionMode;
if (_config.automaticBackgroundHandling) {
_detector.setApplicationStateListener((state) => {
this._applicationState = state;
this._handleState();
});
}
if (_config.automaticNetworkHandling) {
_detector.setNetworkStateListener((state) => {
this._networkState = state;
this._handleState();
});
}
}
setOffline(offline) {
this._offline = offline;
this._handleState();
}
setConnectionMode(mode) {
this._currentConnectionMode = mode;
this._handleState();
}
close() {
this._detector.stopListening();
}
_handleState() {
this._logger.debug(`Handling state: ${this._applicationState}:${this._networkState}`);
switch (this._networkState) {
case NetworkState.Unavailable:
this._destination.setNetworkAvailability(false);
break;
case NetworkState.Available:
this._destination.setNetworkAvailability(true);
switch (this._applicationState) {
case ApplicationState.Foreground:
this._setForegroundAvailable();
break;
case ApplicationState.Background:
this._setBackgroundAvailable();
break;
default:
break;
}
break;
default:
break;
}
}
_setForegroundAvailable() {
if (this._offline) {
this._destination.setConnectionMode('offline');
// Don't attempt to flush. If the user wants to flush when entering offline
// mode, then they can do that directly.
this._destination.setEventSendingEnabled(false, false);
return;
}
// Currently the foreground mode will always be whatever the last active
// connection mode was.
this._destination.setConnectionMode(this._currentConnectionMode);
this._destination.setEventSendingEnabled(true, false);
}
_setBackgroundAvailable() {
if (!this._config.runInBackground) {
this._destination.setConnectionMode('offline');
this._destination.setEventSendingEnabled(false, true);
return;
}
// This SDK doesn't currently support automatic background polling.
// If connections in the background are allowed, then use the same mode
// as is configured for the foreground.
this._setForegroundAvailable();
}
}
//# sourceMappingURL=ConnectionManager.js.map