<A, E = never, R = never>(
f: (
queue: Queue.Queue<A, E | Cause.Done>
) => Effect.Effect<unknown, E, R | Scope.Scope>,
options?: {
readonly bufferSize?: number | undefined
readonly strategy?: "sliding" | "dropping" | "suspend" | undefined
}
): Stream<A, E, Exclude<R, Scope.Scope>>Creates a stream from a callback that can emit values into a queue.
When to use
Use when you need callback-based code to emit stream values by offering to a
Queue, or signal stream completion through the Queue module APIs.
By default it uses an "unbounded" buffer size.
You can customize the buffer size and strategy by passing an object as the
second argument with the bufferSize and strategy fields.
Example (Creating a stream from a callback that can emit values into a queue)
import { Console, Effect, Queue, Stream } from "effect"
const stream = Stream.callback<number>((queue) =>
Effect.sync(() => {
// Emit values to the stream
Queue.offerUnsafe(queue, 1)
Queue.offerUnsafe(queue, 2)
Queue.offerUnsafe(queue, 3)
// Signal completion
Queue.endUnsafe(queue)
})
)
const program = Effect.gen(function*() {
const values = yield* stream.pipe(Stream.runCollect)
yield* Console.log(values)
// [ 1, 2, 3 ]
})
Effect.runPromise(program)export const const callback: <A, E = never, R = never>(
f: (
queue: Queue.Queue<A, E | Cause.Done>
) => Effect.Effect<unknown, E, R | Scope.Scope>,
options?: {
readonly bufferSize?: number | undefined
readonly strategy?:
| "sliding"
| "dropping"
| "suspend"
| undefined
}
) => Stream<A, E, Exclude<R, Scope.Scope>>
Creates a stream from a callback that can emit values into a queue.
When to use
Use when you need callback-based code to emit stream values by offering to a
Queue, or signal stream completion through the Queue module APIs.
By default it uses an "unbounded" buffer size.
You can customize the buffer size and strategy by passing an object as the
second argument with the bufferSize and strategy fields.
Example (Creating a stream from a callback that can emit values into a queue)
import { Console, Effect, Queue, Stream } from "effect"
const stream = Stream.callback<number>((queue) =>
Effect.sync(() => {
// Emit values to the stream
Queue.offerUnsafe(queue, 1)
Queue.offerUnsafe(queue, 2)
Queue.offerUnsafe(queue, 3)
// Signal completion
Queue.endUnsafe(queue)
})
)
const program = Effect.gen(function*() {
const values = yield* stream.pipe(Stream.runCollect)
yield* Console.log(values)
// [ 1, 2, 3 ]
})
Effect.runPromise(program)
callback = <function (type parameter) A in <A, E = never, R = never>(f: (queue: Queue.Queue<A, E | Cause.Done>) => Effect.Effect<unknown, E, R | Scope.Scope>, options?: {
readonly bufferSize?: number | undefined;
readonly strategy?: "sliding" | "dropping" | "suspend" | undefined;
}): Stream<A, E, Exclude<R, Scope.Scope>>
A, function (type parameter) E in <A, E = never, R = never>(f: (queue: Queue.Queue<A, E | Cause.Done>) => Effect.Effect<unknown, E, R | Scope.Scope>, options?: {
readonly bufferSize?: number | undefined;
readonly strategy?: "sliding" | "dropping" | "suspend" | undefined;
}): Stream<A, E, Exclude<R, Scope.Scope>>
E = never, function (type parameter) R in <A, E = never, R = never>(f: (queue: Queue.Queue<A, E | Cause.Done>) => Effect.Effect<unknown, E, R | Scope.Scope>, options?: {
readonly bufferSize?: number | undefined;
readonly strategy?: "sliding" | "dropping" | "suspend" | undefined;
}): Stream<A, E, Exclude<R, Scope.Scope>>
R = never>(
f: (
queue: Queue.Queue<A, E | Cause.Done>
) => Effect.Effect<unknown, E, R | Scope.Scope>
f: (queue: Queue.Queue<A, E | Cause.Done>(parameter) queue: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
queue: import QueueQueue.type Queue.Queue = /*unresolved*/ anyQueue<function (type parameter) A in <A, E = never, R = never>(f: (queue: Queue.Queue<A, E | Cause.Done>) => Effect.Effect<unknown, E, R | Scope.Scope>, options?: {
readonly bufferSize?: number | undefined;
readonly strategy?: "sliding" | "dropping" | "suspend" | undefined;
}): Stream<A, E, Exclude<R, Scope.Scope>>
A, function (type parameter) E in <A, E = never, R = never>(f: (queue: Queue.Queue<A, E | Cause.Done>) => Effect.Effect<unknown, E, R | Scope.Scope>, options?: {
readonly bufferSize?: number | undefined;
readonly strategy?: "sliding" | "dropping" | "suspend" | undefined;
}): Stream<A, E, Exclude<R, Scope.Scope>>
E | import CauseCause.type Cause.Done = /*unresolved*/ anyDone>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<unknown, function (type parameter) E in <A, E = never, R = never>(f: (queue: Queue.Queue<A, E | Cause.Done>) => Effect.Effect<unknown, E, R | Scope.Scope>, options?: {
readonly bufferSize?: number | undefined;
readonly strategy?: "sliding" | "dropping" | "suspend" | undefined;
}): Stream<A, E, Exclude<R, Scope.Scope>>
E, function (type parameter) R in <A, E = never, R = never>(f: (queue: Queue.Queue<A, E | Cause.Done>) => Effect.Effect<unknown, E, R | Scope.Scope>, options?: {
readonly bufferSize?: number | undefined;
readonly strategy?: "sliding" | "dropping" | "suspend" | undefined;
}): Stream<A, E, Exclude<R, Scope.Scope>>
R | import ScopeScope.type Scope.Scope = /*unresolved*/ anyScope>,
options: | {
readonly bufferSize?: number | undefined
readonly strategy?:
| "sliding"
| "dropping"
| "suspend"
| undefined
}
| undefined
options?: {
readonly bufferSize?: number | undefinedbufferSize?: number | undefined
readonly strategy?: | "suspend"
| "sliding"
| "dropping"
| undefined
strategy?: "sliding" | "dropping" | "suspend" | undefined
}
): 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) A in <A, E = never, R = never>(f: (queue: Queue.Queue<A, E | Cause.Done>) => Effect.Effect<unknown, E, R | Scope.Scope>, options?: {
readonly bufferSize?: number | undefined;
readonly strategy?: "sliding" | "dropping" | "suspend" | undefined;
}): Stream<A, E, Exclude<R, Scope.Scope>>
A, function (type parameter) E in <A, E = never, R = never>(f: (queue: Queue.Queue<A, E | Cause.Done>) => Effect.Effect<unknown, E, R | Scope.Scope>, options?: {
readonly bufferSize?: number | undefined;
readonly strategy?: "sliding" | "dropping" | "suspend" | undefined;
}): Stream<A, E, Exclude<R, Scope.Scope>>
E, type Exclude<T, U> = T extends U
? never
: T
Exclude from T those types that are assignable to U
Exclude<function (type parameter) R in <A, E = never, R = never>(f: (queue: Queue.Queue<A, E | Cause.Done>) => Effect.Effect<unknown, E, R | Scope.Scope>, options?: {
readonly bufferSize?: number | undefined;
readonly strategy?: "sliding" | "dropping" | "suspend" | undefined;
}): Stream<A, E, Exclude<R, Scope.Scope>>
R, import ScopeScope.type Scope.Scope = /*unresolved*/ anyScope>> => const fromChannel: <
Arr extends Arr.NonEmptyReadonlyArray<any>,
E,
R
>(
channel: Channel.Channel<
Arr,
E,
void,
unknown,
unknown,
unknown,
R
>
) => Stream<
Arr extends Arr.NonEmptyReadonlyArray<infer A>
? A
: never,
E,
R
>
Creates a stream from a array-emitting Channel.
Example (Creating a stream from an array-emitting channel)
import { Channel, Console, Effect, Stream } from "effect"
const program = Effect.gen(function*() {
const channel = Channel.succeed([1, 2, 3] as const)
const stream = Stream.fromChannel(channel)
const result = yield* Stream.runCollect(stream)
yield* Console.log(result)
})
// Output: [ 1, 2, 3 ]
fromChannel(import ChannelChannel.callbackArray(f: (
queue: Queue.Queue<A, E | Cause.Done>
) => Effect.Effect<unknown, E, R | Scope.Scope>
f, options: | {
readonly bufferSize?: number | undefined
readonly strategy?:
| "sliding"
| "dropping"
| "suspend"
| undefined
}
| undefined
options))