Hyperlinkv0.8.0-beta.28

Effect

Effect.provideContextconsteffect/Effect.ts:5950
<XR>(context: Context.Context<XR>): <A, E, R>(
  self: Effect<A, E, R>
) => Effect<A, E, Exclude<R, XR>>
<A, E, R, XR>(
  self: Effect<A, E, R>,
  context: Context.Context<XR>
): Effect<A, E, Exclude<R, XR>>

Provides a context to an effect, fulfilling its service requirements.

Details

This function provides multiple services at once by supplying a context that contains all the required services. It removes the provided services from the effect's requirements, making them available to the effect.

Example (Providing a complete context)

import { Context, Effect } from "effect"

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

// Create a context with multiple services
const context = Context.make(Logger, { log: console.log })
  .pipe(Context.add(Database, { query: () => "result" }))

// An effect that requires both services
const program = Effect.gen(function*() {
  const logger = yield* Effect.service(Logger)
  const db = yield* Effect.service(Database)
  logger.log("Querying database")
  return db.query("SELECT * FROM users")
})

const provided = Effect.provideContext(program, context)
environment
Source effect/Effect.ts:59509 lines
export const provideContext: {
  <XR>(
    context: Context.Context<XR>
  ): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, XR>>
  <A, E, R, XR>(
    self: Effect<A, E, R>,
    context: Context.Context<XR>
  ): Effect<A, E, Exclude<R, XR>>
} = internal.provideContext
Referenced by 8 symbols