@@ -116,16 +116,7 @@ export class RemoteStore implements TargetMetadataProvider {
116
116
* Set to true by enableNetwork() and false by disableNetwork() and indicates
117
117
* the user-preferred network state.
118
118
*/
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 ;
129
120
130
121
private isPrimary = false ;
131
122
@@ -169,17 +160,14 @@ export class RemoteStore implements TargetMetadataProvider {
169
160
* LocalStore, etc.
170
161
*/
171
162
start ( ) : Promise < void > {
172
- // Start is a no-op for RemoteStore.
173
- return Promise . resolve ( ) ;
163
+ return this . enableNetwork ( ) ;
174
164
}
175
165
176
166
/** Re-enables the network. Idempotent. */
177
167
async enableNetwork ( ) : Promise < void > {
178
168
this . networkEnabled = true ;
179
169
180
- if ( this . isPrimary && ! this . canUseNetwork ) {
181
- this . canUseNetwork = true ;
182
-
170
+ if ( this . canUseNetwork ( ) ) {
183
171
this . writeStream . lastStreamToken = await this . localStore . getLastStreamToken ( ) ;
184
172
185
173
if ( this . shouldStartWatchStream ( ) ) {
@@ -206,28 +194,23 @@ export class RemoteStore implements TargetMetadataProvider {
206
194
}
207
195
208
196
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 ( ) ;
224
199
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 = [ ] ;
226
206
}
207
+
208
+ this . cleanUpWatchStreamState ( ) ;
227
209
}
228
210
229
211
shutdown ( ) : Promise < void > {
230
212
log . debug ( LOG_TAG , 'RemoteStore shutting down.' ) ;
213
+ this . networkEnabled = false ;
231
214
this . disableNetworkInternal ( ) ;
232
215
233
216
// Set the OnlineState to Unknown (rather than Offline) to avoid potentially
@@ -314,12 +297,16 @@ export class RemoteStore implements TargetMetadataProvider {
314
297
*/
315
298
private shouldStartWatchStream ( ) : boolean {
316
299
return (
317
- this . canUseNetwork &&
300
+ this . canUseNetwork ( ) &&
318
301
! this . watchStream . isStarted ( ) &&
319
302
! objUtils . isEmpty ( this . listenTargets )
320
303
) ;
321
304
}
322
305
306
+ private canUseNetwork ( ) : boolean {
307
+ return this . isPrimary && this . networkEnabled ;
308
+ }
309
+
323
310
private cleanUpWatchStreamState ( ) : void {
324
311
this . watchChangeAggregator = null ;
325
312
}
@@ -514,7 +501,9 @@ export class RemoteStore implements TargetMetadataProvider {
514
501
* enabled and the write pipeline is not full).
515
502
*/
516
503
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
+ ) ;
518
507
}
519
508
520
509
// For testing
@@ -540,7 +529,7 @@ export class RemoteStore implements TargetMetadataProvider {
540
529
541
530
private shouldStartWriteStream ( ) : boolean {
542
531
return (
543
- this . canUseNetwork &&
532
+ this . canUseNetwork ( ) &&
544
533
! this . writeStream . isStarted ( ) &&
545
534
this . writePipeline . length > 0
546
535
) ;
@@ -697,10 +686,11 @@ export class RemoteStore implements TargetMetadataProvider {
697
686
async handleUserChange ( user : User ) : Promise < void > {
698
687
log . debug ( LOG_TAG , 'RemoteStore changing users: uid=' , user . uid ) ;
699
688
700
- if ( this . networkEnabled ) {
689
+ if ( this . canUseNetwork ( ) ) {
701
690
// Tear down and re-create our network streams. This will ensure we get a fresh auth token
702
691
// for the new user and re-fill the write pipeline with new mutations from the LocalStore
703
692
// (since mutations are per-user).
693
+ this . networkEnabled = false ;
704
694
this . disableNetworkInternal ( ) ;
705
695
this . onlineStateTracker . set ( OnlineState . Unknown ) ;
706
696
await this . enableNetwork ( ) ;
0 commit comments