<A, E>(self: Dequeue<A, E>): Effect<Option.Option<A>>Attempts to take one item from the queue without waiting.
Details
Returns Option.some when an item is immediately available. Returns
Option.none when no item is available, when the queue is done, or when the
immediate take observes a queue failure.
Example (Polling without blocking)
import { Effect, Option, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number>(10)
// Poll returns Option.none if empty
const maybe1 = yield* Queue.poll(queue)
console.log(Option.isNone(maybe1)) // true
// Add an item
yield* Queue.offer(queue, 42)
// Poll returns Option.some with the item
const maybe2 = yield* Queue.poll(queue)
console.log(Option.getOrNull(maybe2)) // 42
})export const const poll: <A, E>(
self: Dequeue<A, E>
) => Effect<Option.Option<A>>
Attempts to take one item from the queue without waiting.
Details
Returns Option.some when an item is immediately available. Returns
Option.none when no item is available, when the queue is done, or when the
immediate take observes a queue failure.
Example (Polling without blocking)
import { Effect, Option, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number>(10)
// Poll returns Option.none if empty
const maybe1 = yield* Queue.poll(queue)
console.log(Option.isNone(maybe1)) // true
// Add an item
yield* Queue.offer(queue, 42)
// Poll returns Option.some with the item
const maybe2 = yield* Queue.poll(queue)
console.log(Option.getOrNull(maybe2)) // 42
})
poll = <function (type parameter) A in <A, E>(self: Dequeue<A, E>): Effect<Option.Option<A>>A, function (type parameter) E in <A, E>(self: Dequeue<A, E>): Effect<Option.Option<A>>E>(self: Dequeue<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 Dequeue<out A, out E = never>A Dequeue is a queue that can be taken from.
Details
This interface represents the read-only part of a Queue, allowing you to take
elements from the queue but not offer elements to it.
Example (Taking through dequeue handles)
import { Effect, Queue } from "effect"
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<string, never>(10)
// A Dequeue can only take elements
const dequeue: Queue.Dequeue<string> = queue
// Pre-populate the queue
yield* Queue.offerAll(queue, ["a", "b", "c"])
// Take elements using dequeue interface
const item = yield* Queue.take(dequeue)
console.log(item) // "a"
})
Companion namespace containing type-level metadata for the Dequeue
read-only queue interface.
Dequeue<function (type parameter) A in <A, E>(self: Dequeue<A, E>): Effect<Option.Option<A>>A, function (type parameter) E in <A, E>(self: Dequeue<A, E>): Effect<Option.Option<A>>E>): 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<import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) A in <A, E>(self: Dequeue<A, E>): Effect<Option.Option<A>>A>> =>
import internalEffectinternalEffect.const suspend: <A, E, R>(
evaluate: LazyArg<Effect.Effect<A, E, R>>
) => Effect.Effect<A, E, R>
suspend(() => {
const const result: Exit<A, E> | undefinedresult = const takeUnsafe: <A, E>(
self: Dequeue<A, E>
) => Exit<A, E> | undefined
Attempts to take one message from the queue synchronously.
When to use
Use when polling queue internals must not suspend or register a waiting taker,
and undefined is an acceptable result for an empty queue.
Details
Returns an Exit for an immediately available message or for the queue's
terminal state. Returns undefined when no message is immediately available.
This operation does not wait or register a taker.
Example (Taking one value synchronously)
import { Effect, Queue } from "effect"
// Create a queue and use unsafe operations
const program = Effect.gen(function*() {
const queue = yield* Queue.bounded<number>(10)
// Add some messages
Queue.offerUnsafe(queue, 1)
Queue.offerUnsafe(queue, 2)
// Take a message synchronously
const result1 = Queue.takeUnsafe(queue)
console.log(result1) // Success(1) or Exit containing value 1
const result2 = Queue.takeUnsafe(queue)
console.log(result2) // Success(2)
// No more messages - returns undefined
const result3 = Queue.takeUnsafe(queue)
console.log(result3) // undefined
})
takeUnsafe(self: Dequeue<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)
if (const result: Exit<A, E> | undefinedresult === var undefinedundefined) {
return import internalEffectinternalEffect.const succeed: <A>(
value: A
) => Effect.Effect<A>
succeed(import OptionOption.const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none())
}
if (const result: Exit<A, E>result._tag: "Failure" | "Success"_tag === "Success") {
return import internalEffectinternalEffect.const succeed: <A>(
value: A
) => Effect.Effect<A>
succeed(import OptionOption.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(const result: Success<A, E>const result: {
_tag: "Success";
value: A;
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;
}
result.Success<A, E>.value: Avalue))
}
return import internalEffectinternalEffect.const succeed: <A>(
value: A
) => Effect.Effect<A>
succeed(import OptionOption.const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none())
})