2026-04-16 03:15:58 +00:00
|
|
|
import { LocalContext } from "../util"
|
2026-04-10 14:47:27 +00:00
|
|
|
import type { WorkspaceID } from "../control-plane/schema"
|
|
|
|
|
|
|
|
|
|
export interface WorkspaceContext {
|
2026-04-16 16:24:40 +00:00
|
|
|
workspaceID: WorkspaceID
|
2026-04-10 14:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-11 03:06:28 +00:00
|
|
|
const context = LocalContext.create<WorkspaceContext>("instance")
|
2026-04-10 14:47:27 +00:00
|
|
|
|
|
|
|
|
export const WorkspaceContext = {
|
|
|
|
|
async provide<R>(input: { workspaceID: WorkspaceID; fn: () => R }): Promise<R> {
|
2026-04-16 16:24:40 +00:00
|
|
|
return context.provide({ workspaceID: input.workspaceID }, () => input.fn())
|
2026-04-10 14:47:27 +00:00
|
|
|
},
|
|
|
|
|
|
2026-04-16 16:24:40 +00:00
|
|
|
restore<R>(workspaceID: WorkspaceID, fn: () => R): R {
|
2026-04-15 15:22:34 +00:00
|
|
|
return context.provide({ workspaceID }, fn)
|
|
|
|
|
},
|
|
|
|
|
|
2026-04-10 14:47:27 +00:00
|
|
|
get workspaceID() {
|
|
|
|
|
try {
|
|
|
|
|
return context.use().workspaceID
|
2026-04-16 01:33:54 +00:00
|
|
|
} catch {
|
2026-04-10 14:47:27 +00:00
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|