Skip to content

Do not throw error on duplicate service registration. #2085

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 7 commits into from
Aug 16, 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
4 changes: 1 addition & 3 deletions packages/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import { FirebaseNamespace } from '@firebase/app-types';
import { createFirebaseNamespace } from './src/firebaseNamespace';
import { isNode, isBrowser } from '@firebase/util';
import { Logger } from '@firebase/logger';

const logger = new Logger('@firebase/app');
import { logger } from './src/logger';

// Firebase Lite detection
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
3 changes: 0 additions & 3 deletions packages/app/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const enum AppError {
BAD_APP_NAME = 'bad-app-name',
DUPLICATE_APP = 'duplicate-app',
APP_DELETED = 'app-deleted',
DUPLICATE_SERVICE = 'duplicate-service',
INVALID_APP_ARGUMENT = 'invalid-app-argument'
}

Expand All @@ -33,8 +32,6 @@ const ERRORS: ErrorMap<AppError> = {
[AppError.BAD_APP_NAME]: "Illegal App name: '{$appName}",
[AppError.DUPLICATE_APP]: "Firebase App named '{$appName}' already exists",
[AppError.APP_DELETED]: "Firebase App named '{$appName}' already deleted",
[AppError.DUPLICATE_SERVICE]:
"Firebase service named '{$appName}' already registered",
[AppError.INVALID_APP_ARGUMENT]:
'firebase.{$appName}() takes either no argument or a ' +
'Firebase App instance.'
Expand Down
9 changes: 7 additions & 2 deletions packages/app/src/firebaseNamespaceCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { ERROR_FACTORY, AppError } from './errors';
import { FirebaseAppLiteImpl } from './lite/firebaseAppLite';
import { DEFAULT_ENTRY_NAME } from './constants';
import { version } from '../../firebase/package.json';
import { logger } from './logger';

/**
* Because auth can't share code with other components, we attach the utility functions
Expand Down Expand Up @@ -180,9 +181,13 @@ export function createFirebaseNamespaceCore(
appHook?: AppHook,
allowMultipleInstances = false
): FirebaseServiceNamespace<FirebaseService> {
// Cannot re-register a service that already exists
// If re-registering a service that already exists, return existing service
if (factories[name]) {
throw ERROR_FACTORY.create(AppError.DUPLICATE_SERVICE, { appName: name });
logger.debug(`There were multiple attempts to register service ${name}.`);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (namespace as any)[name] as FirebaseServiceNamespace<
FirebaseService
>;
}

// Capture the service factory for later service instantiation
Expand Down
20 changes: 20 additions & 0 deletions packages/app/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @license
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Logger } from '@firebase/logger';

export const logger = new Logger('@firebase/app');
23 changes: 23 additions & 0 deletions packages/app/test/firebaseApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { createFirebaseNamespace } from '../src/firebaseNamespace';
import { createFirebaseNamespaceLite } from '../src/lite/firebaseNamespaceLite';
import { assert } from 'chai';
import { stub } from 'sinon';

executeFirebaseTests();
executeFirebaseLiteTests();
Expand Down Expand Up @@ -87,6 +88,28 @@ function executeFirebaseTests(): void {
assert.equal(registrations, 2);
});

it('Will do nothing if registerService is called again with the same name', () => {
const registerStub = stub(
(firebase as _FirebaseNamespace).INTERNAL,
'registerService'
).callThrough();
(firebase as _FirebaseNamespace).INTERNAL.registerService(
'test',
(app: FirebaseApp) => new TestService(app)
);
firebase.initializeApp({});
const serviceNamespace = (firebase as any).test;

(firebase as _FirebaseNamespace).INTERNAL.registerService(
'test',
(app: FirebaseApp) => new TestService(app)
);

const serviceNamespace2 = (firebase as any).test;
assert.strictEqual(serviceNamespace, serviceNamespace2);
assert.doesNotThrow(registerStub);
});

it('Can lazy load a service', () => {
let registrations = 0;

Expand Down