effect-io-ai

Package: effect
Module: Stream

Stream.provideServiceEffect

Provides a service to the stream using an effect, removing the requirement and adding the effect’s error and environment.

Example (Providing a stream service effectfully)

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

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

const stream = Stream.fromEffect(
  Effect.gen(function*() {
    const config = yield* Effect.service(ApiConfig)
    return config.baseUrl
  })
)

const withConfig = stream.pipe(
  Stream.provideServiceEffect(
    ApiConfig,
    Effect.succeed({ baseUrl: "https://example.com" }).pipe(
      Effect.tap(() => Console.log("Loading config..."))
    )
  )
)

const program = Stream.runCollect(withConfig).pipe(
  Effect.flatMap((values) => Console.log(values))
)

Effect.runPromise(program)
// Output:
// Loading config...
// ["https://example.com"]

Signature

declare const provideServiceEffect: { <I, S, ES, RS>(key: Context.Key<I, S>, service: Effect.Effect<NoInfer<S>, ES, RS>): <A, E, R>(self: Stream<A, E, R>) => Stream<A, E | ES, Exclude<R, I> | RS>; <A, E, R, I, S, ES, RS>(self: Stream<A, E, R>, key: Context.Key<I, S>, service: Effect.Effect<NoInfer<S>, ES, RS>): Stream<A, E | ES, Exclude<R, I> | RS>; }

Source

Since v2.0.0