Generator functions
See original GitHub issueHello!
I am a new user to comlink and am thrilled to begin integrating it into my projects as the primary way to communicate between the main thread and web workers. My question is, does comlink provide any way of working with generator functions?
My base example is as follows:
//////////// worker.js ///////////////
const fibGen = function*(max: number) {
if(max > 0 ) yield 1
if(max > 1 ) yield 1
let a=1, b=1
for(let i=2; i<max; i++) {
const c = a
a = b
b = a+c
yield b
}
}
Comlink.expose({fibGen})
///////// main.js ////////////
const wrapped = Comlink.wrap(new Worker("./worker.js"))
// cannot serialize because the return value is a generator
const gen = await wrapped.fibGen(50)
// throws saying cannot use as an async generator
for await(i of wrapped.fibGen(50)) {
console.log(i)
}
I know there are other API methods available with Comlink, I’m just a little green on the best way to go about using them in this case.
Thanks in advance! This is my fourth stopping point after workerize-loader, workerize, and comlink-loader, and has been by far the easiest implementation with my existing setup.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:9
Top Results From Across the Web
function* - JavaScript - MDN Web Docs - Mozilla
Generators are functions that can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.
Read more >Generators - The Modern JavaScript Tutorial
Regular functions return only one, single value (or nothing). Generators can return (“yield”) multiple values, one after another, on-demand.
Read more >Python Generator Functions - TutorialsTeacher
A generator is a special type of function which does not return a single value, instead, it returns an iterator object with a...
Read more >Why would anyone need JavaScript generator functions?
Generators also allow us to pass messages in two directions between a pair of functions. Now, to be fair, functions can do this...
Read more >Understanding Generators in JavaScript | DigitalOcean
An asynchronous function is a type of function available in ES6+ JavaScript that makes working with asynchronous data easier to understand by ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @sebinsua - well found! I meant to post here but ended up being distracted by other things.
Looking over the handler provided by @xepher0coded, that seems a much better solution as it’s reusing the proxy transfer handler rather than essentially duplicating it like I do (although I found it a useful exercise to better understand how Comlink was using message channels to pass values back and forth). It also looks like it allows for other properties to be called directly on the method, whereas my solution simply supports the iterator properties (
next
,return
,throw
).The only thing I spotted is when manually calling
iterator.return()
as in this example, my transfer handler would result in thefor-await-of
loop exiting immediately, whereas the transfer handler as suggested by @xepher0coded would iterate one more time before exiting. Not quite sure what exactly is the expected behaviour in this case and it’s probably a bit of an edge case 😄.Older versions of Comlink supported generators by turning them into async generators, but support for async generators was lacking at the time. I should revisit this now.