Hyperlinkv0.8.0-beta.28

Stream

Stream.provideServiceconsteffect/Stream.ts:10136
<I, S>(key: Context.Key<I, S>, service: NoInfer<S>): <A, E, R>(
  self: Stream<A, E, R>
) => Stream<A, E, Exclude<R, I>>
<A, E, R, I, S>(
  self: Stream<A, E, R>,
  key: Context.Key<I, S>,
  service: NoInfer<S>
): Stream<A, E, Exclude<R, I>>

Provides the stream with a single required service, eliminating that requirement from its environment.

Example (Providing a stream service)

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

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

const stream = Stream.fromEffect(
  Effect.service(Greeter).pipe(
    Effect.map((greeter) => greeter.greet("Ada"))
  )
)

const program = Effect.gen(function*() {
  const collected = yield* Stream.runCollect(
    stream.pipe(
      Stream.provideService(Greeter, {
        greet: (name) => `Hello, ${name}`
      })
    )
  )
  yield* Console.log(collected)
})

Effect.runPromise(program)
//=> ["Hello, Ada"]
services
Source effect/Stream.ts:1013617 lines
export const provideService: {
  <I, S>(
    key: Context.Key<I, S>,
    service: NoInfer<S>
  ): <A, E, R>(
    self: Stream<A, E, R>
  ) => Stream<A, E, Exclude<R, I>>
  <A, E, R, I, S>(
    self: Stream<A, E, R>,
    key: Context.Key<I, S>,
    service: NoInfer<S>
  ): Stream<A, E, Exclude<R, I>>
} = dual(3, <A, E, R, I, S>(
  self: Stream<A, E, R>,
  key: Context.Key<I, S>,
  service: NoInfer<S>
): Stream<A, E, Exclude<R, I>> => fromChannel(Channel.provideService(self.channel, key, service)))