#web-worker-csp
A js-csp channel
interface for Web Workers
csp.put
andcsp.take
from web workers
main.js
var csp = require('js-csp'),
workerChannel = require('./index.js');
var worker = new Worker('echo.js'); // or preferably webworkify + compatible modified echo.js!
var end = csp.timeout(100);
var w = workerChannel(worker);
csp.go(function* () {
var count = 0;
var chi = w.chi; // Input channel
var cho = w.cho; // Output channel
while(true) {
yield csp.put(chi, ++count);
var result = yield csp.alts(cho, end);
if (result.channel === end || result.value === csp.CLOSED) {
return;
}
var msg = result.value;
console.log(msg.data);
}
}
echo.js
self.addEventListener('message', function (msg) {
setTimeout( function() {
self.postMessage(msg);
}, 10);
})
index.html
<script src="bundle.js"></script>
where bundle.js
is output of running browserify on main.js
The browser console should show
1
2
3
...
npm i web-worker-csp
MIT