Skip to main content
Module

x/async/mod.ts>Condition

🦕 Asynchronous primitive modules loosely port from Python's asyncio for Deno.
Go to Latest
class Condition
Re-export
import { Condition } from "https://deno.land/x/async@v1.1.2/mod.ts";

A Condition object. Not thread-safe.

A condition primitive can be used by a task to wait for some event to happen and then get exclusive access to a shared resource.

In essence, a Condition object combines the functionality of an Event and Lock. It is possible to have multiple Condition objects share one Lock, which allows coordinating exclusive access to a shared resource between different tasks interested in particular states of that shared resource.

The optional lock argument must be a Lock object or undefined. In the latter case a new Lock object is created automatically.

The preferred way to use a Condition is with() method.

Constructors

new
Condition(lock?: Lock)

Methods

acquire(): Promise<true>

Acquire the underlying lock.

This method waits until the underlying lock is unlocked, sets it to locked and returns true.

locked(): boolean

Return true if the underlying lock is acquired.

notify(n?): void

Wake up at most n tasks (1 by default) waiting on this condition. The method is no-op if no tasks are waiting.

The lock must be acquired before this method is called and released shortly after. If called with an unlocked lock an Error is thrown.

notify_all(): void

Wake up all tasks waiting on this condition.

This method acts like notify(), but wakes up all waiting tasks.

The lock must be acquired before this method is called and released shortly after. If called with an unlocked lock an Error is thrown.

release(): void

Release the underlying lock.

When invoked on an unlocked lock, an Error is thrown.

wait(): Promise<true>

Wait until notified.

If the calling task has not acquired the lock when this method is called, an Error is thrown.

This method release the underlying lock, and the blocks until it is awakened by a notify() or notify_all() call. Once awakened, the Condition re-acquires itsock and this method returns true.

wait_for(predicate: () => boolean): Promise<void>

Wait until a predicate becomes true.

The predicate must be a callable which result is a boolean value.

with(callback: () => void | Promise<void>): Promise<void>

Acuire the lock and execute callback to access shared state.

This is preferred way to use a Condition.