UNPKG

@launchdarkly/react-native-client-sdk

Version:
115 lines 5.88 kB
/* eslint-disable max-classes-per-file */ import { base64UrlEncode, BasicLogger, internal, LDClientImpl, } from '@launchdarkly/js-client-sdk-common'; import MobileDataManager from './MobileDataManager'; import validateOptions, { filterToBaseOptions } from './options'; import createPlatform from './platform'; import { ConnectionManager } from './platform/ConnectionManager'; import RNStateDetector from './RNStateDetector'; /** * The React Native LaunchDarkly client. Instantiate this class to create an * instance of the ReactNativeLDClient and pass it to the {@link LDProvider}. * * @example * ```tsx * const featureClient = new ReactNativeLDClient(MOBILE_KEY, AutoEnvAttributes.Enabled); * * <LDProvider client={featureClient}> * <Welcome /> * </LDProvider> * ``` */ export default class ReactNativeLDClient extends LDClientImpl { /** * Creates an instance of the LaunchDarkly client. * * @param sdkKey The LaunchDarkly mobile key. * @param autoEnvAttributes Enable / disable Auto environment attributes. When enabled, the SDK will automatically * provide data about the mobile environment where the application is running. To learn more, * read [Automatic environment attributes](https://docs.launchdarkly.com/sdk/features/environment-attributes). * for more documentation. * @param options {@link LDOptions} to initialize the client with. */ constructor(sdkKey, autoEnvAttributes, options = {}) { var _a; const { logger: customLogger, debug } = options; const logger = customLogger !== null && customLogger !== void 0 ? customLogger : new BasicLogger({ level: debug ? 'debug' : 'info', // eslint-disable-next-line no-console destination: console.log, }); const validatedRnOptions = validateOptions(options, logger); const internalOptions = { analyticsEventPath: `/mobile`, diagnosticEventPath: `/mobile/events/diagnostic`, highTimeoutThreshold: 15, getImplementationHooks: (_environmentMetadata) => internal.safeGetHooks(logger, _environmentMetadata, validatedRnOptions.plugins), credentialType: 'mobileKey', }; const platform = createPlatform(logger, options, validatedRnOptions.storage); super(sdkKey, autoEnvAttributes, platform, Object.assign(Object.assign({}, filterToBaseOptions(options)), { logger }), (flagManager, configuration, baseHeaders, emitter, diagnosticsManager) => new MobileDataManager(platform, flagManager, sdkKey, configuration, validatedRnOptions, () => ({ pathGet(encoding, _plainContextString) { return `/msdk/evalx/contexts/${base64UrlEncode(_plainContextString, encoding)}`; }, pathReport(_encoding, _plainContextString) { return `/msdk/evalx/context`; }, pathPing(_encoding, _plainContextString) { // Note: if you are seeing this error, it is a coding error. This DataSourcePaths implementation is for polling endpoints. /ping is not currently // used in a polling situation. It is probably the case that this was called by streaming logic erroneously. throw new Error('Ping for polling unsupported.'); }, }), () => ({ pathGet(encoding, _plainContextString) { return `/meval/${base64UrlEncode(_plainContextString, encoding)}`; }, pathReport(_encoding, _plainContextString) { return `/meval`; }, pathPing(_encoding, _plainContextString) { return `/mping`; }, }), baseHeaders, emitter, diagnosticsManager), internalOptions); this.setEventSendingEnabled(!this.isOffline(), false); const dataManager = this.dataManager; const destination = { setNetworkAvailability: (available) => { dataManager.setNetworkAvailability(available); }, setEventSendingEnabled: (enabled, flush) => { this.setEventSendingEnabled(enabled, flush); }, setConnectionMode: async (mode) => { // Pass the connection mode to the base implementation. // The RN implementation will pass the connection mode through the connection manager. dataManager.setConnectionMode(mode); }, }; const initialConnectionMode = (_a = options.initialConnectionMode) !== null && _a !== void 0 ? _a : 'streaming'; this._connectionManager = new ConnectionManager(logger, { initialConnectionMode, automaticNetworkHandling: validatedRnOptions.automaticNetworkHandling, automaticBackgroundHandling: validatedRnOptions.automaticBackgroundHandling, runInBackground: validatedRnOptions.runInBackground, }, destination, new RNStateDetector()); internal.safeRegisterPlugins(logger, this.environmentMetadata, this, validatedRnOptions.plugins); } async setConnectionMode(mode) { // Set the connection mode before setting offline, in case there is any mode transition work // such as flushing on entering the background. this._connectionManager.setConnectionMode(mode); // For now the data source connection and the event processing state are connected. this._connectionManager.setOffline(mode === 'offline'); } /** * Gets the SDK connection mode. */ getConnectionMode() { const dataManager = this.dataManager; return dataManager.getConnectionMode(); } isOffline() { const dataManager = this.dataManager; return dataManager.getConnectionMode() === 'offline'; } } //# sourceMappingURL=ReactNativeLDClient.js.map