GoogleChromeLabs / GoogleChromeLabs/comlink
TypeError: ep.addEventListener is not a function
- Dominant language
- TypeScript
- Stars
- 12.8k
- Forks
- 435
- PR merge metrics
- No merged PRs in 30d
Description
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 ()
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 ()
```
```
// in main process
import * as Comlink from 'comlink';
const nodeEndpoint = require('comlink/dist/umd/node-adapter.js')
const dPro = Comlink.wrap(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));
```
Contributor guide
Assessment
This issue has not been assessed yet.