Skip to content

Commit f9e3b28

Browse files
committed
Initial implementation
1 parent 8e3290e commit f9e3b28

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

src/app/lifecycle.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ 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+
3242
public initializeApp(options?: AppOptions, appName: string = DEFAULT_APP_NAME): App {
3343
if (typeof options === 'undefined') {
3444
options = loadOptionsFromEnvVar();
@@ -40,29 +50,25 @@ export class AppStore {
4050
AppErrorCodes.INVALID_APP_NAME,
4151
`Invalid Firebase app name "${appName}" provided. App name must be a non-empty string.`,
4252
);
43-
} else if (this.appStore.has(appName)) {
44-
if (appName === DEFAULT_APP_NAME) {
45-
throw new FirebaseAppError(
46-
AppErrorCodes.DUPLICATE_APP,
47-
'The default Firebase app already exists. This means you called initializeApp() ' +
48-
'more than once without providing an app name as the second argument. In most cases ' +
49-
'you only need to call initializeApp() once. But if you do want to initialize ' +
50-
'multiple apps, pass a second argument to initializeApp() to give each app a unique ' +
51-
'name.',
52-
);
53-
} else {
54-
throw new FirebaseAppError(
55-
AppErrorCodes.DUPLICATE_APP,
56-
`Firebase app named "${appName}" already exists. This means you called initializeApp() ` +
57-
'more than once with the same app name as the second argument. Make sure you provide a ' +
58-
'unique name every time you call initializeApp().',
59-
);
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+
}
6066
}
61-
}
6267

63-
const app = new FirebaseApp(options, appName, this);
64-
this.appStore.set(app.name, app);
65-
return app;
68+
const app = new FirebaseApp(options, appName, this);
69+
this.appStore.set(app.name, app);
70+
return app;
71+
}
6672
}
6773

6874
public getApp(appName: string = DEFAULT_APP_NAME): App {

0 commit comments

Comments
 (0)