Channel module
Usage
npm i -E @prelude/channel
import * as Ch from '@prelude/channel'
Reading and completion
read()returns the next value and throwsChannel closed.after completion.next()returns the complete iterator result. A value hasdone: false; completion hasdone: true.maybeRead()returns the next value orundefinedafter completion.
When a channel can contain undefined, maybeRead() cannot distinguish a queued undefined value from completion because both produce undefined. Use next() and inspect its done property when that distinction matters.
Failure
fail(err)closes the channel and rejects pending and subsequent reads witherr;failedanderrorexpose the state.ofIterable()andofAsyncIterable()fail their channel when the source throws, so consumers see the error instead of a silent completion.
Cancellation
read, maybeRead, write, maybeWrite and writeIgnore take { signal }; select, selectNext and selectAsync take { signal } as their first argument; after, ofIterable and ofAsyncIterable take it in a trailing options object.
- Aborting a pending read or write withdraws it — nothing is consumed or delivered — and rejects it with
signal.reason; a write that was already accepted (buffered or handed to a reader) is unaffected. - Aborting a
selectwithdraws every attempt and rejects (theselectgenerator throws) withsignal.reason. - Aborting
after,ofIterableorofAsyncIterablefails the channel withsignal.reason, so readers reject and the producer stops.
const controller = new AbortController()
const value = await ch.read({ signal: controller.signal })
for await (const value of Ch.select({ signal: controller.signal }, a, b)) { ... }
Closing a channel (close, closeWriting, breaking out of for await) remains the way to stop all readers and writers.
License
This package is dedicated to the public domain under CC0 1.0.