js-csp - Buffered channel VS Putting collections onto a channel
CSP is primarily defined by 'blocking/unbuffered' channels. This allows processes communicating over a channel to block execution until both parties are ready to send/receive a message.
js-csp however allows channels to be buffered:
var channel = csp.chan(5); // buffer size of 5
With a buffered channel, it is possible to place multiple values onto it before any 'blocking' occurs.
I also noticed that by using the convenience function
csp.operations.onto(), it is possible for a process to place a collection of values onto a channel, even if the channel is unbuffered:
var unbuffered = csp.chan(); csp.operations.onto(unbuffered, [1,2,3], true);
Other processes 'taking' from the
unbuffered channel will then grab each value from the collection one-by-one as normal. In other words, it's as though the
unbuffered channel became buffered. To extend on this, I noticed that it was possible to continue placing collections onto the channel (even if the 'unbuffered' channel already contained values).
var unbuffered = csp.chan(); // no values yet csp.operations.onto(unbuffered, , true); // value of 1 on the channel csp.operations.onto(unbuffered, , true); // value of 1 & 2 on the channel
No we've turned our
unbuffered channel into an infinitely-buffered channel!
So why is this capability there? And in what scenarios would I use a
buffered channel VS an
unbuffered (i.e infinitely-buffered) channel?