<A = unknown>(
target: EventListener<A>,
type: string,
options?:
| boolean
| {
readonly capture?: boolean
readonly passive?: boolean
readonly once?: boolean
readonly bufferSize?: number | undefined
}
| undefined
): Stream<A>Creates a stream from an event listener.
Example (Creating a stream from an event listener)
import { Effect, Stream } from "effect"
class NumberTarget implements Stream.EventListener<number> {
addEventListener(event: string, f: (event: number) => void) {
if (event === "data") {
f(1)
f(2)
f(3)
}
}
removeEventListener(_event: string, _f: (event: number) => void) {}
}
Effect.runPromise(Effect.gen(function*() {
const stream = Stream.fromEventListener(new NumberTarget(), "data").pipe(
Stream.take(3)
)
const values = yield* Stream.runCollect(stream)
yield* Effect.sync(() => console.log(values))
}))
// [ 1, 2, 3 ]export const const fromEventListener: <A = unknown>(
target: EventListener<A>,
type: string,
options?:
| boolean
| {
readonly capture?: boolean
readonly passive?: boolean
readonly once?: boolean
readonly bufferSize?: number | undefined
}
| undefined
) => Stream<A>
Creates a stream from an event listener.
Example (Creating a stream from an event listener)
import { Effect, Stream } from "effect"
class NumberTarget implements Stream.EventListener<number> {
addEventListener(event: string, f: (event: number) => void) {
if (event === "data") {
f(1)
f(2)
f(3)
}
}
removeEventListener(_event: string, _f: (event: number) => void) {}
}
Effect.runPromise(Effect.gen(function*() {
const stream = Stream.fromEventListener(new NumberTarget(), "data").pipe(
Stream.take(3)
)
const values = yield* Stream.runCollect(stream)
yield* Effect.sync(() => console.log(values))
}))
// [ 1, 2, 3 ]
fromEventListener = <function (type parameter) A in <A = unknown>(target: EventListener<A>, type: string, options?: boolean | {
readonly capture?: boolean;
readonly passive?: boolean;
readonly once?: boolean;
readonly bufferSize?: number | undefined;
} | undefined): Stream<A>
A = unknown>(
target: EventListener<A>(parameter) target: {
addEventListener: (event: string, f: (event: A) => void, options?: { readonly capture?: boolean; readonly passive?: boolean; readonly once?: boolean; readonly signal?: AbortSignal } | boolean) => void;
removeEventListener: (event: string, f: (event: A) => void, options?: { readonly capture?: boolean } | boolean) => void;
}
target: interface EventListener<A>Interface representing an event listener target.
EventListener<function (type parameter) A in <A = unknown>(target: EventListener<A>, type: string, options?: boolean | {
readonly capture?: boolean;
readonly passive?: boolean;
readonly once?: boolean;
readonly bufferSize?: number | undefined;
} | undefined): Stream<A>
A>,
type: stringtype: string,
options: | boolean
| {
readonly capture?: boolean
readonly passive?: boolean
readonly once?: boolean
readonly bufferSize?: number | undefined
}
| undefined
options?: boolean | {
readonly capture?: boolean | undefinedcapture?: boolean
readonly passive?: boolean | undefinedpassive?: boolean
readonly once?: boolean | undefinedonce?: boolean
readonly bufferSize?: number | undefinedbufferSize?: number | undefined
} | 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 = unknown>(target: EventListener<A>, type: string, options?: boolean | {
readonly capture?: boolean;
readonly passive?: boolean;
readonly once?: boolean;
readonly bufferSize?: number | undefined;
} | undefined): Stream<A>
A> =>
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 = unknown>(target: EventListener<A>, type: string, options?: boolean | {
readonly capture?: boolean;
readonly passive?: boolean;
readonly once?: boolean;
readonly bufferSize?: number | undefined;
} | undefined): Stream<A>
A>((queue: Queue.Queue<A, Cause.Done<void>>(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) => {
function function (local function) emit(event: A): voidemit(event: A = unknownevent: function (type parameter) A in <A = unknown>(target: EventListener<A>, type: string, options?: boolean | {
readonly capture?: boolean;
readonly passive?: boolean;
readonly once?: boolean;
readonly bufferSize?: number | undefined;
} | undefined): Stream<A>
A) {
import QueueQueue.offerUnsafe(queue: Queue.Queue<A, Cause.Done<void>>(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, event: A = unknownevent)
}
return import EffectEffect.acquireRelease(
import EffectEffect.sync(() => target: EventListener<A>(parameter) target: {
addEventListener: (event: string, f: (event: A) => void, options?: { readonly capture?: boolean; readonly passive?: boolean; readonly once?: boolean; readonly signal?: AbortSignal } | boolean) => void;
removeEventListener: (event: string, f: (event: A) => void, options?: { readonly capture?: boolean } | boolean) => void;
}
target.EventListener<A>.addEventListener(event: string, f: (event: A) => void, options?: {
readonly capture?: boolean;
readonly passive?: boolean;
readonly once?: boolean;
readonly signal?: AbortSignal;
} | boolean): void
addEventListener(type: stringtype, function (local function) emit(event: A): voidemit, options: | boolean
| {
readonly capture?: boolean
readonly passive?: boolean
readonly once?: boolean
readonly bufferSize?: number | undefined
}
| undefined
options)),
() => import EffectEffect.sync(() => target: EventListener<A>(parameter) target: {
addEventListener: (event: string, f: (event: A) => void, options?: { readonly capture?: boolean; readonly passive?: boolean; readonly once?: boolean; readonly signal?: AbortSignal } | boolean) => void;
removeEventListener: (event: string, f: (event: A) => void, options?: { readonly capture?: boolean } | boolean) => void;
}
target.EventListener<A>.removeEventListener(event: string, f: (event: A) => void, options?: {
readonly capture?: boolean;
} | boolean): void
removeEventListener(type: stringtype, function (local function) emit(event: A): voidemit, options: | boolean
| {
readonly capture?: boolean
readonly passive?: boolean
readonly once?: boolean
readonly bufferSize?: number | undefined
}
| undefined
options))
)
}, { bufferSize?: number | undefinedbufferSize: typeof options: | boolean
| {
readonly capture?: boolean
readonly passive?: boolean
readonly once?: boolean
readonly bufferSize?: number | undefined
}
| undefined
options === "object" ? options: {
readonly capture?: boolean
readonly passive?: boolean
readonly once?: boolean
readonly bufferSize?: number | undefined
}
options.bufferSize?: number | undefinedbufferSize : var undefinedundefined })