<I, S>(service: Context.Key<I, S>): Stream<S, never, I>Accesses a service from the context and emits it as a single element.
Example (Accessing a service as a stream)
import { Context, Effect, Stream } from "effect"
class Greeter extends Context.Service<Greeter, {
readonly greet: (name: string) => string
}>()("Greeter") {}
const stream = Stream.service(Greeter).pipe(
Stream.map((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 service: <I, S>(
service: Context.Key<I, S>
) => Stream<S, never, I>
Accesses a service from the context and emits it as a single element.
Example (Accessing a service as a stream)
import { Context, Effect, Stream } from "effect"
class Greeter extends Context.Service<Greeter, {
readonly greet: (name: string) => string
}>()("Greeter") {}
const stream = Stream.service(Greeter).pipe(
Stream.map((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!" ]
service = <function (type parameter) I in <I, S>(service: Context.Key<I, S>): Stream<S, never, I>I, function (type parameter) S in <I, S>(service: Context.Key<I, S>): Stream<S, never, I>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<S, never, I>I, function (type parameter) S in <I, S>(service: Context.Key<I, S>): Stream<S, never, I>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<function (type parameter) S in <I, S>(service: Context.Key<I, S>): Stream<S, never, I>S, never, function (type parameter) I in <I, S>(service: Context.Key<I, S>): Stream<S, never, I>I> => 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 service: <I, S>(
service: Context.Key<I, S>
) => Effect<S, never, I>
Accesses a service from the context.
Example (Accessing a required service)
import { Context, Effect } from "effect"
interface Database {
readonly query: (sql: string) => Effect.Effect<string>
}
const Database = Context.Service<Database>("Database")
const program = Effect.gen(function*() {
const db = yield* Effect.service(Database)
return yield* db.query("SELECT * FROM users")
})
service(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))