Dead simple communicating sequential processes for Javascript (like Clojurescript core.async, or Go channels).
npm install channel4
let channel = Channel();
Channel.put(channel, 'World!');
Channel.take(channel, (value) => {
console.log('Hello ');
});
// prints `Hello World!`
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.
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);
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`
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
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, Channel.KEEP_OPEN, (x) => x/2);
Channel.take(output, console.log.bind(console, 'received: '))
Channel.put(input, 44);
// prints: `received: 22`
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`
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`
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!`
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, Channel.KEEP_OPEN);
Channel.take(output, console.log.bind(console, 'received: '))
Channel.put(input, Channel.END);
Channel.put(output, 44);
// prints: `received: 44`
Close the output channel when the input receives a Channel.end
value.
let input = Channel();
let output = Channel();
Channel.pipe(input, output, Channel.CLOSE_BOTH);
Channel.take(output, (value) => {
if (value === CLOSED) console.log('Closed!');
});
Channel.put(input, Channel.END);
// prints: `Closed!`
This project is heavily inspired by: