-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathcheckpoint-worker.ts
More file actions
75 lines (69 loc) · 1.98 KB
/
checkpoint-worker.ts
File metadata and controls
75 lines (69 loc) · 1.98 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { parentPort as maybeParentPort } from 'worker_threads'
import { restoreFileState, storeFileState } from './checkpoints/file-manager'
import { setProjectRoot } from './project-files'
/**
* Message format for worker operations
*/
interface WorkerMessage {
/** The ID of this message */
id: string
/** Operation type - either storing or restoring checkpoint state */
type: 'store' | 'restore'
projectDir: string
bareRepoPath: string
relativeFilepaths: string[]
/** Git commit hash for restore operations */
commit?: string
/** Commit message for store operations */
message?: string
}
if (maybeParentPort) {
const parentPort = maybeParentPort
/**
* Handle incoming messages from the main thread.
* Executes git operations for storing or restoring checkpoints.
*/
parentPort.on('message', async (message: WorkerMessage) => {
const {
id,
type,
projectDir,
bareRepoPath,
commit,
message: commitMessage,
relativeFilepaths,
} = message
setProjectRoot(projectDir)
try {
let result: string | boolean
if (type === 'store') {
// Store the current state as a git commit
result = await storeFileState({
projectDir,
bareRepoPath,
message: commitMessage!,
relativeFilepaths,
})
} else if (type === 'restore') {
// Restore files to a previous git commit state
await restoreFileState({
projectDir,
bareRepoPath,
commit: commit!,
relativeFilepaths,
})
result = true
} else {
throw new Error(`Unknown operation type: ${type}`)
}
parentPort.postMessage({ id, success: true, result })
} catch (error) {
// Note: logger is not available in worker threads, so we just send the error back
parentPort.postMessage({
id,
success: false,
error: error instanceof Error ? error.message : String(error),
})
}
})
}