Skip to content

Commit 7ad7827

Browse files
committed
Update to modify repo in place
1 parent 5cd2930 commit 7ad7827

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

packages/database/src/api/Database.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class Database implements FirebaseService {
3636
INTERNAL: DatabaseInternals;
3737

3838
/** Track if the instance has been used (root or repo accessed) */
39-
private instanceUsed_: boolean = false;
39+
private instanceStarted_: boolean = false;
4040

4141
/** Backing state for root_ */
4242
private rootInternal_: Reference;
@@ -70,20 +70,19 @@ export class Database implements FirebaseService {
7070
}
7171

7272
private get repo_(): Repo {
73-
this.instanceUsed_ = true;
73+
this.instanceStarted_ = true;
7474
return this.repoInternal_;
7575
}
7676

7777
private set repo_(repo: Repo) {
78-
if (repo instanceof Repo) {
79-
this.root_ = new Reference(repo, Path.Empty);
80-
}
81-
8278
this.repoInternal_ = repo;
8379
}
8480

8581
get root_(): Reference {
86-
this.instanceUsed_ = true;
82+
if (!this.rootInternal_) {
83+
this.rootInternal_ = new Reference(this.repo_, Path.Empty);
84+
}
85+
8786
return this.rootInternal_;
8887
}
8988

@@ -104,19 +103,15 @@ export class Database implements FirebaseService {
104103
* @param port the emulator port (ex: 8080)
105104
*/
106105
useEmulator(host: string, port: number): void {
107-
if (this.instanceUsed_) {
106+
if (this.instanceStarted_) {
108107
fatal(
109108
'Cannot call useEmulator() after instance has already been initialized.'
110109
);
111110
return;
112111
}
113112

114-
// Get a new Repo which has the emulator settings applied
115-
const manager = RepoManager.getInstance();
116-
const oldRepo = this.repo_;
117-
118-
this.repo_ = manager.cloneRepoForEmulator(oldRepo, host, port);
119-
manager.deleteRepo(oldRepo);
113+
// Modify the repo to apply emulator settings
114+
RepoManager.getInstance().applyEmulatorSettings(this.repoInternal_, host, port);
120115
}
121116

122117
/**

packages/database/src/core/RepoManager.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,27 @@ export class RepoManager {
8888
}
8989

9090
/**
91-
* Create a new repo based on an old one but pointing to a particular host and port.
91+
* Update an existing repo in place to point to a new host/port.
9292
*/
93-
cloneRepoForEmulator(repo: Repo, host: string, port: number): Repo {
94-
const nodeAdmin = repo.repoInfo_.nodeAdmin;
93+
applyEmulatorSettings(repo: Repo, host: string, port: number): void {
9594
const url = `http://${host}:${port}?ns=${repo.repoInfo_.namespace}`;
95+
96+
const nodeAdmin = repo.repoInfo_.nodeAdmin;
9697
const authTokenProvider = nodeAdmin
9798
? new EmulatorAdminTokenProvider()
9899
: repo.authTokenProvider;
99100

100-
const parsedUrl = parseRepoInfo(url, nodeAdmin);
101+
// Before we modify the repo, get the key used in the repo manager
102+
const oldRepoKey = repo.repoInfo_.toURLString();
103+
104+
// Update the repo in-place
105+
const { repoInfo } = parseRepoInfo(url, nodeAdmin);
106+
repo.repoInfo_ = repoInfo;
107+
repo.authTokenProvider = authTokenProvider;
101108

102-
return this.createRepo(parsedUrl.repoInfo, repo.app, authTokenProvider);
109+
// Replace the repomanager cache entry
110+
delete this.repos_[repo.app.name][oldRepoKey];
111+
this.repos_[repo.app.name][repoInfo.toURLString()] = repo;
103112
}
104113

105114
/**

0 commit comments

Comments
 (0)