Hyperlinkv0.8.0-beta.28

Effect

Effect.provideServiceconsteffect/Effect.ts:6221
<I, S>(service: Context.Key<I, S>): {
  (implementation: S): <A, E, R>(
    self: Effect<A, E, R>
  ) => Effect<A, E, Exclude<R, I>>
  <A, E, R>(self: Effect<A, E, R>, implementation: S): Effect<
    A,
    E,
    Exclude<R, I>
  >
}
<I, S>(service: Context.Key<I, S>, implementation: S): <A, E, R>(
  self: Effect<A, E, R>
) => Effect<A, E, Exclude<R, I>>
<A, E, R, I, S>(
  self: Effect<A, E, R>,
  service: Context.Key<I, S>,
  implementation: S
): Effect<A, E, Exclude<R, I>>

Provides one concrete service implementation to an effect.

When to use

Use to satisfy one service requirement with an already-built implementation.

Details

The service requirement identified by the Context.Key is removed from the effect requirements after the implementation is provided.

Example (Providing a service value)

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

// Define a service for configuration
const Config = Context.Service<{
  apiUrl: string
  timeout: number
}>("Config")

const fetchData = Effect.gen(function*() {
  const config = yield* Effect.service(Config)
  yield* Console.log(`Fetching from: ${config.apiUrl}`)
  yield* Console.log(`Timeout: ${config.timeout}ms`)
  return "data"
})

// Provide the service implementation
const program = Effect.provideService(fetchData, Config, {
  apiUrl: "https://api.example.com",
  timeout: 5000
})

Effect.runPromise(program).then(console.log)
// Output:
// Fetching from: https://api.example.com
// Timeout: 5000ms
// data
Source effect/Effect.ts:622117 lines
export const provideService: {
  <I, S>(
    service: Context.Key<I, S>
  ): {
    (implementation: S): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, I>>
    <A, E, R>(self: Effect<A, E, R>, implementation: S): Effect<A, E, Exclude<R, I>>
  }
  <I, S>(
    service: Context.Key<I, S>,
    implementation: S
  ): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, I>>
  <A, E, R, I, S>(
    self: Effect<A, E, R>,
    service: Context.Key<I, S>,
    implementation: S
  ): Effect<A, E, Exclude<R, I>>
} = internal.provideService
Referenced by 10 symbols