Hyperlinkv0.8.0-beta.28

Stream

Stream.provideContextconsteffect/Stream.ts:10090
<R2>(context: Context.Context<R2>): <A, E, R>(
  self: Stream<A, E, R>
) => Stream<A, E, Exclude<R, R2>>
<A, E, R, R2>(
  self: Stream<A, E, R>,
  context: Context.Context<R2>
): Stream<A, E, Exclude<R, R2>>

Provides multiple services to the stream using a context.

Example (Providing multiple services to the stream using a context)

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

class Config extends Context.Service<Config, { readonly prefix: string }>()("Config") {}
class Greeter extends Context.Service<Greeter, { greet: (name: string) => string }>()("Greeter") {}

const context = Context.make(Config, { prefix: "Hello" }).pipe(
  Context.add(Greeter, { greet: (name: string) => `${name}!` })
)

const stream = Stream.fromEffect(
  Effect.gen(function*() {
    const config = yield* Effect.service(Config)
    const greeter = yield* Effect.service(Greeter)
    return greeter.greet(config.prefix)
  })
)

const program = Effect.gen(function*() {
  const result = yield* Stream.runCollect(Stream.provideContext(stream, context))
  yield* Console.log(result)
})

Effect.runPromise(program)
// ["Hello!"]
services
export const provideContext: {
  <R2>(context: Context.Context<R2>): <A, E, R>(self: Stream<A, E, R>) => Stream<A, E, Exclude<R, R2>>
  <A, E, R, R2>(self: Stream<A, E, R>, context: Context.Context<R2>): Stream<A, E, Exclude<R, R2>>
} = dual(
  2,
  <A, E, R, R2>(self: Stream<A, E, R>, context: Context.Context<R2>): Stream<A, E, Exclude<R, R2>> =>
    fromChannel(Channel.provideContext(self.channel, context))
)