Skip to content

Commit 38e46a4

Browse files
mikelehenschmidt-sebastian
authored andcommitted
Remove canUseNetwork state. (#1076)
1 parent 8556bcc commit 38e46a4

File tree

1 file changed

+25
-35
lines changed

1 file changed

+25
-35
lines changed

packages/firestore/src/remote/remote_store.ts

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,7 @@ export class RemoteStore implements TargetMetadataProvider {
116116
* Set to true by enableNetwork() and false by disableNetwork() and indicates
117117
* the user-preferred network state.
118118
*/
119-
private networkEnabled = true;
120-
121-
/**
122-
* Indicates whether the network can accept requests (as determined by both
123-
* the `isPrimary` flag and the user specified `networkEnabled` flag). If
124-
* the connection is already established, new requests will be sent over the
125-
* existing stream. If the stream has not yet been established, it will be
126-
* established if there are outstanding requests.
127-
*/
128-
private canUseNetwork = false;
119+
private networkEnabled = false;
129120

130121
private isPrimary = false;
131122

@@ -169,17 +160,14 @@ export class RemoteStore implements TargetMetadataProvider {
169160
* LocalStore, etc.
170161
*/
171162
start(): Promise<void> {
172-
// Start is a no-op for RemoteStore.
173-
return Promise.resolve();
163+
return this.enableNetwork();
174164
}
175165

176166
/** Re-enables the network. Idempotent. */
177167
async enableNetwork(): Promise<void> {
178168
this.networkEnabled = true;
179169

180-
if (this.isPrimary && !this.canUseNetwork) {
181-
this.canUseNetwork = true;
182-
170+
if (this.canUseNetwork()) {
183171
this.writeStream.lastStreamToken = await this.localStore.getLastStreamToken();
184172

185173
if (this.shouldStartWatchStream()) {
@@ -206,28 +194,23 @@ export class RemoteStore implements TargetMetadataProvider {
206194
}
207195

208196
private disableNetworkInternal(): void {
209-
if (this.canUseNetwork) {
210-
this.canUseNetwork = false;
211-
212-
this.writeStream.stop();
213-
this.watchStream.stop();
214-
215-
if (this.writePipeline.length > 0) {
216-
log.debug(
217-
LOG_TAG,
218-
`Stopping write stream with ${
219-
this.writePipeline.length
220-
} pending writes`
221-
);
222-
this.writePipeline = [];
223-
}
197+
this.writeStream.stop();
198+
this.watchStream.stop();
224199

225-
this.cleanUpWatchStreamState();
200+
if (this.writePipeline.length > 0) {
201+
log.debug(
202+
LOG_TAG,
203+
`Stopping write stream with ${this.writePipeline.length} pending writes`
204+
);
205+
this.writePipeline = [];
226206
}
207+
208+
this.cleanUpWatchStreamState();
227209
}
228210

229211
shutdown(): Promise<void> {
230212
log.debug(LOG_TAG, 'RemoteStore shutting down.');
213+
this.networkEnabled = false;
231214
this.disableNetworkInternal();
232215

233216
// Set the OnlineState to Unknown (rather than Offline) to avoid potentially
@@ -314,12 +297,16 @@ export class RemoteStore implements TargetMetadataProvider {
314297
*/
315298
private shouldStartWatchStream(): boolean {
316299
return (
317-
this.canUseNetwork &&
300+
this.canUseNetwork() &&
318301
!this.watchStream.isStarted() &&
319302
!objUtils.isEmpty(this.listenTargets)
320303
);
321304
}
322305

306+
private canUseNetwork(): boolean {
307+
return this.isPrimary && this.networkEnabled;
308+
}
309+
323310
private cleanUpWatchStreamState(): void {
324311
this.watchChangeAggregator = null;
325312
}
@@ -514,7 +501,9 @@ export class RemoteStore implements TargetMetadataProvider {
514501
* enabled and the write pipeline is not full).
515502
*/
516503
private canAddToWritePipeline(): boolean {
517-
return this.canUseNetwork && this.writePipeline.length < MAX_PENDING_WRITES;
504+
return (
505+
this.canUseNetwork() && this.writePipeline.length < MAX_PENDING_WRITES
506+
);
518507
}
519508

520509
// For testing
@@ -540,7 +529,7 @@ export class RemoteStore implements TargetMetadataProvider {
540529

541530
private shouldStartWriteStream(): boolean {
542531
return (
543-
this.canUseNetwork &&
532+
this.canUseNetwork() &&
544533
!this.writeStream.isStarted() &&
545534
this.writePipeline.length > 0
546535
);
@@ -697,10 +686,11 @@ export class RemoteStore implements TargetMetadataProvider {
697686
async handleUserChange(user: User): Promise<void> {
698687
log.debug(LOG_TAG, 'RemoteStore changing users: uid=', user.uid);
699688

700-
if (this.networkEnabled) {
689+
if (this.canUseNetwork()) {
701690
// Tear down and re-create our network streams. This will ensure we get a fresh auth token
702691
// for the new user and re-fill the write pipeline with new mutations from the LocalStore
703692
// (since mutations are per-user).
693+
this.networkEnabled = false;
704694
this.disableNetworkInternal();
705695
this.onlineStateTracker.set(OnlineState.Unknown);
706696
await this.enableNetwork();

0 commit comments

Comments
 (0)