Skip to content

Commit 9fc2c04

Browse files
committed
Implementation. Needs tests.
1 parent d1c6e31 commit 9fc2c04

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

packages/database/src/api/Database.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { Provider } from '@firebase/component';
2727
import {
2828
getModularInstance,
2929
createMockUserToken,
30+
deepEqual,
3031
EmulatorMockTokenOptions,
3132
getDefaultEmulatorHostnameAndPort
3233
} from '@firebase/util';
@@ -85,11 +86,10 @@ let useRestClient = false;
8586
function 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
/**

packages/database/src/core/RepoInfo.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { assert } from '@firebase/util';
18+
import { assert, EmulatorMockTokenOptions } from '@firebase/util';
1919

2020
import { LONG_POLLING, WEBSOCKET } from '../realtime/Constants';
2121

@@ -28,6 +28,9 @@ import { each } from './util/util';
2828
export class RepoInfo {
2929
private _host: string;
3030
private _domain: string;
31+
private _emulatorOptions: {
32+
mockUserToken?: EmulatorMockTokenOptions | string;
33+
};
3134
internalHost: string;
3235

3336
/**
@@ -50,6 +53,7 @@ export class RepoInfo {
5053
) {
5154
this._host = host.toLowerCase();
5255
this._domain = this._host.substr(this._host.indexOf('.') + 1);
56+
this._emulatorOptions = {};
5357
this.internalHost =
5458
(PersistentStorage.get('host:' + host) as string) || this._host;
5559
}
@@ -78,6 +82,12 @@ export class RepoInfo {
7882
}
7983
}
8084

85+
get emulatorOptions(): {
86+
mockUserToken?: EmulatorMockTokenOptions | string;
87+
} {
88+
return this._emulatorOptions;
89+
}
90+
8191
toString(): string {
8292
let str = this.toURLString();
8393
if (this.persistenceKey) {

0 commit comments

Comments
 (0)