Skip to content

PeterHancock/web-worker-csp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#web-worker-csp

A js-csp channel interface for Web Workers

csp.put and csp.take from web workers

Example

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
...

install

npm i web-worker-csp

license

MIT

About

Create js-csp channels on top of Web Workers

Resources

License

Stars

Watchers

Forks

Packages

No packages published