Hyperlinkv0.8.0-beta.28

Stream

Stream.updateContextconsteffect/Stream.ts:10251
<R, R2>(f: (context: Context.Context<R2>) => Context.Context<R>): <A, E>(
  self: Stream<A, E, R>
) => Stream<A, E, R2>
<A, E, R, R2>(
  self: Stream<A, E, R>,
  f: (context: Context.Context<R2>) => Context.Context<R>
): Stream<A, E, R2>

Transforms the stream's required services by mapping the current context to a new one.

Example (Updating the stream context)

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

class Logger extends Context.Service<Logger, { prefix: string }>()("Logger") {}
class Config extends Context.Service<Config, { name: string }>()("Config") {}

const stream = Stream.fromEffect(
  Effect.gen(function*() {
    const logger = yield* Effect.service(Logger)
    const config = yield* Effect.service(Config)
    return `${logger.prefix}${config.name}`
  })
)

const updated = stream.pipe(
  Stream.updateContext((context: Context.Context<Logger>) =>
    Context.add(context, Config, { name: "World" })
  )
)

const program = Effect.gen(function*() {
  const values = yield* Stream.runCollect(updated)
  yield* Console.log(values)
})

Effect.runPromise(
  Effect.provideService(program, Logger, { prefix: "Hello " })
)
//=> [ "Hello World" ]
services
Source effect/Stream.ts:1025114 lines
export const updateContext: {
  <R, R2>(
    f: (context: Context.Context<R2>) => Context.Context<R>
  ): <A, E>(
    self: Stream<A, E, R>
  ) => Stream<A, E, R2>
  <A, E, R, R2>(
    self: Stream<A, E, R>,
    f: (context: Context.Context<R2>) => Context.Context<R>
  ): Stream<A, E, R2>
} = dual(2, <A, E, R, R2>(
  self: Stream<A, E, R>,
  f: (context: Context.Context<R2>) => Context.Context<R>
): Stream<A, E, R2> => fromChannel(Channel.updateContext(self.channel, f)))
Referenced by 1 symbols