Lightweight utility to proxy api requests from main thread to web worker. Run the tests in your browser here!
// Main thread
const {ProxyWorker} = require('proxyworker');
const worker = new ProxyWorker(new Worker('./path/to/worker.js'));
// Call 'echo' method on worker with some arguments.
// This returns a promise that will resolve with the worker api response.
worker.callWithArgs('echo', ['a', 'b', 'c', 1, 2, 3]);
// Subscribe to worker event.
worker.subscribe('breakfast', breakfast => {
// breakfast => ['green', 'eggs', 'and', 'tofu']
});
// worker.js
const {emit, proxy} = require('proxyworker');
proxy({
methods: {
echo: function() {
let result = Array.prototype.slice.call(arguments);
return Promise.resolve(result);
}
},
events: [
'breakfast',
]
});
// Time for breakfast?
setInterval(function() {
emit('breakfast', [
'green',
'eggs',
'and',
'tofu',
]);
}, 24 * 60 * 60 * 1000);