Hyperlinkv0.8.0-beta.28

Effect

Effect.contextWithconsteffect/Effect.ts:5824
<R, A, E, R2>(
  f: (context: Context.Context<R>) => Effect<A, E, R2>
): Effect<A, E, R | R2>

Transforms the current context using the provided function.

When to use

Use to derive an effect from the complete Context.

Details

This function allows you to access the complete context and perform computations based on all available services. This is useful when you need to conditionally execute logic based on what services are available.

Example (Deriving values from the context)

import { Console, Context, Effect, Option } from "effect"

const Logger = Context.Service<{
  log: (msg: string) => void
}>("Logger")
const Cache = Context.Service<{
  get: (key: string) => string | null
}>("Cache")

const program = Effect.contextWith((services) => {
  const cacheOption = Context.getOption(services, Cache)
  const hasCache = Option.isSome(cacheOption)

  if (hasCache) {
    return Effect.gen(function*() {
      const cache = yield* Effect.service(Cache)
      yield* Console.log("Using cached data")
      return cache.get("user:123") || "default"
    })
  } else {
    return Effect.gen(function*() {
      yield* Console.log("No cache available, using fallback")
      return "fallback data"
    })
  }
})

const withCache = Effect.provideService(program, Cache, {
  get: () => "cached_value"
})
environmentcontextservice
Source effect/Effect.ts:58243 lines
export const contextWith: <R, A, E, R2>(
  f: (context: Context.Context<R>) => Effect<A, E, R2>
) => Effect<A, E, R | R2> = internal.contextWith
Referenced by 6 symbols