MCPcopy Index your code
hub / github.com/developit/workerize / setup

Function setup

src/index.js:55–79  ·  view source on GitHub ↗
(ctx, rpcMethods, callbacks)

Source from the content-addressed store, hash-verified

53}
54
55function setup(ctx, rpcMethods, callbacks) {
56 ctx.addEventListener('message', ({ data }) => {
57 let id = data.id;
58 if (data.type!=='RPC' || id==null) return;
59 if (data.method) {
60 let method = rpcMethods[data.method];
61 if (method==null) {
62 ctx.postMessage({ type: 'RPC', id, error: 'NO_SUCH_METHOD' });
63 }
64 else {
65 Promise.resolve()
66 .then( () => method.apply(null, data.params) )
67 .then( result => { ctx.postMessage({ type: 'RPC', id, result }); })
68 .catch( err => { ctx.postMessage({ type: 'RPC', id, error: ''+err }); });
69 }
70 }
71 else {
72 let callback = callbacks[id];
73 if (callback==null) throw Error(`Unknown callback ${id}`);
74 delete callbacks[id];
75 if (data.error) callback[1](Error(data.error));
76 else callback[0](data.result);
77 }
78 });
79}
80
81function toCjs(code, exportsObjName, exports) {
82 code = code.replace(/^(\s*)export\s+default\s+/m, (s, before) => {

Callers 1

workerizeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected