@@ -32,8 +32,19 @@ use super::{
3232 Checksum ,
3333} ;
3434
35+ /// The sync client implementation, responsible for parsing lines received by the sync service and
36+ /// persisting them to the database.
37+ ///
38+ /// The client consumes no resources and prepares no statements until a sync iteration is
39+ /// initialized.
3540pub struct SyncClient {
3641 db : * mut sqlite:: sqlite3 ,
42+ /// The current [ClientState] (essentially an optional [StreamingSyncIteration]).
43+ ///
44+ /// This is guarded behind a mutex so that we can mutate the state without forcing callers to
45+ /// obtain a mutable reference to the [SyncClient] itself. It doesn't mean much in practice
46+ /// because it's impossible to run two `powersync_control` calls on the same database connection
47+ /// concurrently.
3748 state : Mutex < ClientState > ,
3849}
3950
@@ -91,7 +102,9 @@ impl SyncClient {
91102}
92103
93104enum ClientState {
105+ /// No sync iteration is currently active.
94106 Idle ,
107+ /// A sync iteration has begun on the database.
95108 IterationActive ( SyncIterationHandle ) ,
96109}
97110
@@ -108,11 +121,19 @@ impl ClientState {
108121 }
109122}
110123
124+ /// A handle that allows progressing a [StreamingSyncIteration].
125+ ///
126+ /// The sync itertion itself is implemented as an `async` function, as this allows us to treat it
127+ /// as a coroutine that preserves internal state between multiple `powersync_control` invocations.
128+ /// At each invocation, the future is polled once (and gets access to context that allows it to
129+ /// render [Instruction]s to return from the function).
111130struct SyncIterationHandle {
112131 future : Pin < Box < dyn Future < Output = Result < ( ) , SQLiteError > > > > ,
113132}
114133
115134impl SyncIterationHandle {
135+ /// Creates a new sync iteration in a pending state by preparing statements for
136+ /// [StorageAdapter] and setting up the initial downloading state for [StorageAdapter] .
116137 fn new (
117138 db : * mut sqlite:: sqlite3 ,
118139 parameters : Option < serde_json:: Map < String , serde_json:: Value > > ,
@@ -128,6 +149,8 @@ impl SyncIterationHandle {
128149 Ok ( Self { future } )
129150 }
130151
152+ /// Forwards a [SyncEvent::Initialize] to the current sync iteration, returning the initial
153+ /// instructions generated.
131154 fn initialize ( & mut self ) -> Result < Vec < Instruction > , SQLiteError > {
132155 let mut event = ActiveEvent :: new ( SyncEvent :: Initialize ) ;
133156 let result = self . run ( & mut event) ?;
@@ -160,9 +183,12 @@ impl SyncIterationHandle {
160183 }
161184}
162185
186+ /// A [SyncEvent] currently being handled by a [StreamingSyncIteration].
163187struct ActiveEvent < ' a > {
164188 handled : bool ,
189+ /// The event to handle
165190 event : SyncEvent < ' a > ,
191+ /// Instructions to forward to the client when the `powersync_control` invocation completes.
166192 instructions : Vec < Instruction > ,
167193}
168194
@@ -420,6 +446,10 @@ impl StreamingSyncIteration {
420446 ) ?)
421447 }
422448
449+ /// Prepares a sync iteration by handling the initial [SyncEvent::Initialize].
450+ ///
451+ /// This prepares a [StreamingSyncRequest] by fetching local sync state and the requested bucket
452+ /// parameters.
423453 async fn prepare_request ( & mut self ) -> Result < Vec < String > , SQLiteError > {
424454 let event = Self :: receive_event ( ) . await ;
425455 let SyncEvent :: Initialize = event. event else {
@@ -484,6 +514,10 @@ impl SyncTarget {
484514 }
485515 }
486516
517+ /// Starts tracking the received `Checkpoint`.
518+ ///
519+ /// This updates the internal state and returns a set of buckets to delete because they've been
520+ /// tracked locally but not in the new checkpoint.
487521 fn track_checkpoint < ' a > ( & mut self , checkpoint : & Checkpoint < ' a > ) -> BTreeSet < String > {
488522 let mut to_delete: BTreeSet < String > = match & self {
489523 SyncTarget :: Tracking ( checkpoint) => checkpoint. buckets . keys ( ) . cloned ( ) . collect ( ) ,
0 commit comments