A queue/channel implementation geared for use with generators.
npm install tower-channelTower Channel provides you with a pretty simple, yet extremely powerful Channel system.
The channels are inspired by Clojure's core.async library and a bunch of talks that Rich Hickey gave.
These are the premise that Channels provide. In the era of JavaScript, evented data flow are plentiful. This inheritely creates a set of tightly coupled components: The event and it's callbacks.
The callbacks are then at the mercy of the callee -- the emitter, where as with Channels, you have much more control over this behaviour.

Instead of using callback driven programming, we can leverage generator-based programming with the use of Channels. This will decoupled components that are event-driven, allowing the component itself when, where, and how data is being fetched.
Here's an example of pushing events into a channel (we're also using the go primitive):
``js
/**
* Module dependencies.
*/
var chan = require('tower-channel');
var put = chan.put;
var take = chan.take;
/**
* Create a new channel;
*/
var c = chan();
window.onscroll = function(event) {
go(function *() {
yield c.put(event); // or put(c, event)`
});
};
Now we have decoupled our logic from the events themselves. We can use select blocks or simply takes to operate on channels.
`js`
go(function* () {
var event = yield c.take();
// Do something with the event.
});
Channels also give you a way to limit the amount of events that come through at a time, thus, giving the system time to operate on the current events before taking any new ones.
When you have a bounded/buffered channel and you select or take, it will block if it's empty until a value is present. Note that by "block", it doesn't truly block the system, otherwise that would stall everything. When a buffered channel is full and you try and write to it, it will again block until it can write.
`js`
var c = chan(10); // Buffered channel with a size of 10
Channels can also be closed. When a channel is closed, writes will not be accepted and won't block. Any reads will still be served until the Channel is empty.
`js
var c = chan(10);
setTimeout(function() {
c.close();
}, 1200);
go(function* () {
yield c.put(10); // won't block and won't write.
});
`
Closed channels do not throw an exception when you try to write to it.
This also gives you the ability to have Timeout Channels.
`js
var t = chan.timeout(200); // Close the channel within 200 ms.
go(function*() {
yield t.put(10); // Works!
});
setTimeout(function() {
go(function* () {
yield t.take(); // 10 ... Works
yield t.put(40); // Nope.
});
}, 400);
`
You can also check when a channel is closed:
`js``
// c being a channel.
if (c.closed) {
// Something here...
}
MIT