drift/js/channel
A channel, similar to a gleam/erlang
Subject
.
Provided for convenience, not required to use drift_js
.
Types
An unbounded single-consumer channel with synchronous sending
and asynchronous receiving.
Intended to be used similarly to a gleam/erlang
Subject
.
pub type Channel(a)
pub type ReceiveError {
AlreadyReceiving
ReceiveTimeout
}
Constructors
-
AlreadyReceiving
A promise previously returned from
receive
has not yet resolved. -
ReceiveTimeout
The receive timed out
Values
pub fn receive(
from channel: Channel(a),
within timeout: Int,
) -> promise.Promise(Result(a, ReceiveError))
Returns a promise that will resolve with a value when the channel has one available. The promise will resolve with an error if the timeout (in milliseconds) is reached before a value is available, or if a receive is already active. The promise may resolve synchronously if a value is already available, and will resolve synchronously if a receive is already active.
pub fn receive_forever(
from channel: Channel(a),
) -> promise.Promise(Result(a, Nil))
Returns a promise that will resolve with a value when the channel has one available, or an error, if a receive is already active. The promise may resolve synchronously if a value is already available, and will resolve synchronously for errors. Returning the error in the promise provides a more ergonomic interface, even though it’s always synchronous.
pub fn send(channel: Channel(a), value: a) -> Nil
Sends a value to the channel, either completing a promise, or queuing the value for future receives.
pub fn try_receive(channel: Channel(a)) -> Result(a, Nil)
Tries to receive a value synchronously, and returns an error if no value is available.