CSP channels with ES6 generators, inspired by Clojurescript's core.async and Go
var csp = require"js-csp";
Pingpong (ported from Go).
while truevar ball = yield csptaketable;if ball === cspCLOSEDconsole.logname + ": table's gone";return;ballhits += 1;console.logname + " " + ballhits;yield csptimeout100;yield cspputtable ball;cspgovar table = cspchan;cspgoplayer "ping" table;cspgoplayer "pong" table;yield cspputtable hits: 0;yield csptimeout1000;tableclose;;
There are more under examples directory.
This is a very close port of Clojurescript's core.async. The most significant difference is that the IOC logic is encapsulated using generators (
yield) instead of macros. Therefore resources on
core.async or Go channels are also helpful.
js-csp requires ES6 generators.
Earlier versions of Firefox either had ES6 generators turned off, or supported only old style generators.
--harmony-generators flag. Check support using
node --v8-options | grep harmony
Use one of the js-to-js compilers:
- Facebook Regenerator.
- Google Closure Compiler with
--language_in ECMASCRIPT6 --language_out ECMASCRIPT3flags.
- Google Traceur.
Or, if you use Python's Twisted: https://github.com/ubolonton/twisted-csp
Or, if you want a better language: https://github.com/clojure/core.async
It's available for both
bower. For browsers, use browserify+
npm install js-csp
bower install js-csp
Feel free to open issues for questions/discussions, or create pull requests for improvement.
Some areas that need attention:
- More documentation, examples, and maybe some visualization. Porting RxJS/Bacon examples may help.
- Multiplexing, mixing, publishing/subscribing. These need to be tested more. The API could also be improved.
- Deadlock detection.
Distributed under MIT License.