weevil v0.1.2
Weevil
Like eval, but in a webworker.
npm install weevil --saveWhat?
Weevil makes it simple to eval JavaScript in a separate event-loop "thread" via a webworker. It also simplifies sending and receiving messages with that other thread via postmessage.
Why?
If you're kinda crazy, then you might find this handy.
Examples
Basic Usage
Pass weevil a string with javascript code in it, that'll be run in the webworker.
var weevil = require('weevil');
//This code will be run on the webworker
var workerCode = [
"//do something slow here",
"for (var i=0; i<1000000; i++) {",
" Math.pow(2,50);
"}",
"console.log('done');"
].join("\n");
weevil(workerCode);we can simplify generating the code with deval, of course it still can't access any of the variables in scope of the host page, but at least it's easier to write the code.
var weevil = require('weevil');
//This code will be run on the webworker
var workerCode = deval(function () {
//do something slow here
for (var i=0; i<1000000; i++) {
Math.pow(2,50);
}
console.log('done');
});
weevil(workerCode);Sending messages to the worker
In the context of the webworker, there is a weevil object which effectively acts as a simple event emitter. In the context of the main thread, the worker object returned from calling weevil(theCode) has the same methods:
weevil.emit(name, [args...])/worker.emit(name, [args...])- send a message to the host page / to the worker. With
name, and optionalargslist which theoncallback will receive. Also aliased asweevil.send/worker.send.
- send a message to the host page / to the worker. With
weevil.on(name, callback)/worker.on(name, callback)- listen to messages from the host page / from the worker. With
nameof the messages to listen for, andcallbackto run when the message is received. The callback will receive the list ofargs...from theemitcall in the other process.
- listen to messages from the host page / from the worker. With
weevil.once(name, callback)/worker.once(name, callback)- as per
.on(), listen to messages from the host page / from the worker, but only run the callback only once.
- as per
weevil.off(name, [callback])/worker.off(name, [callback])- unregister listen callback(s). If a
callbackis specified, only that callback will be deregistered for thenamed event. If nocallbackis specified, all callbacks for messagenamewill be deregistered.
- unregister listen callback(s). If a
weevil.kill()/worker.kill()- Terminate the worker immediately, and deregister all handlers.
(Example: again using deval to generate the worker code string).
var deval = require('deval');
var weevil = require('weevil');
//This code will run on the webworker (note it will be run in a completely different scope
var workerCode = deval(function () {
weevil.on('count-to', function (limit) {
var start = new Date();
for (var i=0; i<limit; i++) {
Math.pow(2,50);
}
vae end = new Date();
weevil.emit('count-done', end - start);
});
});
var limit = 1000000000;
var worker = weevil(workerCode);
worker.send('count-to', limit)
.on('count-done', function (time) {
alert('Worker counted to ' + limit + ' in ' + time + 'ms');
});Who?
If you wish to heckle: Philip Roberts.
License
MIT