Migrate test inits from Promise to Effect (#25377)
This commit is contained in:
@@ -1,42 +1,18 @@
|
||||
import { Effect } from "effect"
|
||||
import { InstanceRef } from "@/effect/instance-ref"
|
||||
import * as Project from "./project"
|
||||
import { context, type InstanceContext } from "./instance-context"
|
||||
import { InstanceStore } from "./instance-store"
|
||||
|
||||
export type { InstanceContext } from "./instance-context"
|
||||
export type { LoadInput } from "./instance-store"
|
||||
|
||||
type LegacyLoadInput = {
|
||||
directory: string
|
||||
init?: () => Promise<unknown>
|
||||
project?: Project.Info
|
||||
worktree?: string
|
||||
}
|
||||
|
||||
// Promise-style legacy inits often read Instance.directory etc. from the ALS context.
|
||||
// The new Effect-typed init path doesn't bind ALS — it provides InstanceRef. To keep
|
||||
// legacy inits working without forcing every test to convert, bind ALS around the
|
||||
// Promise call here using the instance ctx that the store provides via InstanceRef.
|
||||
const liftLegacyInput = (input: LegacyLoadInput): InstanceStore.LoadInput => {
|
||||
const { init, ...rest } = input
|
||||
if (!init) return rest
|
||||
return {
|
||||
...rest,
|
||||
init: Effect.gen(function* () {
|
||||
const ctx = yield* InstanceRef
|
||||
yield* Effect.promise(() => (ctx ? context.provide(ctx, init) : init()))
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
export const Instance = {
|
||||
load(input: LegacyLoadInput): Promise<InstanceContext> {
|
||||
return InstanceStore.runtime.runPromise((store) => store.load(liftLegacyInput(input)))
|
||||
load(input: InstanceStore.LoadInput): Promise<InstanceContext> {
|
||||
return InstanceStore.runtime.runPromise((store) => store.load(input))
|
||||
},
|
||||
async provide<R>(input: { directory: string; init?: () => Promise<unknown>; fn: () => R }): Promise<R> {
|
||||
return context.provide(await Instance.load({ directory: input.directory, init: input.init }), async () =>
|
||||
input.fn(),
|
||||
async provide<R>(input: { directory: string; init?: Effect.Effect<void>; fn: () => R }): Promise<R> {
|
||||
return context.provide(
|
||||
await Instance.load({ directory: input.directory, init: input.init }),
|
||||
async () => input.fn(),
|
||||
)
|
||||
},
|
||||
get current() {
|
||||
@@ -69,8 +45,8 @@ export const Instance = {
|
||||
restore<R>(ctx: InstanceContext, fn: () => R): R {
|
||||
return context.provide(ctx, fn)
|
||||
},
|
||||
async reload(input: LegacyLoadInput) {
|
||||
return InstanceStore.runtime.runPromise((store) => store.reload(liftLegacyInput(input)))
|
||||
async reload(input: InstanceStore.LoadInput) {
|
||||
return InstanceStore.runtime.runPromise((store) => store.reload(input))
|
||||
},
|
||||
async dispose() {
|
||||
return InstanceStore.runtime.runPromise((store) => store.dispose(Instance.current))
|
||||
|
||||
Reference in New Issue
Block a user