Hyperlinkv0.8.0-beta.28

Layer

Layer.effectContextconsteffect/Layer.ts:1031
<A, E, R>(effect: Effect<Context.Context<A>, E, R>): Layer<
  A,
  E,
  Exclude<R, Scope.Scope>
>

Constructs a layer from an effect that produces all services in a Context.

When to use

Use when you need a Layer that effectfully constructs a Context with multiple services.

Details

This allows you to create a Layer from an effectful computation that returns multiple services. The Effect is executed in the scope of the layer.

Example (Creating a layer from an effectful context)

import { Context, Effect, Layer } from "effect"

class Database extends Context.Service<
  Database,
  { readonly query: (sql: string) => Effect.Effect<string> }
>()("Database") {}

const layer = Layer.effectContext(
  Effect.succeed(Context.make(Database, {
    query: (sql: string) => Effect.succeed(`Query: ${sql}`)
  }))
)
constructorseffect
Source effect/Layer.ts:10313 lines
export const effectContext = <A, E, R>(
  effect: Effect<Context.Context<A>, E, R>
): Layer<A, E, Exclude<R, Scope.Scope>> => fromBuildMemo((_, scope) => Scope.provide(effect, scope))
Referenced by 8 symbols