Hyperlinkv0.8.0-beta.28

Effect

Effect.setContextconsteffect/Effect.ts:6002
<R>(context: Context.Context<R>): <A, E>(
  self: Effect<A, E, R>
) => Effect<A, E>
<A, E, R>(self: Effect<A, E, R>, context: Context.Context<R>): Effect<
  A,
  E
>

Runs an effect with the provided context as its complete environment.

When to use

Use when you already have a Context containing every service required by the effect and want the wrapped effect to run with exactly that context.

Gotchas

setContext replaces the current context for the wrapped effect. Services from an outer context are not inherited unless they are also present in the context passed to setContext.

Example (Running with a complete context)

import { Context, Effect } from "effect"

class Config extends Context.Service<Config, {
  readonly greeting: string
}>()("Config") {}

const program = Effect.gen(function*() {
  const config = yield* Effect.service(Config)
  return `${config.greeting}, World!`
})

const context = Context.make(Config, { greeting: "Hello" })

const runnable = Effect.setContext(program, context)

Effect.runPromise(runnable).then(console.log)
// Output: "Hello, World!"
Source effect/Effect.ts:60024 lines
export const setContext: {
  <R>(context: Context.Context<R>): <A, E>(self: Effect<A, E, R>) => Effect<A, E>
  <A, E, R>(self: Effect<A, E, R>, context: Context.Context<R>): Effect<A, E>
} = internal.setContext