@@ -35,7 +35,7 @@ class StreamingSyncImplementation implements StreamingSync {
35
35
final InternalConnector connector;
36
36
final ResolvedSyncOptions options;
37
37
38
- final Logger logger = isolateLogger ;
38
+ final Logger logger;
39
39
40
40
final Stream <void > crudUpdateTriggerStream;
41
41
@@ -68,14 +68,16 @@ class StreamingSyncImplementation implements StreamingSync {
68
68
required http.Client client,
69
69
Mutex ? syncMutex,
70
70
Mutex ? crudMutex,
71
+ Logger ? logger,
71
72
72
73
/// A unique identifier for this streaming sync implementation
73
74
/// A good value is typically the DB file path which it will mutate when syncing.
74
75
String ? identifier = "unknown" ,
75
76
}) : _client = client,
76
77
syncMutex = syncMutex ?? Mutex (identifier: "sync-$identifier " ),
77
78
crudMutex = crudMutex ?? Mutex (identifier: "crud-$identifier " ),
78
- _userAgentHeaders = userAgentHeaders ();
79
+ _userAgentHeaders = userAgentHeaders (),
80
+ logger = logger ?? isolateLogger;
79
81
80
82
Duration get _retryDelay => options.retryDelay;
81
83
@@ -122,6 +124,7 @@ class StreamingSyncImplementation implements StreamingSync {
122
124
@override
123
125
Future <void > streamingSync () async {
124
126
try {
127
+ assert (_abort == null );
125
128
_abort = AbortController ();
126
129
clientId = await adapter.getClientId ();
127
130
_crudLoop ();
@@ -310,7 +313,7 @@ class StreamingSyncImplementation implements StreamingSync {
310
313
var merged = addBroadcast (requestStream, _nonLineSyncEvents.stream);
311
314
312
315
Future <void >? credentialsInvalidation;
313
- bool haveInvalidated = false ;
316
+ bool shouldStopIteration = false ;
314
317
315
318
// Trigger a CRUD upload on reconnect
316
319
_internalCrudTriggerController.add (null );
@@ -336,6 +339,7 @@ class StreamingSyncImplementation implements StreamingSync {
336
339
case StreamingSyncCheckpointComplete ():
337
340
final result = await _applyCheckpoint (targetCheckpoint! , _abort);
338
341
if (result.abort) {
342
+ shouldStopIteration = true ;
339
343
return ;
340
344
}
341
345
case StreamingSyncCheckpointPartiallyComplete (: final bucketPriority):
@@ -345,6 +349,7 @@ class StreamingSyncImplementation implements StreamingSync {
345
349
// This means checksums failed. Start again with a new checkpoint.
346
350
// TODO: better back-off
347
351
// await new Promise((resolve) => setTimeout(resolve, 50));
352
+ shouldStopIteration = true ;
348
353
return ;
349
354
} else if (! result.ready) {
350
355
// If we have pending uploads, we can't complete new checkpoints
@@ -398,13 +403,14 @@ class StreamingSyncImplementation implements StreamingSync {
398
403
if (tokenExpiresIn == 0 ) {
399
404
// Token expired already - stop the connection immediately
400
405
connector.prefetchCredentials (invalidate: true ).ignore ();
406
+ shouldStopIteration = true ;
401
407
break ;
402
408
} else if (tokenExpiresIn <= 30 ) {
403
409
// Token expires soon - refresh it in the background
404
410
credentialsInvalidation ?? =
405
411
connector.prefetchCredentials ().then ((_) {
406
412
// Token has been refreshed - we should restart the connection.
407
- haveInvalidated = true ;
413
+ shouldStopIteration = true ;
408
414
// trigger next loop iteration ASAP, don't wait for another
409
415
// message from the server.
410
416
if (! aborted) {
@@ -421,7 +427,7 @@ class StreamingSyncImplementation implements StreamingSync {
421
427
}
422
428
423
429
await for (var line in merged) {
424
- if (aborted || haveInvalidated ) {
430
+ if (aborted || shouldStopIteration ) {
425
431
break ;
426
432
}
427
433
@@ -434,10 +440,10 @@ class StreamingSyncImplementation implements StreamingSync {
434
440
break ;
435
441
case TokenRefreshComplete ():
436
442
// We have a new token, so stop the iteration.
437
- haveInvalidated = true ;
443
+ shouldStopIteration = true ;
438
444
}
439
445
440
- if (haveInvalidated ) {
446
+ if (shouldStopIteration ) {
441
447
// Stop this connection, so that a new one will be started
442
448
break ;
443
449
}
0 commit comments