Skip to content

Migrate RC to component framework #2324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 74 additions & 58 deletions packages/remote-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/remote-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down