You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

45 lines
1.0 KiB

function addMethods(worker, methods) {
var c = 0;
var callbacks = {};
worker.addEventListener('message', function (e) {
var d = e.data;
if (d.type !== 'RPC') return;
if (d.id) {
var f = callbacks[d.id];
if (f) {
delete callbacks[d.id];
if (d.error) {
f[1](Object.assign(Error(d.error.message), d.error));
} else {
f[0](d.result);
}
}
} else {
var evt = document.createEvent('Event');
evt.initEvent(d.method, false, false);
evt.data = d.params;
worker.dispatchEvent(evt);
}
});
methods.forEach(function (method) {
worker[method] = function () {
var _arguments = arguments;
return new Promise(function (a, b) {
var id = ++c;
callbacks[id] = [a, b];
worker.postMessage({
type: 'RPC',
id: id,
method: method,
params: [].slice.call(_arguments)
});
});
};
});
}
module.exports = addMethods;
//# sourceMappingURL=rpc-wrapper.js.map