Skip to content

Commit

Permalink
Fixed issue where we call connectDatabaseToEmulator twice (#6883)
Browse files Browse the repository at this point in the history
  • Loading branch information
maneesht committed Dec 13, 2022
1 parent 37f31c5 commit d8af08f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
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

0 comments on commit d8af08f

Please sign in to comment.