File tree Expand file tree Collapse file tree 5 files changed +51
-8
lines changed Expand file tree Collapse file tree 5 files changed +51
-8
lines changed Original file line number Diff line number Diff line change 18
18
import { FirebaseNamespace } from '@firebase/app-types' ;
19
19
import { createFirebaseNamespace } from './src/firebaseNamespace' ;
20
20
import { isNode , isBrowser } from '@firebase/util' ;
21
- import { Logger } from '@firebase/logger' ;
22
-
23
- const logger = new Logger ( '@firebase/app' ) ;
21
+ import { logger } from './src/logger' ;
24
22
25
23
// Firebase Lite detection
26
24
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Original file line number Diff line number Diff line change @@ -22,7 +22,6 @@ export const enum AppError {
22
22
BAD_APP_NAME = 'bad-app-name' ,
23
23
DUPLICATE_APP = 'duplicate-app' ,
24
24
APP_DELETED = 'app-deleted' ,
25
- DUPLICATE_SERVICE = 'duplicate-service' ,
26
25
INVALID_APP_ARGUMENT = 'invalid-app-argument'
27
26
}
28
27
@@ -33,8 +32,6 @@ const ERRORS: ErrorMap<AppError> = {
33
32
[ AppError . BAD_APP_NAME ] : "Illegal App name: '{$appName}" ,
34
33
[ AppError . DUPLICATE_APP ] : "Firebase App named '{$appName}' already exists" ,
35
34
[ AppError . APP_DELETED ] : "Firebase App named '{$appName}' already deleted" ,
36
- [ AppError . DUPLICATE_SERVICE ] :
37
- "Firebase service named '{$appName}' already registered" ,
38
35
[ AppError . INVALID_APP_ARGUMENT ] :
39
36
'firebase.{$appName}() takes either no argument or a ' +
40
37
'Firebase App instance.'
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ import { ERROR_FACTORY, AppError } from './errors';
35
35
import { FirebaseAppLiteImpl } from './lite/firebaseAppLite' ;
36
36
import { DEFAULT_ENTRY_NAME } from './constants' ;
37
37
import { version } from '../../firebase/package.json' ;
38
+ import { logger } from './logger' ;
38
39
39
40
/**
40
41
* Because auth can't share code with other components, we attach the utility functions
@@ -180,9 +181,13 @@ export function createFirebaseNamespaceCore(
180
181
appHook ?: AppHook ,
181
182
allowMultipleInstances = false
182
183
) : FirebaseServiceNamespace < FirebaseService > {
183
- // Cannot re-register a service that already exists
184
+ // If re-registering a service that already exists, return existing service
184
185
if ( factories [ name ] ) {
185
- throw ERROR_FACTORY . create ( AppError . DUPLICATE_SERVICE , { appName : name } ) ;
186
+ logger . debug ( `There were multiple attempts to register service ${ name } .` ) ;
187
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
188
+ return ( namespace as any ) [ name ] as FirebaseServiceNamespace <
189
+ FirebaseService
190
+ > ;
186
191
}
187
192
188
193
// Capture the service factory for later service instantiation
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @license
3
+ * Copyright 2019 Google Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { Logger } from '@firebase/logger' ;
19
+
20
+ export const logger = new Logger ( '@firebase/app' ) ;
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ import {
24
24
import { createFirebaseNamespace } from '../src/firebaseNamespace' ;
25
25
import { createFirebaseNamespaceLite } from '../src/lite/firebaseNamespaceLite' ;
26
26
import { assert } from 'chai' ;
27
+ import { stub } from 'sinon' ;
27
28
28
29
executeFirebaseTests ( ) ;
29
30
executeFirebaseLiteTests ( ) ;
@@ -87,6 +88,28 @@ function executeFirebaseTests(): void {
87
88
assert . equal ( registrations , 2 ) ;
88
89
} ) ;
89
90
91
+ it ( 'Will do nothing if registerService is called again with the same name' , ( ) => {
92
+ const registerStub = stub (
93
+ ( firebase as _FirebaseNamespace ) . INTERNAL ,
94
+ 'registerService'
95
+ ) . callThrough ( ) ;
96
+ ( firebase as _FirebaseNamespace ) . INTERNAL . registerService (
97
+ 'test' ,
98
+ ( app : FirebaseApp ) => new TestService ( app )
99
+ ) ;
100
+ firebase . initializeApp ( { } ) ;
101
+ const serviceNamespace = ( firebase as any ) . test ;
102
+
103
+ ( firebase as _FirebaseNamespace ) . INTERNAL . registerService (
104
+ 'test' ,
105
+ ( app : FirebaseApp ) => new TestService ( app )
106
+ ) ;
107
+
108
+ const serviceNamespace2 = ( firebase as any ) . test ;
109
+ assert . strictEqual ( serviceNamespace , serviceNamespace2 ) ;
110
+ assert . doesNotThrow ( registerStub ) ;
111
+ } ) ;
112
+
90
113
it ( 'Can lazy load a service' , ( ) => {
91
114
let registrations = 0 ;
92
115
You can’t perform that action at this time.
0 commit comments