diff --git a/packages/remote-config/index.ts b/packages/remote-config/index.ts index b3456eb031a..09c8454ae1e 100644 --- a/packages/remote-config/index.ts +++ b/packages/remote-config/index.ts @@ -16,8 +16,8 @@ */ import firebase from '@firebase/app'; -import { _FirebaseNamespace } from '@firebase/app-types/private'; import '@firebase/installations'; +import { _FirebaseNamespace } from '@firebase/app-types/private'; import { RemoteConfig as RemoteConfigType } from '@firebase/remote-config-types'; import { CachingClient } from './src/client/caching_client'; import { RestClient } from './src/client/rest_client'; @@ -28,6 +28,11 @@ import { ERROR_FACTORY, ErrorCode } from './src/errors'; import { RetryingClient } from './src/client/retrying_client'; import { Logger, LogLevel as FirebaseLogLevel } from '@firebase/logger'; import { name as packageName } from './package.json'; +import { + Component, + ComponentType, + ComponentContainer +} from '@firebase/component'; // Facilitates debugging by enabling settings changes without rebuilding asset. // Note these debug options are not part of a documented, supported API and can change at any time. @@ -42,71 +47,82 @@ declare global { export function registerRemoteConfig( firebaseInstance: _FirebaseNamespace ): void { - firebaseInstance.INTERNAL.registerService( - 'remoteConfig', - (app, _, namespace) => { - // Guards against the SDK being used in non-browser environments. - if (typeof window === 'undefined') { - throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_WINDOW); - } + firebaseInstance.INTERNAL.registerComponent( + new Component( + 'remoteConfig', + remoteConfigFactory, + ComponentType.PUBLIC + ).setMultipleInstances(true) + ); - // Normalizes optional inputs. - const { projectId, apiKey, appId } = app.options; - if (!projectId) { - throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_PROJECT_ID); - } - if (!apiKey) { - throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_API_KEY); - } - if (!appId) { - throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_APP_ID); - } - namespace = namespace || 'firebase'; + function remoteConfigFactory( + container: ComponentContainer, + namespace?: string + ): RemoteConfig { + /* Dependencies */ + // getImmediate for FirebaseApp will always succeed + const app = container.getProvider('app').getImmediate(); + // The following call will always succeed because rc has `import '@firebase/installations'` + const installations = container.getProvider('installations').getImmediate(); - const storage = new Storage(appId, app.name, namespace); - const storageCache = new StorageCache(storage); + // Guards against the SDK being used in non-browser environments. + if (typeof window === 'undefined') { + throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_WINDOW); + } - const logger = new Logger(packageName); + // Normalizes optional inputs. + const { projectId, apiKey, appId } = app.options; + if (!projectId) { + throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_PROJECT_ID); + } + if (!apiKey) { + throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_API_KEY); + } + if (!appId) { + throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_APP_ID); + } + namespace = namespace || 'firebase'; - // Sets ERROR as the default log level. - // See RemoteConfig#setLogLevel for corresponding normalization to ERROR log level. - logger.logLevel = FirebaseLogLevel.ERROR; + const storage = new Storage(appId, app.name, namespace); + const storageCache = new StorageCache(storage); - const restClient = new RestClient( - app.installations(), - // Uses the JS SDK version, by which the RC package version can be deduced, if necessary. - firebaseInstance.SDK_VERSION, - namespace, - projectId, - apiKey, - appId - ); - const retryingClient = new RetryingClient(restClient, storage); - const cachingClient = new CachingClient( - retryingClient, - storage, - storageCache, - logger - ); + const logger = new Logger(packageName); - const remoteConfigInstance = new RemoteConfig( - app, - cachingClient, - storageCache, - storage, - logger - ); + // Sets ERROR as the default log level. + // See RemoteConfig#setLogLevel for corresponding normalization to ERROR log level. + logger.logLevel = FirebaseLogLevel.ERROR; - // Starts warming cache. - // eslint-disable-next-line @typescript-eslint/no-floating-promises - remoteConfigInstance.ensureInitialized(); + const restClient = new RestClient( + installations, + // Uses the JS SDK version, by which the RC package version can be deduced, if necessary. + firebaseInstance.SDK_VERSION, + namespace, + projectId, + apiKey, + appId + ); + const retryingClient = new RetryingClient(restClient, storage); + const cachingClient = new CachingClient( + retryingClient, + storage, + storageCache, + logger + ); - return remoteConfigInstance; - }, - undefined, - undefined, - true /* allowMultipleInstances */ - ); + const remoteConfigInstance = new RemoteConfig( + app, + cachingClient, + storageCache, + storage, + logger + ); + + // Starts warming cache. + // eslint-disable-next-line @typescript-eslint/no-floating-promises + remoteConfigInstance.ensureInitialized(); + + return remoteConfigInstance; + } } registerRemoteConfig(firebase as _FirebaseNamespace); diff --git a/packages/remote-config/package.json b/packages/remote-config/package.json index f97e7b58941..741f05f7c78 100644 --- a/packages/remote-config/package.json +++ b/packages/remote-config/package.json @@ -30,6 +30,7 @@ "@firebase/logger": "0.1.28", "@firebase/remote-config-types": "0.1.2", "@firebase/util": "0.2.31", + "@firebase/component": "0.1.0", "tslib": "1.10.0" }, "license": "Apache-2.0",