comlink
comlink copied to clipboard
TypeError: ep.addEventListener is not a function
could u give an example about how to transfer function which in main process to worker thread?
I just want to use callback from main process in the worker process which in the node enviroment with worker thread.
error msg:
(node:44027) UnhandledPromiseRejectionWarning: TypeError: ep.addEventListener is not a function
at expose (/Users/Documents/demo/shimo-download/bootstrap/node_modules/comlink/dist/umd/comlink.js:78:10)
at Object.serialize (/Users/Documents/demo/shimo-download/bootstrap/node_modules/comlink/dist/umd/comlink.js:32:11)
at toWireValue (/Users/Documents/demo/shimo-download/bootstrap/node_modules/comlink/dist/umd/comlink.js:256:64)
at Array.map (<anonymous>)
at processArguments (/Users/Documents/demo/shimo-download/bootstrap/node_modules/comlink/dist/umd/comlink.js:235:38)
at Object.apply (/Users/Documents/demo/shimo-download/bootstrap/node_modules/comlink/dist/umd/comlink.js:212:53)
at new Promise (<anonymous>)
// in main process
import * as Comlink from 'comlink';
const nodeEndpoint = require('comlink/dist/umd/node-adapter.js')
const dPro = Comlink.wrap<any>(nodeEndpoint(global.dworker) as any) as any;
console.log('111111', dPro);
await dPro.downloadConsumerHandler({
task: {
id: this.id,
jobId: this.job.id,
data: this.data,
type: TaskType.Mo,
}
}, Comlink.proxy(((args) => console.log('hello callback', args))));
// in worker thead
import * as Comlink from 'comlink';
const nodeEndpoint = require('comlink/dist/umd/node-adapter.js')
const proxyEventEmitter = {
...Comlink.transferHandlers.get('proxy'),
serialize(obj) {
const { port1, port2 } = new MessageChannel();
Comlink.expose(obj, nodeEndpoint(port1));
return [port2, [port2]];
},
deserialize(port) {
port.start();
return Comlink.wrap(nodeEndpoint(port));
},
} as any;
Comlink.transferHandlers.set('proxy', proxyEventEmitter);
const api = { downloadConsumerHandler, fileHandler }
console.log('w.parentPort', w.parentPort);
Comlink.expose(api, nodeEndpoint(w.parentPort));
Is the callback example not helpful?
Is the callback example not helpful?
I use it as callback example in node worker thread, it's error, i am not sure whether is about the node worker thread, your callback example is about web worker in browser, right?
Can you provide a runnable sample in a gist or something? That way I can turn it into a test.
This happens for me when I run a jasmine test against a class which creates a worker using comlink.
In case it's helpful to others, this error appeared for me when I mistakenly wrapped a worker constructor rather than an instance:
Error
import Worker from "..."
const bad = Comlink.wrap(Worker);
Fix
import Worker from "..."
const works = Comlink.wrap(new Worker());
for me at least - quick fumble, quick fix.