| 16 | |
| 17 | export const Instance = { |
| 18 | async provide<R>(input: { directory: string; init?: () => Promise<any>; fn: () => R }): Promise<R> { |
| 19 | let existing = cache.get(input.directory) |
| 20 | if (!existing) { |
| 21 | Log.Default.info("creating instance", { directory: input.directory }) |
| 22 | existing = iife(async () => { |
| 23 | const project = await Project.fromDirectory(input.directory) |
| 24 | const ctx = { |
| 25 | directory: input.directory, |
| 26 | worktree: project.worktree, |
| 27 | project, |
| 28 | } |
| 29 | await context.provide(ctx, async () => { |
| 30 | await input.init?.() |
| 31 | }) |
| 32 | return ctx |
| 33 | }) |
| 34 | cache.set(input.directory, existing) |
| 35 | } |
| 36 | const ctx = await existing |
| 37 | return context.provide(ctx, async () => { |
| 38 | return input.fn() |
| 39 | }) |
| 40 | }, |
| 41 | async provideContext<R>( |
| 42 | ctx: { directory: string; worktree: string; project: Project.Info; eventDirectory?: string }, |
| 43 | fn: () => R, |