|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | import type { PathConfig } from '../types/paths.js'; |
9 | | -import type { SyncResult } from '../types/sync.js'; |
| 9 | +import type { SyncResult, LocalSyncState } from '../types/sync.js'; |
10 | 10 | import type { CategoryData } from '../sync/operations/types.js'; |
11 | 11 | import { writeLocalData, deleteTombstonedItems, saveLocalState } from '../data/index.js'; |
12 | 12 | import { syncLog } from '../logging/index.js'; |
13 | | -import { getPluginState } from '../plugin/state-manager.js'; |
| 13 | + |
| 14 | +/** Interface for engine that can provide local state */ |
| 15 | +export interface StateProvider { |
| 16 | + getLocalState(): LocalSyncState | null; |
| 17 | +} |
14 | 18 |
|
15 | 19 | /** Actions that should trigger writing pulled data to disk */ |
16 | 20 | const WRITE_ACTIONS = new Set(['pulled', 'merged', 'pushed']); |
@@ -43,22 +47,28 @@ export async function writePulledData(pathConfig: PathConfig, result: SyncResult |
43 | 47 |
|
44 | 48 | /** |
45 | 49 | * Persist engine's local state to disk after successful sync. |
46 | | - * Updates both the state file and the in-memory plugin state. |
| 50 | + * Returns the new state for the caller to update their in-memory state. |
47 | 51 | */ |
48 | | -export async function persistLocalState(pathConfig: PathConfig): Promise<void> { |
49 | | - const state = getPluginState(); |
50 | | - const newState = state.engine?.getLocalState(); |
| 52 | +export async function persistLocalState( |
| 53 | + pathConfig: PathConfig, |
| 54 | + engine: StateProvider | null | undefined |
| 55 | +): Promise<LocalSyncState | null> { |
| 56 | + const newState = engine?.getLocalState() ?? null; |
51 | 57 | if (newState) { |
52 | 58 | await saveLocalState(pathConfig, newState); |
53 | | - state.localState = newState; |
54 | 59 | } |
| 60 | + return newState; |
55 | 61 | } |
56 | 62 |
|
57 | 63 | /** |
58 | 64 | * Process a successful sync result - write data and persist state. |
59 | 65 | * Combines writePulledData and persistLocalState into a single call. |
60 | 66 | */ |
61 | | -export async function processSyncResult(pathConfig: PathConfig, result: SyncResult): Promise<void> { |
| 67 | +export async function processSyncResult( |
| 68 | + pathConfig: PathConfig, |
| 69 | + result: SyncResult, |
| 70 | + engine: StateProvider | null | undefined |
| 71 | +): Promise<LocalSyncState | null> { |
62 | 72 | await writePulledData(pathConfig, result); |
63 | | - await persistLocalState(pathConfig); |
| 73 | + return persistLocalState(pathConfig, engine); |
64 | 74 | } |
0 commit comments