Hyperlinkv0.8.0-beta.28

Layer

Layer.effectconsteffect/Layer.ts:974
<I, S>(service: Context.Key<I, S>): <E, R>(
  effect: Effect<S, E, R>
) => Layer<I, E, Exclude<R, Scope.Scope>>
<I, S, E, R>(
  service: Context.Key<I, S>,
  effect: Effect<Types.NoInfer<S>, E, R>
): Layer<I, E, Exclude<R, Scope.Scope>>

Constructs a layer from an effect that produces a single service.

When to use

Use when you need to construct a Layer-provided service with an Effect, dependencies, or scoped resource acquisition.

Details

This allows you to create a Layer from an Effect that produces a service. The Effect is executed in the scope of the layer, allowing for proper resource management.

Example (Creating a layer from an effect)

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

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

const layer = Layer.effect(Database,
  Effect.sync(() => ({
    query: (sql: string) => Effect.succeed(`Query: ${sql}`)
  }))
)
Source effect/Layer.ts:97414 lines
export const effect: {
  <I, S>(service: Context.Key<I, S>): <E, R>(
    effect: Effect<S, E, R>
  ) => Layer<I, E, Exclude<R, Scope.Scope>>
  <I, S, E, R>(
    service: Context.Key<I, S>,
    effect: Effect<Types.NoInfer<S>, E, R>
  ): Layer<I, E, Exclude<R, Scope.Scope>>
} = function() {
  if (arguments.length === 1) {
    return (effect: any) => effectImpl(arguments[0], effect)
  }
  return effectImpl(arguments[0], arguments[1])
} as any
Referenced by 30 symbols