11using System . Text ;
2+ using Newtonsoft . Json . Linq ;
23
34namespace PowerSync . Common . Client . Sync . Stream ;
45
@@ -107,12 +108,12 @@ public class StreamingSyncImplementation : EventStream<StreamingSyncImplementati
107108 private Task ? streamingSyncTask ;
108109 public Action TriggerCrudUpload { get ; }
109110 private Action ? notifyCompletedUploads ;
110-
111+
111112 private CancellationTokenSource ? crudUpdateCts ;
112113 private readonly ILogger logger ;
113114
114115 private readonly StreamingSyncLocks locks ;
115-
116+
116117
117118 public StreamingSyncImplementation ( StreamingSyncImplementationOptions options )
118119 {
@@ -286,7 +287,7 @@ protected async Task StreamingSync(CancellationToken? signal, PowerSyncConnectio
286287 catch ( Exception ex )
287288 {
288289 logger . LogError ( "Caught exception in streaming sync: {message}" , ex . Message ) ;
289-
290+ Console . WriteLine ( ex . StackTrace ) ;
290291 // Either:
291292 // - A network request failed with a failed connection or not OKAY response code.
292293 // - There was a sync processing error.
@@ -344,9 +345,9 @@ protected async Task<bool> StreamingSyncIteration(CancellationToken signal,
344345 {
345346 Params = options ? . Params ?? DEFAULT_STREAM_CONNECTION_OPTIONS . Params
346347 } ;
347-
348+
348349 await RustSyncIteration ( signal , resolvedOptions ) ;
349-
350+
350351 return true ;
351352 }
352353 } ) ;
@@ -638,15 +639,14 @@ private async Task RustSyncIteration(CancellationToken? signal, RequiredPowerSyn
638639 var nestedCts = new CancellationTokenSource ( ) ;
639640 signal ? . Register ( ( ) => { nestedCts . Cancel ( ) ; } ) ;
640641
641-
642+
642643 try
643644 {
644- notifyCompletedUploads = ( ) => {
645- Task . Run ( async ( ) => await Control ( "completed_upload" ) ) ;
646- } ;
647-
645+ notifyCompletedUploads = ( ) => { Task . Run ( async ( ) => await Control ( "completed_upload" ) ) ; } ;
646+
648647 await Control ( "start" , JsonConvert . SerializeObject ( resolvedOptions . Params ) ) ;
649- if ( receivingLines != null ) {
648+ if ( receivingLines != null )
649+ {
650650 await receivingLines ;
651651 }
652652 }
@@ -657,7 +657,7 @@ private async Task RustSyncIteration(CancellationToken? signal, RequiredPowerSyn
657657 }
658658
659659 return ;
660-
660+
661661 async Task Connect ( EstablishSyncStream instruction )
662662 {
663663 var syncOptions = new SyncStreamOptions
@@ -666,17 +666,16 @@ async Task Connect(EstablishSyncStream instruction)
666666 CancellationToken = nestedCts . Token ,
667667 Data = instruction . Request
668668 } ;
669-
670- using var stream = await Options . Remote . PostStreamRaw ( syncOptions ) ;
669+
670+ var stream = await Options . Remote . PostStreamRaw ( syncOptions ) ;
671671 using var reader = new StreamReader ( stream , Encoding . UTF8 ) ;
672672 string ? line ;
673673
674674 while ( ( line = await reader . ReadLineAsync ( ) ) != null )
675675 {
676676 logger . LogDebug ( "Parsing line for rust sync stream {message}" , line ) ;
677- await Control ( "line_binary " , line ) ;
677+ await Control ( "line_text " , line ) ;
678678 }
679-
680679 }
681680
682681 async Task Stop ( )
@@ -689,7 +688,7 @@ async Task Control(string op, object? payload = null)
689688 logger . LogDebug ( "Control call {message}" , op ) ;
690689
691690 var rawResponse = await Options . Adapter . Control ( op , payload ) ;
692- await HandleInstructions ( JsonConvert . DeserializeObject < Instruction [ ] > ( rawResponse ) ) ;
691+ await HandleInstructions ( Instruction . ParseInstructions ( rawResponse ) ) ;
693692 }
694693
695694 async Task HandleInstructions ( Instruction [ ] instructions )
@@ -699,10 +698,11 @@ async Task HandleInstructions(Instruction[] instructions)
699698 await HandleInstruction ( instruction ) ;
700699 }
701700 }
702-
701+
703702 DB . Crud . SyncPriorityStatus CoreStatusToSyncStatus ( SyncPriorityStatus status )
704703 {
705- logger . LogWarning ( "Sync status {status}" , status ? . LastSyncedAt != null ? new DateTime ( status ! . LastSyncedAt ) : null ) ;
704+ logger . LogWarning ( "Sync status {status}" ,
705+ status ? . LastSyncedAt != null ? new DateTime ( status ! . LastSyncedAt ) : null ) ;
706706 return new DB . Crud . SyncPriorityStatus
707707 {
708708 Priority = status . Priority ,
@@ -733,9 +733,10 @@ async Task HandleInstruction(Instruction instruction)
733733 break ;
734734 case UpdateSyncStatus syncStatus :
735735 var info = syncStatus . Status ;
736- var coreCompleteSync = info . PriorityStatus . FirstOrDefault ( s => s . Priority == SyncProgress . FULL_SYNC_PRIORITY ) ;
736+ var coreCompleteSync =
737+ info . PriorityStatus . FirstOrDefault ( s => s . Priority == SyncProgress . FULL_SYNC_PRIORITY ) ;
737738 var completeSync = coreCompleteSync != null ? CoreStatusToSyncStatus ( coreCompleteSync ) : null ;
738-
739+
739740 UpdateSyncStatus ( new SyncStatusOptions
740741 {
741742 Connected = info . Connected ,
@@ -753,21 +754,22 @@ async Task HandleInstruction(Instruction instruction)
753754 // TODO handle errors later?
754755 new UpdateSyncStatusOptions
755756 {
756- ClearDownloadError = true ,
757- ClearUploadError = true
757+ // ClearDownloadError = true,
758+ // ClearUploadError = true
758759 }
759760 ) ;
760-
761+
761762 break ;
762763 case EstablishSyncStream establishSyncStream :
763- if ( receivingLines != null ) {
764+ if ( receivingLines != null )
765+ {
764766 // Already connected, this shouldn't happen during a single iteration.
765767 throw new Exception ( "Unexpected request to establish sync stream, already connected" ) ;
766768 }
767-
769+
768770 receivingLines = Connect ( establishSyncStream ) ;
769771 break ;
770- case FetchCredentials { DidExpire : true , } :
772+ case FetchCredentials { DidExpire : true , } :
771773 Options . Remote . InvalidateCredentials ( ) ;
772774 break ;
773775 case FetchCredentials :
@@ -781,7 +783,7 @@ async Task HandleInstruction(Instruction instruction)
781783 break ;
782784 case DidCompleteSync :
783785 UpdateSyncStatus (
784- new SyncStatusOptions { } ,
786+ new SyncStatusOptions { } ,
785787 new UpdateSyncStatusOptions { ClearDownloadError = true } ) ;
786788 break ;
787789 }
@@ -929,7 +931,7 @@ protected void UpdateSyncStatus(SyncStatusOptions options, UpdateSyncStatusOptio
929931 ? null
930932 : options . DataFlow ? . UploadError ?? SyncStatus . DataFlowStatus . UploadError ,
931933 } ,
932- PriorityStatusEntries = options . PriorityStatusEntries ?? SyncStatus . PriorityStatusEntries
934+ PriorityStatusEntries = options . PriorityStatusEntries ?? SyncStatus . PriorityStatusEntries
933935 } ) ;
934936
935937 if ( ! SyncStatus . Equals ( updatedStatus ) )
0 commit comments