Hyperlinkv0.8.0-beta.28

Layer

Layer.syncconsteffect/Layer.ts:890
<I, S>(service: Context.Key<I, S>): (evaluate: LazyArg<S>) => Layer<I>
<I, S>(
  service: Context.Key<I, S>,
  evaluate: LazyArg<Types.NoInfer<S>>
): Layer<I>

Constructs a layer lazily that provides a single service.

When to use

Use when you need a Layer that provides one service whose value is created synchronously, but creation should be deferred until the layer is built.

Details

This is a lazy version of succeed where the service value is computed synchronously only when the layer is built.

Example (Lazily providing a service)

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

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

const layer = Layer.sync(Database, () => ({
  query: (sql: string) => Effect.succeed(`Query: ${sql}`)
}))
constructorssucceed
Source effect/Layer.ts:8909 lines
export const sync: {
  <I, S>(service: Context.Key<I, S>): (evaluate: LazyArg<S>) => Layer<I>
  <I, S>(service: Context.Key<I, S>, evaluate: LazyArg<Types.NoInfer<S>>): Layer<I>
} = function() {
  if (arguments.length === 1) {
    return (evaluate: LazyArg<any>) => syncContext(() => Context.make(arguments[0], evaluate()))
  }
  return syncContext(() => Context.make(arguments[0], arguments[1]()))
} as any
Referenced by 3 symbols