Tower Channel
Tower 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.
Indirection Of Data Flow
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.
Leveraging Channels As A Form Of Control Flow
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):
/** * Module dependencies. */ var chan = ;var put = chanput;var take = chantake; /** * Create a new channel; */ var c = ; window { ;};
Now we have decoupled our logic from the events themselves. We can use select
blocks or simply take
s to operate on channels.
;
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.
var c = ; // 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.
var c = ; ; ;
Closed channels do not throw an exception when you try to write to it.
This also gives you the ability to have Timeout Channels.
var t = chan; // Close the channel within 200 ms. ; ;
You can also check when a channel is closed:
// c being a channel.if cclosed // Something here...
License
MIT