Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@channel/channel
Advanced tools
The missing constructor function for creating safe async iterators
The missing constructor for creating safe async iterators
For more information, visit channel.js.org.
class Channel<T> implements AsyncIterableIterator<T> {
constructor(executor: ChannelExecutor<T>, buffer?: ChannelBuffer<T>);
next(value?: any): Promise<IteratorResult<T>>;
return(value?: any): Promise<IteratorResult<T>>;
throw(error: any): Promise<IteratorResult<T>>;
[Symbol.asyncIterator](): this;
}
The Channel
class implements the AsyncIterableIterator
interface. Channels are designed to be indistinguishable from async generator objects.
type Push<T> = (value: PromiseLike<T> | T) => Promise<any | void>;
interface Stop extends Promise<any | void> {
(error?: any): void;
}
type ChannelExecutor<T> = (
push: Push<T>,
stop: Stop
) => Promise<T | void> | T | void;
The ChannelExecutor
is passed the arguments push
and stop
.
push
is a function which allows you to enqueue values onto the channel. It synchronously throws an error if there are too many pending pushes on the channel (currently set to 1024). It returns a promise which resolves when it’s safe to push more values.
stop
is a both a promise and a function. As a function, stop
can be called to stop a channel. Calling stop
without any arguments stops the channel without error, and passing an error both stops the channel and causes the final iteration to reject with that error.
As a promise, stop
can be awaited to defer event handler cleanup, and it can also be used with Promise.race
to abort pending promises. If you pass a value to Channel.prototype.return
, stop
will resolve to that value.
The value of the final interation of the channel will be the return value of the executor. If the executor throws an error or returns a promise rejection, the channel will be immediately stopped and the final iteration will throw.
interface ChannelBuffer<T> {
full: boolean;
empty: boolean;
add(value: T): void;
remove(): T | undefined;
}
class FixedBuffer<T> implements ChannelBuffer<T> {
constructor(capacity: number);
}
class SlidingBuffer<T> implements ChannelBuffer<T> {
constructor(capacity: number);
}
class DroppingBuffer<T> implements ChannelBuffer<T> {
constructor(capacity: number);
}
The Channel
constructor optionally takes a ChannelBuffer
instance as its second argument. Buffers allow multiple values to be pushed onto channels without waiting. FixedBuffer
allows channels to push a set number of values, DroppingBuffer
drops the latest values when the buffer has reached capacity, and SlidingBuffer
drops the earliest values when the buffer has reached capacity. You can define custom buffering behaviors by implementing the ChannelBuffer
interface.
FAQs
The missing constructor function for creating safe async iterators
We found that @channel/channel demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.