import { Channel } from "https://deno.land/x/async_channels@v1.0.0-alpha54/channel.ts";
Properties
Methods
Closes the channel.
Closing a closed channel have no effect (positive or negative).
Sending a message to a closed channel will throw an AbortedError
.
Receiving a message from a closed channel will resolve the promise immediately.
See Channel.receive
for more information.
duplicate creates multiple channels (determined by n
), and consumes this
channel.
The consumed values are then sent to all channels
filter applies fn
to each value in this
channel, and returns a new channel
that will only contain value for which fn
returned true
(or a promise that resolves to true
).
The returned channel will close after this
channel closes (or if the provided signal is triggered).
flat returns a receiver channel that contains the flattened (1 level)
values of each value of this
channel.
The receiver channel will close, when the original channel closes (or if the provided signal is triggered).
flatMap returns a receiver channel that contains the flattened (1 level)
results of applying fn
to each value of this
channel.
The receiver channel will close, when the original channel closes (or if the provided signal is triggered).
forEach applies fn
to each value in this
channel, and returns a channel
that will contain the results.
The returned channel will close after this
channel closes (or if
the provided signal is triggered).
map returns a receiver channel that contains the results of applying fn
to each value of this
channel.
The receiver channel will close, when the original channel closes (or if the provided signal is triggered).
Receive returns a promise that will be resolved with [T, true]
when a value is available, or rejected if a
provided AbortController
is aborted
If the channel is closed, then the promise will be resolved immediately with [undefined, false]
.
Receiving from a closed channel:
import {Channel} from "./channel.ts";
const ch = new Channel();
ch.close();
const [val, ok] = await ch.receive()
console.assert(val === undefined)
console.assert(ok === false)
Receiving from a buffered channel:
import {Channel} from "./channel.ts";
const ch = new Channel(1);
await ch.send("Hello world!")
ch.close();
const [val, ok] = await ch.receive()
console.assert(val === "Hello world!")
console.assert(ok === true)
Aborting a receive request:
import {Channel, AbortedError} from "./channel.ts";
const ch = new Channel(1);
await ch.send("Hello world!")
ch.close();
const abortCtrl = new AbortController()
abortCtrl.abort()
try {
await ch.receive(abortCtrl);
console.assert(false, "unreachable");
} catch (e) {
console.assert(e instanceof AbortedError);
}
Sends a value on the channel, and returns a promise that will be resolved when a the value is received (see
Channel.receive
), or rejected if a provided AbortController
is aborted.
If the channel is closed, then the promise will be rejected with an InvalidTransitionError
.
import {Channel, InvalidTransitionError} from "./channel.ts"
const ch = new Channel()
ch.close();
try {
await ch.send("should fail")
console.assert(false, "unreachable")
} catch (e) {
console.assert(e instanceof InvalidTransitionError)
}
Creates an AsyncGenerator
that yields all values sent to this channel,
and returns when the channel closes.