<I, S>(service: Context.Key<I, S>): Stream<Option.Option<S>>Optionally accesses a service from the context and emits the result as a single element.
When to use
Use when you need a stream that emits an optional service from the context without requiring that service to be present.
Example (Accessing an optional service as a stream)
import { Context, Effect, Option, Stream } from "effect"
class Greeter extends Context.Service<Greeter, {
readonly greet: (name: string) => string
}>()("Greeter") {}
const stream = Stream.serviceOption(Greeter).pipe(
Stream.map((maybeGreeter) =>
Option.match(maybeGreeter, {
onNone: () => "No greeter",
onSome: (greeter) => greeter.greet("World")
})
)
)
const program = Effect.gen(function*() {
return yield* stream.pipe(
Stream.provideService(Greeter, {
greet: (name) => `Hello, ${name}!`
}),
Stream.runCollect
)
})
Effect.runPromise(program)
// Output: [ "Hello, World!" ]export const const serviceOption: <I, S>(
service: Context.Key<I, S>
) => Stream<Option.Option<S>>
Optionally accesses a service from the context and emits the result as a
single element.
When to use
Use when you need a stream that emits an optional service from the context
without requiring that service to be present.
Example (Accessing an optional service as a stream)
import { Context, Effect, Option, Stream } from "effect"
class Greeter extends Context.Service<Greeter, {
readonly greet: (name: string) => string
}>()("Greeter") {}
const stream = Stream.serviceOption(Greeter).pipe(
Stream.map((maybeGreeter) =>
Option.match(maybeGreeter, {
onNone: () => "No greeter",
onSome: (greeter) => greeter.greet("World")
})
)
)
const program = Effect.gen(function*() {
return yield* stream.pipe(
Stream.provideService(Greeter, {
greet: (name) => `Hello, ${name}!`
}),
Stream.runCollect
)
})
Effect.runPromise(program)
// Output: [ "Hello, World!" ]
serviceOption = <function (type parameter) I in <I, S>(service: Context.Key<I, S>): Stream<Option.Option<S>>I, function (type parameter) S in <I, S>(service: Context.Key<I, S>): Stream<Option.Option<S>>S>(service: Context.Key<I, S>(parameter) service: {
Identifier: Identifier;
Service: Shape;
key: string;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
service: import ContextContext.type Context.Key = /*unresolved*/ anyKey<function (type parameter) I in <I, S>(service: Context.Key<I, S>): Stream<Option.Option<S>>I, function (type parameter) S in <I, S>(service: Context.Key<I, S>): Stream<Option.Option<S>>S>): interface Stream<out A, out E = never, out R = never>A Stream<A, E, R> describes a program that can emit many A values, fail
with E, and require R.
Details
Streams are pull-based with backpressure and emit chunks to amortize effect
evaluation. They support monadic composition and error handling similar to
Effect, adapted for multiple values.
Example (Creating and consuming streams)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
yield* Stream.make(1, 2, 3).pipe(
Stream.map((n) => n * 2),
Stream.runForEach((n) => Console.log(n))
)
})
Effect.runPromise(program)
// Output:
// 2
// 4
// 6
Stream<import OptionOption.type Option.Option = /*unresolved*/ anyOption<function (type parameter) S in <I, S>(service: Context.Key<I, S>): Stream<Option.Option<S>>S>> =>
const fromEffect: <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Stream<A, E, R>
Creates a stream from an effect.
Example (Creating a stream from an effect)
import { Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const stream = Stream.fromEffect(Effect.succeed(42))
const values = yield* Stream.runCollect(stream)
yield* Console.log(values)
})
Effect.runPromise(program)
// Output: [ 42 ]
fromEffect(import EffectEffect.serviceOption(service: Context.Key<I, S>(parameter) service: {
Identifier: Identifier;
Service: Shape;
key: string;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
service))