16
16
*/
17
17
18
18
import firebase from '@firebase/app' ;
19
- import { _FirebaseNamespace } from '@firebase/app-types/private' ;
20
19
import '@firebase/installations' ;
20
+ import { _FirebaseNamespace } from '@firebase/app-types/private' ;
21
21
import { RemoteConfig as RemoteConfigType } from '@firebase/remote-config-types' ;
22
22
import { CachingClient } from './src/client/caching_client' ;
23
23
import { RestClient } from './src/client/rest_client' ;
@@ -28,6 +28,11 @@ import { ERROR_FACTORY, ErrorCode } from './src/errors';
28
28
import { RetryingClient } from './src/client/retrying_client' ;
29
29
import { Logger , LogLevel as FirebaseLogLevel } from '@firebase/logger' ;
30
30
import { name as packageName } from './package.json' ;
31
+ import {
32
+ Component ,
33
+ ComponentType ,
34
+ ComponentContainer
35
+ } from '@firebase/component' ;
31
36
32
37
// Facilitates debugging by enabling settings changes without rebuilding asset.
33
38
// Note these debug options are not part of a documented, supported API and can change at any time.
@@ -42,71 +47,82 @@ declare global {
42
47
export function registerRemoteConfig (
43
48
firebaseInstance : _FirebaseNamespace
44
49
) : 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
+ ) ;
52
57
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 ( ) ;
65
67
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
+ }
68
72
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' ;
70
85
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 ) ;
74
88
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 ) ;
91
90
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 ;
99
94
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
+ ) ;
103
111
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
+ }
110
126
}
111
127
112
128
registerRemoteConfig ( firebase as _FirebaseNamespace ) ;
0 commit comments