1import type * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import type * as Scope from "effect/Scope";
5
6/**
7 * Builds a layer with the current scope.
8 * @param layer - The layer to build.
9 * @returns A scoped effect returning a Context with the provided layer.
10 */
11export const buildLayerScoped = <ROut, E, RIn>(
12 layer: Layer.Layer<ROut, E, RIn>,
13): Effect.Effect<Context.Context<ROut>, E, RIn | Scope.Scope> =>
14 Effect.flatMap(Effect.scope, (scope) => Layer.buildWithScope(layer, scope));
15
16/**
17 * Builds a layer with the current scope and provides it to the given effect.
18 * @param layer - The layer to build.
19 */
20export const provideLayerScoped =
21 <ROut, E1, RIn>(layer: Layer.Layer<ROut, E1, RIn>) =>
22 <A, E2, R>(effect: Effect.Effect<A, E2, R>) =>
23 Effect.flatMap(buildLayerScoped(layer), (context) =>
24 Effect.provideContext(effect, context),
25 );