comlink icon indicating copy to clipboard operation
comlink copied to clipboard

Expose main to worker, same remote-object-style

Open wh1t3cAt1k opened this issue 3 years ago • 3 comments

Could not find this in examples.

worker-rpc allows the worker to request values from main in a similar manner, (await remote.func()).

I understand you can pass callbacks using Comlink.proxy, but in some cases (an apparent) direct duplex communication indicates the intent better than passing the ball back-and-forth where the main should has first call a function, then the worker must memoize the callback etc.

Is this kind of duplex communication also possible with Comlink? What would be an example?

Thanks!

wh1t3cAt1k avatar Mar 23 '21 11:03 wh1t3cAt1k

I would really prefer to use comlink (I have discovered this library after growing increasingly frustrated by the boilerplate incurred by worker-rpc, especially getting the typings right). At the same time, the seeming absence of two-way communication is a major stumbling block for me.

wh1t3cAt1k avatar Mar 23 '21 16:03 wh1t3cAt1k

It's possible. I made a minimal Example here. And there are some tests outlining the setup.

MrMadClown avatar Mar 23 '21 19:03 MrMadClown

@MrMadClown it's nice! Thank you, I will experiment with this again. Do you think this example should be added to the main repo and explicitly outlined in the README? I expect it would be a moderately common question.

wh1t3cAt1k avatar Mar 23 '21 19:03 wh1t3cAt1k