-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoyRideLifecycle.ts
More file actions
50 lines (43 loc) · 1.42 KB
/
Copy pathJoyRideLifecycle.ts
File metadata and controls
50 lines (43 loc) · 1.42 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
/**
* [LAYER: CORE]
* JoyRide lifecycle helpers — centralized task/workspace flush and shutdown.
*/
import type { JoyRideCache } from "./JoyRideCache"
import { JOYRIDE_REASON } from "./JoyRideReasonCodes"
import type { JoyRideInvalidationReason } from "./types"
export function registerTaskLifecycle(cache: JoyRideCache, taskId: string, generation = 0): void {
cache.registerTask(taskId, generation)
}
export async function withTaskCacheScope<T>(
cache: JoyRideCache,
taskId: string,
generation: number,
fn: () => Promise<T>,
): Promise<T> {
registerTaskLifecycle(cache, taskId, generation)
try {
return await fn()
} finally {
// Caller decides when to flush; scope only registers generation.
}
}
export function bumpTaskGeneration(cache: JoyRideCache, taskId: string, _reason = JOYRIDE_REASON.STALE_TASK_GENERATION): number {
return cache.bumpTaskGeneration(taskId)
}
export function flushTaskGeneration(
cache: JoyRideCache,
taskId: string,
reason: JoyRideInvalidationReason = "task_completed",
): number {
return cache.flushTask(taskId, reason)
}
export function flushWorkspace(
cache: JoyRideCache,
workspaceFingerprint: string,
reason: JoyRideInvalidationReason = "workspace_drift",
): number {
return cache.invalidateWorkspace(workspaceFingerprint, reason)
}
export function shutdownJoyRide(cache: JoyRideCache, reason: JoyRideInvalidationReason = "workspace_closed"): number {
return cache.shutdown(reason)
}