Skip to content

Commit 254768c

Browse files
committed
Migrate RC to component framework (#2324)
* Migrate RC to component framework * [AUTOMATED]: Prettier Code Styling * remove unused import
1 parent 6746e9a commit 254768c

File tree

2 files changed

+75
-58
lines changed

2 files changed

+75
-58
lines changed

packages/remote-config/index.ts

Lines changed: 74 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717

1818
import firebase from '@firebase/app';
19-
import { _FirebaseNamespace } from '@firebase/app-types/private';
2019
import '@firebase/installations';
20+
import { _FirebaseNamespace } from '@firebase/app-types/private';
2121
import { RemoteConfig as RemoteConfigType } from '@firebase/remote-config-types';
2222
import { CachingClient } from './src/client/caching_client';
2323
import { RestClient } from './src/client/rest_client';
@@ -28,6 +28,11 @@ import { ERROR_FACTORY, ErrorCode } from './src/errors';
2828
import { RetryingClient } from './src/client/retrying_client';
2929
import { Logger, LogLevel as FirebaseLogLevel } from '@firebase/logger';
3030
import { name as packageName } from './package.json';
31+
import {
32+
Component,
33+
ComponentType,
34+
ComponentContainer
35+
} from '@firebase/component';
3136

3237
// Facilitates debugging by enabling settings changes without rebuilding asset.
3338
// Note these debug options are not part of a documented, supported API and can change at any time.
@@ -42,71 +47,82 @@ declare global {
4247
export function registerRemoteConfig(
4348
firebaseInstance: _FirebaseNamespace
4449
): void {
45-
firebaseInstance.INTERNAL.registerService(
46-
'remoteConfig',
47-
(app, _, namespace) => {
48-
// Guards against the SDK being used in non-browser environments.
49-
if (typeof window === 'undefined') {
50-
throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_WINDOW);
51-
}
50+
firebaseInstance.INTERNAL.registerComponent(
51+
new Component(
52+
'remoteConfig',
53+
remoteConfigFactory,
54+
ComponentType.PUBLIC
55+
).setMultipleInstances(true)
56+
);
5257

53-
// Normalizes optional inputs.
54-
const { projectId, apiKey, appId } = app.options;
55-
if (!projectId) {
56-
throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_PROJECT_ID);
57-
}
58-
if (!apiKey) {
59-
throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_API_KEY);
60-
}
61-
if (!appId) {
62-
throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_APP_ID);
63-
}
64-
namespace = namespace || 'firebase';
58+
function remoteConfigFactory(
59+
container: ComponentContainer,
60+
namespace?: string
61+
): RemoteConfig {
62+
/* Dependencies */
63+
// getImmediate for FirebaseApp will always succeed
64+
const app = container.getProvider('app').getImmediate();
65+
// The following call will always succeed because rc has `import '@firebase/installations'`
66+
const installations = container.getProvider('installations').getImmediate();
6567

66-
const storage = new Storage(appId, app.name, namespace);
67-
const storageCache = new StorageCache(storage);
68+
// Guards against the SDK being used in non-browser environments.
69+
if (typeof window === 'undefined') {
70+
throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_WINDOW);
71+
}
6872

69-
const logger = new Logger(packageName);
73+
// Normalizes optional inputs.
74+
const { projectId, apiKey, appId } = app.options;
75+
if (!projectId) {
76+
throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_PROJECT_ID);
77+
}
78+
if (!apiKey) {
79+
throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_API_KEY);
80+
}
81+
if (!appId) {
82+
throw ERROR_FACTORY.create(ErrorCode.REGISTRATION_APP_ID);
83+
}
84+
namespace = namespace || 'firebase';
7085

71-
// Sets ERROR as the default log level.
72-
// See RemoteConfig#setLogLevel for corresponding normalization to ERROR log level.
73-
logger.logLevel = FirebaseLogLevel.ERROR;
86+
const storage = new Storage(appId, app.name, namespace);
87+
const storageCache = new StorageCache(storage);
7488

75-
const restClient = new RestClient(
76-
app.installations(),
77-
// Uses the JS SDK version, by which the RC package version can be deduced, if necessary.
78-
firebaseInstance.SDK_VERSION,
79-
namespace,
80-
projectId,
81-
apiKey,
82-
appId
83-
);
84-
const retryingClient = new RetryingClient(restClient, storage);
85-
const cachingClient = new CachingClient(
86-
retryingClient,
87-
storage,
88-
storageCache,
89-
logger
90-
);
89+
const logger = new Logger(packageName);
9190

92-
const remoteConfigInstance = new RemoteConfig(
93-
app,
94-
cachingClient,
95-
storageCache,
96-
storage,
97-
logger
98-
);
91+
// Sets ERROR as the default log level.
92+
// See RemoteConfig#setLogLevel for corresponding normalization to ERROR log level.
93+
logger.logLevel = FirebaseLogLevel.ERROR;
9994

100-
// Starts warming cache.
101-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
102-
remoteConfigInstance.ensureInitialized();
95+
const restClient = new RestClient(
96+
installations,
97+
// Uses the JS SDK version, by which the RC package version can be deduced, if necessary.
98+
firebaseInstance.SDK_VERSION,
99+
namespace,
100+
projectId,
101+
apiKey,
102+
appId
103+
);
104+
const retryingClient = new RetryingClient(restClient, storage);
105+
const cachingClient = new CachingClient(
106+
retryingClient,
107+
storage,
108+
storageCache,
109+
logger
110+
);
103111

104-
return remoteConfigInstance;
105-
},
106-
undefined,
107-
undefined,
108-
true /* allowMultipleInstances */
109-
);
112+
const remoteConfigInstance = new RemoteConfig(
113+
app,
114+
cachingClient,
115+
storageCache,
116+
storage,
117+
logger
118+
);
119+
120+
// Starts warming cache.
121+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
122+
remoteConfigInstance.ensureInitialized();
123+
124+
return remoteConfigInstance;
125+
}
110126
}
111127

112128
registerRemoteConfig(firebase as _FirebaseNamespace);

packages/remote-config/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@firebase/logger": "0.1.31",
3131
"@firebase/remote-config-types": "0.1.3",
3232
"@firebase/util": "0.2.34",
33+
"@firebase/component": "0.1.0",
3334
"tslib": "1.10.0"
3435
},
3536
"license": "Apache-2.0",

0 commit comments

Comments
 (0)