UNPKG

suspenders-js

Version:

Asynchronous programming library utilizing coroutines, functional reactive programming and structured concurrency.

36 lines (35 loc) 1.45 kB
import { Suspender } from "./Types"; /** * Channels are used to send and receive messages between coroutines. Channels can be buffered so * that senders do not suspend if there isn't a receiver waiting to receive the next message. * If there are multiple coroutines receiving on the same channel, they receive new values in first * come first served order. */ export declare class Channel<T> { private _receiverSuspenders; private _buffer; private _bufferSize; constructor(options?: { bufferSize?: number; }); /** * Receives a message on this channel. A buffered message is received or if there is none, this * suspends the receiver until one is available. Multiple suspended receivers are resumed in first * come first served order. * @param resultCallback */ receive: Suspender<T>; /** * Sends a message on this channel. If there is a queued receiver coroutine, it is resumed * immediately to process the message. If there are no queued receivers and buffer is full, the * sending coroutine is suspened until next message is received on this channel. Multiple * suspended senders are resumed in order they were suspended. * @param value */ send(value: T): Suspender<void>; /** * Tries to send a message on the channel. Returns true if successful, or false if buffer is full. * @param value */ trySend(value: T): boolean; }