Hyperlinkv0.8.0-beta.28

Effect

Effect.updateContextconsteffect/Effect.ts:6115
<R2, R>(
  f: (context: Context.Context<R2>) => Context.Context<NoInfer<R>>
): <A, E>(self: Effect<A, E, R>) => Effect<A, E, R2>
<A, E, R, R2>(
  self: Effect<A, E, R>,
  f: (context: Context.Context<R2>) => Context.Context<NoInfer<R>>
): Effect<A, E, R2>

Provides part of the required context while leaving the rest unchanged.

Details

This function allows you to transform the context required by an effect, providing part of the context and leaving the rest to be fulfilled later.

Example (Updating the context before running)

import { Context, Effect } from "effect"

// Define services
const Logger = Context.Service<{
  log: (msg: string) => void
}>("Logger")
const Config = Context.Service<{
  name: string
}>("Config")

const program = Effect.service(Config).pipe(
  Effect.map((config) => `Hello ${config.name}!`)
)

// Transform services by providing Config while keeping Logger requirement
const configured = program.pipe(
  Effect.updateContext((context: Context.Context<typeof Logger>) =>
    Context.add(context, Config, { name: "World" })
  )
)

// The effect now requires only Logger service
const result = Effect.provideService(configured, Logger, {
  log: (msg) => console.log(msg)
})
context
Source effect/Effect.ts:61159 lines
export const updateContext: {
  <R2, R>(
    f: (context: Context.Context<R2>) => Context.Context<NoInfer<R>>
  ): <A, E>(self: Effect<A, E, R>) => Effect<A, E, R2>
  <A, E, R, R2>(
    self: Effect<A, E, R>,
    f: (context: Context.Context<R2>) => Context.Context<NoInfer<R>>
  ): Effect<A, E, R2>
} = internal.updateContext
Referenced by 2 symbols