forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncState.ts
More file actions
61 lines (53 loc) · 1.65 KB
/
Copy pathsyncState.ts
File metadata and controls
61 lines (53 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Per-source sync state (lastSyncAt + processed IDs) in userData JSON. Wraps the
// pure syncStateLogic so the merge/dedup/bound rules stay unit-tested.
import { app } from 'electron'
import { existsSync, readFileSync, writeFileSync, rmSync } from 'fs'
import { join } from 'path'
import {
emptySourceState,
normalizeSourceState,
recordProcessed,
type SourceState
} from './syncStateLogic'
import type { GoogleSource } from '../../shared/types'
type SyncFile = { gmail: SourceState; calendar: SourceState }
function file(): string {
return join(app.getPath('userData'), 'google-sync.json')
}
function read(): SyncFile {
try {
if (existsSync(file())) {
const raw = JSON.parse(readFileSync(file(), 'utf8')) as Partial<SyncFile>
return {
gmail: normalizeSourceState(raw.gmail),
calendar: normalizeSourceState(raw.calendar)
}
}
} catch {
/* fall through to empty */
}
return { gmail: emptySourceState(), calendar: emptySourceState() }
}
function write(state: SyncFile): void {
writeFileSync(file(), JSON.stringify(state), 'utf8')
}
export function getSourceState(source: GoogleSource): SourceState {
return read()[source]
}
export function markProcessed(source: GoogleSource, ids: string[]): void {
const state = read()
state[source] = recordProcessed(state[source], ids, Date.now())
write(state)
}
/** Most recent successful sync across both sources (0 if never). */
export function lastSyncAt(): number {
const s = read()
return Math.max(s.gmail.lastSyncAt, s.calendar.lastSyncAt)
}
export function clearSyncState(): void {
try {
rmSync(file(), { force: true })
} catch {
/* best-effort */
}
}