gochan
golang inspired channels for doing CSP-style concurrency in node.js.
This library is to give node.js without generators the ability to use golang style channels for doing concurrent programming.
Installation
This module is installed via npm:
$ npm install gochan
Example Usage
Create a channel and synchronize two processes
// simple.js// Create a new channelvar gochan = ;// This function sends messages to the channel{console;ch;}// This function waits and receives a message from the channel{ch;}var ch = ;;;
This will result in the following output:
$ node examples/simple.jsabout to buildreceived 42after building
Note how the order of execution is deterministic. Even though the consumer was run first, it happily waited until there was a message in the channel.
Similarly, the builder can't continue after putting something into the channel unless there is somone to read it.
Listen to multiple channels, and only proceed when there is a message
This emulates the golang "select" primitive:
// two channelsvar a = ;var b = ;// wait until there is a message on any of the channelsgochan;// put something on the "a" channela;
API
var gochan = require('gochan');
Creates a new channel factory that can make channels.
gochan(size)
Creates a new instance of a channel.
size
- The size of the channel. By default the size is 0, which is essentially infinite. To get golang-like behaviour where the size of a channel is only one, and thus forces channel getters and setters to synchronize around channel communication, set this value to be 1.
Eg:
var gochan = ;var ch = ;
ch.put(val, cb)
Puts a raw value, a callback function, or a promise on the channel.
val
- A raw value, a callback function (signaturefunction(err, value)
) or a promise.cb
- If provided, this callback will be called. If ommitted, then a promise will be returned.
NB: If the channel is a fixed size and there isn't remove for the message, then the put operation will essentially stall until there is space for the message. So the callback won't be called into there is room.
var gochan = ;var ch = ;ch;var fs = ;ch;ch;
ch.get(cb)
Gets a value from the channel. If there is nothing in the channel, then the callback won't be called until there is a message on the channel.
cb
- If provided, this callback will be called. If ommitted, then a promise will be returned.
NB: Even though node callback functions and values and promises can be put onto the channel, the final value that gets retrieved will be a final value.
var gochan = ;var ch = ;var fs = ;ch;ch;
gochan.select(arrChannels, cb)
Waits until there is data on an array of channels. This function attempts to
emultate the golang select
primitive.
arrChannels
- An array of channels that will be monitored until there is activity on the channel. Once there is, the callback will be called.cb
- Called when there is data on a channel. The first argument is an error, and the second parameter is the channel with the data. You'll still need to call.get()
on the channel to get the data.
var gochan = ;var a = ;var b = ;gochan;a;