Skip to content

Concurrency Domain

The Concurrency domain unlocks the power of doing multiple things at once.

It provides Fibers (lightweight threads) and Channels for safe communication. Whether you're fetching two APIs in parallel or building a background worker, this domain gives you the high-level tools (par, scope) to write concurrent code that doesn't melt your brain.

aivi
use aivi.concurrency as concurrent

Types

aivi
type Scope = Unit
type ChannelError = Closed

Functions

FunctionExplanation
par left right
Effect E A -> Effect E B -> Effect E (A, B)
Runs both effects concurrently and returns both results; fails if either fails.
scope run
(Scope -> Effect E A) -> Effect E A
Creates a structured concurrency scope and runs run (current Scope is Unit).

Channels

Channels provide a mechanism for synchronization and communication between concurrent fibers.

make

FunctionExplanation
make sample
A -> Effect E (Sender A, Receiver A)
Creates a new channel and returns (Sender, Receiver).

send

FunctionExplanation
send sender value
Sender A -> A -> Effect E Unit
Sends value to the channel; may block if buffered and full or no receiver is ready.

recv

FunctionExplanation
recv receiver
Receiver A -> Effect E (Result A ChannelError)
Waits for the next value; returns Ok value or Err Closed.

close

FunctionExplanation
close sender
Sender A -> Effect E Unit
Closes the channel from the sender side; receivers observe Err Closed.