@@ -27,6 +27,7 @@ import { Provider } from '@firebase/component';
2727import {
2828 getModularInstance ,
2929 createMockUserToken ,
30+ deepEqual ,
3031 EmulatorMockTokenOptions ,
3132 getDefaultEmulatorHostnameAndPort
3233} from '@firebase/util' ;
@@ -85,11 +86,10 @@ let useRestClient = false;
8586function repoManagerApplyEmulatorSettings (
8687 repo : Repo ,
8788 host : string ,
88- port : number ,
8989 tokenProvider ?: AuthTokenProvider
9090) : void {
9191 repo . repoInfo_ = new RepoInfo (
92- ` ${ host } : ${ port } ` ,
92+ host ,
9393 /* secure= */ false ,
9494 repo . repoInfo_ . namespace ,
9595 repo . repoInfo_ . webSocketOnly ,
@@ -350,13 +350,22 @@ export function connectDatabaseEmulator(
350350) : void {
351351 db = getModularInstance ( db ) ;
352352 db . _checkNotDeleted ( 'useEmulator' ) ;
353+ const hostAndPort = `${ host } :${ port } ` ;
354+ const repo = db . _repoInternal ;
353355 if ( db . _instanceStarted ) {
356+ // If the instance has already been started, and this function is called again with the same
357+ // parameters, then silently return. If the parameters differ then assert.
358+ if (
359+ hostAndPort === repo . repoInfo_ . host &&
360+ deepEqual ( options , repo . repoInfo_ . emulatorOptions )
361+ ) {
362+ return ;
363+ }
354364 fatal (
355- 'Cannot call useEmulator () after instance has already been initialized .'
365+ 'connectDatabaseEmulator () cannot alter the emulator configuration after the database instance has started .'
356366 ) ;
357367 }
358368
359- const repo = db . _repoInternal ;
360369 let tokenProvider : EmulatorTokenProvider | undefined = undefined ;
361370 if ( repo . repoInfo_ . nodeAdmin ) {
362371 if ( options . mockUserToken ) {
@@ -374,7 +383,7 @@ export function connectDatabaseEmulator(
374383 }
375384
376385 // Modify the repo to apply emulator settings
377- repoManagerApplyEmulatorSettings ( repo , host , port , tokenProvider ) ;
386+ repoManagerApplyEmulatorSettings ( repo , host , tokenProvider ) ;
378387}
379388
380389/**
0 commit comments