-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update for comlink 4.x #12
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here (e.g. What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
(I signed the CLA) |
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
So there's good news and bad news. 👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there. 😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request. Note to project maintainer: This is a terminal state, meaning the ℹ️ Googlers: Go here for more info. |
This PR appears to change the behavior to always allow multi-instancing. |
Not quite. I did change the api slightly, partly because it didn't make sense to me to return a singleton when using the keyword Anyways this is what i came up with, that lets you use singletons and create new workers at the same time. New api example // my-worker.js
export class MyClass {
constructor(){ ... }
}
export function helper(){ ... } // consumer
import factory from 'comlink-loader!my-worker'
// singleton
let worker = factory()
let myClass = await new worker.MyClass()
let sameWorker = factory() // === instance
// run helper function
worker.helper() // a different consumer
import factory from 'comlink-loader!my-worker'
let worker = factory() // same instance as before
// create multiple threads
Promise.map(Array.from('1234'), ( id ) => {
// using the "new" keyword here to create new threads for this consumer
let thread = new factory()
// run the helpers... each runs on a parallel thread
return thread.helper()
}).then( results => {
// do something with results
}) |
package.json
Outdated
"loader-utils": "^1.1.0", | ||
"worker-loader": "^2.0.0" | ||
"worker-loader": "https://github.com/wellcaffeinated/worker-loader" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't ship this with a github dependency tbh.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem. The reason for that fork was to get around a WASM bug in worker-loader. It's still not merged but I can change it in the PR
@wellcaffeinated your example is how I'd love to merge pieces of this PR! Just not sure I can merge the worker-loader fork or API change. The reason the API was set up this way was to achieve type parity for the simple (singleton) use-case. One option would be to invert the boolean value. Instead of:
The API could be inverted: import { Foo } from 'comlink-loader?singleton!foo';
new Foo(); // implicitly creates worker on first use
import createWorker from 'comlink-loader!foo';
new (createWorker().Foo)(); Thoughts? The latter would be very similar to what you have here, but retains the ability to do optional workers and consistent types. |
I guess I don't really understand the concern about "types". Nor do I understand the motivation behind a I mean, this isn't my repo, so you can implement it how you like, but since it's an update to a different major version of comlink, which itself has a different API, if there's a time to update comlink-loader's api, this would be it. also, I hope i don't come off as being difficult. I just think this is an awesome project and intend to use this a lot in my projects, so I care about its implementation. :) |
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
Hey @wellcaffeinated - apologies for dropping the ball on this. I separately updated to Comlink 4, and looking at the diff I think we're pretty close to these changes. I'm going to merge this PR, but I would actually like to change the behavior of the default ( |
@developit Cool! It would be great to have examples of all the ways to use this in the README :) |
I've updated this for 4.x
I've also modified the api very slightly to make a bit more sense.
All the tests pass but more should be added.
README updated to reflect the new way of importing.
If anyone wants to use this before it gets merged, I created a "dist" branch for that purpose.