Skip to content

Make initializeFirestore() idempotent #5280

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 5 commits into from
Aug 12, 2021
Merged
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
18 changes: 14 additions & 4 deletions packages/firestore/src/exp/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from '@firebase/app-exp';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { Provider } from '@firebase/component';
import { deepEqual } from '@firebase/util';

import {
IndexedDbOfflineComponentProvider,
Expand Down Expand Up @@ -128,10 +129,19 @@ export function initializeFirestore(
const provider = _getProvider(app, 'firestore-exp');

if (provider.isInitialized()) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'Firestore can only be initialized once per app.'
);
const existingInstance = provider.getImmediate();
const initialSettings = provider.getOptions() as FirestoreSettings;
if (deepEqual(initialSettings, settings)) {
return existingInstance;
} else {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'initializeFirestore() has already been called with ' +
'different options. To avoid this error, call initializeFirestore() with the ' +
'same options as when it was originally called, or call getFirestore() to return the' +
' already initialized instance.'
);
}
}

if (
Expand Down