@@ -29,55 +29,25 @@ export class AppStore {
2929
3030 private readonly appStore = new Map < string , FirebaseApp > ( ) ;
3131
32- private appOptionsDeeplyEqual ( newAppOptions : AppOptions , existingAppOptions : AppOptions ) : boolean {
33- return newAppOptions . credential === existingAppOptions . credential
34- && newAppOptions . databaseAuthVariableOverride === existingAppOptions . databaseAuthVariableOverride
35- && newAppOptions . databaseURL === existingAppOptions . databaseURL
36- && newAppOptions . httpAgent === existingAppOptions . httpAgent
37- && newAppOptions . projectId === existingAppOptions . projectId
38- && newAppOptions . serviceAccountId === existingAppOptions . serviceAccountId
39- && newAppOptions . storageBucket === existingAppOptions . storageBucket ;
40- }
41-
4232 public initializeApp ( options ?: AppOptions , appName : string = DEFAULT_APP_NAME ) : App {
4333 if ( typeof options === 'undefined' ) {
4434 options = loadOptionsFromEnvVar ( ) ;
4535 options . credential = getApplicationDefault ( ) ;
4636 }
4737
48- if ( typeof appName !== 'string' || appName === '' ) {
49- throw new FirebaseAppError (
50- AppErrorCodes . INVALID_APP_NAME ,
51- `Invalid Firebase app name "${ appName } " provided. App name must be a non-empty string.` ,
52- ) ;
53- } else {
54- const existingApp : FirebaseApp | undefined = this . appStore . get ( appName ) ;
55- if ( existingApp !== undefined ) {
56- if ( this . appOptionsDeeplyEqual ( options , existingApp . options ) ) {
57- return existingApp ;
58- }
59- else {
60- throw new FirebaseAppError (
61- AppErrorCodes . DUPLICATE_APP ,
62- `Firebase app named "${ appName } " already exists but with a different configuration. This means you ` +
63- 'called initializeApp() more than once with the same app name but a variant of AppOptions.'
64- ) ;
65- }
66- }
67-
68- const app = new FirebaseApp ( options , appName , this ) ;
69- this . appStore . set ( app . name , app ) ;
70- return app ;
38+ validateAppNameFormat ( appName ) ;
39+ if ( this . appStore . has ( appName ) ) {
40+ return this . appStore . get ( appName ) ! ;
7141 }
42+
43+ const app = new FirebaseApp ( options , appName , this ) ;
44+ this . appStore . set ( app . name , app ) ;
45+ return app ;
7246 }
7347
7448 public getApp ( appName : string = DEFAULT_APP_NAME ) : App {
75- if ( typeof appName !== 'string' || appName === '' ) {
76- throw new FirebaseAppError (
77- AppErrorCodes . INVALID_APP_NAME ,
78- `Invalid Firebase app name "${ appName } " provided. App name must be a non-empty string.` ,
79- ) ;
80- } else if ( ! this . appStore . has ( appName ) ) {
49+ validateAppNameFormat ( appName ) ;
50+ if ( ! this . appStore . has ( appName ) ) {
8151 let errorMessage : string = ( appName === DEFAULT_APP_NAME )
8252 ? 'The default Firebase app does not exist. ' : `Firebase app named "${ appName } " does not exist. ` ;
8353 errorMessage += 'Make sure you call initializeApp() before using any of the Firebase services.' ;
@@ -125,6 +95,15 @@ export class AppStore {
12595 }
12696}
12797
98+ function validateAppNameFormat ( appName : string ) : void {
99+ if ( typeof appName !== 'string' || appName === '' ) {
100+ throw new FirebaseAppError (
101+ AppErrorCodes . INVALID_APP_NAME ,
102+ `Invalid Firebase app name "${ appName } " provided. App name must be a non-empty string.` ,
103+ ) ;
104+ }
105+ }
106+
128107export const defaultAppStore = new AppStore ( ) ;
129108
130109export function initializeApp ( options ?: AppOptions , appName : string = DEFAULT_APP_NAME ) : App {
0 commit comments