@@ -4,7 +4,6 @@ import 'dart:typed_data';
4
4
5
5
import 'package:http/http.dart' as http;
6
6
import 'package:logging/logging.dart' ;
7
- import 'package:logging/logging.dart' ;
8
7
import 'package:meta/meta.dart' ;
9
8
import 'package:powersync_core/src/abort_controller.dart' ;
10
9
import 'package:powersync_core/src/exceptions.dart' ;
@@ -444,6 +443,7 @@ class StreamingSyncImplementation implements StreamingSync {
444
443
case UploadCompleted ():
445
444
// Only relevant for the Rust sync implementation.
446
445
break ;
446
+ case AbortCurrentIteration ():
447
447
case TokenRefreshComplete ():
448
448
// We have a new token, so stop the iteration.
449
449
haveInvalidated = true ;
@@ -579,6 +579,7 @@ typedef BucketDescription = ({
579
579
580
580
final class _ActiveRustStreamingIteration {
581
581
final StreamingSyncImplementation sync ;
582
+ var _isActive = true ;
582
583
583
584
StreamSubscription <void >? _completedUploads;
584
585
final Completer <void > _completedStream = Completer ();
@@ -591,6 +592,7 @@ final class _ActiveRustStreamingIteration {
591
592
assert (_completedStream.isCompleted, 'Should have started streaming' );
592
593
await _completedStream.future;
593
594
} finally {
595
+ _isActive = true ;
594
596
_completedUploads? .cancel ();
595
597
await _stop ();
596
598
}
@@ -604,7 +606,7 @@ final class _ActiveRustStreamingIteration {
604
606
final events = addBroadcast (
605
607
_receiveLines (request.request), sync ._nonLineSyncEvents.stream);
606
608
607
- listen :
609
+ loop :
608
610
await for (final event in events) {
609
611
switch (event) {
610
612
case ReceivedLine (line: final Uint8List line):
@@ -613,10 +615,10 @@ final class _ActiveRustStreamingIteration {
613
615
await _control ('line_text' , line);
614
616
case UploadCompleted ():
615
617
await _control ('completed_upload' );
618
+ case AbortCurrentIteration ():
619
+ break loop;
616
620
case TokenRefreshComplete ():
617
621
await _control ('refreshed_token' );
618
- case AbortRequested ():
619
- break listen;
620
622
}
621
623
}
622
624
}
@@ -647,11 +649,20 @@ final class _ActiveRustStreamingIteration {
647
649
_completedStream.complete (_handleLines (instruction));
648
650
case UpdateSyncStatus (: final status):
649
651
sync ._state.updateStatus ((m) => m.applyFromCore (status));
650
- case FetchCredentials ():
651
- // TODO: Handle this case.
652
- throw UnimplementedError ();
652
+ case FetchCredentials (: final didExpire):
653
+ if (didExpire) {
654
+ await sync .connector.prefetchCredentials (invalidate: true );
655
+ } else {
656
+ sync .connector.prefetchCredentials ().then ((_) {
657
+ if (_isActive && ! sync .aborted) {
658
+ sync ._nonLineSyncEvents.add (const TokenRefreshComplete ());
659
+ }
660
+ }, onError: (Object e, StackTrace s) {
661
+ sync .logger.warning ('Could not prefetch credentials' , e, s);
662
+ });
663
+ }
653
664
case CloseSyncStream ():
654
- sync ._nonLineSyncEvents.add (AbortRequested ());
665
+ sync ._nonLineSyncEvents.add (const AbortCurrentIteration ());
655
666
case FlushFileSystem ():
656
667
await sync .adapter.flushFileSystem ();
657
668
case DidCompleteSync ():
@@ -677,3 +688,7 @@ final class UploadCompleted implements SyncEvent {
677
688
final class TokenRefreshComplete implements SyncEvent {
678
689
const TokenRefreshComplete ();
679
690
}
691
+
692
+ final class AbortCurrentIteration implements SyncEvent {
693
+ const AbortCurrentIteration ();
694
+ }
0 commit comments