npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

channel4

v0.1.0

Published

Dead simple communicating sequential processes for Javascript

Downloads

15

Readme

Channel4

Dead simple communicating sequential processes for Javascript (like Clojurescript core.async, or Go channels).

npm install channel4

Usage

let channel = Channel();
Channel.put(channel, 'World!');
Channel.take(channel, (value) => {
  console.log('Hello ');
});

// prints `Hello World!`

Motivation

Consider the following code:

const listenerFn = (event) => {
  event.preventDefault();
  doSomething(event);
};

document.querySelector('button').addEventListener('click', listenerFn);

There's a one-to-one relationship between the producer addEventListener and the consumer listenerFn. Also the producer knows 1) there's a single listener attached, 2) how the data is feeded to the listener and 3) when the data is going to be processed. Adding a second listener introduces some challenges:

document.querySelector('button').addEventListener('click', (e) => {
  listenerFn() && otherListenerFn();
});

// or

document.querySelector('button').addEventListener('click', listenerFn);
document.querySelector('button').addEventListener('click', otherListenerFn);

It's immediately obvious that this couples the producer and consumer: when you introduce another listener, the producer has to cater for it.

This is poor separation of concerns.

But what if you could decouple consumers from producers? What if the producer could send the message without the need to worry about who's consuming it? What if the consumer could consume messages at its own peace?

You won't need to fiddle with such poor code, that's for sure. Decoupling would also lead to better and easier testing.

As you might have guessed by now channels can help you decouple producers and consumers. The decoupling is obtained through a simple queue.

The producer places items of work on the queue for later processing. The consumer is free to remove the work item from the queue at any time.

Producer and consumer only have to know about the channel to communicate. Also, multiple producers can put values for multiple consumers to take.

let channel = Channel();
document.querySelector('button')
  .addEventListener('click', Channel.put.bind(null, channel));

channel.take(channel, (value) => console.log('Hello World'));

In the example above, addEventListener isn't aware that there's a consumer listening to click events.

let channel = Channel();
document.querySelector('a')
  .addEventListener('click', Channel.put.bind(null, channel));
document.querySelector('button')
  .addEventListener('click', Channel.put.bind(null, channel));

channel.take(channel, (value) => console.log('Hello'));

In this other example, the consumer isn't aware that multiple producers are placing items of work on the queue.

API

Channel.put :: (channel, a) -> channel

Put a value into a channel and return the channel. The result in a noop when called on closed channel.

let channel = Channel();
Channel.put(channel, 46);

Channel.take :: (channel, (a) -> b) -> channel

Take values from the channel and fire the callback. The result in a noop when called on closed channel.

let channel = Channel();
Channel.take(channel, console.log.bind(console, 'received: '));
Channel.put(channel, 47);

// prints `received: 47`

Channel.close :: (channel) -> channel

Close the current channel. This is a shorthand for Channel.put(channel, Channel.END).

let channel = Channel();
Channel.take(channel, (value) => {
  if (value === Channel.END) console.log('Closed!');
});
Channel.close(channel);

// prints `Closed!`

...

Channel.put(channel, 46); // has no effect
Channel.take(channel, () => ()); // callback will never fire

Channel.pipe :: (input, output, (a) -> b, keepOpen) -> output

Pipe all the incoming values from the input to the output channel. If the input channel is closed, the output channel is kept open unless it is specified otherwise. You can apply a transformation function while piping the values.

let input = Channel();
let output = Channel();
Channel.pipe(input, output, (x) => x/2);
Channel.take(output, console.log.bind(console, 'received: '))
Channel.put(input, 44);

// prints: `received: 22`

Channel.demux :: ([channel], output, (a) -> b, keepOpen) -> output

Merge all values from an array of channels into the output. If one of the input channels is closed, the output channel is kept open unless it is specified otherwise. You can apply a transformation function while merging the values.

let one = Channel();
let two = Channel();
let output = Channel();
Channel.demux([one, two], output);
Channel.take(output, console.log.bind(console, 'received: '));
Channel.take(output, console.log.bind(console, 'received: '));
Channel.put(one, 1);
Channel.put(two, 2);

// prints `received: 1` and `received 2`

Channel.mux :: (input, [channel], (a) -> b, keepOpen) -> [channel]

Broadcast all the values from input channel to an array of channels. If the input channel is closed, the output channels are kept open unless it is specified otherwise. You can apply a transformation function to the value before it is broadcasted.

let one = Channel();
let two = Channel();
let input = Channel();
Channel.mux(input, [one, two]);
Channel.take(one, console.log.bind(console, 'received: '));
Channel.take(two, console.log.bind(console, 'received: '));
Channel.put(input, 1);

// prints `received: 1` and `received 1`

Channel.END

Emitted from the channel on close.

let channel = Channel();
Channel.take(channel, (value) {
  if (value === END) console.log('Closed!');
})
Channel.put(channel, Channel.END);

// prints `Closed!`

Channel.KEEP_OPEN

Keep the output channel open during Channel.pipe, Channel.demux or Channel.mux as a response to an incoming Channel.END value.

let input = Channel();
let output = Channel();
Channel.pipe(input, output, ((x) => x), Channel.KEEP_OPEN);
Channel.take(output, console.log.bind(console, 'received: '))
Channel.put(input, Channel.END);
Channel.put(output, 44);

// prints: `received: 44`

Channel.CLOSE_BOTH

Close the output channel when the input receives a Channel.end value.

let input = Channel();
let output = Channel();
Channel.pipe(input, output, ((x) => x), Channel.CLOSE_BOTH);
Channel.take(output, (value) => {
  if (value === CLOSED) console.log('Closed!');
});
Channel.put(input, Channel.END);

// prints: `Closed!`

Resources

This project is heavily inspired by: