Skip to content

Commit bfbdaac

Browse files
Lazy-initialize Auth provider
1 parent a3883c5 commit bfbdaac

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

packages/database/src/api/Database.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ export class Database implements FirebaseService {
7474
};
7575

7676
private get repo_(): Repo {
77-
this.instanceStarted_ = true;
77+
if (!this.instanceStarted_) {
78+
this.repoInternal_.start();
79+
this.instanceStarted_ = true;
80+
}
7881
return this.repoInternal_;
7982
}
8083

packages/database/src/core/Repo.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,28 @@ export class Repo {
8484

8585
constructor(
8686
public repoInfo_: RepoInfo,
87-
forceRestClient: boolean,
87+
private forceRestClient_: boolean,
8888
public app: FirebaseApp,
89-
authTokenProvider: AuthTokenProvider
89+
public authTokenProvider_: AuthTokenProvider
9090
) {
91-
this.stats_ = StatsManager.getCollection(repoInfo_);
91+
// This key is intentionally not updated if RepoInfo is later changed or replaced
92+
this.key = this.repoInfo_.toURLString();
93+
}
94+
95+
start(): void {
96+
this.stats_ = StatsManager.getCollection(this.repoInfo_);
9297

93-
if (forceRestClient || beingCrawled()) {
98+
if (this.forceRestClient_ || beingCrawled()) {
9499
this.server_ = new ReadonlyRestClient(
95100
this.repoInfo_,
96101
this.onDataUpdate_.bind(this),
97-
authTokenProvider
102+
this.authTokenProvider_
98103
);
99104

100105
// Minor hack: Fire onConnect immediately, since there's no actual connection.
101106
setTimeout(this.onConnectStatus_.bind(this, true), 0);
102107
} else {
103-
const authOverride = app.options['databaseAuthVariableOverride'];
108+
const authOverride = this.app.options['databaseAuthVariableOverride'];
104109
// Validate authOverride
105110
if (typeof authOverride !== 'undefined' && authOverride !== null) {
106111
if (typeof authOverride !== 'object') {
@@ -117,25 +122,25 @@ export class Repo {
117122

118123
this.persistentConnection_ = new PersistentConnection(
119124
this.repoInfo_,
120-
app.options.appId,
125+
this.app.options.appId,
121126
this.onDataUpdate_.bind(this),
122127
this.onConnectStatus_.bind(this),
123128
this.onServerInfoUpdate_.bind(this),
124-
authTokenProvider,
129+
this.authTokenProvider_,
125130
authOverride
126131
);
127132

128133
this.server_ = this.persistentConnection_;
129134
}
130135

131-
authTokenProvider.addTokenChangeListener(token => {
136+
this.authTokenProvider_.addTokenChangeListener(token => {
132137
this.server_.refreshAuthToken(token);
133138
});
134139

135140
// In the case of multiple Repos for the same repoInfo (i.e. there are multiple Firebase.Contexts being used),
136141
// we only want to create one StatsReporter. As such, we'll report stats over the first Repo created.
137142
this.statsReporter_ = StatsManager.getOrCreateReporter(
138-
repoInfo_,
143+
this.repoInfo_,
139144
() => new StatsReporter(this.stats_, this.server_)
140145
);
141146

@@ -177,9 +182,6 @@ export class Repo {
177182
this.server_.unlisten(query, tag);
178183
}
179184
});
180-
181-
// This key is intentionally not updated if RepoInfo is later changed or replaced
182-
this.key = this.repoInfo_.toURLString();
183185
}
184186

185187
/**

packages/database/src/core/RepoManager.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ export class RepoManager {
102102
);
103103

104104
if (repo.repoInfo_.nodeAdmin) {
105-
// TODO(samtstern): We need to re-run the initialization of the
106-
// `authTokenProvider` related code in the RepoInfo constructor
107-
repo.authTokenProvider = new EmulatorAdminTokenProvider();
105+
repo.authTokenProvider_ = new EmulatorAdminTokenProvider();
108106
}
109107
}
110108

0 commit comments

Comments
 (0)