Hyperlinkv0.8.0-beta.28

Layer

Layer.succeedconsteffect/Layer.ts:775
<I, S>(service: Context.Key<I, S>): (resource: S) => Layer<I>
<I, S>(service: Context.Key<I, S>, resource: Types.NoInfer<S>): Layer<I>

Constructs a layer that provides a single service from an already available value.

When to use

Use when you need a Layer that provides a service from an already constructed implementation without effectful acquisition.

Example (Creating a layer from a service implementation)

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

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

const DatabaseLive = Layer.succeed(Database, {
  query: Effect.fn("Database.query")((sql: string) => Effect.succeed(`Query result: ${sql}`))
})
constructorssync
Source effect/Layer.ts:7759 lines
export const succeed: {
  <I, S>(service: Context.Key<I, S>): (resource: S) => Layer<I>
  <I, S>(service: Context.Key<I, S>, resource: Types.NoInfer<S>): Layer<I>
} = function() {
  if (arguments.length === 1) {
    return (resource: any) => succeedContext(Context.make(arguments[0], resource))
  }
  return succeedContext(Context.make(arguments[0], arguments[1]))
} as any
Referenced by 19 symbols