mirror of
https://github.com/anomalyco/opencode.git
synced 2026-04-25 15:24:58 +00:00
35 lines
1.5 KiB
TypeScript
35 lines
1.5 KiB
TypeScript
import { Effect, Layer, ManagedRuntime } from "effect"
|
|
import * as ServiceMap from "effect/ServiceMap"
|
|
import { Instance } from "@/project/instance"
|
|
import { Context } from "@/util/context"
|
|
import { InstanceRef } from "./instance-ref"
|
|
import { Observability } from "./oltp"
|
|
|
|
export const memoMap = Layer.makeMemoMapUnsafe()
|
|
|
|
function attach<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> {
|
|
try {
|
|
const ctx = Instance.current
|
|
return Effect.provideService(effect, InstanceRef, ctx)
|
|
} catch (err) {
|
|
if (!(err instanceof Context.NotFound)) throw err
|
|
}
|
|
return effect
|
|
}
|
|
|
|
export function makeRuntime<I, S, E>(service: ServiceMap.Service<I, S>, layer: Layer.Layer<I, E>) {
|
|
let rt: ManagedRuntime.ManagedRuntime<I, E> | undefined
|
|
const getRuntime = () => (rt ??= ManagedRuntime.make(Layer.merge(layer, Observability.layer), { memoMap }))
|
|
|
|
return {
|
|
runSync: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runSync(attach(service.use(fn))),
|
|
runPromiseExit: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>, options?: Effect.RunOptions) =>
|
|
getRuntime().runPromiseExit(attach(service.use(fn)), options),
|
|
runPromise: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>, options?: Effect.RunOptions) =>
|
|
getRuntime().runPromise(attach(service.use(fn)), options),
|
|
runFork: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runFork(attach(service.use(fn))),
|
|
runCallback: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) =>
|
|
getRuntime().runCallback(attach(service.use(fn))),
|
|
}
|
|
}
|