<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
}
): Channel<
Arr.NonEmptyReadonlyArray<A>,
E,
void,
unknown,
unknown,
unknown,
Exclude<R, Scope.Scope>
>Creates a Channel that interacts with a callback function using a queue, emitting arrays.
Example (Creating array channels from callbacks)
import { Channel, Effect, Queue } from "effect"
const channel = Channel.callbackArray<number>(Effect.fn(function*(queue) {
yield* Queue.offer(queue, 1)
yield* Queue.offer(queue, 2)
}))
// Emits arrays of numbers instead of individual numbersexport const const callbackArray: <
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
}
) => Channel<
Arr.NonEmptyReadonlyArray<A>,
E,
void,
unknown,
unknown,
unknown,
Exclude<R, Scope.Scope>
>
Creates a Channel that interacts with a callback function using a queue, emitting arrays.
Example (Creating array channels from callbacks)
import { Channel, Effect, Queue } from "effect"
const channel = Channel.callbackArray<number>(Effect.fn(function*(queue) {
yield* Queue.offer(queue, 1)
yield* Queue.offer(queue, 2)
}))
// Emits arrays of numbers instead of individual numbers
callbackArray = <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;
}): Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, 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;
}): Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, 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;
}): Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, 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.interface Queue<in out A, in out E = never>A Queue is an asynchronous queue that can be offered to and taken from.
Details
It also supports signaling that it is done or failed.
Example (Offering and taking queue values)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
// Create a bounded queue
const queue = yield* Queue.bounded<string>(10)
// Producer: offer items to the queue
yield* Queue.offer(queue, "hello")
yield* Queue.offerAll(queue, ["world", "!"])
// Consumer: take items from the queue
const item1 = yield* Queue.take(queue)
const item2 = yield* Queue.take(queue)
const item3 = yield* Queue.take(queue)
console.log([item1, item2, item3]) // ["hello", "world", "!"]
})
Companion namespace containing type-level metadata and low-level state types
for Queue.
Queue<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;
}): Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, 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;
}): Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, Exclude<R, Scope.Scope>>
E | import CauseCause.type Cause.Done = /*unresolved*/ anyDone>) => import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<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;
}): Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, 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;
}): Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, Exclude<R, Scope.Scope>>
R | import ScopeScope.Scope>,
options: | {
readonly bufferSize?: number | undefined
readonly strategy?:
| "sliding"
| "dropping"
| "suspend"
| undefined
}
| undefined
options?: {
readonly bufferSize?: number | undefinedbufferSize?: number | undefined
readonly strategy?: | "sliding"
| "dropping"
| "suspend"
| undefined
strategy?: "sliding" | "dropping" | "suspend" | undefined
}
): interface Channel<out OutElem, out OutErr = never, out OutDone = void, in InElem = unknown, in InErr = unknown, in InDone = unknown, out Env = never>A Channel is a nexus of I/O operations, which supports both reading and
writing. A channel may read values of type InElem and write values of type
OutElem. When the channel finishes, it yields a value of type OutDone. A
channel may fail with a value of type OutErr.
Details
Channels are the foundation of Streams: both streams and sinks are built on
channels. Most users shouldn't have to use channels directly, as streams and
sinks are much more convenient and cover all common use cases. However, when
adding new stream and sink operators, or doing something highly specialized,
it may be useful to use channels directly.
Channels compose in a variety of ways:
- Piping: One channel can be piped to another channel, assuming the
input type of the second is the same as the output type of the first.
- Sequencing: The terminal value of one channel can be used to create
another channel, and both the first channel and the function that makes
the second channel can be composed into a channel.
- Concatenating: The output of one channel can be used to create other
channels, which are all concatenated together. The first channel and the
function that makes the other channels can be composed into a channel.
Example (Typing channels)
import type { Channel } from "effect"
// A channel that outputs numbers and requires no environment
type NumberChannel = Channel.Channel<number>
// A channel that outputs strings, can fail with Error, completes with boolean
type StringChannel = Channel.Channel<string, Error, boolean>
// A channel with all type parameters specified
type FullChannel = Channel.Channel<
string, // OutElem - output elements
Error, // OutErr - output errors
number, // OutDone - completion value
number, // InElem - input elements
string, // InErr - input errors
boolean, // InDone - input completion
{ db: string } // Env - required environment
>
Channel<import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<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;
}): Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, 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;
}): Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, Exclude<R, Scope.Scope>>
E, void, unknown, unknown, unknown, 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;
}): Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, Exclude<R, Scope.Scope>>
R, import ScopeScope.Scope>> =>
const fromTransform: <
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
EX,
EnvX,
Env
>(
transform: (
upstream: Pull.Pull<InElem, InErr, InDone>,
scope: Scope.Scope
) => Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone, EnvX>,
EX,
Env
>
) => Channel<
OutElem,
Pull.ExcludeDone<OutErr> | EX,
OutDone,
InElem,
InErr,
InDone,
Env | EnvX
>
Creates a Channel from a transformation function that operates on upstream pulls.
Example (Creating channels from transforms)
import { Channel, Effect } from "effect"
const channel = Channel.fromTransform((upstream, scope) =>
Effect.succeed(upstream)
)
fromTransform((_: Pull.Pull<
unknown,
unknown,
unknown,
never
>
(parameter) _: {
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;
}
_, scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope) => import EffectEffect.const map: {
<A, B>(f: (a: A) => B): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E, R>
<A, E, R, B>(
self: Effect<A, E, R>,
f: (a: A) => B
): Effect<B, E, R>
}
map(const asyncQueue: <
A,
E = never,
R = never
>(
scope: Scope.Scope,
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
}
) => Effect.Effect<
Queue.Queue<A, Cause.Done<void> | E>,
never,
Exclude<R, Scope.Scope>
>
asyncQueue(scope: Scope.Scope(parameter) scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope, 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), import QueueQueue.const takeAll: <A, E>(
self: Dequeue<A, E>
) => Effect<Arr.NonEmptyArray<A>, E>
Takes all currently available messages, waiting until at least one message
is available when the queue is empty.
When to use
Use when consumers should process the next non-empty batch of buffered
messages instead of repeatedly taking one message at a time.
Details
Returns a non-empty array. If the queue completes or fails before a message
can be taken, the effect fails with the queue's terminal error.
Example (Taking all available values)
import { Cause, Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number, Cause.Done>(5)
// Add several messages
yield* Queue.offerAll(queue, [1, 2, 3, 4, 5])
// Take all available messages
const messages1 = yield* Queue.takeAll(queue)
console.log(messages1) // [1, 2, 3, 4, 5]
})
takeAll))