Latch
Reusable synchronization primitives for coordinating fibers. A Latch is either open or closed: when it is closed, await and whenOpen suspend until the latch opens or the current waiters are released. The module includes effectful and synchronous constructors plus helpers to open, release, close, wait, and gate effects behind the latch.
Combinators
Signature
declare function close(self: Latch): Effect<boolean>;Opens the latch and releases fibers waiting on it.
When to use
Use to open a latch and release all fibers that are waiting on it.
Details
The returned effect succeeds with true when this call changed the latch from closed to open, or false if it was already open.
See
openUnsafefor a synchronous variantreleaseto release waiting fibers without opening the latch
Signature
declare function open(self: Latch): Effect<boolean>;Releases the fibers currently waiting on a closed latch without opening it.
When to use
Use to let the fibers currently waiting on a latch proceed while keeping the latch closed for future waiters.
Details
The returned effect succeeds with true when release was requested while the latch was closed, or false if the latch was already open. Future waiters still suspend until the latch is opened or released again.
See
openfor opening the latch for current and future waiters
Signature
declare function release(self: Latch): Effect<boolean>;Waits on the latch, then runs the provided effect.
When to use
Use to gate another effect so it starts only after the latch is opened or the current waiters are released.
Details
If the latch is open, the effect runs immediately. If it is closed, the returned effect suspends until the latch is opened or the current waiters are released. The provided effect's success, failure, and requirements are preserved.
See
Signature
declare const whenOpen: {
(self: Latch): <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>;
<A, E, R>(self: Latch, effect: Effect<A, E, R>): Effect<A, E, R>;
};Constructors
Creates a Latch inside Effect.
When to use
Use to create a latch for coordinating fibers inside Effect code.
Details
The latch starts closed by default; pass true to create it open.
See
makeUnsafefor synchronous allocation outside Effect code
Signature
declare const make: (open?: boolean) => Effect.Effect<Latch>;makeUnsafe
Creates a Latch synchronously, outside of Effect.
When to use
Use when you need to allocate a Latch synchronously outside an Effect workflow.
Details
The latch starts closed by default; pass true to create it open.
See
makefor creating a latch inside Effect code
Signature
declare const makeUnsafe: (open?: boolean) => Latch;Models
A reusable coordination primitive that lets fibers wait until they are released by the latch.
When to use
Use to coordinate fibers that must wait for an explicit open or release signal before continuing.
Details
A closed latch causes await and whenOpen to suspend. open opens the latch and releases current and future waiters, release releases only current waiters without opening it, and close makes future waiters suspend again.
See
Signature
interface Latch {
readonly await: Effect<void>;
readonly close: Effect<boolean>;
readonly open: Effect<boolean>;
readonly release: Effect<boolean>;
closeUnsafe(this: Latch): boolean;
isOpen(this: Latch): boolean;
openUnsafe(this: Latch): boolean;
whenOpen<A, E, R>(self: Effect<A, E, R>): Effect<A, E, R>;
}Other
Predicates
Unsafe
closeUnsafe
Closes the latch synchronously so future await and whenOpen calls suspend.
When to use
Use to close a latch synchronously when the state change must happen outside an Effect.
Details
Returns true when this call changed the latch from open to closed, or false if it was already closed. This unsafe variant performs the state change immediately instead of returning an Effect.
See
closefor the effectful variantopenUnsafeto synchronously open the latch and release waiting fibers
Signature
declare function closeUnsafe(self: Latch): boolean;openUnsafe
Opens the latch synchronously and releases fibers waiting on it.
When to use
Use when you need synchronous code to open a latch immediately and release the fibers waiting on it.
Details
Returns true when this call changed the latch from closed to open, or false if it was already open. This unsafe variant performs the state change immediately instead of returning an Effect.
See
openfor the effectful variantreleaseto release waiting fibers without opening the latchcloseUnsafefor the synchronous inverse operation
Signature
declare function openUnsafe(self: Latch): boolean;
Closes the latch so future
awaitandwhenOpencalls suspend.When to use
Use to re-enable waiting on a latch after it was opened, so later
awaitandwhenOpencalls suspend again.Details
The returned effect succeeds with
truewhen this call changed the latch from open to closed, orfalseif it was already closed.See
closeUnsafefor a synchronous variantopenfor opening the latch for current and future waiters