Skip to content

Commit 9738a5c

Browse files
committed
create RepoInfoEmulatorOptions
1 parent 1c270b1 commit 9738a5c

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

packages/database/src/api/Database.ts

Lines changed: 21 additions & 9 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';
@@ -38,7 +39,7 @@ import {
3839
FirebaseAuthTokenProvider
3940
} from '../core/AuthTokenProvider';
4041
import { Repo, repoInterrupt, repoResume, repoStart } from '../core/Repo';
41-
import { RepoInfo } from '../core/RepoInfo';
42+
import { RepoInfo, RepoInfoEmulatorOptions} from '../core/RepoInfo';
4243
import { parseRepoInfo } from '../core/util/libs/parser';
4344
import { newEmptyPath, pathIsEmpty } from '../core/util/Path';
4445
import {
@@ -84,19 +85,20 @@ let useRestClient = false;
8485
*/
8586
function repoManagerApplyEmulatorSettings(
8687
repo: Repo,
87-
host: string,
88-
port: number,
89-
tokenProvider?: AuthTokenProvider
88+
hostAndPort: string,
89+
emulatorOptions: RepoInfoEmulatorOptions,
90+
tokenProvider?: AuthTokenProvider,
9091
): void {
9192
repo.repoInfo_ = new RepoInfo(
92-
`${host}:${port}`,
93+
hostAndPort,
9394
/* secure= */ false,
9495
repo.repoInfo_.namespace,
9596
repo.repoInfo_.webSocketOnly,
9697
repo.repoInfo_.nodeAdmin,
9798
repo.repoInfo_.persistenceKey,
9899
repo.repoInfo_.includeNamespaceInQueryParams,
99-
/*isUsingEmulator=*/ true
100+
/*isUsingEmulator=*/ true,
101+
emulatorOptions
100102
);
101103

102104
if (tokenProvider) {
@@ -350,13 +352,23 @@ export function connectDatabaseEmulator(
350352
): void {
351353
db = getModularInstance(db);
352354
db._checkNotDeleted('useEmulator');
355+
const hostAndPort = `${host}:${port}`;
356+
const repo = db._repoInternal;
353357
if (db._instanceStarted) {
358+
// If the instance has already been started, then silenty fail if this function is called again
359+
// with the same parameters. If the parameters differ then assert.
360+
if (
361+
true
362+
// hostAndPort === db._repoInternal.repoInfo_.host //&&
363+
//deepEqual(options, repo.repoInfo_.emulatorOptions)
364+
) {
365+
return;
366+
}
354367
fatal(
355-
'Cannot call useEmulator() after instance has already been initialized.'
368+
'connectDatabaseEmulator() cannot alter the emulator configuration after the database instance has started.'
356369
);
357370
}
358371

359-
const repo = db._repoInternal;
360372
let tokenProvider: EmulatorTokenProvider | undefined = undefined;
361373
if (repo.repoInfo_.nodeAdmin) {
362374
if (options.mockUserToken) {
@@ -374,7 +386,7 @@ export function connectDatabaseEmulator(
374386
}
375387

376388
// Modify the repo to apply emulator settings
377-
repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider);
389+
repoManagerApplyEmulatorSettings(repo, hostAndPort, options, tokenProvider);
378390
}
379391

380392
/**

packages/database/src/core/RepoInfo.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ import { LONG_POLLING, WEBSOCKET } from '../realtime/Constants';
2222
import { PersistentStorage } from './storage/storage';
2323
import { each } from './util/util';
2424

25+
export interface RepoInfoEmulatorOptions {
26+
mockUserToken?: string | EmulatorMockTokenOptions;
27+
};
28+
2529
/**
2630
* A class that holds metadata about a Repo object
2731
*/
2832
export class RepoInfo {
2933
private _host: string;
3034
private _domain: string;
31-
private _emulatorOptions: {
32-
mockUserToken?: EmulatorMockTokenOptions | string;
33-
};
3435
internalHost: string;
3536

3637
/**
@@ -49,11 +50,11 @@ export class RepoInfo {
4950
public readonly nodeAdmin: boolean = false,
5051
public readonly persistenceKey: string = '',
5152
public readonly includeNamespaceInQueryParams: boolean = false,
52-
public readonly isUsingEmulator: boolean = false
53+
public readonly isUsingEmulator: boolean = false,
54+
public readonly emulatorOptions: RepoInfoEmulatorOptions | null = null
5355
) {
5456
this._host = host.toLowerCase();
5557
this._domain = this._host.substr(this._host.indexOf('.') + 1);
56-
this._emulatorOptions = {};
5758
this.internalHost =
5859
(PersistentStorage.get('host:' + host) as string) || this._host;
5960
}
@@ -82,12 +83,6 @@ export class RepoInfo {
8283
}
8384
}
8485

85-
get emulatorOptions(): {
86-
mockUserToken?: EmulatorMockTokenOptions | string;
87-
} {
88-
return this._emulatorOptions;
89-
}
90-
9186
toString(): string {
9287
let str = this.toURLString();
9388
if (this.persistenceKey) {

0 commit comments

Comments
 (0)