Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@channel/channel

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@channel/channel

The missing constructor function for creating safe async iterators

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@channel/channel

The missing constructor for creating safe async iterators

For more information, visit channel.js.org.

API

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

Package last updated on 09 Jun 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc