Skip to content

Fixed issue where we call connectDatabaseToEmulator twice #6883

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
Dec 13, 2022
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
5 changes: 5 additions & 0 deletions .changeset/nervous-ads-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/database": patch
---

Fixed issue where connectDatabaseToEmulator can be called twice during a hot reload
8 changes: 5 additions & 3 deletions packages/database/src/api/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,11 @@ export function getDatabase(
const db = _getProvider(app, 'database').getImmediate({
identifier: url
}) as Database;
const emulator = getDefaultEmulatorHostnameAndPort('database');
if (emulator) {
connectDatabaseEmulator(db, ...emulator);
if (!db._instanceStarted) {
const emulator = getDefaultEmulatorHostnameAndPort('database');
if (emulator) {
connectDatabaseEmulator(db, ...emulator);
}
}
return db;
}
Expand Down
18 changes: 18 additions & 0 deletions packages/database/test/exp/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ describe('Database@exp Tests', () => {
const db = getDatabase(defaultApp);
expect(db).to.be.ok;
});
it("doesn't try to connect to emulator after database has already started", async () => {
const db = getDatabase(defaultApp);
const r = ref(db, '.info/connected');
const deferred = new Deferred();
onValue(r, snapshot => {
if (snapshot.val()) {
deferred.resolve();
}
});
await deferred.promise;
process.env.__FIREBASE_DEFAULTS__ = JSON.stringify({
emulatorHosts: {
database: 'localhost:9000'
}
});
expect(() => getDatabase(defaultApp)).to.not.throw();
delete process.env.__FIREBASE_DEFAULTS__;
});

it('Can get database with custom URL', () => {
const db = getDatabase(defaultApp, 'http://foo.bar.com');
Expand Down