Hyperlinkv0.8.0-beta.28

Effect

Effect.contextconsteffect/Effect.ts:5768
<R = never>(): Effect<Context.Context<R>, never, R>

Returns the complete context.

When to use

Use to read the complete Context available to the current effect.

Details

This function allows you to access all services that are currently available in the effect's environment. This can be useful for debugging, introspection, or when you need to pass the entire context to another function.

Example (Reading the full context)

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

const Logger = Context.Service<{
  log: (msg: string) => void
}>("Logger")
const Database = Context.Service<{
  query: (sql: string) => string
}>("Database")

const program = Effect.gen(function*() {
  const allServices = yield* Effect.context()

  // Check if specific services are available
  const loggerOption = Context.getOption(allServices, Logger)
  const databaseOption = Context.getOption(allServices, Database)

  yield* Console.log(`Logger available: ${Option.isSome(loggerOption)}`)
  yield* Console.log(`Database available: ${Option.isSome(databaseOption)}`)
})

const context = Context.make(Logger, { log: console.log })
  .pipe(Context.add(Database, { query: () => "result" }))

const provided = Effect.provideContext(program, context)
Source effect/Effect.ts:57681 lines
export const context: <R = never>() => Effect<Context.Context<R>, never, R> = internal.context
Referenced by 10 symbols