55import type { PathConfig } from '../types/paths.js' ;
66import type { SyncCategory } from '../types/index.js' ;
77import type { SyncResult } from '../types/sync.js' ;
8- import type { CategoryData } from '../sync/operations/types.js' ;
9- import {
10- loadLocalData ,
11- saveLocalState ,
12- writeLocalData ,
13- deleteTombstonedItems ,
14- } from '../data/index.js' ;
8+ import { loadLocalData } from '../data/index.js' ;
159import { FileWatcher } from '../sync/watcher/index.js' ;
16- import { syncLog , log } from '../logging/index.js' ;
10+ import { log } from '../logging/index.js' ;
1711import { getPluginState } from './state-manager.js' ;
12+ import { isContinuousSyncReady } from './validation.js' ;
13+ import { getErrorMessage , writePulledData , persistLocalState } from '../shared/index.js' ;
1814
1915/** Active file watcher instance */
2016let activeWatcher : FileWatcher | null = null ;
2117
2218/** Active interval timer for periodic sync */
2319let syncInterval : NodeJS . Timeout | null = null ;
2420
25- /** Write pulled data to disk after a successful pull/merge */
26- export async function writePulledData ( pathConfig : PathConfig , result : SyncResult ) : Promise < void > {
27- if ( result . action !== 'pulled' && result . action !== 'merged' ) {
28- return ;
29- }
30-
31- if ( result . pulledData ) {
32- const data = result . pulledData as CategoryData [ ] ;
33- syncLog ( `[WRITE] Writing ${ String ( data . length ) } categories to disk` ) ;
34- await writeLocalData ( pathConfig , data ) ;
35- syncLog ( `[WRITE] Finished writing to disk` ) ;
36- }
37-
38- if ( result . tombstonedItems ) {
39- await deleteTombstonedItems ( pathConfig , result . tombstonedItems ) ;
40- }
41- }
42-
43- /** Persist engine's local state to disk after successful sync */
44- export async function persistLocalState ( pathConfig : PathConfig ) : Promise < void > {
45- const state = getPluginState ( ) ;
46- const newState = state . engine ?. getLocalState ( ) ;
47- if ( newState ) {
48- await saveLocalState ( pathConfig , newState ) ;
49- state . localState = newState ;
50- }
51- }
52-
5321/** Start file watcher for continuous sync */
5422export function startFileWatcher ( pathConfig : PathConfig ) : void {
5523 const state = getPluginState ( ) ;
5624
57- if ( ! state . config ?. continuousSync || ! state . engine ) {
25+ if ( ! isContinuousSyncReady ( state ) ) {
5826 return ;
5927 }
6028
@@ -81,24 +49,22 @@ export function startFileWatcher(pathConfig: PathConfig): void {
8149 await persistLocalState ( pathConfig ) ;
8250 }
8351 } catch ( error ) {
84- const errMsg = error instanceof Error ? error . message : String ( error ) ;
85- log ( `WARNING: File watcher sync failed: ${ errMsg } ` ) ;
52+ log ( `WARNING: File watcher sync failed: ${ getErrorMessage ( error ) } ` ) ;
8653 }
8754 } ,
8855 } ) ;
8956 void activeWatcher . start ( ) ;
9057 log ( 'File watcher started' ) ;
9158 } catch ( error ) {
92- const errMsg = error instanceof Error ? error . message : String ( error ) ;
93- log ( `WARNING: Failed to start file watcher: ${ errMsg } ` ) ;
59+ log ( `WARNING: Failed to start file watcher: ${ getErrorMessage ( error ) } ` ) ;
9460 }
9561}
9662
9763/** Start interval-based sync */
9864export function startIntervalSync ( pathConfig : PathConfig ) : void {
9965 const state = getPluginState ( ) ;
10066
101- if ( ! state . config ?. continuousSync || ! state . engine ) {
67+ if ( ! isContinuousSyncReady ( state ) ) {
10268 return ;
10369 }
10470
@@ -117,8 +83,7 @@ export function startIntervalSync(pathConfig: PathConfig): void {
11783 }
11884 logIntervalResult ( result ) ;
11985 } catch ( error ) {
120- const errMsg = error instanceof Error ? error . message : String ( error ) ;
121- log ( `WARNING: Interval sync failed: ${ errMsg } ` ) ;
86+ log ( `WARNING: Interval sync failed: ${ getErrorMessage ( error ) } ` ) ;
12287 }
12388 } ) ( ) ;
12489 } , intervalMs ) ;
@@ -151,3 +116,6 @@ export function stopBackgroundSync(): void {
151116 log ( 'Interval sync stopped' ) ;
152117 }
153118}
119+
120+ // Re-export for backward compatibility
121+ export { writePulledData , persistLocalState } from '../shared/index.js' ;
0 commit comments