<A, E>(self: Enqueue<A, E>, message: Types.NoInfer<A>): Effect<boolean>Adds a message to the queue. Returns false if the queue is done.
Details
For bounded queues, this operation may suspend if the queue is at capacity, depending on the backpressure strategy. For dropping/sliding queues, it may return false or succeed immediately by dropping/sliding existing messages.
Example (Offering a value)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number>(3)
// Successfully add messages to queue
const success1 = yield* Queue.offer(queue, 1)
const success2 = yield* Queue.offer(queue, 2)
console.log(success1, success2) // true, true
// Queue state
const size = yield* Queue.size(queue)
console.log(size) // 2
})export const const offer: <A, E>(
self: Enqueue<A, E>,
message: Types.NoInfer<A>
) => Effect<boolean>
Adds a message to the queue. Returns false if the queue is done.
Details
For bounded queues, this operation may suspend if the queue is at capacity,
depending on the backpressure strategy. For dropping/sliding queues, it may
return false or succeed immediately by dropping/sliding existing messages.
Example (Offering a value)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number>(3)
// Successfully add messages to queue
const success1 = yield* Queue.offer(queue, 1)
const success2 = yield* Queue.offer(queue, 2)
console.log(success1, success2) // true, true
// Queue state
const size = yield* Queue.size(queue)
console.log(size) // 2
})
offer = <function (type parameter) A in <A, E>(self: Enqueue<A, E>, message: Types.NoInfer<A>): Effect<boolean>A, function (type parameter) E in <A, E>(self: Enqueue<A, E>, message: Types.NoInfer<A>): Effect<boolean>E>(self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self: interface Enqueue<in A, in E = never>An Enqueue is a queue that can be offered to.
Details
This interface represents the write-only part of a Queue, allowing you to offer
elements to the queue but not take elements from it.
Example (Offering through enqueue handles)
import { Effect, Queue } from "effect"
// Function that only needs write access to a queue
const producer = (enqueue: Queue.Enqueue<string>) =>
Effect.gen(function*() {
yield* Queue.offer(enqueue, "hello")
yield* Queue.offerAll(enqueue, ["world", "!"])
})
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<string>(10)
yield* producer(queue)
})
Companion namespace containing type-level metadata for the Enqueue
write-only queue interface.
Enqueue<function (type parameter) A in <A, E>(self: Enqueue<A, E>, message: Types.NoInfer<A>): Effect<boolean>A, function (type parameter) E in <A, E>(self: Enqueue<A, E>, message: Types.NoInfer<A>): Effect<boolean>E>, message: Types.NoInfer<A>message: import TypesTypes.type NoInfer<A> = [A][A extends any
? 0
: never]
Prevents TypeScript from inferring a type parameter from a specific
position.
When to use
Use when a function parameter must match an inferred type without becoming
an inference source.
Details
The parameter using NoInfer must still match the inferred type.
Example (Controlling inference)
import type { Types } from "effect"
declare function withDefault<T>(value: T, fallback: Types.NoInfer<T>): T
// T is inferred as "a" | "b" from the first argument only
const result = withDefault<"a" | "b">("a", "b")
NoInfer<function (type parameter) A in <A, E>(self: Enqueue<A, E>, message: Types.NoInfer<A>): Effect<boolean>A>): 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<boolean> =>
import internalEffectinternalEffect.const suspend: <A, E, R>(
evaluate: LazyArg<Effect.Effect<A, E, R>>
) => Effect.Effect<A, E, R>
suspend(() => {
if (self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self.Enqueue<in A, in E = never>.state: Queue.State<any, any>state._tag: "Open" | "Closing" | "Done"_tag !== "Open") {
return const exitFalse: Exit<boolean, never>exitFalse
} else if (self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self.Enqueue<in A, in E = never>.messages: MutableList.MutableList<any>(property) Enqueue<in A, in E = never>.messages: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
messages.MutableList<any>.length: numberlength >= self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self.Enqueue<in A, in E = never>.capacity: numbercapacity) {
switch (self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self.Enqueue<in A, in E = never>.strategy: "suspend" | "dropping" | "sliding"strategy) {
case "dropping":
return const exitFalse: Exit<boolean, never>exitFalse
case "suspend":
if (self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self.Enqueue<in A, in E = never>.capacity: numbercapacity <= 0 && self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self.Enqueue<in A, in E = never>.state: {
readonly _tag: "Open";
readonly takers: Set<(_: Effect<void, any, never>) => void>;
readonly offers: Set<Queue.OfferEntry<any>>;
readonly awaiters: Set<(_: Effect<void, any, never>) => void>;
}
state.takers: Set<(_: Effect<void, E>) => void>takers.Set<(_: Effect<void, any, never>) => void>.size: numbersize > 0) {
import MutableListMutableList.const append: <A>(
self: MutableList<A>,
message: A
) => void
Appends an element to the end of the MutableList.
This operation is optimized for high-frequency usage.
Example (Appending elements)
import { MutableList } from "effect"
const list = MutableList.make<number>()
// Append elements one by one
MutableList.append(list, 1)
MutableList.append(list, 2)
MutableList.append(list, 3)
console.log(list.length) // 3
// Elements are taken from head (FIFO)
console.log(MutableList.take(list)) // 1
console.log(MutableList.take(list)) // 2
console.log(MutableList.take(list)) // 3
// High-throughput usage
for (let i = 0; i < 10000; i++) {
MutableList.append(list, i)
}
append(self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self.Enqueue<in A, in E = never>.messages: MutableList.MutableList<any>(property) Enqueue<in A, in E = never>.messages: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
messages, message: Types.NoInfer<A>message)
const releaseTakers: <A, E>(
self: Enqueue<A, E>
) => void
releaseTakers(self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self as 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>(self: Enqueue<A, E>, message: Types.NoInfer<A>): Effect<boolean>A, function (type parameter) E in <A, E>(self: Enqueue<A, E>, message: Types.NoInfer<A>): Effect<boolean>E>)
return const exitTrue: Exit<boolean, never>exitTrue
}
return const offerRemainingSingle: <A, E>(
self: Enqueue<A, E>,
message: A
) => Effect<boolean, never, never>
offerRemainingSingle(self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self as 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>(self: Enqueue<A, E>, message: Types.NoInfer<A>): Effect<boolean>A, function (type parameter) E in <A, E>(self: Enqueue<A, E>, message: Types.NoInfer<A>): Effect<boolean>E>, message: Types.NoInfer<A>message)
case "sliding":
import MutableListMutableList.const take: <A>(
self: MutableList<A>
) => Empty | A
Takes a single element from the beginning of the MutableList.
Returns the element if available, or the Empty symbol if the list is empty.
The taken element is removed from the list.
Example (Taking one element)
import { MutableList } from "effect"
const list = MutableList.make<string>()
MutableList.appendAll(list, ["first", "second", "third"])
// Take elements one by one
console.log(MutableList.take(list)) // "first"
console.log(list.length) // 2
console.log(MutableList.take(list)) // "second"
console.log(MutableList.take(list)) // "third"
console.log(list.length) // 0
// Take from empty list
console.log(MutableList.take(list)) // Empty symbol
// Check for empty using the Empty symbol
const result = MutableList.take(list)
if (result === MutableList.Empty) {
console.log("List is empty")
} else {
console.log("Got element:", result)
}
// Consumer pattern
function processNext<T>(
queue: MutableList.MutableList<T>,
processor: (item: T) => void
): boolean {
const item = MutableList.take(queue)
if (item !== MutableList.Empty) {
processor(item)
return true
}
return false
}
take(self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self.Enqueue<in A, in E = never>.messages: MutableList.MutableList<any>(property) Enqueue<in A, in E = never>.messages: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
messages)
import MutableListMutableList.const append: <A>(
self: MutableList<A>,
message: A
) => void
Appends an element to the end of the MutableList.
This operation is optimized for high-frequency usage.
Example (Appending elements)
import { MutableList } from "effect"
const list = MutableList.make<number>()
// Append elements one by one
MutableList.append(list, 1)
MutableList.append(list, 2)
MutableList.append(list, 3)
console.log(list.length) // 3
// Elements are taken from head (FIFO)
console.log(MutableList.take(list)) // 1
console.log(MutableList.take(list)) // 2
console.log(MutableList.take(list)) // 3
// High-throughput usage
for (let i = 0; i < 10000; i++) {
MutableList.append(list, i)
}
append(self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self.Enqueue<in A, in E = never>.messages: MutableList.MutableList<any>(property) Enqueue<in A, in E = never>.messages: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
messages, message: Types.NoInfer<A>message)
return const exitTrue: Exit<boolean, never>exitTrue
}
}
import MutableListMutableList.const append: <A>(
self: MutableList<A>,
message: A
) => void
Appends an element to the end of the MutableList.
This operation is optimized for high-frequency usage.
Example (Appending elements)
import { MutableList } from "effect"
const list = MutableList.make<number>()
// Append elements one by one
MutableList.append(list, 1)
MutableList.append(list, 2)
MutableList.append(list, 3)
console.log(list.length) // 3
// Elements are taken from head (FIFO)
console.log(MutableList.take(list)) // 1
console.log(MutableList.take(list)) // 2
console.log(MutableList.take(list)) // 3
// High-throughput usage
for (let i = 0; i < 10000; i++) {
MutableList.append(list, i)
}
append(self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self.Enqueue<in A, in E = never>.messages: MutableList.MutableList<any>(property) Enqueue<in A, in E = never>.messages: {
head: MutableList.Bucket<A> | undefined;
tail: MutableList.Bucket<A> | undefined;
length: number;
}
messages, message: Types.NoInfer<A>message)
const scheduleReleaseTaker: <A, E>(
self: Enqueue<A, E>
) => void
scheduleReleaseTaker(self: Enqueue<A, E>(parameter) self: {
strategy: "suspend" | "dropping" | "sliding";
dispatcher: SchedulerDispatcher;
capacity: number;
messages: MutableList.MutableList<any>;
state: Queue.State<any, any>;
scheduleRunning: boolean;
toString: () => string;
toJSON: () => unknown;
}
self as 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>(self: Enqueue<A, E>, message: Types.NoInfer<A>): Effect<boolean>A, function (type parameter) E in <A, E>(self: Enqueue<A, E>, message: Types.NoInfer<A>): Effect<boolean>E>)
return const exitTrue: Exit<boolean, never>exitTrue
})