<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.interface Key<out Identifier, out Shape>Typed identifier for a service stored in a Context.
When to use
Use as the typed handle for storing, retrieving, and requiring a specific
service in a Context.
Details
Identifier tracks the requirement in Effect types, while Shape is the
service implementation retrieved by the key. A key is also an Effect value,
so yielding it inside Effect.gen retrieves the service from the current
fiber context.
Key<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<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<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.const serviceOption: <I, S>(
key: Context.Key<I, S>
) => Effect<Option<S>>
Optionally accesses a service from the environment.
When to use
Use to read an optional dependency from the current context without making
that dependency part of the effect's required environment.
Details
This function attempts to access a service from the environment. If the
service is available, it returns Some(service). If the service is not
available, it returns None. Unlike service, this function does not
require the service to be present in the environment.
Example (Accessing an optional service)
import { Context, Effect, Option } from "effect"
// Define a service key
const Logger = Context.Service<{
log: (msg: string) => void
}>("Logger")
// Use serviceOption to optionally access the logger
const program = Effect.gen(function*() {
const maybeLogger = yield* Effect.serviceOption(Logger)
if (Option.isSome(maybeLogger)) {
maybeLogger.value.log("Service is available")
} else {
console.log("Service not available")
}
})
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))