Hyperlinkv0.8.0-beta.28

Stream

Stream.updateServiceconsteffect/Stream.ts:10292
<I, S>(key: Context.Key<I, S>, f: (service: NoInfer<S>) => S): <A, E, R>(
  self: Stream<A, E, R>
) => Stream<A, E, R | I>
<A, E, R, I, S>(
  self: Stream<A, E, R>,
  key: Context.Key<I, S>,
  f: (service: NoInfer<S>) => S
): Stream<A, E, R | I>

Updates a single service in the stream environment by applying a function.

Example (Updating a stream service)

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

class Counter extends Context.Service<Counter, { count: number }>()("Counter") {}

const stream = Stream.fromEffect(Effect.service(Counter)).pipe(
  Stream.updateService(Counter, (counter) => ({ count: counter.count + 1 }))
)

const program = Effect.gen(function*() {
  const counters = yield* Stream.runCollect(stream)
  yield* Console.log(`Updated count: ${counters[0].count}`)
})

Effect.runPromise(Effect.provideService(program, Counter, { count: 0 }))
// Output: Updated count: 1
services
Source effect/Stream.ts:1029223 lines
export const updateService: {
  <I, S>(
    key: Context.Key<I, S>,
    f: (service: NoInfer<S>) => S
  ): <A, E, R>(
    self: Stream<A, E, R>
  ) => Stream<A, E, R | I>
  <A, E, R, I, S>(
    self: Stream<A, E, R>,
    key: Context.Key<I, S>,
    f: (service: NoInfer<S>) => S
  ): Stream<A, E, R | I>
} = dual(3, <A, E, R, I, S>(
  self: Stream<A, E, R>,
  service: Context.Key<I, S>,
  f: (service: NoInfer<S>) => S
): Stream<A, E, R | I> =>
  updateContext(self, (context) =>
    Context.add(
      context,
      service,
      f(Context.get(context, service))
    )))