@launchdarkly/react-native-client-sdk
Version:
React Native LaunchDarkly SDK
116 lines • 5.65 kB
JavaScript
import { BaseDataManager, Context, makeRequestor, readFlagsFromBootstrap, } from '@launchdarkly/js-client-sdk-common';
const logTag = '[MobileDataManager]';
export default class MobileDataManager extends BaseDataManager {
constructor(platform, flagManager, credential, config, _rnConfig, getPollingPaths, getStreamingPaths, baseHeaders, emitter, diagnosticsManager) {
super(platform, flagManager, credential, config, getPollingPaths, getStreamingPaths, baseHeaders, emitter, diagnosticsManager);
this._rnConfig = _rnConfig;
// Not implemented yet.
this.networkAvailable = true;
this.connectionMode = 'streaming';
this.connectionMode = _rnConfig.initialConnectionMode;
}
_debugLog(message, ...args) {
this.logger.debug(`${logTag} ${message}`, ...args);
}
async identify(identifyResolve, identifyReject, context, identifyOptions) {
if (this.closed) {
this._debugLog('Identify called after data manager was closed.');
return;
}
this.context = context;
// When bootstrap is provided, resolve identify immediately then fall through to connect.
if (identifyOptions === null || identifyOptions === void 0 ? void 0 : identifyOptions.bootstrap) {
this._finishIdentifyFromBootstrap(context, identifyOptions, identifyResolve);
}
// Bootstrap path already called resolve so we use this to prevent duplicate resolve calls.
const resolvedFromBootstrap = !!(identifyOptions === null || identifyOptions === void 0 ? void 0 : identifyOptions.bootstrap);
const offline = this.connectionMode === 'offline';
// In offline mode we do not support waiting for results.
const waitForNetworkResults = !!(identifyOptions === null || identifyOptions === void 0 ? void 0 : identifyOptions.waitForNetworkResults) && !offline;
const loadedFromCache = await this.flagManager.loadCached(context);
if (loadedFromCache && !waitForNetworkResults && !resolvedFromBootstrap) {
this._debugLog('Identify completing with cached flags');
identifyResolve();
}
if (loadedFromCache && waitForNetworkResults) {
this._debugLog('Identify - Flags loaded from cache, but identify was requested with "waitForNetworkResults"');
}
if (this.connectionMode === 'offline') {
if (loadedFromCache) {
this._debugLog('Offline identify - using cached flags.');
}
else {
this._debugLog('Offline identify - no cached flags, using defaults or already loaded flags.');
if (!resolvedFromBootstrap) {
identifyResolve();
}
}
}
else {
// Context has been validated in LDClientImpl.identify
this._setupConnection(context, identifyResolve, identifyReject);
}
}
_finishIdentifyFromBootstrap(context, identifyOpts, identifyResolve) {
let { bootstrapParsed } = identifyOpts;
if (!bootstrapParsed) {
bootstrapParsed = readFlagsFromBootstrap(this.logger, identifyOpts.bootstrap);
}
this.flagManager.setBootstrap(context, bootstrapParsed);
this._debugLog('Identify - Initialization completed from bootstrap');
identifyResolve();
}
_setupConnection(context, identifyResolve, identifyReject) {
var _a;
const rawContext = Context.toLDContext(context);
const plainContextString = JSON.stringify(Context.toLDContext(context));
const requestor = makeRequestor(plainContextString, this.config.serviceEndpoints, this.getPollingPaths(), this.platform.requests, this.platform.encoding, this.baseHeaders, [], this.config.withReasons, this.config.useReport);
(_a = this.updateProcessor) === null || _a === void 0 ? void 0 : _a.close();
switch (this.connectionMode) {
case 'streaming':
this.createStreamingProcessor(rawContext, context, requestor, identifyResolve, identifyReject);
break;
case 'polling':
this.createPollingProcessor(rawContext, context, requestor, identifyResolve, identifyReject);
break;
default:
break;
}
this.updateProcessor.start();
}
setNetworkAvailability(available) {
this.networkAvailable = available;
}
async setConnectionMode(mode) {
var _a;
if (this.closed) {
this._debugLog('setting connection mode after data manager was closed');
return;
}
if (this.connectionMode === mode) {
this._debugLog(`setConnectionMode ignored. Mode is already '${mode}'.`);
return;
}
this.connectionMode = mode;
this._debugLog(`setConnectionMode ${mode}.`);
switch (mode) {
case 'offline':
(_a = this.updateProcessor) === null || _a === void 0 ? void 0 : _a.close();
break;
case 'polling':
case 'streaming':
if (this.context) {
// identify will start the update processor
this._setupConnection(this.context);
}
break;
default:
this.logger.warn(`Unknown ConnectionMode: ${mode}. Only 'offline', 'streaming', and 'polling' are supported.`);
break;
}
}
getConnectionMode() {
return this.connectionMode;
}
}
//# sourceMappingURL=MobileDataManager.js.map