Skip to content

Commit 06bad3e

Browse files
authored
Do not throw error on duplicate service registration. (#2085)
1 parent 5b5afa2 commit 06bad3e

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

packages/app/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import { FirebaseNamespace } from '@firebase/app-types';
1919
import { createFirebaseNamespace } from './src/firebaseNamespace';
2020
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';
2422

2523
// Firebase Lite detection
2624
// eslint-disable-next-line @typescript-eslint/no-explicit-any

packages/app/src/errors.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export const enum AppError {
2222
BAD_APP_NAME = 'bad-app-name',
2323
DUPLICATE_APP = 'duplicate-app',
2424
APP_DELETED = 'app-deleted',
25-
DUPLICATE_SERVICE = 'duplicate-service',
2625
INVALID_APP_ARGUMENT = 'invalid-app-argument'
2726
}
2827

@@ -33,8 +32,6 @@ const ERRORS: ErrorMap<AppError> = {
3332
[AppError.BAD_APP_NAME]: "Illegal App name: '{$appName}",
3433
[AppError.DUPLICATE_APP]: "Firebase App named '{$appName}' already exists",
3534
[AppError.APP_DELETED]: "Firebase App named '{$appName}' already deleted",
36-
[AppError.DUPLICATE_SERVICE]:
37-
"Firebase service named '{$appName}' already registered",
3835
[AppError.INVALID_APP_ARGUMENT]:
3936
'firebase.{$appName}() takes either no argument or a ' +
4037
'Firebase App instance.'

packages/app/src/firebaseNamespaceCore.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { ERROR_FACTORY, AppError } from './errors';
3535
import { FirebaseAppLiteImpl } from './lite/firebaseAppLite';
3636
import { DEFAULT_ENTRY_NAME } from './constants';
3737
import { version } from '../../firebase/package.json';
38+
import { logger } from './logger';
3839

3940
/**
4041
* Because auth can't share code with other components, we attach the utility functions
@@ -180,9 +181,13 @@ export function createFirebaseNamespaceCore(
180181
appHook?: AppHook,
181182
allowMultipleInstances = false
182183
): FirebaseServiceNamespace<FirebaseService> {
183-
// Cannot re-register a service that already exists
184+
// If re-registering a service that already exists, return existing service
184185
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+
>;
186191
}
187192

188193
// Capture the service factory for later service instantiation

packages/app/src/logger.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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');

packages/app/test/firebaseApp.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
import { createFirebaseNamespace } from '../src/firebaseNamespace';
2525
import { createFirebaseNamespaceLite } from '../src/lite/firebaseNamespaceLite';
2626
import { assert } from 'chai';
27+
import { stub } from 'sinon';
2728

2829
executeFirebaseTests();
2930
executeFirebaseLiteTests();
@@ -87,6 +88,28 @@ function executeFirebaseTests(): void {
8788
assert.equal(registrations, 2);
8889
});
8990

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+
90113
it('Can lazy load a service', () => {
91114
let registrations = 0;
92115

0 commit comments

Comments
 (0)