Skip to content

Commit d8af08f

Browse files
authored
Fixed issue where we call connectDatabaseToEmulator twice (#6883)
1 parent 37f31c5 commit d8af08f

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

.changeset/nervous-ads-pretend.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/database": patch
3+
---
4+
5+
Fixed issue where connectDatabaseToEmulator can be called twice during a hot reload

packages/database/src/api/Database.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,11 @@ export function getDatabase(
320320
const db = _getProvider(app, 'database').getImmediate({
321321
identifier: url
322322
}) as Database;
323-
const emulator = getDefaultEmulatorHostnameAndPort('database');
324-
if (emulator) {
325-
connectDatabaseEmulator(db, ...emulator);
323+
if (!db._instanceStarted) {
324+
const emulator = getDefaultEmulatorHostnameAndPort('database');
325+
if (emulator) {
326+
connectDatabaseEmulator(db, ...emulator);
327+
}
326328
}
327329
return db;
328330
}

packages/database/test/exp/integration.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ describe('Database@exp Tests', () => {
7777
const db = getDatabase(defaultApp);
7878
expect(db).to.be.ok;
7979
});
80+
it("doesn't try to connect to emulator after database has already started", async () => {
81+
const db = getDatabase(defaultApp);
82+
const r = ref(db, '.info/connected');
83+
const deferred = new Deferred();
84+
onValue(r, snapshot => {
85+
if (snapshot.val()) {
86+
deferred.resolve();
87+
}
88+
});
89+
await deferred.promise;
90+
process.env.__FIREBASE_DEFAULTS__ = JSON.stringify({
91+
emulatorHosts: {
92+
database: 'localhost:9000'
93+
}
94+
});
95+
expect(() => getDatabase(defaultApp)).to.not.throw();
96+
delete process.env.__FIREBASE_DEFAULTS__;
97+
});
8098

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

0 commit comments

Comments
 (0)