@@ -76,9 +76,6 @@ const MAX_PENDING_WRITES = 10;
76
76
* - pulling pending mutations from LocalStore and sending them to Datastore.
77
77
* - retrying mutations that failed because of network problems.
78
78
* - acking mutations to the SyncEngine once they are accepted or rejected.
79
- *
80
- * RemoteStore always starts out offline. A call to `enableNetwork()`
81
- * initializes the network connection.
82
79
*/
83
80
export class RemoteStore implements TargetMetadataProvider {
84
81
/**
@@ -111,19 +108,25 @@ export class RemoteStore implements TargetMetadataProvider {
111
108
*/
112
109
private listenTargets : { [ targetId : number ] : QueryData } = { } ;
113
110
114
- private networkEnabled = false ;
115
-
116
111
private watchStream : PersistentListenStream ;
117
112
private writeStream : PersistentWriteStream ;
118
113
private watchChangeAggregator : WatchChangeAggregator = null ;
119
114
120
115
/**
121
116
* Set to true by enableNetwork() and false by disableNetwork() and indicates
122
- * the user-preferred network state. A network connection is only established
123
- * if `networkAllowed` is true, the client is primary and there are
124
- * outstanding mutations or active listens.
117
+ * the user-preferred network state.
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 send over the
125
+ * existing stream. If the stream has not yet been established, it will be
126
+ * established if there are outstanding requests.
125
127
*/
126
- private networkAllowed = true ;
128
+ private canUseNetwork = false ;
129
+
127
130
private isPrimary = false ;
128
131
129
132
private onlineStateTracker : OnlineStateTracker ;
@@ -165,60 +168,28 @@ export class RemoteStore implements TargetMetadataProvider {
165
168
* Starts up the remote store, creating streams, restoring state from
166
169
* LocalStore, etc.
167
170
*/
168
- < << << << HEAD
169
171
start ( ) : Promise < void > {
170
172
// Start is a no-op for RemoteStore.
171
173
return Promise . resolve ( ) ;
172
174
}
173
175
174
- private isNetworkEnabled ( ) : boolean {
175
- assert (
176
- ( this . watchStream == null ) === ( this . writeStream == null ) ,
177
- 'WatchStream and WriteStream should both be null or non-null'
178
- ) ;
179
- return this . watchStream != null ;
180
- = === ===
181
- async start ( ) : Promise < void > {
182
- await this . enableNetwork ( ) ;
183
- > >>> >>> master
184
- }
185
-
186
176
/** Re-enables the network. Idempotent. */
187
177
async enableNetwork ( ) : Promise < void > {
188
- << < < < << HEAD
189
- this . networkAllowed = true ;
178
+ this . networkEnabled = true ;
190
179
191
- if ( this . isPrimary ) {
192
- if ( this . isNetworkEnabled ( ) ) {
193
- return ;
194
- }
180
+ if ( this . isPrimary && ! this . canUseNetwork ) {
181
+ this . canUseNetwork = true ;
195
182
196
- // Create new streams (but note they're not started yet).
197
- this . watchStream = this . datastore . newPersistentWatchStream ( ) ;
198
- this . writeStream = this . datastore . newPersistentWriteStream ( ) ;
199
- = === ===
200
- if ( ! this . networkEnabled ) {
201
- this . networkEnabled = true ;
202
183
this . writeStream . lastStreamToken = await this . localStore . getLastStreamToken ( ) ;
203
- > >>> >>> master
204
-
205
- // Load any saved stream token from persistent storage
206
- return this . localStore . getLastStreamToken ( ) . then ( token => {
207
- this . writeStream . lastStreamToken = token ;
208
184
209
- < < < << << HEAD
210
- if ( this . shouldStartWatchStream ( ) ) {
211
- this . startWatchStream ( ) ;
212
- } else {
213
- this. onlineStateTracker . set ( OnlineState . Unknown ) ;
214
- }
185
+ if ( this . shouldStartWatchStream ( ) ) {
186
+ this . startWatchStream ( ) ;
187
+ } else {
188
+ this . onlineStateTracker . set ( OnlineState . Unknown ) ;
189
+ }
215
190
216
- return this . fillWritePipeline ( ) ; // This may start the writeStream.
217
- } ) ;
218
- === === =
219
191
// This will start the write stream if necessary.
220
192
await this . fillWritePipeline ( ) ;
221
- > >>> >>> master
222
193
}
223
194
}
224
195
@@ -227,21 +198,16 @@ export class RemoteStore implements TargetMetadataProvider {
227
198
* enableNetwork().
228
199
*/
229
200
async disableNetwork ( ) : Promise < void > {
230
- << < < < << HEAD
231
- this . networkAllowed = false ;
232
-
201
+ this . networkEnabled = false ;
233
202
this . disableNetworkInternal ( ) ;
234
- = === ===
235
- await this . disableNetworkInternal ( ) ;
236
203
237
- > >>> >>> master
238
204
// Set the OnlineState to Offline so get()s return from cache, etc.
239
205
this . onlineStateTracker . set ( OnlineState . Offline ) ;
240
206
}
241
207
242
- private async disableNetworkInternal ( ) : Promise < void > {
243
- if ( this . networkEnabled ) {
244
- this . networkEnabled = false ;
208
+ private disableNetworkInternal ( ) : void {
209
+ if ( this . canUseNetwork ) {
210
+ this . canUseNetwork = false ;
245
211
246
212
this . writeStream . stop ( ) ;
247
213
this . watchStream . stop ( ) ;
@@ -348,18 +314,12 @@ export class RemoteStore implements TargetMetadataProvider {
348
314
*/
349
315
private shouldStartWatchStream ( ) : boolean {
350
316
return (
351
- this . canUseNetwork ( ) &&
317
+ this . canUseNetwork &&
352
318
! this . watchStream . isStarted ( ) &&
353
319
! objUtils . isEmpty ( this . listenTargets )
354
320
) ;
355
321
}
356
322
357
- private canUseNetwork ( ) : boolean {
358
- // TODO(mikelehen): This could take into account isPrimary when we merge
359
- // with multitab.
360
- return this . networkEnabled ;
361
- }
362
-
363
323
private cleanUpWatchStreamState ( ) : void {
364
324
this . watchChangeAggregator = null ;
365
325
}
@@ -555,7 +515,7 @@ export class RemoteStore implements TargetMetadataProvider {
555
515
*/
556
516
private canAddToWritePipeline ( ) : boolean {
557
517
return (
558
- this . networkEnabled && this . writePipeline . length < MAX_PENDING_WRITES
518
+ this . canUseNetwork && this . writePipeline . length < MAX_PENDING_WRITES
559
519
) ;
560
520
}
561
521
@@ -582,7 +542,7 @@ export class RemoteStore implements TargetMetadataProvider {
582
542
583
543
private shouldStartWriteStream ( ) : boolean {
584
544
return (
585
- this . canUseNetwork ( ) &&
545
+ this . canUseNetwork &&
586
546
! this . writeStream . isStarted ( ) &&
587
547
this . writePipeline . length > 0
588
548
) ;
@@ -755,9 +715,9 @@ export class RemoteStore implements TargetMetadataProvider {
755
715
async applyPrimaryState ( isPrimary : boolean ) : Promise < void > {
756
716
this . isPrimary = isPrimary ;
757
717
758
- if ( isPrimary && this . networkAllowed ) {
718
+ if ( isPrimary && this . networkEnabled ) {
759
719
await this . enableNetwork ( ) ;
760
- } else if ( ! isPrimary && this . isNetworkEnabled ( ) ) {
720
+ } else if ( ! isPrimary ) {
761
721
this . disableNetworkInternal ( ) ;
762
722
this . onlineStateTracker . set ( OnlineState . Unknown ) ;
763
723
}
0 commit comments