Hyperlinkv0.8.0-beta.28

Layer

Layer.unwrapconsteffect/Layer.ts:1126
<A, E1, R1, E, R>(self: Effect<Layer<A, E1, R1>, E, R>): Layer<
  A,
  E | E1,
  R1 | Exclude<R, Scope.Scope>
>

Unwraps a Layer from an Effect, flattening the nested structure.

When to use

Use when you have an Effect that produces a Layer and you want to use that layer directly.

Details

The resulting Layer will have the combined error and dependency types from both the outer Effect and the inner Layer.

Example (Unwrapping an effectful layer)

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

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

const layerEffect = Effect.succeed(
  Layer.succeed(Database, { query: Effect.fn("Database.query")((sql: string) => Effect.succeed("result")) })
)

const unwrappedLayer = Layer.unwrap(layerEffect)
converting
Source effect/Layer.ts:11266 lines
export const unwrap = <A, E1, R1, E, R>(
  self: Effect<Layer<A, E1, R1>, E, R>
): Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>> => {
  const service = Context.Service<Layer<A, E1, R1>>("effect/Layer/unwrap")
  return flatMap(effect(service)(self), Context.get(service))
}
Referenced by 38 symbols