@@ -128,55 +128,61 @@ export function powerSyncCollectionOptions<
128128 * "sync"
129129 * Notice that this describes the Sync between the local SQLite table
130130 * and the in-memory tanstack-db collection.
131- * It is not about sync between a client and a server!
132131 */
133132 const sync : SyncConfig < T , string > = {
134133 sync : ( params ) => {
135134 const { begin, write, commit, markReady } = params
136- // Manually create a tracking operation for optimization purposes
137135 const abortController = new AbortController ( )
138136
139137 // The sync function needs to be synchronous
140138 async function start ( ) {
139+ database . logger . info ( `Sync is starting` )
141140 database . onChangeWithCallback (
142141 {
143142 onChange : async ( ) => {
144- await database . writeTransaction ( async ( context ) => {
145- begin ( )
146- const operations = await context . getAll < TriggerDiffRecord > (
147- `SELECT * FROM ${ trackedTableName } ORDER BY timestamp ASC`
148- )
149- const pendingOperations : Array < PendingOperation > = [ ]
143+ await database
144+ . writeTransaction ( async ( context ) => {
145+ begin ( )
146+ const operations = await context . getAll < TriggerDiffRecord > (
147+ `SELECT * FROM ${ trackedTableName } ORDER BY timestamp ASC`
148+ )
149+ const pendingOperations : Array < PendingOperation > = [ ]
150150
151- for ( const op of operations ) {
152- const { id, operation, timestamp, value } = op
153- const parsedValue = {
154- id,
155- ...JSON . parse ( value ) ,
151+ for ( const op of operations ) {
152+ const { id, operation, timestamp, value } = op
153+ const parsedValue = {
154+ id,
155+ ...JSON . parse ( value ) ,
156+ }
157+ const parsedPreviousValue =
158+ op . operation == DiffTriggerOperation . UPDATE
159+ ? { id, ...JSON . parse ( op . previous_value ) }
160+ : null
161+ write ( {
162+ type : mapOperation ( operation ) ,
163+ value : parsedValue ,
164+ previousValue : parsedPreviousValue ,
165+ } )
166+ pendingOperations . push ( {
167+ id,
168+ operation,
169+ timestamp,
170+ tableName,
171+ } )
156172 }
157- const parsedPreviousValue =
158- op . operation == DiffTriggerOperation . UPDATE
159- ? { id, ...JSON . parse ( op . previous_value ) }
160- : null
161- write ( {
162- type : mapOperation ( operation ) ,
163- value : parsedValue ,
164- previousValue : parsedPreviousValue ,
165- } )
166- pendingOperations . push ( {
167- id,
168- operation,
169- timestamp,
170- tableName,
171- } )
172- }
173173
174- // clear the current operations
175- await context . execute ( `DELETE FROM ${ trackedTableName } ` )
174+ // clear the current operations
175+ await context . execute ( `DELETE FROM ${ trackedTableName } ` )
176176
177- commit ( )
178- pendingOperationStore . resolvePendingFor ( pendingOperations )
179- } )
177+ commit ( )
178+ pendingOperationStore . resolvePendingFor ( pendingOperations )
179+ } )
180+ . catch ( ( error ) => {
181+ database . logger . error (
182+ `An error has been detected in the sync handler` ,
183+ error
184+ )
185+ } )
180186 } ,
181187 } ,
182188 {
@@ -207,6 +213,7 @@ export function powerSyncCollectionOptions<
207213 }
208214 commit ( )
209215 markReady ( )
216+ database . logger . info ( `Sync is ready` )
210217 } ,
211218 } ,
212219 } )
@@ -215,15 +222,22 @@ export function powerSyncCollectionOptions<
215222 if ( abortController . signal . aborted ) {
216223 await disposeTracking ( )
217224 } else {
218- abortController . signal . addEventListener ( `abort` , ( ) => {
219- disposeTracking ( )
220- } )
225+ abortController . signal . addEventListener (
226+ `abort` ,
227+ ( ) => {
228+ disposeTracking ( )
229+ } ,
230+ { once : true }
231+ )
221232 }
222233 }
223234
224- start ( )
235+ start ( ) . catch ( ( error ) =>
236+ database . logger . error ( `Could not start syncing process` , error )
237+ )
225238
226239 return ( ) => {
240+ database . logger . info ( `Sync has been stopped` )
227241 abortController . abort ( )
228242 }
229243 } ,
0 commit comments